$(function() {

/* 
     	Home page slider - modified from Moving boxes by Chris Coyier (http://css-tricks.com/moving-boxes/)
    */  
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h3").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var movingDistance	    = 108;
	
	var curWidth			= 268;
	var curImgWidth			= 105;
	var curTitleSize		= "14px";
	var curParSize			= "11px";

	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) + 800 )
		.css('left', "-4px");

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

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth }).removeClass('currentSlide')
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h3")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth }).addClass('currentSlide')
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h3")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
	}
	
	/* direction true = right, false = left */
	function change(direction) {	
        
        /* if not currently moving */
        if (($("#slider").data("currentlyMoving") == false)) {
	
			/* if at the end of the panels, append some more */
			if((direction && (curPanel+1)==totalPanels) || (!direction && ((curPanel-1)==1))) { 
				appendPanels(direction,6);
			}
            
			$("#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
				},500 , 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();
		}		
	} //
	
	/* direction true = right, false = left */
	function appendPanels(direction,appendNum) {
		var sc = $('.scrollContainer');
		var p = "";
		
		if (direction==0) {
			var lastPanelIndex = totalPanels-1;
			for (i=0;i<appendNum;i++) {
				p = $('#slider .scrollContainer > div').eq(lastPanelIndex).detach();
				p.prependTo(sc);
			}
			
			var scleft = parseFloat(sc.css("left"), 10);
			scleft -= (appendNum * movingDistance);
			sc.css("left", scleft+"px");
			curPanel = 1 + appendNum + 1;
			
		}
		if (direction==1) {
			for (i=0;i<appendNum;i++) {
				p = $('#slider .scrollContainer > div').eq(0).detach();
				p.appendTo(sc);
			}
			
			var scleft = parseFloat(sc.css("left"), 10);
			scleft += (appendNum * movingDistance);
			sc.css("left", scleft+"px");
			curPanel = totalPanels - appendNum - 1;
			
		}
		for (i=0;i<totalPanels;i++) {
			panelno = eval(i+1);
			$('#slider .scrollContainer > div').eq(i).attr("id","panel_"+panelno);
		}
	} //
	
	function randomXToY(minVal,maxVal,floatVal)
	{
	  var randVal = minVal+(Math.random()*(maxVal-minVal));
	  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	}
	
	/* Set up "Current" panel and next and prev */
	
	var random_slide = randomXToY(2,totalPanels-1);
    growBigger("#panel_" + random_slide);
    var curPanel = random_slide;
    if (curPanel < 2) {
        $(".scrollContainer").css({
            left: "108px"
        });
    }
    if (curPanel > 2) {
        var offset = -(curPanel - 2);
        var left_pos = offset * 108 + "px";
        $(".scrollContainer").css({
            left: left_pos
        });
    }	

	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	/* when the left/right arrows are clicked */
	$(".scrollButtons.right").click(function(){ 
		change(true);
	});//
	
	$(".scrollButtons.left").click(function(){ change(false); });
	
	$(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$(".scrollButtons.right").click();
				break;
			case 32: //space
				$(".scrollButtons.right").click();
				break;
	    case 37: //left arrow
				$(".scrollButtons.left").click();
				break;
			case 39: //right arrow
				$(".scrollButtons.right").click();
				break;
	  }
	}); //

});// end loaded jQuery
