var MovementMode=1;
var CurX=0;
var CurY=0;
var FirstLoad=true;
var CurrentImage=1;
var ImageArray;
var ScreenWidth,ScreenHeight,ScreenCenterX,ScreenCenterY;
var ScreenWidthBuffer,ScreenHeightBuffer;
var ImgWidth=0;
var ImgHeight=0; //Size of the CurrentImageLoaded after scaling based on BufferSize.
var LeftPos=0; //Position of the last Cycle Div;
var TopPos=0;
var Duration;
var Panning;
var FadeOnly=true;
var FadeTimeout;

if(PageID=='Home'){
	Panning=true;
}
$(document).ready(
	function(){
		var pathname = window.location.hash;
		if(pathname){
			document.location = '/'+pathname.replace('#','');
		}
		
		$('#Mute').click(
			function(){
				if($(this).hasClass('Off')){
					$(this).removeClass('Off');
					$("audio").get(0).pause();
				}
				else{
					$(this).addClass('Off');
					$("audio").get(0).play();
				}
			}
		)
		
		
		ScreenWidth=parseInt($(document).width());
		ScreenHeight=parseInt($(document).height());
		ScreenCenterX=parseInt(ScreenWidth/2);
		ScreenCenterY=parseInt(ScreenHeight/2);
		Random=randomFromTo(2,5);
		ScreenWidthBuffer=parseInt(ScreenWidth+(ScreenWidth/10));
		ScreenHeightBuffer=parseInt(ScreenHeight+(ScreenHeight/10));
		//console.log(ScreenWidthBuffer+' '+ScreenHeightBuffer);		
		if (($.browser.msie  && parseInt($.browser.version) <= 8)){
			$('#SiteHolder').css(
				{
					width	:	(ScreenWidth-5)+'px',
					height	:	(ScreenHeight-5)+'px'
				}
			)
		}
		else{
			$('#SiteHolder').css(
				{
					width	:	ScreenWidth+'px',
					height	:	ScreenHeight+'px'
				}
			)
		}
		Duration=5000;
		if (($.browser.msie  && parseInt($.browser.version) <= 8) || PageID!='Home') {
			var Content=$('body').html();
			var Pos1 = Content.indexOf('<!-- START IMAGE')+16;
			var Pos2 = Content.indexOf('END IMAGE -->');
			var IMG = Content.substr(Pos1,Pos2-Pos1);
			$('#Backing2').html(IMG);
			$('#Backing2').waitForImages(function() {
				var Sizes=GetImgSize($('#Backing2 img').attr('src'));
				ImgWidth=Sizes.ImgWidth;
				ImgHeight=Sizes.ImgHeight;
				RatioX=ImgWidth/ImgHeight;
				RatioY=ImgHeight/ImgWidth;
				while (ImgWidth<ScreenWidth || ImgHeight<ScreenHeight){
					ImgWidth+=RatioX;
					ImgHeight+=RatioY;
				}
				ImgWidth=parseInt(ImgWidth);
				ImgHeight=parseInt(ImgHeight);				
				$('#Backing2 img').css('width',ImgWidth+'px');	
				$('#Backing2 img').css('height',ImgHeight+'px');
				$('#Backing2').css('z-index',-1);
				$('#Backing2').fadeIn(3000);
			});
		}
		else{
			Preloader();
		}		
		NavSlider();
		$('#PageContentPanel, #SliderArrow').css('opacity',.8);
		DoFonts();
	}
)

function Preloader(){
	LoadSpinner(true);
	ImageArray = ['/images/HomepageBackgrounds/Homepage-1.jpg', '/images/HomepageBackgrounds/Homepage-2.jpg', '/images/HomepageBackgrounds/Homepage-3.jpg', '/images/HomepageBackgrounds/Homepage-4.jpg', '/images/HomepageBackgrounds/Homepage-5.jpg'];
	var hidden = $('body').append('<div id="ImgCache"/>').children('#ImgCache');
	$.each(ImageArray, function (i, val) {
		$('<img class="Img'+(i+1)+'"/>').attr('src', val).appendTo(hidden);
	});
	$('#ImgCache').waitForImages(function() {
		setTimeout('LoadSpinner(false)',1000);
	});
	
};

function LoadSpinner(State){
	if(State){
		$('#LoadSpinner').css({
			left	:	ScreenCenterX-16+'px',
			top		:	ScreenCenterY-16+'px'
		});
		$('#LoadSpinner').fadeIn(250);
	}
	else{
		$('#LoadSpinner').fadeOut(500,
			function(){
				NextPanel();
			}
		);
	}
}

function NextPanel(){
	var NextImg=NextImage();	
	if (Panning){
		SwitchBG('<img src="'+$('.Img'+NextImg).attr('src')+'">');
		if (FadeOnly){
			FadeTimeOut = setTimeout('NextPanel()',Duration);
		}
	}
}

function MoveBG(){	
	if(MovementMode==1){
		$('#Backing2').animate(
			{
				top		:	(ScreenHeight-ImgHeight)+'px',
				left	:	(ScreenWidth-ImgWidth)+'px'
			}
			,Duration
			,function(){
				TopPos=parseInt($(this).css('top'));
				LeftPos=parseInt($(this).css('left'));
				MovementMode++;
				if (Panning){NextPanel();}
			}
		)
	}
	if(MovementMode==2){
		$('#Backing2').animate(
			{
				left	:	0+'px'
			}
			,Duration
			,function(){
				TopPos=parseInt($(this).css('top'));
				LeftPos=parseInt($(this).css('left'));
				MovementMode++;
				if (Panning){NextPanel();}
			}
		)
	}
	if(MovementMode==3){
		$('#Backing2').animate(
			{
				top		:	0+'px',
				left	:	(ScreenWidth-ImgWidth)+'px'
			}
			,Duration
			,function(){		
				TopPos=parseInt($(this).css('top'));
				LeftPos=parseInt($(this).css('left'));
				MovementMode++;
				if (Panning){NextPanel();}
			}
		)
	}
	if(MovementMode==4){
		$('#Backing2').animate(
			{
				top		:	0+'px',
				left	:	0+'px'
			}
			,Duration
			,function(){		
				TopPos=parseInt($(this).css('top'));
				LeftPos=parseInt($(this).css('left'));
				MovementMode=1;
				if (Panning){NextPanel();}
			}
		)
	}
}

function GetImgSize(imgSrc){
	var newImg = new Image();
	newImg.src = imgSrc;
	ImgWidth = newImg.width;
	ImgHeight = newImg.height;
	return {ImgWidth:ImgWidth,ImgHeight:ImgHeight}
}

function NextImage(){
	if(FirstLoad){
		return 1;
	}
	else{
		CurrentImage++;
		var Tmp=CurrentImage;
		if (Tmp>ImageArray.length){
			CurrentImage=1;
			Tmp=1;
		}
		return Tmp;
	}
}

function randomFromTo(from, to){
	var temp;
	if( from == to ){
		return from;
	}
	else if( from > to ){
		temp = from;
		from = to;
		to = temp;
	}
	return Math.floor(Math.random() * (to - from + 1) + from);
}

function DoFonts(){
	Cufon.replace('#PageContentPanel h1', { fontFamily: 'Nobile'});
	Cufon.replace('#PageContentPanel h2', { fontFamily: 'Nobile'});
	Cufon.replace('#NavPanel ul li a', { fontFamily: 'Nobile'});
	Cufon.replace('#NavPanel ul li.FooterTitle', { fontFamily: 'Nobile'});
	Cufon.replace('p', { fontFamily: 'TeXGyreAdventor'});
}

