/*======================================================================================
 Home JS handler
 Created: TH - 03/11/2009
======================================================================================*/



//======================================================================================
// Handles Puma Home Pane Slider Widgets
//======================================================================================
ppanes = { 
	
	pane_builder: function(){
	
		
		//Set all of the buttons for initial state
		// checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 4);
		checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		checkButtons(false, "#new_arrows .dn a", "#new_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
		
		function gotoSelectedPane(iPaneNumber){
			iPainDifference =  iCurrentFeature - iPaneNumber;

			stMod = ((iPainDifference < 0)? '+' : '-');
			
			iMoveLocation = iPainDifference*968;
			
			$("#feature_panes_wrapper").animate({"left": "+="+iMoveLocation+"px"}, "slow");
			iCurrentFeature = iPaneNumber;
		}
		
		
		//Add an "all" class to any product that does not have a class
		$("#product_list_wrapper li").addClass("all");
		$("#product_list_wrapper li.accessories").removeClass("all");
		$("#product_list_wrapper li.apparel").removeClass("all");
		$("#product_list_wrapper li.footwear").removeClass("all");
		$("#product_list_wrapper li.women").removeClass("all");
		$("#product_list_wrapper li.men").removeClass("all");
		
		
		$("#all").click(function () {
			productTab('all');

			$(".accessories, .apparel, .footwear, .women, .men, .all").animate({width: "194px"}).show();
		
			iVisibleProducts = ($("#product_list_wrapper li").length);
			
			resetProductShift(".product_list li");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		});
		
		$("#accessories").click(function () {
			productTab('accessories');
			$(".apparel").animate({opacity: 'hide', width: "0px"});
			$(".footwear").animate({opacity: 'hide', width: "0px"});
			$(".women").animate({opacity: 'hide', width: "0px"});
			$(".men").animate({opacity: 'hide', width: "0px"});
			iVisibleProducts = ($("#product_list_wrapper li.accessories").length);
			$(".accessories, .all").animate({width: "194px", height: "210px"});
			
			resetProductShift(".accessories");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		})
		
		$("#apparel").click(function () {
			productTab('apparel');
			$(".accessories").animate({opacity: 'hide', width: "0px"});
			$(".footwear").animate({opacity: 'hide', width: "0px"});
			$(".women").animate({opacity: 'hide', width: "0px"});
			$(".men").animate({opacity: 'hide', width: "0px"});
			iVisibleProducts = ($("#product_list_wrapper li.apparel").length);
			$(".apparel, .all").animate({width: "194px", height: "210px"});
			
			resetProductShift(".apparel");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		})
		
		$("#footwear").click(function () {
			productTab('footwear');
			$(".accessories").animate({opacity: 'hide', width: "0px"});
			$(".apparel").animate({opacity: 'hide', width: "0px"});
			$(".women").animate({opacity: 'hide', width: "0px"});
			$(".men").animate({opacity: 'hide', width: "0px"});
			$(".footwear, .all").animate({width: "194px", height: "210px"});
				
			iVisibleProducts = ($("#product_list_wrapper li.footwear").length);
			
			resetProductShift(".footwear");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		})
		
		$("#women").click(function () {
			productTab('women');
			$(".accessories").animate({opacity: 'hide', width: "0px"});
			$(".apparel").animate({opacity: 'hide', width: "0px"});
			$(".footwear").animate({opacity: 'hide', width: "0px"});
			$(".men").animate({opacity: 'hide', width: "0px"});
			$(".women, .all").animate({width: "194px", height: "210px"});
			
			iVisibleProducts = ($("#product_list_wrapper li.women").length);
						
			resetProductShift(".women");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		});
		
		$("#men").click(function () {
			productTab('men');
			$(".women").animate({opacity: 'hide', width: "0px"});
			$(".accessories").animate({opacity: 'hide', width: "0px"});
			$(".apparel").animate({opacity: 'hide', width: "0px"});
			$(".footwear").animate({opacity: 'hide', width: "0px"});
			$(".men").animate({opacity: 'hide', width: "0px"});
			$(".men, .all").animate({width: "194px", height: "210px"});

			iVisibleProducts = ($("#product_list_wrapper li.men").length);
			
			resetProductShift(".men");
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		  return true;
		});
		
		function resetProductShift(iVisibleProducts){
			iShiftAmount = (iProductsRight-5)*194;
			
			//console.log('iShiftAmount = ' + iShiftAmount);
			
			//iShiftAmount = iProductsRight*194;
			$("#product_list_wrapper").animate({"left": "+="+iShiftAmount+"px"}, "slide");
			iProductsRight = 5;
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
		}
		
		function productTab(theTab){
			$("#products_selection li").removeClass("selected");
			$("#" + theTab).addClass("selected");
		}
		
		/* $("#feature_arrows .rev a").click(function(event){
			controlClick('stop');
			iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'left', iTotalFeatures, 3, iFeaturesRight);
			checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);
			return false;
		}); */
		
		/* $("#feature_arrows .fwd a").click(function(event){
			controlClick('stop');
			iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'right', iTotalFeatures, 3, iFeaturesRight);
			checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);
			return false;
		}); */
		
		
		
		$("#product_arrows .rev a").click(function(event){
			iProductsRight = moveFrames("#product_list_wrapper", 194, 'left', iVisibleProducts, 5, iProductsRight);
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
			return false;
		});
		
		$("#product_arrows .fwd a").click(function(event){
			iProductsRight = moveFrames("#product_list_wrapper", 194, 'right', iVisibleProducts, 5, iProductsRight);
			checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
			return false;
		});
		
		
		$("#new_arrows .dn a").click(function(event){
			iNewsBottom = moveFrames("#news_wrapper", 134, 'down', iTotalNewsFeatures, 3, iNewsBottom);
			checkButtons(false, "#new_arrows .dn a", "#new_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
			return false;
		});
		
		$("#new_arrows .up a").click(function(event){
			iNewsBottom = moveFrames("#news_wrapper", 134, 'up', iTotalNewsFeatures, 3, iNewsBottom);
			checkButtons(false, "#new_arrows .dn a", "#new_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
			return false;
		});


		$("#puma_event_arrows .rev a").click(function(event){
			iEventBottom = moveFrames("#puma_event_wrapper", 326, 'left', iTotalEventFeatures, 1, iEventBottom);
			checkButtons(true, "#puma_event_arrows .rev a", "#puma_event_arrows .fwd a", iEventBottom, iTotalEventFeatures, 1);
			return false;
		});
		
		$("#puma_event_arrows .fwd a").click(function(event){
			iEventBottom = moveFrames("#puma_event_wrapper", 326, 'right', iTotalEventFeatures, 1, iEventBottom);
			checkButtons(true, "#puma_event_arrows .rev a", "#puma_event_arrows .fwd a", iEventBottom, iTotalEventFeatures, 1);
			return false;
		});

		
		//this script was disabled, but can be used if uncommented
		//it's a method for moving the main picture based on what's clicked
		
		
		
		/* $('#features_list li').bind('click', function(event){
			$(document).stopTime("features");
			
			$('#features_list li').each(function(){
				$(this).removeClass('selected_feature');
			});
			$(this).addClass('selected_feature'); 
			
			stFeatureSelected = $(this).attr('id').substr(13);
			iFeatureSelected = parseFloat(stFeatureSelected);
			gotoSelectedPane(iFeatureSelected);
			return false;
		}) */
		
		
		
		
	
	},
	
	/* pane_auto_play: function(){
	
			isLooping = 1;
			inc = 0;
			
			checkFeatures = function() {
				
				$("#features_list li").removeClass("selected_feature");
				$("#features_list li").eq(iCurrentFeature).addClass("selected_feature");
				
				//3 at a time
				if (iCurrentFeature < iTotalFeatures) {
					if(iCurrentFeature > 0 && iCurrentFeature%3 == 0){
						iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'right', iTotalFeatures, 3, iFeaturesRight);
					}
					if (iCurrentFeature == 0 && inc > 0) {
						iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'zero', iTotalFeatures, iTotalFeatures, iFeaturesRight);
					}
				}

				checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);

				inc++;
				
			}

			slideTo = function(pos) {
				currentFeature = pos;
				pos = (pos) * -968;
				pos = pos + "px";
				$("#feature_panes_wrapper").animate({ 
					left: pos
				}, 500 );
				checkFeatures();
			}
			
			$("#feature_panes li a").click(function () {
				controlClick('stop');
			});
			
			
			nextFeature = function() {
				if (iCurrentFeature < iTotalFeatures-1 ) {
					iCurrentFeature =  ++iCurrentFeature;
					slideTo(iCurrentFeature);
				} else {
					iCurrentFeature = 0;
					slideTo(0);
				}
			}
			
			checkFeatures();

			$(document).everyTime(5000, "features", function(i) {
				nextFeature();
			});
			
		
			$("#loop_controls").click(function () { controlClick() });
																				 
			controlClick = function (request) {
				if ((isLooping == 1) || (request == "stop")) {
					$(document).stopTime("features");
					$("#loop_controls").addClass('paused');
					isLooping = 0;
				} else {
					$("#loop_controls").removeClass('paused');
					$(document).everyTime(5000, "features", function(i) {
						nextFeature();
					});
					isLooping = 1;
				}
			};
	}, */
	
	get_latest_listener: function(){
		$('#news_items li').bind('click', function(){
			window.location = this.title;
			//console.log(this.title);
		})
	},
	
	
	
	init: function(){
		// iCurrentFeature = 0;
		// iCurrentPane = 0;
	
		// iTotalFeatures = $("#features_list li").length;
		// iFeaturesRight = 3; // Keep this many visible in frame
		
		iTotalProducts = $("#product_list_wrapper li").length;
		iProductsRight = 5; // Keep this many visible in frame
		iVisibleProducts = iTotalProducts;
		
		iTotalNewsFeatures = $("#news_wrapper li").length;
		iNewsBottom = 3; // Keep this many visible in frame

		iTotalEventFeatures = $("#puma_event_panes li").length;
		iEventBottom = 1; // Keep this many visible in frame
		
		//iTotalProducts = ($("#product_list_wrapper li").length);
		//iTotalNewsFeatures = ($("#news_wrapper li").length);
		//iVisibleProducts = iTotalProducts;
		//iCurrentFeature = 0;
		//iCurrentPane = 0;
		//iFeaturesRight = 3;
		//iProductsRight = 4;
		//iNewsBottom = 3;
		
		
		ppanes.pane_builder();
		// ppanes.pane_auto_play();
		ppanes.get_latest_listener();
	}
}



function moveFrames(stFrameID, iFrameWidth, stDirection, iTotalQuantity, iMoveAmount, iCurrentEdgeElement){
			
	if(stDirection == 'right'){
		iFeaturesLeft = iTotalQuantity-(iCurrentEdgeElement);
		
		if(iFeaturesLeft <= 0){
			return iCurrentEdgeElement;
		}
		
		if(iMoveAmount >= iFeaturesLeft){
			iMoveAmount = (iFeaturesLeft);
		}
		
		iCurrentEdgeElement += iMoveAmount;
		if(iCurrentEdgeElement >= iTotalQuantity){
			iCurrentEdgeElement = iTotalQuantity;
		}
		
		iFullMove = iMoveAmount*iFrameWidth;
		$(stFrameID).animate({"left": "-="+iFullMove+"px"}, "slow");
		
		
		
	}else if(stDirection == 'left'){
		
		if(iCurrentEdgeElement == iMoveAmount){
			return iCurrentEdgeElement;
			
		}else if((iCurrentEdgeElement-iMoveAmount) > iMoveAmount){
			iShiftAmount = iMoveAmount;
			
		}else{
			iShiftAmount = (iCurrentEdgeElement-iMoveAmount);
		}

		iFullMove = iShiftAmount*iFrameWidth;
		$(stFrameID).animate({"left": "+="+iFullMove+"px"}, "slow");

		iCurrentEdgeElement -= iShiftAmount;
				
		
	}else if(stDirection == 'up'){
		
		iFeaturesLeft = iTotalQuantity-(iCurrentEdgeElement);
		
		if(iFeaturesLeft <= 0){
			return iCurrentEdgeElement;
		}
		
		if(iMoveAmount >= iFeaturesLeft){
			iMoveAmount = (iFeaturesLeft);
		}
		
		iCurrentEdgeElement += iMoveAmount;
		if(iCurrentEdgeElement >= iTotalQuantity){
			iCurrentEdgeElement = iTotalQuantity;
		}

		
		iFullMove = iMoveAmount*iFrameWidth;

		$(stFrameID).animate({top: "-="+iFullMove+"px"}, "slow");
		
		iCurrentEdgeElement;
		
	}else if(stDirection == 'down'){
		
		if(iCurrentEdgeElement == iMoveAmount){
			return iCurrentEdgeElement;
			
		}else if((iCurrentEdgeElement-iMoveAmount) > iMoveAmount){
			iShiftAmount = iMoveAmount;
			
		}else{
			iShiftAmount = (iCurrentEdgeElement-iMoveAmount);
		}
		
		iFullMove = iShiftAmount*iFrameWidth;
		
		$(stFrameID).animate({top: "+="+iFullMove+"px"}, "slow");

		iCurrentEdgeElement -= iShiftAmount;
		
	}else{
		iCurrentEdgeElement = 3;
		$(stFrameID).animate({"left": "0px"}, "slow");
	}
	
	return iCurrentEdgeElement;
	
}

function checkButtons(isHorizontal, stLeftArrowID, stRightArrowID, iRightFeature, iTotalFeatures, iQuantityVisible){
	
	if(isHorizontal){
		if(iRightFeature >= iTotalFeatures){
			$(stRightArrowID).removeClass('on').addClass('off');
			$(stRightArrowID).show();
		}else{
			$(stRightArrowID).removeClass('off').addClass('on');
			$(stRightArrowID).show();
		}
		
		if((iRightFeature-iQuantityVisible) > 0){
			$(stLeftArrowID).removeClass('off').addClass('on');
			$(stLeftArrowID).show();
		}else{
			$(stLeftArrowID).removeClass('on').addClass('off');
			$(stLeftArrowID).show();
		}
		
	}else{
		if(iRightFeature > (iTotalFeatures-1)){
			$(stRightArrowID).removeClass('on').addClass('off');
			$(stRightArrowID).show();
		}else{
			$(stRightArrowID).removeClass('off').addClass('on');
			$(stRightArrowID).show();
		}
		
		if((iRightFeature-iQuantityVisible) > 0){
			$(stLeftArrowID).removeClass('off').addClass('on');
			$(stLeftArrowID).show();
		}else{
			$(stLeftArrowID).removeClass('on').addClass('off');
			$(stLeftArrowID).show();
		}
		
	}
}




$(document).ready(function() {
	ppanes.init();
	
	$('#features_list li').click(function () {
		window.location = $(this).find('a').attr('href');
	})
	
});
