$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regImgHeight		= $(".panel img").css("height");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var movingDistance	    = 90;
    
    var midWidth            = 150;
    var midImgWidth         = 150;
    var midImgHeight        = 141;
    var midTitleSize        = "16px";
    var midParSize          = "12px";
	
	var curWidth			= 205;
	var curImgWidth			= 205;
	var curImgHeight		= 192;
	var curTitleSize		= "20px";
	var curParSize			= "15px";

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

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

	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 340 )
		.css('left', "0px");

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

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth, height: regImgHeight })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
    
    function middleImg(element) {
        $(element)
            .animate({ width: midWidth })
            .find("img")
            .animate({ width: midImgWidth, height: midImgHeight })
            .end()
            .find("h2")
            .animate({ fontSize: midTitleSize })
            .end()
            .find("p")
            .animate({ fontSize: midParSize });
    };

	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth, height: curImgHeight })
		    .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 leftValue = $(".scrollContainer").css("left");
            
            if(direction)
            {
                var previous = curPanel - 1;
                var next = curPanel + 1;
                var end = curPanel +2;
                var movement = parseFloat(leftValue, 10) - movingDistance;
                
                // A hack to get around the lack of movement when the first panel is in the centre
                if(curPanel == 1)
                    movement = parseFloat(leftValue, 10) - movingDistance - 60;
            }
            else
            {
                var previous = curPanel + 1;
                var next = curPanel - 1;
                var end = curPanel - 2;
                var movement = parseFloat(leftValue, 10) + movingDistance;
                
                // A hack to get around the lack of movement when the first panel is in moving into the centre
                if(next == 1)
                    movement = parseFloat(leftValue, 10) + movingDistance + 60;
            }
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
            
            returnToNormal("#panel_"+previous);
            middleImg("#panel_"+curPanel);
			growBigger("#panel_"+next);
            middleImg("#panel_"+end);
			
			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();
            
            // Add the shaking
            $("#panel_"+curPanel).mouseover(function(){ shake("#panel_"+curPanel); });
            $("#panel_"+curPanel).mouseout(function(){ stopShake("#panel_"+curPanel); });
		}
	}
    
    // On mouseover for the centre element
    function shake(element)
    {
        $(element).css('z-index',1000);
        $(element).find("img").stop()
                .animate({
                    width: '211px', 
                    height: '198px',
                    marginLeft: "-4px",
                    marginRight: "-4px"
                }, 50);
    }
    
    // On mouseout for the centre element
    function stopShake(element)
    {
        $(element).find("img").stop()
            .animate({
                width: '205px', 
                height: '192px',
                marginLeft: "0px",
                marginRight: "0px"
            }, 50);
        $(element).css('z-index',1);
    }
	
	// Set up "Current" panel and next and prev
    middleImg("#panel_2");    
	growBigger("#panel_3");
    middleImg("#panel_4");	
	var curPanel = 3;
    
    /* IE6 / IE7 hack to display all the panels */
    $($panels).each(function(i, o){
        if(o.id != "panel_2" && o.id != "panel_3" && o.id != "panel_4")
        {
            returnToNormal('#'+o.id);
        }
    });

    
	
    $("#panel_"+(curPanel+1)).click(function(){ change(true); });
    $("#panel_"+curPanel).mouseover(function(){ shake("#panel_"+curPanel); });
    $("#panel_"+curPanel).mouseout(function(){ stopShake("#panel_"+curPanel); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//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;
	  }
	});
	
});
