//function
//change
//returnToNormal
//growBigger
//changeClass
$(function() {
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	var regTopMargin 		= 48;
	
	var movingDistance	    = 95;
	
	var curWidth			= 201;
	var curHeight			= 126;
	var curImgWidth		    = 201;
    var curImgHeight		= 126;
	var currTopMargin 		= 	0;
	var curTitleSize		= "20px";
	var curParSize		    = "15px";
	
	var curDivclass		= "currentBox";

	var $panels				= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');
	var $synopsys			= $('.inside > div');
	$panels.css({'float' : 'left','position' : 'relative'});

	$("#slider").data("currentlyMoving", false);
	
	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100 )
		.css('left', "26px");
		//.css('left', "-135px");

	var scroll = $('#slider .scroll').css('overflow', 'hidden');
	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth, marginTop: regTopMargin })
			.find("img")
			.animate({ width: regImgWidth})
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth, marginTop: currTopMargin }, function() {displayInfo();})
			.find("img")
			.animate({ width: curImgWidth, marginTop: currTopMargin })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
		var next         = true ? curPanel + 1 : curPanel - 1;
	}
	
	//direction true = right, false = left
	function displayInfo(){
		// change current panel class
		$('#panel_'+curPanel).addClass("current-panel");
		//display synopsis
		$('#synopsis_'+curPanel).css("display","block");
		//display description
		$('#description_'+curPanel).css("display","block");
	}
	
	function change(direction) {
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 1) - movingDistance : parseFloat(leftValue, 1) + movingDistance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});

			returnToNormal("#panel_"+curPanel);
			var previuesPanel	=	(next-1);
			var previuesNextPanel	=	(previuesPanel+1);
			$(".synopsis").css("display","none");
			$(".description").css("display","none");
			$('#panel_'+previuesPanel).removeClass("current-panel");	
			$('#panel_'+previuesNextPanel).removeClass("current-panel");
			$('#panel_'+curPanel).removeClass("current-panel");
			
			$('#panel_'+previuesPanel).removeClass("distance");	
			$('#panel_'+previuesNextPanel).removeClass("distance");
			$('#panel_'+curPanel).removeClass("distance");
			//alert(previuesPanel);

			growBigger("#panel_"+next);
			curPanel = next;
			
			//remove all previous bound functions
			$("#panel_"+(curPanel+1)).unbind();				
			//go forward
			$("#panel_"+(curPanel+1)).click(function(){ change(true); });			
            //remove all previous bound functions															
			$("#panel_"+(curPanel-1)).unbind();			
			//go back
			$("#panel_"+(curPanel-1)).click(function(){ change(false); });			
			//remove all previous bound functions
			$("#panel_"+curPanel).unbind();
		}
	}
	function callback() {
		alert('Callbacked!');
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_1");
	var curPanel = 1;
	var para	 = (curPanel+1);
	var mbrapa	 = (curPanel+1);
	$("#panel_"+(curPanel+1)).click(function(){ change(true);});
	$("#panel_"+(curPanel-1)).click(function(){ change(false);});
	
	//when the left/right arrows are clicked
	$(".right").click(function(){ change(true); });
	$(".left").click(function(){ change(false); });
	
	//Keyboard handler
	$(window).keydown(function(event){
		switch (event.keyCode) {
			case 13: //enter
			$(".right").click();
			break;
			case 32: //space
			$(".right").click();
			break;
			case 37: //left arrow
			$(".left").click();
			break;
			case 39: //right arrow
			$(".right").click();
			break;
		}
	});
	
});
