$l(function() {
	
	var totalPanels			= $l(".scrollContainer").children().size();
		
	var regWidth			= $l(".panel").css("width");
	var regImgWidth			= $l(".panel img").css("width");
	var regTitleSize		= $l(".panel h2").css("font-size");
	var regParSize			= $l(".panel p").css("font-size");
	
	/*/Determina a DISTANCIA que a IMAGEM percorre/*/
	var movingDistance	    = 100;
	/*/Determina altura da DIV principal/*/
	var curWidth			= 229;
	/*/Determina altura da IMAGEM MAIOR/*/	
	var curImgWidth			= 125;
	/*/Determina o TAMANHO da FONT do H2 quando a imagem esta ON/*/
	var curTitleSize		= "16px";
	/*/Determina o TAMANHO da FONT do P quando a imagem esta ON/*/	
	var curParSize			= "16px";

	var $lpanels				= $l('#centro .scrollContainer > div');
	var $lcontainer			= $l('#centro .scrollContainer');

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

	$lcontainer
		.css('width', ($lpanels[0].offsetWidth * $lpanels.length) + 100 )
		.css('left', "0");

	var scroll = $l('#centro .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$l(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$l(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
	}
	
	//direction true = right, false = left
	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 (($l("#centro").data("currentlyMoving") == false)) {
            
			$l("#centro").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $l(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$l(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$l("#centro").data("currentlyMoving", false);
				});
			
			returnToNormal("#panel_"+curPanel);
			growBigger("#panel_"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$l("#panel_"+(curPanel+1)).unbind();	
			
			//go forward
			$l("#panel_"+(curPanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$l("#panel_"+(curPanel-1)).unbind();
			
			//go back
			$l("#panel_"+(curPanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$l("#panel_"+curPanel).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev - Primeira DIV Maior
	growBigger("#panel_2");	
	var curPanel = 1;
	
	$l("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$l("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$l(".right").click(function(){ change(true); });	
	$l(".left").click(function(){ change(false); });
	
	$l(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$l(".right").click();
				break;
			case 32: //space
				$l(".right").click();
				break;
	    	case 37: //left arrow
				$l(".left").click();
				break;
			case 39: //right arrow
				$l(".right").click();
				break;
	  }
	});
	
});
