/**
* @fileoverview ebiz.js: A module used for client specific functionality
*
* This module defines a single symbol named "Venda.Ebiz"
* all ebiz utility functions are stored as properties of this namespace
* functions that are spacific this site shoudl be added to this file only.
*/

//Declare namespace for ebiz
Venda.namespace("Ebiz");

 /**
 * The following global variables (directly below) are NEEDED to support legacy javascript functions - DO NOT REMOVE! see RT#113376 for more details.
 * 1. shown
 * 2. hidden
 */

var shown = new Image();
shown.src = "/venda-support/images/bulleton.gif";
var hidden = new Image();
hidden.src = "/venda-support/images/bulletoff.gif";

 /**
 * Split a string so it can be displayed on multiple lines so it does not break display layout - used on order confirmation and order receipt page
 * @param {string} strToSplit string that needs to be split 
 * @param {Integer} rowLen length of row which will hold the string
 * @param {string} displayElem the html container which will display the splitted string
 */
Venda.Ebiz.splitString = function(strToSplit, rowLen, dispElem) {
	var stringlist = new Array();
	while (strToSplit.length > rowLen) {
	   stringlist.push( strToSplit.slice(0,rowLen));
	   strToSplit=strToSplit.substr(rowLen);
	}
	if (strToSplit.length) {
		stringlist.push(strToSplit);
	}
	document.getElementById(dispElem).innerHTML = stringlist.join('<br>');
};

 /**
 * A skeleton function for validating user extened fields - needs to be amended by the build team
 * @param {object} frmObj HTML form containing user extended field elements
 */
Venda.Ebiz.validateUserExtendedFields = function(frmObj) {
	return true;
};

