var swatchSelector = new function(){
	var swatchSelectors = [];
	var swatchTimers = [];
	
	this.hilight = function(productId,styleId,fullscreen){		
		if(!swatchSelectors[productId]){
			swatchSelectors[productId] = new SwatchSelector(productId);
		}
		swatchSelectors[productId].setStyleId(styleId);
		swatchSelectors[productId].setFullscreen(fullscreen);
		
		if(swatchTimers[productId]){
			clearTimeout(swatchSelectors[productId]);
		}
		
		var temp = swatchSelectors[productId].animate;
		swatchTimers[productId] = setTimeout(temp,200);
	}
	
	function SwatchSelector(myProductId){
		var productId = myProductId;
		var styleId = null;
		var fullscreen = null;
		
		this.animate = function(){
			animate(productId,styleId,fullscreen);
		}
		
		this.setStyleId = function(myStyleId){
			if(myStyleId){
				styleId = myStyleId;
			}
			else{
				styleId = null;
			}
		}
		this.setFullscreen = function(myFullscreen){
			if(myFullscreen == "fullscreen"){
				fullscreen = myFullscreen;
			}
			else{
				fullscreen = null;
			}
		}
	}
	
	function animate(productId,styleId,fullscreen){
		// Handle cases where there are no swatch selectors found (example custom item with no colors displayed).
		// Otherwise we do encounter javascript errors. target.position() fails, and it does not fail gracefully.
		var selection;
		if(fullscreen){
			selection = $("#colorChooser_"+productId+"_FULLSCREEN");	
		}
		else if(styleId && productId.toString().indexOf("99999999")!=-1){ // gift card style selector	
			selection = $(".pp-gift-card-selector > div[id*='featureTypeSelector']:visible");
		}
		else if(styleId){
			selection = $("#bundleMinimizedSwatches_"+productId+", #colorChooser_"+productId+"_"+styleId+", #bundleColorChooser_"+productId+"_"+styleId);
		}else{
			selection = $("#bundleMinimizedSwatches_"+productId);	
		}

		$(selection).each(function(i){		
			if($(this).width() != 0){ // only operate on visible swatch selectors
				var target = $(this).find("a.pp-selected, a.pp-available-selected");
				if(target.position()){
					var hilight = $(this).find(".pp-swatch-hilight, .pp-gift-card-hilight");
					
					var targetColor = "#05173D";
					var time = 800;
					
					if(target.hasClass("pp-unavailable")){	
						targetColor = "#CCCCCC";	
					}	
					else if(target.hasClass("pp-backorder")){	
						targetColor = "#FF8800";	
					}
					
					var Xtarget = target.position().left;	
					if(Xtarget == 0){ // ie6 offset issue
						Xtarget = target.parent().position().left; 
					}										
					
					var Ytarget = target.position().top + 25;
					if(hilight.hasClass("pp-gift-card-hilight")){
						Ytarget += 40;
					}
					if(Ytarget == 25){ // ie6 offset issue
						if(target.parent().parent())
						Ytarget = target.parent().position().top + target.parent().parent().position().top + 15; 
					}
					if(hilight.css("top")!=Ytarget+"px"){
						hilight.stop();
						
						hilight.fadeTo(0,0);
						hilight.css({top: Ytarget, left: Xtarget});			
						hilight.css("background-color",targetColor);
						hilight.fadeTo(400,1);
					}
					else if(hilight.position().left != Xtarget){
						hilight.stop();
						hilight.fadeTo(0,1);
						
						// tries to even out speed of slider by evaluating distance traveled and updating the time
						time = (hilight.position().left - Xtarget)*6;						
						if(time<0){time = time * -1;};
						if(time<400){time = 400;}else if(time > 1500){ time = 1500};
						if(hilight.hasClass("pp-gift-card-hilight")){
							time = Math.round(time/2);
						}
						hilight.animate({left: Xtarget, top: Ytarget }, time, function(){$(this).css("background-color",targetColor);});
					}
				}
			}
		});
	}
}




