$(document).ready(function() {

	var totalPanels			= $("#featuredAssetList").children().size();
	var curPanel 			= 3;
	var movingDistance	    = 525;
	var $panels				= $('#slider .scroll .scrollContainer #featuredAssetList > div');
	var $container			= $('#slider .scroll .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});

	$("#slider").data("currentlyMoving", false);

	if(null != $panels && $panels.size() > 0){
		$(".scroll").css('display', 'block'); //to only show the assets after in completely loads
		$(".right").css('display', 'block');
		$(".left").css('display', 'block');
		$(".loadingDivFA").css('display', 'none');
		
		$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 105 )
		.css('left', "0px");
	}

	var scroll = $('#slider .scroll').css('overflow', 'hidden');

	if(null != $.session("featAssetMarq")){
//		if($.session("featAssetMarq") == "6/9"){
//			change_featured_manual(true,525,$.session("featAssetMarq"),6);
//		}else if($.session("featAssetMarq") == "9/9"){
//			change_featured_manual(true,1050,$.session("featAssetMarq"),9);
//		}else{
			//set up arrows
			if(totalPanels <= 3){
				$(".right").css('display','none');
			}
			$(".left").css('display','none');
			
			$(".featured_asset_cntr").text("3/"+totalPanels);
//		}
	}else{
		//set up arrows
		if(totalPanels <= 3){
			$(".right").css('display','none');
		}
		$(".left").css('display','none');
		
		$(".featured_asset_cntr").text("3/"+totalPanels);
	}

	//direction true = right, false = left
	function change_featured(direction) {
		//if not at the first or last panel
		if((direction && !(curPanel < (totalPanels))) || (!direction && (curPanel <= 3))) { return false; }	

		//if not currently moving
		if (($("#slider").data("currentlyMoving") == false)) {
			$("#slider").data("currentlyMoving", true);

			var next         = direction ? curPanel + 3 : curPanel - 3;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;

			if(direction){
				$(".featured_asset_cntr").text(curPanel+3+"/"+totalPanels);
				$.session("featAssetMarq", curPanel+3+"/"+totalPanels);
			}else{
				$(".featured_asset_cntr").text(curPanel-3+"/"+totalPanels);
				$.session("featAssetMarq", curPanel-3+"/"+totalPanels);
			}
			$(".scrollContainer")
			.stop()
			.animate({
				"left": movement
			},1000, function() {
				$("#slider").data("currentlyMoving", false);
			});
			curPanel = next;

			//fade in if not on the side of selection
			if( curPanel > 3 ) {
				$(".left").fadeIn("slow");
			}	
			if(curPanel <= totalPanels){
				$(".right").fadeIn("slow");
			}

			//fadeout is at edge of selections
			if( direction && !(curPanel < (totalPanels)) ) {
				$(".right").fadeOut("slow");
			}	
			if( !direction && (curPanel <= 3) ){
				$(".left").fadeOut("slow");
			}

		}
	}

	//direction true = right, false = left
	function change_featured_manual(direction,times,marquee,curpanel_temp) {
		
		//if not at the first or last panel
		if((direction && !(curPanel < (totalPanels))) || (!direction && (curPanel <= 3))) { return false; }	

		//if not currently moving
		if (($("#slider").data("currentlyMoving") == false)) {
			$("#slider").data("currentlyMoving", true);

			curPanel = curpanel_temp
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - times : parseFloat(leftValue, 10) + times;

			if(direction){
				$(".featured_asset_cntr").text(marquee);
				$.session("featAssetMarq", marquee);
			}
			$(".scrollContainer")
			.stop()
			.animate({
				"left": movement
			},1, function() {
				$("#slider").data("currentlyMoving", false);
			});
			
			//fade in if not on the side of selection
			if( curPanel > 3 ) {
				$(".left").fadeIn("slow");
			}	
			if(curPanel <= totalPanels){
				$(".right").fadeIn("slow");
			}

			//fadeout is at edge of selections
			if( direction && !(curPanel < (totalPanels)) ) {
				$(".right").fadeOut("slow");
			}	
			if( !direction && (curPanel <= 3) ){
				$(".left").fadeOut("slow");
			}

		}
	}

	//when the left/right arrows are clicked
	$(".right").click(function(){ change_featured(true);return false;});	
	$(".left").click(function(){ change_featured(false);return false;});

});



