$(function() {
	
	var totalPanels = $(".scrollContainer").children().size();
		
	var regWidth = "300px";
	var regImgWidth	 = "290px";
	
	var movingDistance = 320;
	
	var curWidth = 343;
	var curImgWidth = 333;
	
	var $panels	= $('#slider .scrollContainer > div');
	var $container	= $('#slider .scrollContainer');

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

	$container
		.css('width', ($panels[1].offsetWidth * $panels.length) + 100 )
		.css('left', "-1315px");

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

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find(".txtDate")
			.animate({ top: "199px", width: "285px", fontSize: "15px" , lineHeight: "175%"})
			.end()
			.find(".eventSoldOut")
			.animate({ height: "213px", width: "290px"})
			.end()
			.find(".eventCancelled")
			.animate({ height: "213px", width: "290px"})
			.end()
			.find(".txtBands")
			.animate({ width: "285px"})
			.end()
			.find("h1")
			.animate({ fontSize: "20px"})
			.end()
			.find("h2")
			.animate({ fontSize: "17px"})
			.end()
			.find(".bandStyle")
			.animate({ fontSize: "9px"})
			.end()
			.find(".bandStyle2")
			.animate({ fontSize: "7px"})
			.end()
			.find(".bandFrom")
			.animate({ fontSize: "9px", top: "0px"})
			.end()
			.find(".bandFrom2")
			.animate({ fontSize: "7px"})
			.end()
			.find(".salle")
			.animate({ width: "285px", top: " 71px"})
			.end()
		};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find(".txtDate")
			.animate({ top: "230px", width: "328px" , fontSize: "20px", lineHeight: "125%"})
			.end()
			.find(".eventSoldOut")
			.animate({ height: "245px", width: "333px"})
			.end()
			.find(".eventCancelled")
			.animate({ height: "245px", width: "333px"})
			.end()
			.find(".txtBands")
			.animate({ width: "328px"})
			.end()
			.find("h1")
			.animate({ fontSize: "25px"})
			.end()
			.find("h2")
			.animate({ fontSize: "20px"})
			.end()
			.find(".bandStyle")
			.animate({ fontSize: "12px"})
			.end()
			.find(".bandStyle2")
			.animate({ fontSize: "10px"})
			.end()
			.find(".bandFrom")
			.animate({ fontSize: "13px", top: "-3px"})
			.end()
			.find(".bandFrom2")
			.animate({ fontSize: "9px"})
			.end()
			.find(".salle")
			.animate({ width: "324px", top: " 39px"})
			.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    = $(".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_6");	
	var curPanel = 6;
	
	$("#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;
	  }
	});
	
});
