$j(function() {

	var totalPanels = $j(".scrollContainer").children().size();

	var regWidth = $j(".panel").css("width");
	var regImgWidth = $j(".panel img").css("width");
	var regTitleSize = $j(".panel h2").css("font-size");
	var regParSize = $j(".panel p").css("font-size");

	var movingDistance = 170;
	var regmarginTop = 50;
	var marginTop = 10;
	var curWidth = 200;
	var curImgWidth = 180;
	var curTitleSize = "15px";
	var curParSize = "13px";

	var $jpanels = $j('#slider .scrollContainer > div');
	var $jcontainer = $j('#slider .scrollContainer');

	$jpanels.css({ 'float': 'left', 'position': 'relative' });

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

	$jcontainer
		.css('width', ($jpanels[0].offsetWidth * $jpanels.length) + 100)
		.css('left', "0px");

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

	function returnToNormal(element) {
		$j(element)
			.animate({ width: regWidth, marginTop: regmarginTop })
			.find("a.book-link")
			.removeClass('show-book')
			.end()
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize })
			.end()
			.find("a")
			.removeClass('selected')

	};

	function growBigger(element) {
		$j(element)

			.animate({ width: curWidth, marginTop: marginTop })
			.find("a.book-link")
			.addClass('show-book')
			.end()
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize })
			.end()
			.find("a")
			.addClass('selected')
	}

	//direction true = right, false = left
	function change(direction) {
		if (totalPanels - curPanel == 0) {
			returnToNormal("#panel_" + curPanel);
			$j(".scrollContainer").animate({
				"left": '0'
			}, 400, function() {
				curPanel = 3;			
				growBigger("#panel_" + curPanel);
			});
		} else {

			//if not at the first or last panel
			if ((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }

			//if not currently moving
			if (($j("#slider").data("currentlyMoving") == false)) {

				$j("#slider").data("currentlyMoving", true);

				var next = direction ? curPanel + 1 : curPanel - 1;
				var leftValue = $j(".scrollContainer").css("left");
				var movement = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
				$j(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$j("#slider").data("currentlyMoving", false);
				});

				returnToNormal("#panel_" + curPanel);
				growBigger("#panel_" + next);
				//$j(".panel").addClass("selected");

				curPanel = next;

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

				//go forward
				$j("#panel_" + (curPanel + 1)).click(function() { change(true); });

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

				//go back
				$j("#panel_" + (curPanel - 1)).click(function() { change(false); });

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

		}
	}

	// Set up "Current" panel and next and prev
	growBigger("#panel_3");
	var curPanel = 3;

	$j("#panel_" + (curPanel + 1)).click(function() { change(true); });
	$j("#panel_" + (curPanel + 2)).click(function() { change(true); });
	$j("#panel_" + (curPanel - 1)).click(function() { change(false); });
	$j("#panel_" + (curPanel - 2)).click(function() { change(false); });

	//when the left/right arrows are clicked
	$j(".moving-box .right").click(function() { change(true); });
	$j(".moving-box .left").click(function() { change(false); });

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

});


