$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".carrouselPan").css("width");
	var regImgWidth			= $(".carrouselPan img").css("width");
	
		
	var curWidth			= 212;
	var curImgWidth			= 210;
	var ImgTop				= 35;
	//var ImgPad	  			  = 10;

	var $panels				= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({marginTop: ImgTop+"px", width: regImgWidth })
		    .end()
			.find("p")
			.animate({opacity: "hide"});
	};
	function speedReturnToNormal(element) {
		$(element)
			.css( 'width', regWidth )
			.find("img")
			.css('margin-top', ImgTop+"px")
			.css('width', regImgWidth )
		    .end()
			.find("p")
			.css('display',"none");
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({marginTop: "0", width: curImgWidth })
		    .end()
			.find("p")
			.animate({opacity: "show"});
	}
	
	function speedGrowBigger(element) {
		$(element)
			.css( 'width', curWidth )
			.find("img")
			.css('margin-top', "0")
			.css('width', curImgWidth )
		    .end()
			.find("p")
			.css('display',"block");
	}
	
	//direction true = right, false = left
	function change(direction) {
	   
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			
			$("#slider").data("currentlyMoving", true);
			
			//if not at the first or last panel
			if(direction && !(curPanel < (totalPanels-2))){
				//return false;
				speedGrowBigger("#panel_end");
				speedReturnToNormal("#panel_"+curPanel);
				curPanel = 0;
				$(".scrollContainer").css('left', movingDistance+"px");
			}	
			if(!direction && (curPanel <= 1)) {
				//return false;
				speedGrowBigger("#panel_first");
				speedReturnToNormal("#panel_"+curPanel);
				curPanel = totalPanels-1;
				$(".scrollContainer").css('left', (-movingDistance*(totalPanels-2))+"px");
			}	
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		//alert('top');
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			if(curPanel == 0)
				returnToNormal("#panel_end");
			else if(curPanel == totalPanels-1)
				returnToNormal("#panel_first");
			else
				returnToNormal("#panel_"+curPanel);
			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();
		}
		return false;
	}
	
	if($panels[0]){
		var movingDistance	    = $panels[0].offsetWidth;

		$panels.css({'float' : 'left','position' : 'relative'});
		
		$("#slider").data("currentlyMoving", false);


		var scroll = $('#slider .scroll').css('overflow', 'hidden');
		
		// Set up "Current" panel and next and prev
		$container
			.css('width', ($panels[0].offsetWidth * $panels.length) + curWidth*2 )
			.css('left', "0px");
		growBigger("#panel_1");	
		var curPanel = 1;
		
		$("#panel_"+(curPanel+1)).click(function(){ change(true); });
		$("#panel_"+(curPanel-1)).click(function(){ change(false); });
		
		$("#panel_end").click(function(){ change(false); });
		$("#panel_first").click(function(){ change(true); });
		
		//when the left/right arrows are clicked
		$(".right").click(function(){ change(true); });	
		$(".left").click(function(){ change(false); });
		
		$(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;
		  }
		});
	}
});
