$(function() {

	var totalPanels			= Math.ceil($(".scrollContainer").children().length / 3);

	var movingDistance	    = 166;

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

	$panels.css({'position' : 'relative'});

	$("#slider").data("currentlyMoving", false);

	$container
		.css('height', ($panels[0].offsetHeight * $panels.length) + 162 )
		.css('top', "0");

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

	//direction true = right, false = left
	function change(direction) {


	    //if not at the first or last panel
		if((direction && curPanel + 2 >= 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 botValue    = $(".scrollContainer").css("top");
			var movement	 = direction ? parseFloat(botValue, 10) - movingDistance : parseFloat(botValue, 10) + movingDistance;

			$(".scrollContainer")
				.stop()
				.animate({
					"top": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});

			curPanel = next;
			//remove all previous bound functions
			$("#panel_"+(curPanel+1)).unbind();

            //remove all previous bound functions
			$("#panel_"+(curPanel-1)).unbind();

			//remove all previous bound functions
			$("#panel_"+curPanel).unbind();
		}
	}

	// Set up "Current" panel and next and prev

	var curPanel = 1;

	//when the left/right arrows are clicked
	$(".bot").click(function(){ change(true); });
	$(".top").click(function(){ change(false); });

});
