$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
	
	var regtop 				= $(".panel").css("top");		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var randomStart = Math.floor(Math.random()*(parseInt(totalPanels) - 1)) + 2;
	//var curPanel = 3;
	var curPanel = randomStart;
	//var startPushLeft = "-220px";
	var startPushLeft = "-" + (((curPanel - 1) * 200) + 20) + "px";
	
	var movingDistance	    = 200;
	
	var curtop				= 0;	
	var curWidth			= 470;
	var curImgWidth			= 470;
	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) + 270 + 400)
		.css('left', startPushLeft);

	var scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	// Clone first and last panels
	$(".panel:first").before($(".panel#panel_" + totalPanels).clone().addClass("cloned").removeAttr("id"));
	$(".panel:last").after($(".panel#panel_1").clone().addClass("cloned").removeAttr("id"));

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth, top: regtop })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth, top: curtop}
					 ,function() {
					  $("#left-shadow").show();
					  $("#right-shadow").show();
					})
			
			.find("img")
			.animate({ width: curImgWidth })
		    .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;
		}


		  $("#left-shadow").hide();
		  $("#right-shadow").hide();
		  
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			
			if (!direction && (curPanel <= 1)) {
				next = totalPanels;
			}
			else if (direction && (curPanel >= totalPanels)) {
				next = 1;
			}
			
			//console.log(next);
			
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
			
			if (!direction && (curPanel <= 1)) {
				var movement = "-" + (((next - 1) * 200) + 20) + "px";;
			}
			else if (direction && (curPanel >= totalPanels)) {
				var movement = "-" + (((next - 1) * 200) + 20) + "px";;
			}
		
			$(".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
	$(".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;
	  }
	});
	

/*		var autoRotate;
		autoRotate = setInterval(function(){
				change(true);
			},3000);  */

});
