$(document).ready(function(){

	curtains();
	
	$('#subFeature').jcarousel({
		scroll:2
	});

});


function curtains(){
	
	center(".feature .curtainContainer",true);
	center(".feature .background",true);
	center(".feature .band",true);
	center(".feature .crowd",true);
	
	setTimeout(function(){
		$(".curtainLeft, .curtainRight").animate({
			width:0
			// height:0
		},2000,function(){
			$(".curtainContainer").fadeOut("fast",function(){
				$(this).remove();
				feature();
			});
		});
	},1000);
	
}

function feature(){

	$(".mainNav li:first").addClass("on");

	$(".feature").bind("mousemove",runFeature);
	
	stageColour();
	
}

function center(item,center){

	var winWidth = $(window).width();
	var itemWidth = $(item).width();
	
	var left = winWidth/2 - itemWidth/2;
	
	if(center == true)
		$(item).css("left",left);
	
	return itemWidth/2;

}

function runFeature(e,backWidth,bandWidth,crowdWidth,threshHold){
	
	var threshHold = 50;
	
	var backWidth = center(".feature .background",false);
	var bandWidth = center(".feature .band",false);
	var crowdWidth = center(".feature .crowd",false);
	
	var mousePosX = e.pageX;
	var mousePosY = e.pageY - 76;
	
	var winWidth = $(window).width();
	var winCenter = winWidth / 2;
	
	var featureWidth = $("feature").width();
	var featureHeight = $("feature").height();
	
	var heightCenter = featureHeight / 2;
	var widthCenter = featureWidth / 2;
	
	//back
	var defaultTopBack = 0;
	var defaultLeftBack = winCenter - backWidth;
	
	var moveTopBack = -mousePosY / threshHold;
	var moveLeftBack = defaultLeftBack + (-mousePosX - backWidth + winWidth) / threshHold;
	
	//crowd
	var defaultBotCrowd = 0;
	var defaultLeftCrowd = winCenter - crowdWidth;
	
	var moveBotCrowd = -mousePosY / threshHold;
	var moveLeftCrowd = -defaultLeftCrowd + (-mousePosX - crowdWidth + winWidth) / threshHold;
	
	$(".feature .background").stop().animate({
		"top": moveTopBack,
		"left": moveLeftBack
	}, 0);
	
	$(".feature .crowd").stop().animate({
		"bottom": moveBotCrowd,
		"left": -moveLeftCrowd
	}, 0);
	
	$(this).mouseleave(function(){
		$(".feature .background").stop().animate({
			"top": defaultTopBack,
			"left": defaultLeftBack
		}, 500);
		
		$(".feature .crowd").stop().animate({
			"bottom": defaultBotCrowd,
			"left": defaultLeftCrowd
		}, 500);
	});

}

function stageColour(){
	
	$(".mainNav li").mouseover(function(){
		
		$(".feature").unbind("mousemove",runFeature);
		
		$(".mainNav li").not(this).addClass("on");
		
		if($(this).hasClass("on")){
			//nothing
		}else{
			var color = $(this).attr("id");
			$(".feature .background").fadeOut(50,function(){
				$(".feature .band").fadeOut(50,function(){
					$(".feature .background").attr("class","background "+color);
					$(".feature .band").attr("class","band "+color);
				});
						
				$(".feature .band").fadeIn(50,function(){
					$(".feature .background").fadeIn(50,function(){
						$(".mainNav li").removeClass("on");
						$(".feature").bind("mousemove",runFeature);
					});
				});
				
			});
			
			$(this).addClass("on");

		}
	});
	
}