/* ----- Attribute Swatch Function : support up to 2 attributes (ie. colour and size) -----*/
  	 
  	 Venda.namespace("Ebiz.AttributeSwatch");
  	 Venda.Ebiz.AttributeSwatch.ListAttributes = new Array();
  	 Venda.Ebiz.AttributeSwatch.filters = new Array();
  	 Venda.Ebiz.AttributeSwatch.existingAttributes = new Array();
  	 Venda.Ebiz.AttributeSwatch.availAttributes = new Array();
  	 Venda.Ebiz.AttributeSwatch.defaultprice = "";
  	 Venda.Ebiz.AttributeSwatch.defaultwasprice = "";
  	 Venda.Ebiz.AttributeSwatch.noswatch = "/content/ebiz/animal/resources/images/noswatch.gif";
  	 
  	 Venda.Ebiz.AttributeSwatch.initListAttributes = function(attrColumn, attrName) {
  	         for (var eachKey in product.attributeValues) {
  	                 if (typeof product.attributeValues[eachKey] != "function") {
  	                         this.addToListAttributes(attrColumn,product.attributeValues[eachKey].values[attrColumn]);
  	                 }
  	         }
  	         Venda.Ebiz.AttributeSwatch.displayListAttributes(attrColumn, attrName);
  	         if(this.attrNum==1){Venda.Ebiz.AttributeSwatch.checkAvailOneAttributes(attrColumn, attrName);}
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.addToListAttributes = function(attrColumn, attrValue) {
  	         if (!this.isExistInListAttributes(attrColumn,attrValue)) {
  	                 if (!this.ListAttributes[attrColumn]) {
  	                         this.ListAttributes[attrColumn] = new Array();
  	                 }
  	                 this.ListAttributes[attrColumn].push(attrValue);
  	 
  	         }
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.isExistInListAttributes = function(attrColumn, attrValue) {
  	         var found = false;
  	         if (this.ListAttributes[attrColumn]) {
  	                 for (var eachValue in this.ListAttributes[attrColumn]) {
  	                         if (this.ListAttributes[attrColumn][eachValue] == attrValue) {
  	                                 found = true;
  	                                 break;
  	                         }
  	                 }
  	         }
  	         return found;
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.createListAttributes=function(attrColumn,attrName,ddObj){
  	         this.ListAttributes[attrColumn] = new Array();
  	         for(i=0; i < ddObj.options.length ; i++){
  	                 this.ListAttributes[attrColumn].push(ddObj.options[i].value);
  	         }
  	         Venda.Ebiz.AttributeSwatch.displayListAttributes(attrColumn, attrName);
  	         if(this.attrNum==1){Venda.Ebiz.AttributeSwatch.checkAvailOneAttributes(attrColumn, attrName);}
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.displayListAttributes = function (attrColumn, attrName){
  	         var str = "<ul class=attribute_"+attrColumn+">";
  	         var chkString = "";
  	         for(var i=0; i < this.ListAttributes[attrColumn].length; i++){
  	                 // if att=color use image for swatch
  	                 if (attrName.toLowerCase() == "colour" || attrName.toLowerCase() == "color") {
  	                         if (!Venda.Ebiz.ProductDetail.allImages[this.ListAttributes[attrColumn][i]] ||  Venda.Ebiz.ProductDetail.allImages[this.ListAttributes[attrColumn][i]].setswatch == "") {
  	 
  	                         str += "<li class=\"swatch\"><a class=available id='swatch"+this.ListAttributes[attrColumn][i]+"' onmouseover=\"Venda.Ebiz.AttributeSwatch.showTooltipMessage('swatch"+this.ListAttributes[attrColumn][i]+"');\" onmouseout=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onmouseup=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onclick=\"Venda.Ebiz.AttributeSwatch.actionSet('"+attrColumn+"','"+this.ListAttributes[attrColumn][i]+"'); Venda.Ebiz.AttributeSwatch.changePrice('attr-sellprice','attr-wasprice'); Venda.Ebiz.ProductDetail.changeSet('"+this.ListAttributes[attrColumn][i]+"'); return false;\"><span class=\"swatchattribute\">"+this.ListAttributes[attrColumn][i]+"</span></a></li>";
  	                         } else {
  	                                 // has swatch image
  	                                 str += "<li class=\"swatch\"><a class=available id='swatch"+this.ListAttributes[attrColumn][i]+"' title=\""+this.ListAttributes[attrColumn][i]+"\" onmouseover=\"Venda.Ebiz.AttributeSwatch.showTooltipMessage('swatch"+this.ListAttributes[attrColumn][i]+"');\" onmouseout=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onmouseup=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onclick=\"Venda.Ebiz.AttributeSwatch.actionSet('"+attrColumn+"','"+this.ListAttributes[attrColumn][i]+"'); Venda.Ebiz.AttributeSwatch.changePrice('attr-sellprice','attr-wasprice'); Venda.Ebiz.ProductDetail.changeSet('"+this.ListAttributes[attrColumn][i]+"'); return false;\"><img class=\"swatchimage\" src=\""+Venda.Ebiz.ProductDetail.allImages[this.ListAttributes[attrColumn][i]].setswatch+"\" alt=\""+this.ListAttributes[attrColumn][i]+"\"></a></li>";
  	                         }
  	 
  	                 } else {
  	                         str += "<li><a class=available id='swatch"+this.ListAttributes[attrColumn][i]+"' onmouseover=\"Venda.Ebiz.AttributeSwatch.showTooltipMessage('swatch"+this.ListAttributes[attrColumn][i]+"');\" onmouseout=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onmouseup=\"Venda.Ebiz.AttributeSwatch.hideTooltipMessage();\" onclick=\"Venda.Ebiz.AttributeSwatch.actionSet('"+attrColumn+"','"+this.ListAttributes[attrColumn][i]+"'); Venda.Ebiz.AttributeSwatch.changePrice('attr-sellprice','attr-wasprice'); return false;\">"+this.ListAttributes[attrColumn][i]+"</a></li>";
  	                 }
  	         }
  	         str = str + "</ul>";
  	         document.getElementById("productdetail-"+attrColumn).innerHTML = str;
  	 };
  	 
  	 
  	 Venda.Ebiz.AttributeSwatch.addFilter = function(attrColumn,attrValue) {
  	         var filterString="";
  	         this.filters[attrColumn] = attrValue;
  	         // clear background alert message
  	         document.getElementById("alertmessage").className = "normal";
  	         // update alert message
  	         Venda.Ebiz.AttributeSwatch.updateMessage();
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.updateMessage = function(){
  	         document.getElementById("alertmessage").style.display="none";
  	         if(this.attrNum==1){
  	                 if(this.filters["att1"]!=""){
  	                         document.getElementById("alertmessage").innerHTML = this.filters['att1'];
  	                         document.getElementById("skuHeader").innerHTML = "Code: "+product.attributeValues[this.filters["att1"]].data["atrsku"];
  	                 }
  	         }else{
  	                 if(this.filters["att1"]=="" || this.filters["att1"]==undefined){
  	                         document.getElementById("alertmessage").innerHTML = "<span class=sizeselected>" + this.attrDisplayName[1] + "</span> "+ this.filters['att2'] + " - Please select a " + this.attrDisplayName[0] + ".";
  	                         document.getElementById("skuHeader").innerHTML='';
  	                 }else if(this.filters["att2"]=="" || this.filters["att2"]==undefined){
  	                         document.getElementById("alertmessage").innerHTML = this.filters['att1'] + " - Please select a " + this.attrDisplayName[1] + ".";
  	                         document.getElementById("skuHeader").innerHTML='';
  	                 }
  	                 if((this.filters["att1"]!="") && (this.filters["att2"]!="") && (this.filters["att1"]!=undefined) && (this.filters["att2"]!=undefined)){
  	                         document.getElementById("alertmessage").innerHTML = this.filters["att1"]+", <span class=sizeselected>" + this.attrDisplayName[1] +"</span> " + this.filters["att2"];
  	                         if(product.attributeValues[this.filters["att1"]+this.filters["att2"]]){
  	                                 document.getElementById("skuHeader").innerHTML = "Code: "+product.attributeValues[this.filters["att1"]+this.filters["att2"]].data["atrsku"];
  	                         }
  	                 }
  	         }
  	         //add Attr Colour under price
  	         if(this.filters["att1"]!=""){
  	                 document.getElementById("colourHeader").innerHTML = this.filters['att1'];
  	         }else{
  	                 document.getElementById("colourHeader").innerHTML = '';
  	         }
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.validateAttributes = function(){
  	         var isSelected = true;
  	         if(this.attrNum==1){
  	                 if(this.filters["att1"]==undefined || this.filters["att1"]==""){
  	                         document.getElementById("alertmessage").className = "warning";
  	                         document.getElementById("alertmessage").style.display="inline";
  	                         isSelected = false;
  	                 }else{
  	                         document.getElementById("alertmessage").style.display="none";
  	                 }
  	         }else{
  	                 if(this.filters["att1"]==undefined || this.filters["att2"]==undefined || this.filters["att1"]=="" || this.filters["att2"]==""){
  	                         document.getElementById("alertmessage").className = "warning";
  	                         document.getElementById("alertmessage").style.display="inline";
  	                         isSelected = false;
  	                 }else{
  	                         document.getElementById("alertmessage").style.display="none";
  	                 }
  	         }
  	         return isSelected;
  	 };
  	 
  	 // Check if attribute exist and has onhand
  	 Venda.Ebiz.AttributeSwatch.checkAvailAttributes = function(attrColumn,attrValue) {
  	         this.existingAttributes = new Array();
  	         this.availAttributes = new Array();
  	         var attrColumnSelect="";
  	         switch(attrColumn){
  	                 case "att1": attrColumn="att2";attrColumnSelect="att1";break;
  	                 case "att2": attrColumn="att1";attrColumnSelect="att2";break;
  	         }
  	         a=0;
  	         var str="<ul class=attribute_"+attrColumn+">";
  	         for (var eachAttrSet in product.attributeValues) {
  	                 if(product.attributeValues[eachAttrSet].values[attrColumnSelect]==attrValue && (product.attributeValues[eachAttrSet].data["atronhand"]>0)){
  	                         this.existingAttributes[a]=product.attributeValues[eachAttrSet].values[attrColumn];
  	                         a++;
  	                 }
  	         }
  	         this.updateListAttributes(attrColumn);
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.checkAvailOneAttributes = function(attrColumn, attrName) {
  	         this.existingAttributes = new Array();
  	         this.availAttributes = new Array();
  	         a=0;
  	         var str="<ul class=attribute_"+attrColumn+">";
  	         for (var eachAttrSet in product.attributeValues) {
  	                 if(product.attributeValues[eachAttrSet].data["atronhand"]>0){
  	                         this.existingAttributes[a]=product.attributeValues[eachAttrSet].values[attrColumn];
  	                         a++;
  	                 }
  	         }
  	         this.updateListAttributes(attrColumn);
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.updateListAttributes = function(attrColumn) {
  	         //compare existingAttributes with the full range
  	         for(i=0; i < this.ListAttributes[attrColumn].length; i++){
  	                 // if there is no any existingAttributes (ie. all out of stock)
  	                 if(this.existingAttributes.length==0){this.availAttributes[i] = false;}
  	                 for(j=0; j < this.existingAttributes.length; j++){
  	                         if(this.ListAttributes[attrColumn][i]==this.existingAttributes[j]){
  	                                 this.availAttributes[i] = this.existingAttributes[j];
  	                                 break;
  	                         }else{
  	                                 this.availAttributes[i] = false;
  	                         }
  	                 }
  	 
  	                 if(this.availAttributes[i] !== false){
  	                         if(this.ListAttributes[attrColumn][i]==this.filters[attrColumn]){
  	                                 document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className="selected";
  	                         }else{
  	                                 document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className="available";
  	                         }
  	                 }else{
  	                         document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className="notavail";
  	                         if(this.attrNum==1){
  	                                 // if has only one attr - unclickable out of stock attribute
  	                                 document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).attributes["onclick"].value="";
  	                                 document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).style.cursor="default";
  	                         }
  	                         if(this.ListAttributes[attrColumn][i]==this.filters[attrColumn]){
  	                                 // clear if no combination
  	                                 this.filters[attrColumn]="";
  	                                 Venda.Ebiz.AttributeSwatch.addFilter(attrColumn,"");
  	                                 document.form.elements[attrColumn].value = "";
  	                         }
  	                 }
  	         }
  	 };
  	 
  	 
  	 // Highlight selected option
  	 Venda.Ebiz.AttributeSwatch.highlightSelection = function(attrColumn,id){
  	         for(i=0; i < this.ListAttributes[attrColumn].length; i++){
  	                 if(this.ListAttributes[attrColumn][i] == id){
  	                         document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className = "selected";
  	 
  	                 }else{
  	                         if(document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className != "notavail"){
  	                                 document.getElementById("swatch"+this.ListAttributes[attrColumn][i]).className = "available";
  	                         }
  	                 }
  	         }
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.changePrice = function(id, wasid){
  	         var price = "";
  	         var wasprice = "";
  	 
  	         if (this.attrNum == 1) {
  	                 for (var eachAttrSet in product.attributeValues) {
  	                         if(product.attributeValues[eachAttrSet].values["att1"]==this.filters["att1"]){
  	                                 price = product.attributeValues[eachAttrSet].data["atrsell"];
  	                                 wasprice = product.attributeValues[eachAttrSet].data["atrwas"];
  	                         }
  	                 }
  	         } else  if (this.attrNum == 2) {
  	                 for (var eachAttrSet in product.attributeValues) {
  	                         if(product.attributeValues[eachAttrSet].values["att1"]==this.filters["att1"] && product.attributeValues[eachAttrSet].values["att2"]==this.filters["att2"]){
  	                                 price = product.attributeValues[eachAttrSet].data["atrsell"];
  	                                 wasprice = product.attributeValues[eachAttrSet].data["atrwas"];
  	                         }
  	                 }
  	         }
  	 
  	         if (price == "") { price = this.defaultprice;}
  	         if (wasprice == "") { wasprice = this.defaultwasprice; }
			 price = parseFloat(price).toFixed(2).toString();
			 wasprice = parseFloat(wasprice).toFixed(2).toString();
  	         if (price != "") {
  	                 document.getElementById(id).innerHTML = product.labels['currsym'] + price;
  	         }
  	         if (wasprice!="" && parseFloat(wasprice)>parseFloat(price)) {
  	                 document.getElementById(wasid).innerHTML = product.labels['atrwas']+product.labels['currsym'] + wasprice;
  	         }else{
  	                 document.getElementById(wasid).innerHTML = "";
  	         }
  	 };
  	 
  	 Venda.Ebiz.AttributeSwatch.actionSet = function(attrColumn,attrValue){
  	         document.form.elements[attrColumn].value = attrValue;
  	         this.addFilter(attrColumn,attrValue);
  	         this.highlightSelection(attrColumn,attrValue);
  	         // do checkAvailAttributes only if has more than one attributes
  	         if(this.attrNum>1){this.checkAvailAttributes(attrColumn,attrValue)}
  	 };
  	 
  	 
  	 
  	 
  	 
  	 
  	 
  	 /* ----- Swatch Function : change image and sideview image -----*/
  	 
	Venda.namespace("Ebiz.ProductDetail");

	Venda.Ebiz.ProductDetail.allImages = new Array();
		Venda.Ebiz.ProductDetail.config = {
		objDropdown: null,
		objDefaultImage: null,
		objMediumArea: null,
		objLinkArea: null,
		objSwatchArea: null,
		objSideviewArea: null,
		objProduct: null,
		swatchTitle: "",
		sideviewTitle: "",
		mediumTitle: "",
		viewLargeTitle: "",
		viewLargeText: "",
		loadingHeader: "",
		loadingImage: "",
		popupHeader: "",
		imageware: ""
		};
	Venda.Ebiz.ProductDetail.init = function(settings) {
			for (var eachProp in settings) {
			this.config[eachProp] = settings[eachProp];
		}
	};
	Venda.Ebiz.ProductDetail.loadImage = function(attValue, imgSources) {
		this.allImages[attValue] = imgSources;
	};
	Venda.Ebiz.ProductDetail.isClickable = function(dataChecking) {
		var able = false;
		if (dataChecking instanceof Array) {
			for (var i = 0; i < dataChecking.length; i++) {
				if (dataChecking[i] != "") {
					able = true;
				}
			}
		}
		return able;
	};
	Venda.Ebiz.ProductDetail.displaySwatch = function() {
		var allSwatch = "";
		for (var eachData in this.allImages) {
		if (this.isClickable(this.allImages[eachData].settsideview) && eachData != "" && this.allImages[eachData].setswatch != "") {
			allSwatch = allSwatch + "<a href=\"#\" onclick=\"Venda.Ebiz.ProductDetail.changeSet('"+eachData+"'); return false;\" title=\""+this.config["swatchTitle"]+" - "+eachData+"\"><img src=\""+this.allImages[eachData].setswatch+"\" alt=\" "+eachData+"\"></a>";

			} else if(eachData!="" && this.allImages[eachData].setswatch) {
				allSwatch = allSwatch + "<img src=\""+this.allImages[eachData].setswatch+"\">";
			}
		}
		this.config["objSwatchArea"].innerHTML = allSwatch;
	};
	Venda.Ebiz.ProductDetail.getImageTag = function(mappingData) {
		var imgTag = "";
		if ((mappingData.imgSource=="")&&(mappingData.noImage !="")) {
			mappingData.imgSource = mappingData.noImage;
		}
		if (mappingData.imgSource!="") {
			imgTag = "<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.alt+"\">";
		}
		if (mappingData.imgChange) {
		imgTag = "<a href=\""+mappingData.imgChange+"\" onclick=\"Venda.Ebiz.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.Ebiz.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.Ebiz.ProductDetail.highlightSideview(this); return false;\" title=\""+mappingData.imgTitle+"\">"+ imgTag + "</a>";
		}
		if (mappingData.imgPopup) {
			if(!Venda.Ebiz.AttributeSwatch.pdxtImageware){
				// not use image ware
				imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.Ebiz.ProductDetail.doPopup('"+mappingData.attValue+"',this.href, "+mappingData.currentImage+"); return false;\" title=\""+mappingData.imgTitle+"\">"+ imgTag + "</a>";
				}else{
				// use image ware
				imgTag = this.config["imagewareLink"] + imgTag + "</a>";
			}
		}

	return imgTag;
	};
	Venda.Ebiz.ProductDetail.highlightSideview = function (obj){
		jQuery("#productdetail-sideview a").removeClass("selected");
		obj.className = "selected";
	};
	Venda.Ebiz.ProductDetail.changeMainImage = function(mappingData) {
		var attValue = mappingData.attValue;
		var imgSource = (mappingData.no)? this.allImages[attValue].setmsideview[mappingData.no-1] : this.allImages[attValue].setmedium;
		var imgPopup = (mappingData.no)? this.allImages[attValue].setlsideview[mappingData.no-1] : this.allImages[attValue].setlarge;
		var mainImage = this.getImageTag({
			noImage: this.config["noImage"]["medium"],
			imgSource: imgSource,
			imgPopup: imgPopup,
			attValue: attValue,
			alt: "Main image - " + attValue,
			imgTitle: this.config["mediumsTitle"],
			currentImage: 0
		});

	this.config["objMediumArea"].innerHTML = "<span></span>" + mainImage;
	YAHOO.util.Event.addListener(["zoom_img2"],"click", Venda.Widget.Lightbox.showImageware);
	};
  	 
  	 Venda.Ebiz.ProductDetail.changeViewLargeLink= function(mappingData) {
  	         var attValue = mappingData.attValue;
  	         var imgPopup = (mappingData.no)? this.allImages[attValue].setlsideview[mappingData.no-1] : this.allImages[attValue].setlarge;
  	         var viewLarge = "";
  	         if (imgPopup !="") {
  	                 viewLarge = "<div class=\"enlarge\"><a href=\""+imgPopup+"\" onclick=\"Venda.Ebiz.ProductDetail.doPopup('"+attValue+"',this.href, 0); return false;\" title=\""+this.config["viewLargeTitle"]+"\"><img src=\""+ this.config["viewLargeText"] + "\" alt=\""+this.config["viewLargeTitle"]+"\"></a></div>";
  	         }
  	         if(this.config["objLinkArea"]!=null){
  	                 this.config["objLinkArea"].innerHTML = viewLarge;
  	         }
  	 };
  	 
  	 Venda.Ebiz.ProductDetail.changeSideviewSet = function(attValue) {
  	         var allSideview = "";
  	         var n = 0;
  	         for (var i = 0; i < this.allImages[attValue].settsideview.length; i++) {
  	                 n++;
  	                 if (this.allImages[attValue].settsideview[i] !="") {
  	                         if (this.allImages[attValue].setmsideview[i] !="") {
									allSideview = allSideview + this.getImageTag({
									currentImage: n,
									imgSource: this.allImages[attValue].settsideview[i],
									imgChange: this.allImages[attValue].setmsideview[i],
									attValue: attValue,
									alt: "Alternative views - " +attValue,
									imgTitle: this.config["sideviewTitle"]
								});
  	                         } else {
									allSideview = allSideview + this.getImageTag({
									currentImage: n,
									imgSource: this.allImages[attValue].settsideview[i],
									imgChange: "",
									attValue: attValue,
									alt: attValue,
									imgTitle: this.config["sideviewTitle"]
								});
							}
						}
					}
				this.config["objSideviewArea"].innerHTML = allSideview;
				this.showSideviewImage();
			};
  	 
  	 Venda.Ebiz.ProductDetail.changeSet = function(attValue) {
		if (this.allImages[attValue]) {
			this.changeMainImage({attValue:attValue});
			// if not tick imageware then use script to change link
		if(!Venda.Ebiz.AttributeSwatch.pdxtImageware){this.changeViewLargeLink({attValue:attValue});}
			this.changeSideviewSet(attValue);
		}
  	 };
	Venda.Ebiz.ProductDetail.changePopup = function(number,objLink) {
		jQuery("#enlargedpopup").attr({src:objLink.href});
		jQuery("#page-popup a").removeClass("selected");
		jQuery(objLink).addClass("selected");
	};
  	 
Venda.Ebiz.ProductDetail.loadingPanel = new YAHOO.widget.Panel("loading_panel",
	{
		width:"240px",
		fixedcenter:true,
		close:false,
		draggable:false,
		zindex:3,
		modal:true,
		visible:false
	}
);
Venda.Ebiz.ProductDetail.enlargePanel = new YAHOO.widget.Panel("enlarge_panel",
	{
		fade: 0.24,
		fixedcenter:true,
		draggable: true,
		zindex:4,
		modal:true,
		visible:false
	}
);

Venda.Ebiz.ProductDetail.createPopupPage = function(attValue,number) {
	var newData = new Array();
	var max = 0;
	var minusNumber = 0;

	if (this.allImages[attValue].setlarge != "") {
		newData[max] = this.allImages[attValue].setlarge;
		max++;
	} else {
		minusNumber = 1;
	}

	for (var i = 0; i < this.allImages[attValue].setlsideview.length; i++) {
		if (this.allImages[attValue].setlsideview[i] != "") {
		newData[max] = this.allImages[attValue].setlsideview[i];
		max++;
		}
	}
var pageNumber = "";

	if (max > 1) {
		pageNumber = pageNumber + "<div id=\"page-popup\">";
		for (var i = 0; i < max; i++) {
			if (i == (number - minusNumber)) {
				pageNumber = pageNumber + "<a href=\""+newData[i]+"\" onclick=\"Venda.Ebiz.ProductDetail.changePopup("+i+",this); return false;\" class=\"selected\">"+(i+1)+"</a>";
			} else {
				pageNumber = pageNumber + "<a href=\""+newData[i]+"\" onclick=\"Venda.Ebiz.ProductDetail.changePopup("+i+",this); return false;\">"+(i+1)+"</a>";
			}
		}
	pageNumber = pageNumber + "</div>";
	}
return pageNumber;
};
Venda.Ebiz.ProductDetail.setWidthPanel = function (panel, objImage) {
	document.getElementById("tag-invtname").style.display = "inline";
	var widthValue = (objImage.width > (document.getElementById("tag-invtname").offsetWidth + 100)) ? objImage.width + 50: document.getElementById("tag-invtname").offsetWidth + 130;
	panel.cfg.setProperty("width", widthValue + "px");
	document.getElementById("tag-invtname").style.display = "none";
};
Venda.Ebiz.ProductDetail.doPopup = function(attValue,sLink,number) {
//      var pageNumber = this.createPopupPage(attValue,number);
	var pageNumber = "";



	Venda.Ebiz.ProductDetail.loadingPanel.setHeader('<div class="tl"></div><span>'+Venda.Ebiz.ProductDetail.config.loadingHeader+'</span><div class="tr"></div>');
	Venda.Ebiz.ProductDetail.loadingPanel.setBody('<img src="'+Venda.Ebiz.ProductDetail.config.loadingImage+'" />');
	Venda.Ebiz.ProductDetail.loadingPanel.render(document.body);
	Venda.Ebiz.ProductDetail.loadingPanel.show();

	Venda.Ebiz.ProductDetail.enlargePanel.setHeader('<div class="tl"></div><span>'+Venda.Ebiz.ProductDetail.config.popupHeader+'</span><div class="tr"></div>');
	Venda.Ebiz.ProductDetail.enlargePanel.setBody( "<div id=\"enlarged\"><img src=\""+sLink+"\" id=\"enlargedpopup\" name=\"enlargedpopup\" onload=\"Venda.Ebiz.ProductDetail.setWidthPanel(Venda.Ebiz.ProductDetail.enlargePanel, this);Venda.Ebiz.ProductDetail.loadingPanel.hide();Venda.Ebiz.ProductDetail.enlargePanel.show();\">"+pageNumber+"</div><a href=\"#\" onClick=\"Venda.Ebiz.ProductDetail.enlargePanel.hide();\">Close</a>");
	Venda.Ebiz.ProductDetail.enlargePanel.render(document.body);
};
  	 
Venda.Ebiz.ProductDetail.preloadImage = function(imgSource) {
	if (imgSource != "") {
		new Image().src = imgSource;
	}
};
  	 
Venda.Ebiz.ProductDetail.preloadAllImage = function() {
	for (var eachAttrValue in this.allImages) {
		var allImageData = this.allImages[eachAttrValue];
		this.preloadImage(allImageData.setswatch);
		this.preloadImage(allImageData.setmedium);
		this.preloadImage(allImageData.setlarge);
			for (var eachImage in allImageData.settsideview) {
				this.preloadImage(allImageData.settsideview[eachImage]);
			}
			for (var eachImage in allImageData.setmsideview) {
				this.preloadImage(allImageData.setmsideview[eachImage]);
			}
			for (var eachImage in allImageData.setlsideview) {
				this.preloadImage(allImageData.setlsideview[eachImage]);
			}
	}
};
  	 
  	 /* display sideview header if exist */
Venda.Ebiz.ProductDetail.showSideviewImage = function(){
	if(this.config["objSideviewArea"].innerHTML!=""){
		//document.getElementById("sideviewheader").innerHTML = "<span>More Images</span>"; 	
		//document.getElementById("sideviewheader").className = "";
	}else{
		//document.getElementById("sideviewheader").innerHTML = "";
		//document.getElementById("sideviewheader").className = "hide";
	}
}; 
  	 
/* Show tooltip for unavailable options */
Venda.Ebiz.AttributeSwatch.showTooltipMessage = function (id){
	if(document.getElementById(id).className=="notavail"){
		document.getElementById("swatchUnavailTooltip").className = "show";
		var posLeft = document.getElementById(id).offsetLeft-(document.getElementById("swatchUnavailTooltip").offsetWidth/2)+(document.getElementById(id).offsetWidth/2);
		var posTop = document.getElementById(id).offsetTop-document.getElementById("swatchUnavailTooltip").offsetHeight-document.getElementById("swatchUnavailTooltipArrow").offsetHeight;
		document.getElementById("swatchUnavailTooltip").style.left = posLeft+"px";
		document.getElementById("swatchUnavailTooltip").style.top = posTop+"px";
	}
};

Venda.Ebiz.AttributeSwatch.hideTooltipMessage = function (){
		document.getElementById("swatchUnavailTooltip").className = "hide";
};

  /**
 * A skeleton function for validating user extened fields - needs to be amended by the build team
 * @param {object} frmObj HTML form containing user extended field elements
 */
/*
Venda.namespace('Ebiz.BKList');
Venda.Ebiz.BKList = function(){};
Venda.Ebiz.BKList.jq = jQuery;

Venda.Ebiz.BKList.init = function(bklist) {
	this.bklist = bklist;
};

Venda.Ebiz.BKList.jq(document).ready(function() {
	var bklist = Venda.Ebiz.BKList.bklist || Venda.Platform.getUrlParam(document.location.href, "bklist") || "";
	if (bklist != "") {
		// change value dropdown
		var selectObj = Venda.Ebiz.BKList.jq("#searchresults #sortby").get(0);
		if (selectObj) {
			var isCorrectObj = selectObj.length || selectObj.options.length > 0 || false;
			if (isCorrectObj) {
				for (var i = 0; i < selectObj.length; i++) {
					selectObj.options[i].value = selectObj.options[i].value + "&bklist=" + bklist;
				}
			}
		}
		var selectObj = Venda.Ebiz.BKList.jq("#searchresults #perpagedpd").get(0);
		if (selectObj) {
			var isCorrectObj = selectObj.length || selectObj.options.length > 0 || false;
			if (isCorrectObj) {
				for (var i = 0; i < selectObj.length; i++) {
					selectObj.options[i].value = selectObj.options[i].value + "&bklist=" + bklist;
				}
			}
		}
		
		// change link in Refine list section
		Venda.Ebiz.BKList.jq(".refinelist a").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist; 
		});
		
		// change link in pagination section
		Venda.Ebiz.BKList.jq("#productlist .pagnNumList a").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist; 
		});
		
		// change link View by Page section
		Venda.Ebiz.BKList.jq("#productlist .pagnShowViewByPaged a").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist; 
		});
		
		// change link in product list section
		Venda.Ebiz.BKList.jq("#bklistonprod a.prodlist, #nobklistonprod a.prodlist, #productlist a.prodlist").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist; 
		});

		// change next/previous link in product detail section
		
		Venda.Ebiz.BKList.jq(".section1 a").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist; 
		});
		
		
		// replace blank bklist with the one from variable
		Venda.Ebiz.BKList.jq(".sortimgmenu a").attr("href", function() {
			return Venda.Ebiz.BKList.jq(this).attr("href").replace("&bklist=?","&bklist=" + bklist + "?");
		});
		
		// remove bklist out of the Slider product
		Venda.Ebiz.BKList.jq("#sliderproductlist a").attr("href", function() {
			var currentLink = Venda.Ebiz.BKList.jq(this).attr("href");
			var newLink = currentLink;
			if (newLink.length > 2) {
				if (currentLink.indexOf("&bklist=") != -1) {
					newLink = currentLink.substring(0, currentLink.indexOf("&bklist="));
				}
			}
			return newLink;
		});
		
		 Breadcrumb not available on this site.
		// remove bklist out of the breadcrumb
		Venda.Ebiz.BKList.jq(".categorytree a").attr("href", function() {
			var currentLink = Venda.Ebiz.BKList.jq(this).attr("href");
			var newLink = currentLink;
			if (newLink.length > 2) {
				if (currentLink.indexOf("&bklist=") != -1) {
					newLink = currentLink.substring(0, currentLink.indexOf("&bklist="));
				}
				if (newLink.indexOf("&view=") != -1) {
					newLink = newLink.substring(0, newLink.indexOf("&view="));
				}
			}
			return newLink;
		});
		
	}
});

Venda.Ebiz.grabURL = function(currURL,urlParam) {
//find out a value where is passed from current url
	var url = unescape(currURL);
	var spliter = '&';
	var sField = spliter+urlParam+'=';

	if (url.search(sField) == -1) {
		sField = '?'+urlParam+'=';
	}

	var urlArray = url.split(sField);
	if (urlArray[1]) {
	//get url param value
		var paramArray = urlArray[1].split(spliter);
		return(paramArray[0]);
	}
};
*/

//Declare namespace for bklist
Venda.namespace("Ebiz.BKList");
Venda.Ebiz.BKList.jq = jQuery;
Venda.Ebiz.BKList.configBKList = {
                bklist: "",
                divArray: ['#sortby','.sort_results', '.searchpsel', '.pagn', '.refinelist'],
                removeDivArray:['.categorytree'],
                enableBklist: true
};
/**
* Sets the config values to each config type
* @param {string} configType this is an configuration type name
* @param {array} settings this is the value of each configuration type
*/

Venda.Ebiz.BKList.init = function(settings) {
        for (var eachProp in settings) {
                this.configBKList[eachProp] = settings[eachProp];
        }
};
Venda.Ebiz.BKList.jq(document).ready(function() {
        if(Venda.Ebiz.BKList.configBKList.enableBklist){
                Venda.Ebiz.BKList.ChangeLink();
        }
});

Venda.Ebiz.BKList.getUrl = function(){
        var curUrl = document.location.href;
        if(curUrl.indexOf("&amp;") != -1){
                 curUrl = curUrl.replace(/&amp;/gi,'&');
        }
        return Venda.Platform.getUrlParam(curUrl, "bklist");
}

Venda.Ebiz.BKList.ChangeLink = function(){
var divArray = Venda.Ebiz.BKList.configBKList.divArray;
var removeDivArray = Venda.Ebiz.BKList.configBKList.removeDivArray;
var bklist = Venda.Ebiz.BKList.configBKList.bklist || Venda.Ebiz.BKList.getUrl() || "";
var strBklist = "&bklist";
        if (bklist != "") {
                var addBklist =  strBklist + "=" + bklist;
                for (var i = 0; i < divArray.length; i++) {

                        if(Venda.Ebiz.BKList.jq(divArray[i]+ " a").attr("href")){
                                Venda.Ebiz.BKList.jq(divArray[i]+ " a").attr("href", function() {
                                        return Venda.Ebiz.BKList.jq(this).attr("href") + addBklist;
                                });
                        }

                        if(Venda.Ebiz.BKList.jq(divArray[i] + " option").attr("value")){
                                Venda.Ebiz.BKList.jq(divArray[i] + " option").attr("value", function() {
                                        return Venda.Ebiz.BKList.jq(this).attr("value") + addBklist;
                                });
                        }
                }

                for (var i = 0; i < removeDivArray.length; i++) {
                        Venda.Ebiz.BKList.jq(removeDivArray[i]+" a").attr("href", function() {
                                var currentLink = Venda.Ebiz.BKList.jq(this).attr("href");
                                var newLink = currentLink;
                                if (newLink.length > 2) {
                                        if (currentLink.indexOf("&bklist=") != -1) {
                                                newLink = currentLink.substring(0, currentLink.indexOf("&bklist="));
                                        }
                                        if (newLink.indexOf("&view=") != -1) {
                                                newLink = newLink.substring(0, newLink.indexOf("&view="));
                                        }
                                }
                                return newLink;
                        });
                }
                if(Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull")){
                        var newUrl = Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull").text()+ addBklist ;
                        Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull").text(newUrl);
                }
                if(Venda.Ebiz.BKList.jq("#tag-pageSearchurl")){
                        var newUrl = Venda.Ebiz.BKList.jq("#tag-pageSearchurl").text()+ addBklist ;
                        Venda.Ebiz.BKList.jq("#tag-pageSearchurl").text(newUrl);
                }
                if(Venda.Ebiz.BKList.jq("#tag-pageIcaturl")){
                        var newUrl = Venda.Ebiz.BKList.jq("#tag-pageIcaturl").text()+ addBklist ;
                        Venda.Ebiz.BKList.jq("#tag-pageIcaturl").text(newUrl);
                }
        }
}

Venda.Ebiz.BKList.init2 = function(bklist) {
        this.bklist = bklist;
};

Venda.Ebiz.BKList.jq(document).ready(function() {
        var bklist = Venda.Ebiz.BKList.bklist || Venda.Platform.getUrlParam(document.location.href, "bklist") || "";
        if (bklist != "") {
        // change value dropdown

        // change link prev/next page
                Venda.Ebiz.BKList.jq("ul.masonry a").attr("href", function() {
                        return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist;
                });
                Venda.Ebiz.BKList.jq(".nextprevious a#previousInvt").attr("href", function() {
                        return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist;
                });
                Venda.Ebiz.BKList.jq(".nextprevious a#nextInvt").attr("href", function() {
                        return Venda.Ebiz.BKList.jq(this).attr("href") + "&bklist=" + bklist;
                 });
        }
});
