$(document).ready(function() {

	var totalPanels			= $("#whatsNewList").children().size();
	var curPanel 			= 1;
	var movingDistance	    = 555;
	var $panels				= $('#slider_whats_new .scroll .whatsNewContainer #whatsNewList > div');
	var $container			= $('#slider_whats_new .scroll .whatsNewContainer');

	$panels.css({'float' : 'left','position' : 'relative'});

	$("#slider_whats_new").data("currentlyMoving", false);

	if(null != $panels && $panels.size() > 0){
		$(".scroll").css('display', 'block'); //to only show the assets after in completely loads
		$(".new_left").css('display', 'block');
		$(".new_right").css('display', 'block');
		$(".loadingDivWN").css('display', 'none');
		
		$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 130 )
		.css('left', "-10px");
	}

	var scroll = $('#slider_whats_new .scroll').css('overflow', 'hidden');
	
	$(".whatsNew_cntr").text(curPanel+"/"+totalPanels);

	//set up arrows
	if(totalPanels == 1){
		$(".new_right").css('display','none');
	}
	$(".new_left").css('display','none');
	
	if(null != $.session("whatsNewMarq")){
		//change_whats_new_manual(true,(movingDistance*($.session("whatsNewMarq")-1)),$.session("whatsNewMarq"),$.session("whatsNewMarq"));
		change_whats_new_manual(true,1,1,1); //edited -frank
	}
	
	//direction true = right, false = left
	function change_whats_new(direction) {
		
		//if not at the first or last panel
		if((direction && !(curPanel < (totalPanels))) || (!direction && (curPanel == 1))) { return false; }	

		//if not currently moving
		if (($("#slider_whats_new").data("currentlyMoving") == false)) {
			$("#slider_whats_new").data("currentlyMoving", true);
			
			curPanel = parseInt(curPanel, 10);
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".whatsNewContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
			
			if(direction){
				$(".whatsNew_cntr").text(next+"/"+totalPanels);
				$.session("whatsNewMarq", next);
			}else{
				$(".whatsNew_cntr").text(next+"/"+totalPanels);
				$.session("whatsNewMarq", next);
			}
			$(".whatsNewContainer")
			.stop()
			.animate({
				"left": movement
			},1000, function() {
				$("#slider_whats_new").data("currentlyMoving", false);
			});
			curPanel = next;
			
			//fade in if not on the side of selection
			if( curPanel > 1 ) {
				$(".new_left").fadeIn("slow");
			}	
			if(curPanel < totalPanels){
				$(".new_right").fadeIn("slow");
			}

			//fadeout is at edge of selections
			if( curPanel == totalPanels ) {
				$(".new_right").fadeOut("slow");
			}	
			if( curPanel == 1 ){
				$(".new_left").fadeOut("slow");
			}

		}
	}

	//direction true = right, false = left
	function change_whats_new_manual(direction,times,marquee,curpanel) {
		//if not at the first or last panel
		if((direction && !(curPanel < (totalPanels))) || (!direction && (curPanel <= 3))) { return false; }	

		//if not currently moving
		if (($("#slider_whats_new").data("currentlyMoving") == false)) {
			$("#slider_whats_new").data("currentlyMoving", true);

			curPanel = parseInt(curPanel, 10);
			var next         = curpanel;
			var leftValue    = $(".whatsNewContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - times : parseFloat(leftValue, 10) + times;

			if(direction){
				$(".whatsNew_cntr").text(marquee+"/"+totalPanels);
				$.session("whatsNewMarq", marquee);
			}
			
			$(".whatsNewContainer")
			.stop()
			.animate({
				"left": movement
			},1, function() {
				$("#slider_whats_new").data("currentlyMoving", false);
			});
			curPanel = next;

			//fade in if not on the side of selection
			if( curPanel > 1 ) {
				$(".new_left").fadeIn("fast");
			}	
			if(curPanel < totalPanels){
				$(".new_right").fadeIn("fast");
			}

			//fadeout is at edge of selections
			if( curPanel == totalPanels ) {
				$(".new_right").fadeOut("fast");
			}	
			if( curPanel == 1 ){
				$(".new_left").fadeOut("fast");
			}

		}
	}

	//when the left/right arrows are clicked
	$(".new_right").click(function(){ change_whats_new(true);return false;});	
	$(".new_left").click(function(){ change_whats_new(false);return false;});

});



