/*********************************************************************************
** simplydivine.com.au
** jquery.slider.js: This code is used for the Birthday and Special Events Widget
** 
**********************************************************************************/

$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
	
	// dimensions of the non-selected events (smaller)
	var regHeight			= $(".panel").css("height");        //40px
	var regWidth			= $(".panel").css("width");         //466px
	var regImgHeight		= $(".panel img").css("height");    //20px
	var regImgWidth 		= $(".panel img").css("width");     //24px
	var regImgPosition      = $(".panel img").css("left");      //44px
	var regLineHeight       = $(".panel h2").css("line-height"); //14px
	var regTitleSize		= $(".panel h2").css("font-size");  //10px
	var regParSize			= $(".panel p").css("font-size");   //8px
	
	var movingDistance	    = 40;
	
	// dimensions of the selected event (larger)
	var curHeight			= 75;
	var curWidth			= 80;
	var curImgHeight		= 57;
	var curImgWidth         = 67;
	var curImgPosition      = 0;
	var curLineHeight       = 26;
	var curTitleSize		= "16px";
	var curParSize			= "15px";

	var $panels				= $('#slider .scrollContainer > div');  // panels
	var $container			= $('#slider .scrollContainer');

	$panels.css({'position' : 'relative'});
    
	$("#slider").data("currentlyMoving", false);

	$container
		.css('height', ($panels[0].offsetHeight * $panels.length) + 100 )
		.css('top', "0px");

	var scroll = $('#slider .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$(element)
			.animate({ height: regHeight })
			.find("img")
			.animate({  left: regImgPosition, height: regImgHeight, width: regImgWidth })
		    .end()
		    .find("h2 a")
			.animate({ lineHeight: regLineHeight, fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ height: curHeight })
			.find("img")
			.animate({ left: curImgPosition, height: curImgHeight, width: curImgWidth })
		    .end()
		    .find("h2 a")
			.animate({ fontSize: curTitleSize, lineHeight: curLineHeight })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
	}
	
	//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);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var topValue     = $(".scrollContainer").css("top");
			var movement	 = direction ? parseFloat(topValue, 10) - movingDistance : parseFloat(topValue, 10) + movingDistance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"top": movement
				}, 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();
		}
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_2");	
	var curPanel = 2;
	
	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the up/down 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;
	  }
	});
	
});