$(function() {

	var totalPanels			= $(".scrollContainer").children().size();
	var widthPanels = new Array();
	$(".scrollContainer").children().size();

	var i=1;
	$(".scroll_image").each(function() {
		widthPanels[i] = $(this).attr("width");
		i++;
  });

  var spaceBetweenPanels	= 20;
	var movingDistance;

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

	$panels.css({'float' : 'left','position' : 'relative'});
	$(".scroll").animate({opacity: 1},1200)
	$(".inside img").animate({opacity: 0.25},0)
	$("#slider").data("currentlyMoving", false);

	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 3000 )
		.css('left', (parseInt($('#slider').css("width"))/2-widthPanels[1]/2-spaceBetweenPanels/2)+"px");

	$(".leftNav").animate({opacity: 0},0);
	$(".leftNav").css("cursor", "default");

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

	function fadeOut(element) {
		$(element)
			.animate({opacity: 0.25},300)
			.find("img")
			.animate({opacity: 0.25},300)
		    .end()
	};
	
	function fadeIn(element) {
		$(element)
			.animate({opacity: 1},300)
			.find("img")
			.animate({opacity: 1},300)
		    .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);
			
			movingDistance =  direction ? widthPanels[curPanel]/2+widthPanels[curPanel+1]/2+spaceBetweenPanels : widthPanels[curPanel]/2+widthPanels[curPanel-1]/2+spaceBetweenPanels;
			//$("#echo").html(movingDistance+" | "+widthPanels[curPanel]+" - "+widthPanels[curPanel+1]+" - "+" - "+" - "+" - ");

			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);
				});
			
			fadeOut("#panel_"+curPanel);
		  fadeIn("#panel_"+next);
			
			curPanel = next;
			
			$("#slide_title").html($("#panel_"+curPanel+" img").attr("title"));
			$("#slide_description").html($("#panel_"+curPanel+" img").attr("description"));
			
			$(".leftNav").stop();
			if (curPanel == 1) {
				$(".leftNav").animate({opacity: 0},1000);
				$(".leftNav").css("cursor", "default");
			}
			else {
				$(".leftNav").animate({opacity: 1},1000);
				$(".leftNav").css("cursor", "pointer");
			}
			
			$(".rightNav").stop();
			if (curPanel == totalPanels) {
				$(".rightNav").animate({opacity: 0},1000);
				$(".rightNav").css("cursor", "default");
			}
			else {
				$(".rightNav").animate({opacity: 1},1000);
				$(".rightNav").css("cursor", "pointer");
			}
			
			//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
 	fadeIn("#panel_1");
	var curPanel = 1;


	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	$("#slide_title").html($("#panel_1 img").attr("title"));
	$("#slide_description").html($("#panel_1 img").attr("description"));
	
	//when the left/right arrows are clicked
	$(".rightNav").click(function(){ change(true);});
	$(".leftNav").click(function(){ change(false);});

	$(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$(".rightNav").click();
				break;
			case 32: //space
				$(".rightNav").click();
				break;
	    case 37: //left arrow
				$(".leftNav").click();
				break;
			case 39: //right arrow
				$(".rightNav").click();
				break;
	  }
	});
	
});
