$(function() {
	
	var curWidth			= 502;
	var curImgWidth			= 500;
		
	var regWidth			= parseInt($(".panel").eq(0).css("width"));
	var regImgWidth			= parseInt($(".panel img").eq(0).width());
	
	var $slider				= $('#slider');
	var $container			= $('.scrollContainer', $slider);
	var $panels				= $('.panel', $container);
	var totalPanels			= $container.children().size();
	var curPanel			= $panels.index($('.current')) + 1;
	var movingDistance	    = regWidth + 22;

	$container
		.css('width', ($panels.eq(0).outerWidth(true) * $panels.length) + (curWidth - regWidth))
		.css('left', -($panels.eq(curPanel - 1).position().left) + ((curWidth - regWidth) / 2));
	
	$('.panel.current', $container).css({width: curWidth + 'px'}).find('img').css({width: curImgWidth + 'px'});	
    
	$slider.data("currentlyMoving", false);

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end();
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end();
	}
	
	//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    = $container.css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$container
				.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();
		}
	}
	
	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#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;
	  }
	});
	
});