$(document).ready(function(){	
	centerBG();
	initSlider();
	initCarousel();
	initFancy();
	initialiseScrollPane();

	$.ajaxSetup({
		cache:	false
	});

	$.address.externalChange(function(event) {
		var url = event.value.replace("/","");
		if (url != "")
			loadContent(url);
	});

	$('.nav li a').click(function(event){
		var url = $(this).attr('href');
//		if (url != $.address.value.replace("/","") ){
			
			if ((url.indexOf('http') == -1)){
				event.preventDefault();
				$('.sidebar.right').fadeOut('slow', function(){
					loadContent(url);
				});
			}
//		}
	});
	
	
	$(window).resize(function(){
		centerBG();
	});
			
});


function loadContent(url){
	$.address.value(url);
	
	$('.active').removeClass('active');
	$('a[href$="'+url+'"]').addClass('active');
	
	$('.sidebar.right').load(url, function(){
		$('.sidebar.right').fadeIn('slow',function(){
			$.address.title($.address.title().split(' | ')[0] + ' | ' + $('h3').html() );
			initSlider();
			initCarousel();
			initFancy();
			initialiseScrollPane ();
		});
	});
}


function centerBG() {
	width = $(window).width();
	height = $(window).height();
	bw = 1920;
	bh = 1577;
	$top = (height - bh)/2;
	$left = (width - bw)/2;			
	$bg = 'url(images/background.jpg) no-repeat ' + $left + 'px ' + $top +'px' + ' fixed';
	$('body').css('background',$bg);
}


function initSlider() {
	$("#slider").easySlider({
		auto: true,
		continuous: true,
		pause: 10000
	});
}


function initCarousel() {
	$(".jCarouselLite").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		auto: 1500,
		speed: 2000,
		visible: 6
	});
	
}
	

function initialiseScrollPane() {
	$(".jScrollPane1").jScrollPane({scrollbarWidth:8, dragMaxHeight: 8, showArrows: 1, arrowSize: 9, scrollbarMargin: 0, topCapHeight:20, bottomCapHeight:20, reinitialiseOnImageLoad: true});						   
}


function initFancy() {
	$('.int-link').fancybox({'hideOnContentClick': false,'frameWidth':($(window).width()/2),'frameHeight':($(window).height()/2)});	
	$('.int-link2').fancybox({'hideOnContentClick': false,'frameWidth':476,'frameHeight':350});	
}
