$(function() {
	
	/**
	 * jQuery Slider - Brenton Bull (Fraynework Multimedia) 2009-12-21
	 */
	var movingWidth = 329;
    var resetLocation = movingWidth + 1;
    
    var $panels = $('#slider .scrollContainer .panel');
    var $container = $('.scrollContainer');
    
    var $noPanels = $panels.length;

    $container
        .css('width', ($panels[0].offsetWidth * $panels.length) + 100000);
    
    for ($i = 3; $i <= $noPanels; $i++) {
        tmpMoveWidth = (($i - 2) * movingWidth);
        $("#panel_" + $i).animate({ right: "+=" + tmpMoveWidth + "px" }, { queue: false, duration: 0 });
    }
    var currentPanel = 1;
    /**
     * Show todays Program Guide
     */
	var $today = new Date();
	var $thisDay = $today.getDay();	
	
	if($thisDay != 1)
	{
		$("#panel_1")
			.animate({ right: "-=" + movingWidth + "px" }, { queue: false, duration: 0 });
		$("#panel_" + $thisDay)
			.animate({ right: "+=" + movingWidth + "px" }, { queue: false, duration: 0 });
		 currentPanel = $thisDay;
	}
    
   
    var nextPanel;
    var currentlyMoving = false;
    
    function change(direction)
    {
    	if (currentlyMoving == false) 
    	{
    		currentlyMoving = true;
            
    		if(direction)
    		{
    			nextPanel = currentPanel + 1;
    		
	            if (nextPanel > $noPanels)
	                nextPanel = 1;
	            
	
	            var $tempNext = $("#panel_" + nextPanel);
	            var $tempCurr = $("#panel_" + currentPanel);
	
	            tmpMoveWidth = (nextPanel * movingWidth) - movingWidth;
	
	            $tempNext.animate({ right: "+=" + movingWidth + "px" }, { queue: true, duration: 500 });
	            $tempCurr
	            .animate({ right: "+=" + movingWidth + "px" }, { queue: true, duration: 500, complete: function() { currentlyMoving = false; } })
	            .animate({ right: "-=" + (2 * movingWidth) + "px" }, { queue: true, duration: 0 })
	            ;
	            currentPanel = nextPanel;
    		} else
    		{
    			nextPanel = currentPanel - 1;
    			if (nextPanel <= 0)
                	nextPanel = $noPanels;
    			
    			var $tempNext = $("#panel_" + nextPanel);
	            var $tempCurr = $("#panel_" + currentPanel);
	            
	            $tempNext
	            .animate({ right: "+=" + (2 * movingWidth) + "px" }, { queue: true, duration: 0 })
	            .animate({ right: "-=" + (movingWidth) + "px" }, { queue: true, duration: 500 });
	            
	            $tempCurr
	            .animate({ right: "-=" + movingWidth + "px" }, { queue: true, duration: 500, complete: function() { currentlyMoving = false; } });
	            
    			currentPanel = nextPanel;
    		}
    	}
    }
    
    /**
     * End Slider
     */
		
	//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;
	  }
	});
	
});
