var page;

var movingPage;

function supersizeImage() {
	var imagewidth = 2911;
	var imageheight = 883;
	var browserwidth = $(window).width();
	var browserheight = $(window).height();

	var ratio = imageheight/imagewidth;

	$('img.supersize').each(function(){
		image = $(this);
		if ((browserheight/browserwidth) > ratio){
			$(image).height(browserheight);
			$(image).width(browserheight / ratio);
		} else {
			$(image).width(browserwidth);
			$(image).height(browserwidth * ratio);
		}
		$(image).css('left', (browserwidth - $(image).width())/2);
		$(image).css('top', (browserheight - $(image).height())/2);
	});
}

var last_url;
function slidePage(urlReceive,isAnimate) {
	if(last_url==urlReceive){
		return;
	}
	else {
		last_url=urlReceive;	
	}
	var pageWidth = $('.container').width();
	var indexVal = $('div.'+urlReceive).index();
	var shiftAmount = 0;
	for (var i=1; i<=indexVal;i++){
		shiftAmount += $('#wrapper div.container:nth-child('+i+')').width();
	}
	shiftAmount *= -1;
	
	if(isAnimate) {
		if(movingPage){
			movingPage.stop();	
		}
		movingPage=$("#wrapper").animate(
			{ left: shiftAmount}, {
				duration: 1400,
				easing: 'expoinout'
			}
		);
	} else {
		page.css("left",shiftAmount);
	}
}

function currUrl(){
	url=document.location.href.split("#");
	return url[0];
}

function deepUrl(animate){
	url=document.location.href.split("#");
	if(url[1]){
		checkurl=url[1].toLowerCase();
		if(checkurl.indexOf('http://')<0 && checkurl.indexOf('https://')<0 && checkurl.indexOf('www.')<0){ 
			slidePage(checkurl,animate);
		}
	}
}

$(window).load(
	function() {
		//RESIZE - all fullscreen image to be fullscreen
		supersizeImage();
		//LISTENER - in window that will resize, reposition everything
		$(window).bind("resize", supersizeImage);
		$('.shadow').fadeOut(500);
		
		page = $("#wrapper");
		$('#nav a').click(function(){
			document.location.href=currUrl()+'#'+$(this).attr('rel');
			return false;
		});
		
		deepUrl(0);
		setInterval("deepUrl(1);",100);
	}
);
