/***************************************************************************************************************************************************
 	Author                  :           PA
	Version		     	    :	        1.0
	Date Created	        :
   
 	Change history 
 	
	Task/Bug 			Date (MM-DD-YYYY)		Author 		       Change Description
	
	Trac 1112		    12-08-2008				HAlagesan		   InitializeValues mothed is modified to add popId,pushId & prePushId.
	
	Color swatches does not  
	work when the same product 
	is assigned in more than 
	one subcategory  	05-13-2009				HAlagesan		   Since count parameter is appended with ProductId to form a unique Id , 
																   Product id(pId) is send explicitly to setSelectedColorForCategorySwatches() function and used in href.
****************************************************************************************************************************************************/

var productSelectorArray = new Array();

 function ProductSelector(pProductId, isLeaderProduct){

this.productId = pProductId;

this.colorList = new Array();

	this.addColorList=function(pColorList){
	
	for(var i=0;i<pColorList.length;i++){
	
	this.colorList.push(pColorList[i]);
	
		}

	}

		this.populateColorSwatches=function(pId){
		
		var htmlStr="<p class=\"swatches\">";
		
		if(getElement("swatches"+this.productId)!=null){
		
		for(var i=0;i<this.colorList.length;i++){ 

		htmlStr+="<a href=\"javascript:setSelectedColorForCategorySwatches('"+this.productId+"','"+this.colorList[i].prodImage+"','"+this.colorList[i].colorCode+"','"+this.colorList[i].colorName+"','"+pId+"');\"><img border=\"1\" id=\"swatches"+this.productId+"\" onmouseout=\"imgMouseOut(this,'"+this.colorList[i].colorCode+"')\" onmouseover=\"imgMouseOver(this,'"+this.colorList[i].colorCode+"')\" src=\""+this.colorList[i].imagePath+"\"  alt=\""+this.colorList[i].colorName+"\"/></a>";
		//"<a href=\"javascript:setSelectedColorForCategorySwatches('"+this.productId+"','"+this.colorList[i].prodImage+"');\"><img border=\"1\" id=\"swatches"+this.productId+"\" onmouseout=\"imgMouseOut(this,'"+this.colorList[i].colorCode+"') onmouseover=\"imgMouseOver(this,'"+this.colorList[i].colorCode+"')\" src=\""+this.colorList[i].imagePath+"\"  alt=\""+this.colorList[i].colorName+"\"/></a>";
		//"<img src=\""+this.colorList[i].imagePath+"\"/>";
		}
		
		}
		htmlStr+="</p>";
		getElement("swatches"+this.productId).innerHTML = htmlStr;
		}
		
} 

function getProductSelector(pProductId, createNew){

/*

* Loops through and returns the productselector object

* from the product selector array for the given product id

*/

for(var i=0;i<productSelectorArray.length;i++){ 

if(productSelectorArray[i].productId == pProductId){ 

return productSelectorArray[i];

}

}


if(createNew){

/* 

* If the createNew boolean flag is set to true and 

* product selector is not available for the given product id,

* then creates a new productselector object for the given product id and

* returns the newly created productselector object.

*/


productSelectorArray.push(new ProductSelector(pProductId)); 

return productSelectorArray[productSelectorArray.length - 1];

} else {

/*If the createNew boolean flag is not set to true and 

* product selector is not available for the given product id

* then return null

*/

return null;

} 


}

function initializeSwatches(pProductId, pId){
getElement("swatches"+pProductId).innerHTML = "<p class=\"swatches\"><img src=\"../images/swatches/TRANSITION.gif\"/></p>";
var productSelector = getProductSelector(pProductId, false);
productSelector.populateColorSwatches(pId);
}

function addColorList(pProductId,pColorList){ 
//alert("addSku"+pProductId+pIsLeaderProduct);

var productSelector = getProductSelector(pProductId, true);

productSelector.addColorList(pColorList);

}

function setSelectedColorForCategorySwatches(pProductId,prodImg,pColorCode,pColorName, pId){
   changeColorForSwatches(pProductId,prodImg,pColorCode,pColorName, pId);
}

function changeColorForSwatches(pProductId, prodImg,pColorCode,pColorName, pId){
	var uniqueId = "l"+pProductId;
	var viewAllinkHandle = "viewAllink"+uniqueId;
	var viewAldeslinkHandle = "viewAldeslink"+uniqueId;
	var viewAldislinkHandle = "viewAldislink"+uniqueId;
	var deslinkHandle = "deslink"+uniqueId;
	var imglinkHandle = "imglink" + uniqueId;

	if(prodImg != null && pProductId!=null){
		document["frml"+pProductId].color.value=pColorCode;
		document["imgl"+pProductId].src = prodImg;
		document["frml"+pProductId].colorName.value=pColorName;
	}
	// Change link on image
	if(getElement(imglinkHandle) != null){
		getElement(imglinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+prItemCount+'&id='+pId+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode+'&popId='+popId+'&pushId='+pushId+'&prepushId='+prepushId);
		}

	// Change link on text
	if(getElement(deslinkHandle) != null){
		getElement(deslinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+prItemCount+'&id='+pId+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode+'&popId='+popId+'&pushId='+pushId+'&prepushId='+prepushId);
		}
	
	if(getElement(viewAllinkHandle) != null){
		getElement(viewAllinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+prItemCount+'&id='+pId+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode+'&popId='+popId+'&pushId='+pushId+'&prepushId='+prepushId);
		}
		
	if(getElement(viewAldeslinkHandle) != null){
      getElement(viewAldeslinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+prItemCount+'&id='+pId+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode+'&popId='+popId+'&pushId='+pushId+'&prepushId='+prepushId);
      }
      
    if(getElement(viewAldislinkHandle) != null){
      getElement(viewAldislinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+prItemCount+'&id='+pId+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode+'&popId='+popId+'&pushId='+pushId+'&prepushId='+prepushId);
      }
      
    // added for snapshot; set selected swatch color
	if (eval("uo"+pProductId)) {
		var prodArr = eval("uo"+pProductId);
		prodArr["selectedColor"] = pColorCode;
	}	 
}

function ColorForSwatches(pColorCode, pColorName, pImagePath, pProdImage ){
     this.colorCode = pColorCode;
     this.colorName = pColorName;
     this.imagePath = pImagePath;
     this.prodImage = pProdImage;

}
 
function getElement(pElementId){
	return document.all? document.all[pElementId] : document.getElementById(pElementId);
}
// code modified for the Trac 1112.
function InitializeValues(pItemdescription,pItemCount,pParentid,pSortProperties,pNavCount,pNavAction,pPopId,pPushId,pPrepushId){
     this.itemdescription = pItemdescription;
     this.prItemCount = pItemCount;
     this.sortProperties = pSortProperties;
     this.navAction = pNavAction;
     this.parentid = pParentid;
     this.navCount = pNavCount;
	 this.popId = pPopId;
	 this.pushId = pPushId;
	 this.prepushId = pPrepushId;
}
// End of code modified for the Trac 1112.