$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img:not(.carousel)").css("width");
	var regCarouselImgWidth = $(".panel img.carousel").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var curWidth			= 130;
	var curImgWidth			= 110;
	var curCarouselImgWidth = 132;
	var curTitleSize		= "20px";
	var curParSize			= "11.8px";

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

	var movingDistance	    = $panels.outerWidth(true);

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

	// calculate the width of the container - (# of panels * width of panel (including padding, border and margin)) - width of a panel + width of active panel
	var width = ($panels.length * $panels.outerWidth(true)) - $panels.width() + curWidth + 100;		// extra 100 for ie6

	var curPanel;

	// set the current panel to be the centre panel
	if($panels.length > 0)
		curPanel = Math.floor($panels.length / 2) + 1;
	else
		return;

	// leftOffset = (scroll width - width of current panel (including padding, border and margin)) / 2;
	var curPanelWidth = $panels.outerWidth(true) - $panels.width() + curWidth;

	var leftOffset = Math.floor(($('#slider .scroll').width() - curPanelWidth) / 2);

	var curPanelOffset = leftOffset - ((curPanel - 1) * 122);

	$container
		.css('width', width )
		.css('left', curPanelOffset+'px');

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

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img:not(.carousel)")
			.animate({ width: regImgWidth })
		    .end()
			.find("img.carousel")
			.animate({ width: regCarouselImgWidth })
			.end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img:not(.carousel)")
			.animate({ width: curImgWidth })
		    .end()
			.find("img.carousel")
			.animate({ width: curCarouselImgWidth })
			.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 (($("#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, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			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();
		}
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_"+curPanel);
	
	//$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	//$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$(".next").click(function(){ change(true); });
	$(".prev").click(function(){ change(false); });
	
});
