$(function() {
	
	var totalPanels			= $(".scrollGallery").children().size();
		
	var regWidth			= $(".div_foto").css("width");
	var regImgWidth			= $(".div_foto img").css("width");
	var regTitleSize		= $(".div_foto h2").css("font-size");
	var regParSize			= $(".div_foto p").css("font-size");
	
	var movingDistance	    = 138;
	
	var curWidth			= 138;
	var curImgWidth			= 138;
	var curTitleSize		= "20px";
	var curParSize			= "15px";

	var $panels				= $('#sliderGal .scrollGallery > div');
	var $container			= $('#sliderGal .scrollGallery');

	$panels.css({'float' : 'left','position' : 'relative'});
    
	$("#sliderGal").data("currentlyMoving", false);
try{
	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100 )
		.css('left', "0px");
 }
        catch(error)
        {
        }
	var scroll = $('#sliderGal .scrollGal').css('overflow', 'hidden');

	//direction true = right, false = left
	function change3(direction) {
	   
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#sliderGal").data("currentlyMoving") == false)) {
            
			$("#sliderGal").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollGallery").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$(".scrollGallery")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#sliderGal").data("currentlyMoving", false);
				});
			
			//returnToNormal("#div_foto_"+curPanel);
			//growBigger("#div_foto"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$("#div_foto"+(curPanel+1)).unbind();	
			
			//go forward
			$("#div_foto"+(curPanel+1)).click(function(){ change3(true); });
			
            //remove all previous bound functions															
			$("#div_foto"+(curPanel-1)).unbind();
			
			//go back
			$("#div_foto"+(curPanel-1)).click(function(){ change3(false); }); 
			
			//remove all previous bound functions
			$("#div_foto"+curPanel).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev
	//growBigger("#panel_3");	
	var curPanel = 1;
	
	$("#div_foto"+(curPanel+1)).click(function(){ change3(true); });
	$("#div_foto"+(curPanel-1)).click(function(){ change3(false); });
	
	//when the left/right arrows are clicked
	$(".rightGal").click(function(){ change3(true); });	
	$(".leftGal").click(function(){ change3(false); });
	
});
