/**
* @fileoverview ProductDetail.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("ProductDetail");

//declare object property
Venda.ProductDetail.jq = jQuery;


/**
 * Stub function is used to support JSDoc.
 * @class Venda.
 * @constructor
 */
 
var jqzoom = false;
Venda.ProductDetail.onhandvalue = "";
Venda.ProductDetail.etayear = "";
Venda.ProductDetail.etamonth = "";
Venda.ProductDetail.etaday = "";
Venda.ProductDetail.prodsku = "";
Venda.ProductDetail.releaseyear = "";
Venda.ProductDetail.releasemonth = "";
Venda.ProductDetail.releaseday = "";
Venda.ProductDetail.releaseDate = "";
Venda.ProductDetail.etaDate = "";

/**
*  Alternate Views
*/
Venda.ProductDetail.allImages = new Array();
Venda.ProductDetail.ImageSwatch = new Array();
/**
*  Alternative View Images
*/
Venda.ProductDetail.configSwitch = {
	alternateViewStyle: "",
  	alternateViewBehavior: ""
};
Venda.ProductDetail.configObjArea = {
	objDropdown: null,
	objDefaultImage: null,
	objProduct: null,
	objMediumArea: null,
	objLinkArea: null,
	objAlternateViewArea: null
};

Venda.ProductDetail.configDefault = {
	productName: "",
	mediumAltText: "",
	mediumTitleText: "",
	viewLargeTitle: "",
	viewLargeText: "",
	largeNotAvailText: "",
	largeNotAvailAltText: "",
	loadingImage: "",
	noImage: ""
};

Venda.ProductDetail.configImageware = {
	alternateViewPagedStyle: "",
	enableImageware: "",
	zoomableText: "",
	zoomableImagelink: "",
	zoomableTextlink: ""
};

Venda.ProductDetail.configAlternateView = {
	alternateViewPagedStyle: "",
	alternateViewBehavior: "",
	alternateViewAltText: "",
	alternateViewTitleText: "",
	alternateViewHeaderText: "",
	enableImageware: ""
};

Venda.ProductDetail.configPopupPanel = {
	popupHeader: "",
	loadingHeader: "",
	loadingPanel: "",
	closePanel: "",
	closePanelTitle: "",
	closeTextLink: ""
};

Venda.ProductDetail.configJZoomImage = {
	enableJZoomImage: null,
	jqClassname: "",
	jqTitle: "",
	jsetting: ""
};

Venda.ProductDetail.stockInstruction = {
outOfStockMessage: "",
emwbisLink: "",
etaDateMessage: "",
releaseMessage: "",
stockIndicatorID: "",
backOrderLink:""
};

Venda.ProductDetail.configSwatch = {
	//swatchTitle: ""
};




/**
* Sets the config values to each config type
* @param {string} configType this is an config type
* @param {array} settings this is the value of each config type
*/
Venda.ProductDetail.init = function(configType,settings) {
	for (var eachProp in settings) {
		this[configType][eachProp] = settings[eachProp];
	}
};

Venda.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;
};

/**
  	 * add activeJzoom function to bind with hover main image
  	 * @param {object} jqClassname - element with specific class name in product detail
  	 * @param {object} jsettings - jqzoom setting in product detail
  	 */
  	 Venda.ProductDetail.activeJzoom = function(){
  	         var enableJZoomImage = Venda.ProductDetail.configJZoomImage.enableJZoomImage;
  	         var jqClassname = Venda.ProductDetail.configJZoomImage.jqClassname;
  	         var jsettings =  Venda.ProductDetail.configJZoomImage.jsetting;
  	         if(enableJZoomImage){
  	                 jQuery(jqClassname).attr({ title: Venda.ProductDetail.configJZoomImage.jqTitle});
  	                 jQuery(jqClassname).jqzoom(jsettings);
  	         }
  	 };
  	 
  	 Venda.ProductDetail.removeZoom = function() {
  	         jQuery('div.jqZoomPup').hide();
  	         jQuery('.jqZoomWindow').hide();
  	 };
  	 
  	 Venda.ProductDetail.loadImage = function(attValue,imgSources) {
  	         this.allImages[attValue] = imgSources;
  	 };
  	 
  	 /**
  	 * Matching attribute 1 name with the possible value
  	 * @returns {Boolean} isMatch    Return true if attribute 1 is colour
  	 *
  	 */
  	 Venda.ProductDetail.checkAttr1Filter = function (att1name) {
  	         att1name = att1name.toLowerCase();
  	         var synonym = ["color","colors","colour","colours"];
  	         var isMatch = false;
  	 
  	         for (var each in synonym) {
  	                 if (typeof synonym[each] != "function") {
  	                         if (att1name.match(synonym[each]) != null) {
  	                                 isMatch = true;
  	                         }
  	                 }
  	         }
  	         return isMatch;
  	 };
  	 
  	 /**
  	 * Change
  	 * @param {string} attValue - attribute 1 (color) value
  	 */

	 
  	 Venda.ProductDetail.changeSet = function(attValue) {
  	         if (this.allImages[attValue]) {
  	                 this.changeMainImage({attValue:attValue});
  	                 this.changeViewLargeLink({attValue:attValue,no:0});
  	                 this.changeAlternateViewSet({attValue:attValue,no:0});
  	                 this.changeDropdown(attValue);
  	         }
  	 };
Venda.ProductDetail.changeSetForGrid = function(attValue) {
	if (this.allImages[attValue]) {
		this.changeMainImage({attValue:attValue});
		this.changeViewLargeLink({attValue:attValue,no:0});
		this.changeAlternateViewSet({attValue:attValue,no:0});
	}
};
  	 /**
  	 * Puts loading image during the time that main image is loaded to show
  	 * @param {object} imgObj -
  	 * @returns {function} imgTag - HTMLCollection of an image tag
  	 */
  	 Venda.ProductDetail.showMainImage = {
  	         imgObj: null,
  	         doIt: function() {
  	                 setTimeout("Venda.ProductDetail.showMainImage.hideLoading()", 1500);
  	         },
  	         hideLoading: function() {
  	                 if (document.getElementById("loadingMain")) {
  	                         document.getElementById("loadingMain").style.display = "none";
  	                 }
  	                 Venda.ProductDetail.showMainImage.imgObj.style.display = "block";
  	         },
  	         setImg: function(imgObj) {
  	                 this.imgObj = imgObj;
  	         }
  	 };
  	 


/**
* To display swatch colour area
*/
Venda.ProductDetail.displaySwatch = function() {
	var allSwatch = "";
	for (var eachData in this.allImages) {		
		if (this.isClickable(this.allImages[eachData].setxsalt) && eachData != "" && this.allImages[eachData].setswatch != "") {
			allSwatch = allSwatch + "<a href=\"#\" onclick=\"Venda.ProductDetail.changeSet('"+eachData+"'); return false;\" title=\""+this.configSwatch["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.configObjArea["objSwatchArea"].innerHTML = allSwatch;
};

/**
* Gets the image HTML tag
* @param {object} mappingData - properties collection of each image
* @returns {string} imgTag - HTMLCollection of an image tag
*/
Venda.ProductDetail.getImageTag = function(mappingData) {
	var imgTag = "";
	var imgTagSuffix = "";
	
	// get image tag for 'Alternative images'
  	         if (mappingData.isAltImage && mappingData.imgChange) {
  	                 if (this.configAlternateView["alternateViewBehavior"] == "onmouseover") { imgTagSuffix = " onclick=\"return false;\">"; } else { imgTagSuffix = ">";}
  	 
  	                 // define classname for each image
  	                 if (mappingData.countData == 0) {
  	                         imgTag = "<div class=\"isFirst\"><a href=\""+mappingData.imgChange+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.highlightAltView('#productdetail-altview',this); return false;\" title=\""+mappingData.imgTitle+"\""+imgTagSuffix+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a></div>";
  	 
  	                 } else if (mappingData.isLastImage == "") {
  	                         imgTag = "<div class=\"isLast\"><a href=\""+mappingData.imgChange+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.highlightAltView('#productdetail-altview',this); return false;\" title=\""+mappingData.imgTitle+"\""+imgTagSuffix+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a></div>";
  	 
  	                 } else if ((mappingData.countData%2) == 0) {
  	                         imgTag = "<div class=\"isOdd\"><a href=\""+mappingData.imgChange+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.highlightAltView('#productdetail-altview',this); return false;\" title=\""+mappingData.imgTitle+"\""+imgTagSuffix+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a></div>";
  	 
  	                 } else {
  	                         imgTag = "<div class=\"isEven\"><a href=\""+mappingData.imgChange+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.highlightAltView('#productdetail-altview',this); return false;\" title=\""+mappingData.imgTitle+"\""+imgTagSuffix+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a></div>";
  	                 }
  	         }
  	 
  	         // get image tag for 'Main images'
  	         if (mappingData.isMainImage) {
  	                 // if 'no image' is shown at the 1st page load when user click any places and back to the main image it should be the same result as 1st time

		// if 'no image' is shown at the 1st page load when user click any places and back to the main image it should be the same result as 1st time
		if ((mappingData.imgSource == "") && (mappingData.noImage != "")) {
			mappingData.imgSource = mappingData.noImage;
		}
		if (mappingData.imgSource) {
		imgTag = "<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\">";
	}
	if (mappingData.imgChange) {		
		//alternative view images
		if (this.configSwitch["alternateViewBehavior"] == "onmouseover") { imgTagSuffix = " onclick=\"return false;\">"; } else { imgTagSuffix = ">";}
		imgTag = "<div id='xsalt'><a href=\""+mappingData.imgChange+"\" "+this.configSwitch["alternateViewBehavior"]+"=\"Venda.ProductDetail.changeMainImage({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.changeViewLargeLink({attValue: '"+mappingData.attValue+"', no: "+mappingData.currentImage+"}); Venda.ProductDetail.highlightAltView(this,this.parentNode.id); return false;\" title=\""+mappingData.imgTitle+"\""+imgTagSuffix+imgTag+"</a></div>";
	}
		
		if (this.configImageware["enableImageware"] != "") {
			//use imageware
			if (this.allImages[mappingData.attValue].clicked[mappingData.currentImage] == true) {
  	        imgTag = this.configImageware["zoomableImagelink"]+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a>";
  	        } else {
  	        imgTag =  this.configImageware["zoomableImagelink"]+this.configDefault["loadingImage"]+"<img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\" style=\"display:none\" onload=\"Venda.ProductDetail.showMainImage.setImg(this); Venda.ProductDetail.showMainImage.doIt();\"></a>";
			 }
		} else {
			//does not use imageware
			if (mappingData.imgPopup != "") {
				if (this.allImages[mappingData.attValue].clicked[mappingData.currentImage] == true) {
					
                    imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.ProductDetail.removeZoom(); return false;\" title=\""+mappingData.imgTitle+"\" class=\"zoomlarge\"><img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a>";
                    
                    //imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.ProductDetail.removeZoom(); Venda.ProductDetail.doPopup('"+mappingData.attValue+"',this.href, "+mappingData.currentImage+"); return false;\" title=\""+mappingData.imgTitle+"\" class=\"zoomlarge\"><img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\"></a>";
                    
					jqzoom = true;
				} else {
					imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.ProductDetail.removeZoom(); return false;\" title=\""+mappingData.imgTitle+"\" class=\"zoomlarge\"><img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\" onload=\"Venda.ProductDetail.showMainImage.setImg(this); Venda.ProductDetail.showMainImage.doIt();\"></a>";
                    
                    //imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.ProductDetail.removeZoom(); Venda.ProductDetail.doPopup('"+mappingData.attValue+"',this.href, "+mappingData.currentImage+"); return false;\" title=\""+mappingData.imgTitle+"\" class=\"zoomlarge\"><img src=\""+mappingData.imgSource+"\" alt=\""+mappingData.imgAlt+"\" onload=\"Venda.ProductDetail.showMainImage.setImg(this); Venda.ProductDetail.showMainImage.doIt();\"></a>";	
					jqzoom = true;
                    
					jqzoom = true;
				}
			} else {
				if (this.allImages[mappingData.attValue].clicked[mappingData.currentImage] == true) {
					imgTag = "<img src=\""+mappingData.imgSource+"\" alt=\""+this.configDefault["largeNotAvailAltText"]+"\">";
				} else {
					imgTag = "<img src=\""+mappingData.imgSource+"\" alt=\""+this.configDefault["largeNotAvailAltText"]+"\"  onload=\"Venda.ProductDetail.showMainImage.setImg(this); Venda.ProductDetail.showMainImage.doIt();\">";				
				}
			}
		}
	}

	return imgTag;
};

/**
* Sets the image HTML tag and update main image
* @param {object} mappingData - properties collection of each image
* 
*/
Venda.ProductDetail.changeMainImage = function(mappingData) {
	var attValue = mappingData.attValue;
	var currentImage = mappingData.no | 0;
	var imgSource = this.allImages[attValue].setmalt[currentImage];
	var imgPopup = this.allImages[attValue].setlalt[currentImage];
	
	var mainImage = this.getImageTag({
										noImage: this.configDefault["noImage"]["medium"],
										imgSource: imgSource,
										imgPopup: imgPopup,
										attValue: attValue,
										imgAlt: this.configDefault["mediumAltText"],
										imgTitle: this.configDefault["mediumTitleText"],
										currentImage: currentImage,
										isMainImage: true
									});
									
	  this.configObjArea["objMediumArea"].innerHTML = mainImage;
	if(jqzoom == true){
		Venda.ProductDetail.activeJzoom();
	}	
	this.allImages[attValue].clicked[mappingData.no] = true;
	if (this.configImageware["enableImageware"] != "") {YAHOO.util.Event.addListener(["zoom_img2"],"click", Venda.Widget.Lightbox.showImageware);}
};

/**
* Sets the image HTML tag to view large link
* @param {object} mappingData - properties collection of each image
*/
Venda.ProductDetail.changeViewLargeLink = function(mappingData) {
	var viewLarge = "";
	var attValue = mappingData.attValue;
	var currentImage = mappingData.no || 0;
	var imgPopup = this.allImages[attValue].setlalt[mappingData.no];

	if (this.configImageware["enableImageware"] != "") {
	 	// use imageware to see large image
	 	viewLarge = this.configImageware["zoomableTextlink"]+this.configImageware["zoomableText"]+"</a>";
	} else {
	 	// does not use imageware to see large image
		if (imgPopup != "") {
            //viewLarge = "&nbsp;";
		 	//viewLarge = "<a href=\""+imgPopup+"\" onclick=\"Venda.ProductDetail.doPopup('"+attValue+"',this.href, "+currentImage+"); return false;\" title=\""+this.configDefault["viewLargeTitle"]+"\">"+this.configDefault["viewLargeText"]+"</a>";
            viewLarge = "<a href=\""+imgPopup+"\" onclick=\"return false;\" >"+this.configDefault["viewLargeText"]+"</a>";
	 	} else {
			viewLarge = "&nbsp;";
		}
	}
	
	this.configObjArea["objLinkArea"].innerHTML = viewLarge;
	if (this.configImageware["enableImageware"] != "") {YAHOO.util.Event.addListener(["zoom_link2"],"click", Venda.Widget.Lightbox.showImageware);}
};

/**
* Generate entire images inside alternative view area
* @param {string} attValue - attribute 1 (color) value
* 
*/
Venda.ProductDetail.changeAlternateViewSet = function(mappingData) {
	var attValue = mappingData.attValue;
	var currentImage = mappingData.no | 0;
	var altviewData = "";
	var isLastImage = "";
	var iNum = 0;
	
	//product name - attribute value (if it does) - Additional view [no.]  is used to define a short description of the image in 'alt' and 'title' attribute
	var imgAlt = (attValue) ? this.configDefault["productName"] + " - " + attValue + " - " + this.configAlternateView["alternateViewAltText"] : this.configDefault["productName"] + " - " + this.configAlternateView["alternateViewAltText"];

	if(attValue){
		for (var i = 0; i < this.allImages[attValue].setmalt.length; i++) {
			if (this.allImages[attValue].setxsalt[i] != "" && this.allImages[attValue].setmalt[i] != "") {
				altviewData = altviewData + this.getImageTag({
																currentImage: i,
																imgSource: this.allImages[attValue].setxsalt[i],
																imgChange: this.allImages[attValue].setmalt[i],
																isLastImage: this.allImages[attValue].setxsalt[i+1],
																attValue: attValue,
																imgAlt: imgAlt + (iNum+1),
																imgTitle: imgAlt + (iNum+1),
																countData: iNum, // To find a real number of data that available to view (must have both 'xsalt' and 'malt' image key)
																isAltImage: true
															});
					iNum++;
			} 
		}
	}
	
	
	if (iNum != 0) {
		altviewData = "<p class=\"altviewHeader\">"+this.configAlternateView["alternateViewHeaderText"]+"</p>" + altviewData;
	} 
	if (iNum == 1) {
		// add class if there is only 1 alternate image.
		this.configObjArea["objAlternateViewArea"].className = "isOne";
	}
	
	this.configObjArea["objAlternateViewArea"].innerHTML = altviewData;
	/* Blank display for missing alternative images */
	jQuery(this.configObjArea["objAlternateViewArea"]).find('img').error(function(){
  	                 jQuery(this).parent().parent().hide();
  	         });
};

/**
* Hightlight current image to make user know which one is viewing
* @param {object} objLink - An element id (or object) representing the list of items in the alternative images
* @param {string} parentId - An element id to specific alternative images area
*/
Venda.ProductDetail.highlightAltView = function (parentId,objLink) {
	Venda.ProductDetail.jq(parentId).find("a").removeClass("selected");
	Venda.ProductDetail.jq(objLink).addClass("selected");
};

/**
* Change attribute 1 association (colour attribute) dropdown
* @param {string} attValue - attribute 1 (color) value
*/
Venda.ProductDetail.changeDropdown = function(attValue) {
	for (var i = 0; i < this.configObjArea["objDropdown"].options.length; i++) {
		if (this.configObjArea["objDropdown"].options[i].value == attValue) {
			this.configObjArea["objDropdown"].selectedIndex = i;
		}
	}
	this.configObjArea["objProduct"].changeAttributes(this.configObjArea["objDropdown"]);
};

/**
* Generate group of alternate views
* @param {string} attValue - attribute 1 (color) value
* @param {interger} number - A number of current image by ordering
* @returns {string} alternateView - HTMLCollection of Alternative view images
*/
Venda.ProductDetail.createPopupPage = function(attValue,number) {
	YAHOO.util.Event.addListener(document, "keydown", Venda.ProductDetail.handleKeyPress);
	var newDataLarge = new Array();
	var	newDataXSmall = new Array();
	var max = 0;
	var minusNumber = 0;
	var pageSequence = 1;
	var countData = 0;
	var alternateView = "";	// define entire images as pagination style
	
	// product name - attribute value (if it does) - Additional view [no.]  is used to define a short description of the image in 'alt' attribute
	var imgAlt = (attValue) ? this.configDefault["productName"] + " - " + attValue + " - " + this.configAlternateView["alternateViewAltText"] : this.configDefault["productName"] + " - " + this.configAlternateView["alternateViewAltText"];
	
	
	
	// Start merge entire datas between 'default image' and 'alternative view image' together
	// This is a default image part
	if (this.allImages[attValue].setxsmall != "" && this.allImages[attValue].setlarge != "") {
		newDataXSmall[max] = this.allImages[attValue].setxsmall;
		newDataLarge[max] = this.allImages[attValue].setlarge;
	} else {
		minusNumber = 1;
	}

	// This is the alternative image part
	for (var i = 0; i < this.allImages[attValue].setxsalt.length; i++) {
		max++;
		if (this.allImages[attValue].setxsalt[i] != "" && this.allImages[attValue].setlalt[i] != "") {
			newDataXSmall[max] = this.allImages[attValue].setxsalt[i];
			newDataLarge[max] = this.allImages[attValue].setlalt[i];
		}
	} 
	// End merge
	
	if (max > 1 && newDataXSmall.length > 0 && this.configSwitch["alternateViewStyle"] != "") {
		alternateView = alternateView + "<div id=\"alternateViewList\">";
		for (var i = 0; i < newDataXSmall.length; i++) {
			if (typeof newDataLarge[i] != "undefined" || typeof newDataXSmall[i] != "undefined") {
				if (this.configSwitch["alternateViewStyle"] == "image") {
					// Start image list
					if (i == (number - minusNumber)) {
						alternateView = alternateView + "<a href=\""+newDataLarge[i]+"\" "+this.configSwitch["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView(this,this.parentNode.id); return false;\" class=\"selected\" title=\""+imgAlt + pageSequence+"\""; 								
					} else {
						alternateView = alternateView + "<a href=\""+newDataLarge[i]+"\" "+this.configSwitch["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView(this,this.parentNode.id); return false;\" title=\""+imgAlt + pageSequence+"\"";	
					}
					if (this.configSwitch["alternateViewBehavior"] == "onmouseover") {alternateView = alternateView + " onclick=\"return false;\"><img src=\""+newDataXSmall[i]+"\" alt=\""+imgAlt + pageSequence+"\"></a>";} else {alternateView = alternateView + "><img src=\""+newDataXSmall[i]+"\" alt=\""+imgAlt + pageSequence+"\"></a>";}
					// End image list
				} else {
					// Start number list
					if (i == (number - minusNumber)) {
						alternateView = alternateView + "<a href=\""+newDataLarge[i]+"\" "+this.configSwitch["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView(this,this.parentNode.id); return false;\" class=\"selected\" title=\""+imgAlt + pageSequence+"\"";
					} else {
						alternateView = alternateView + "<a href=\""+newDataLarge[i]+"\" "+this.configSwitch["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView(this,this.parentNode.id); return false;\" title=\""+imgAlt + pageSequence+"\"";
					}
					if (this.configSwitch["alternateViewBehavior"] == "onmouseover") { alternateView = alternateView + " onclick=\"return false;\"><span>"+(pageSequence)+"</span></a>"; } else { alternateView = alternateView + "><span>"+(pageSequence)+"</span></a>";}
					// End number list
				}
				pageSequence++;
			}
		}
		alternateView = alternateView + "</div>";
	} else {
		// Don't show any paginations (main image ONLY)
		alternateView = "";
	}
	
	if (this.configAlternateView["alternateViewPagedStyle"] != "") {
		for (var i = 0; i < this.allImages[attValue].setxsalt.length; i++) {
			if (this.allImages[attValue].setxsalt[i] != "" && this.allImages[attValue].setlalt[i] != "") {
				if (this.configAlternateView["alternateViewPagedStyle"] == "image") {
					// Start image list
					if (i == number) {
						alternateView = alternateView + "<a href=\""+this.allImages[attValue].setlalt[i]+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView('#alternateViewList',this); return false;\" class=\"selected\" title=\""+imgAlt + (i+1)+"\""; 								
					} else {
						alternateView = alternateView + "<a href=\""+this.allImages[attValue].setlalt[i]+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView('#alternateViewList',this); return false;\" title=\""+imgAlt + (i+1)+"\"";	
					}
					if (this.configAlternateView["alternateViewBehavior"] == "onmouseover") {alternateView = alternateView + " onclick=\"return false;\"><img src=\""+this.allImages[attValue].setxsalt[i]+"\" alt=\""+imgAlt + (i+1)+"\"></a>";} else {alternateView = alternateView + "><img src=\""+this.allImages[attValue].setxsalt[i]+"\" alt=\""+imgAlt + (i+1)+"\"></a>";}
					// End image list
				} else {
					// Start number list
					if (i == number) {
						alternateView = alternateView + "<a href=\""+this.allImages[attValue].setlalt[i]+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView('#alternateViewList',this); return false;\" class=\"selected\" title=\""+imgAlt + (i+1)+"\"";
					} else {
						alternateView = alternateView + "<a href=\""+this.allImages[attValue].setlalt[i]+"\" "+this.configAlternateView["alternateViewBehavior"]+"=\"Venda.ProductDetail.changePopup(this); Venda.ProductDetail.highlightAltView('#alternateViewList',this); return false;\" title=\""+imgAlt + (i+1)+"\"";
					}
					if (this.configAlternateView["alternateViewBehavior"] == "onmouseover") { alternateView = alternateView + " onclick=\"return false;\"><span>"+((i+1))+"</span></a>"; } else { alternateView = alternateView + "><span>"+(i+1)+"</span></a>";}
					// End number list
				}
				countData++;
			}
		}
	}
	
	if (countData != 0 && countData != 1) {
		alternateView = "<div id=\"alternateViewList\"><p class=\"altviewHeader\">"+this.configAlternateView["alternateViewHeaderText"]+"</p>"+alternateView+"</div>";
	}
	if (countData == 1) {
		// add class if there is only 1 alternate image.
		alternateView = "<div id=\"alternateViewList\" class=\"isOne\"><p class=\"altviewHeader\">"+this.configAlternateView["alternateViewHeaderText"]+"</p>"+alternateView+"</div>";
	}
	
	return alternateView;
};

/**
* Represent the lightbox for large image also displays the available xsmall thumbs to choose. 
* @param {string} attValue - attribute 1 (color) value
* @param {string} sLink - URLs to the current Large key size image of selected alt-img
* @param {interger} number - A number of current image by ordering
*/
Venda.ProductDetail.doPopup = function(attValue,sLink,number) {
	var alternateView = this.createPopupPage(attValue,number);
	Venda.ProductDetail.isOpen = true;
	// Main image in 'popupContents' area
	var mainImage = "<div id=\"mainImage\"><a onclick=\"Venda.ProductDetail.enlargePanel.hide(); return false;\" href=\"#\" title=\""+this.configPopupPanel["closePanelTitle"]+"\"><img src=\""+sLink+"\" id=\"enlargedpopup\" name=\"enlargedpopup\" onload=\"Venda.ProductDetail.setWidthPanel(Venda.ProductDetail.enlargePanel, this); Venda.ProductDetail.loadingPanel.hide(); Venda.ProductDetail.enlargePanel.show();\"></a></div>";
	var strCloseText = "<div id=\"closeWindow\"><a href=\"#\" onclick=\"Venda.ProductDetail.enlargePanel.hide(); return false;\">"+this.configPopupPanel["closeTextLink"]+"</a></div>";
	Venda.ProductDetail.loadingPanel.setHeader("<div class=\"tl\"></div><span>"+Venda.ProductDetail.configPopupPanel.loadingHeader+"</span><div class=\"tr\"></div>");
	Venda.ProductDetail.loadingPanel.setBody("<div class=\"loading-img\"></div>");
	Venda.ProductDetail.loadingPanel.render(document.body);
	Venda.ProductDetail.loadingPanel.show();

	Venda.ProductDetail.enlargePanel.setHeader("<div class=\"tl\"></div><span>"+Venda.ProductDetail.configPopupPanel.popupHeader+"</span><div class=\"tr\"></div>");
	Venda.ProductDetail.enlargePanel.setBody( "<div class=\"popupContents\">"+mainImage + alternateView + strCloseText+"</div>");
	Venda.ProductDetail.enlargePanel.render(document.body);
};

Venda.ProductDetail.handleKeyPress = function (e) {
	if (Venda.ProductDetail.isOpen) {
		if (e.keyCode === 27) {
			Venda.ProductDetail.isOpen = false; //reset flag
			YAHOO.util.Event.preventDefault(e); // suppress default link behaviour
			Venda.ProductDetail.enlargePanel.hide();
		}
	}
};

/**
* Change
* @param {string} objLink - URLs to the current Large key size image of selected alt-img
*/
Venda.ProductDetail.changePopup = function(objLink) {
	Venda.ProductDetail.jq("#enlargedpopup").attr({src:objLink.href});
};

/**
* Set panel dimension
* @param {object} panel - 
* @param {object} objImage - 
*/
Venda.ProductDetail.setWidthPanel = function (panel,objImage) {
	document.getElementById("tag-invtname").style.display = "inline";
	var widthValue = (objImage.width > (document.getElementById("tag-invtname").offsetWidth + 100)) ? objImage.width + 20: document.getElementById("tag-invtname").offsetWidth + 130;
	panel.cfg.setProperty("width", widthValue + "px");
	document.getElementById("tag-invtname").style.display = "none";
};

/**
* prepare entire images
* @param {array} imgSource
*/
Venda.ProductDetail.preloadImage = function(imgSource) {
	if (imgSource != "") {
		new Image().src = imgSource;
	}
};

/**
* prepare entire images
* @param {array} imgSource
*/
Venda.ProductDetail.preloadAllImage = function() {
	for (var eachAttrValue in this.allImages) {
		var allImageData = this.allImages[eachAttrValue];
  
		this.preloadImage(allImageData.setmedium);
		this.preloadImage(allImageData.setlarge);
		for (var eachImage in allImageData.setxsalt) {
			this.preloadImage(allImageData.setxsalt[eachImage]);
		}
		for (var eachImage in allImageData.setmalt) {
			this.preloadImage(allImageData.setmalt[eachImage]);
		}
		for (var eachImage in allImageData.setlalt) {
			this.preloadImage(allImageData.setlalt[eachImage]);		
		}		
	}
};


var xPosition = (document.documentElement.clientWidth - 550) / 2;
//var yPosition = document.documentElement.scrollTop;
Venda.ProductDetail.loadingPanel = new YAHOO.widget.Panel("loading_panel",  
														{ 
															width:"240px", 
															fixedcenter:true, 
															close:true, 
															draggable:false,
															zindex:3,
															modal:true,
															visible:false
														}
													);

Venda.ProductDetail.enlargePanel = new YAHOO.widget.Panel("enlarge_panel",  
														{ 
															fade: 0.24,			
															fixedcenter:false,
															close:true,
															draggable:true,
															zindex:4,
															modal:true,
															visible:false,
															x:xPosition,
															y:10
														}
													);
													

/**
 * Represent a status of attribute product based on onhand, ETA date and RELEASE date value
 */
Venda.ProductDetail.checkInStock = function(){
	var etaDate = this.etamonth+"/"+this.etaday+"/"+this.etayear;
	var releaseDate = this.releasemonth+"/"+this.releaseday+"/"+this.releaseyear;
	
	if(this.onhandvalue != ""){
		// Clear all messages
		jQuery("#"+this.stockInstruction["stockIndicatorID"]).hide();
		jQuery("#qtybox").hide();
		jQuery("#addproductbox").hide();
		jQuery("#buynowbox").hide();
		jQuery("#preorderbox").hide();
		
		if(this.onhandvalue == 'default'){ this.onhandvalue = defaultOnhand; } // set 'default' value if nothing is selected
		// ETA DATE
		if(Number(this.onhandvalue) <= 0){
			jQuery("#"+this.stockInstruction["stockIndicatorID"]).html("<p class=\"nostock\">"+this.stockInstruction["outOfStockMessage"]+"</p>");
			if(etaDate.match("//") == null){
				if (!this.checkDate(etaDate)){ 
					// if a date is future
					jQuery("#"+this.stockInstruction["stockIndicatorID"]).append("<p class=\"eta\">"+this.stockInstruction["etaDateMessage"]+etaDate+"</p>");
				}
			}
			jQuery(this.stockInstruction["emwbisLink"]).show();
			jQuery("#"+this.stockInstruction["stockIndicatorID"]).show();
		} else {
			jQuery("#addproductbox input").attr("id","addproduct");
			jQuery("#preorderbox input").attr("id","");
			jQuery("#qtybox").show();
			jQuery("#addproductbox").show();
			jQuery("#buynowbox").show();
			jQuery("#preorderbox").hide();
			jQuery(this.stockInstruction["emwbisLink"]).hide();
			jQuery("#"+this.stockInstruction["stockIndicatorID"]).html("");
		}	
		
		// RELEASE DATE
		if(releaseDate.match("//") == null){
			if (!this.checkDate(releaseDate)){
				jQuery("#preorderbox input").attr("id","addproduct");
				jQuery("#addproductbox input").attr("id","");
				jQuery("#qtybox").show();
				jQuery("#addproductbox").hide();
				jQuery("#buynowbox").hide();
				jQuery("#preorderbox").show();
				jQuery("#"+this.stockInstruction["stockIndicatorID"]).html("<p class=\"release\">"+this.stockInstruction["releaseDateMessage"]+releaseDate+"</p>");
				jQuery("#"+this.stockInstruction["stockIndicatorID"]).show();
			}
			if (Venda.Widget.MinicartPopup.windowsettings.noreload == "1"){
			// only do this if the option is ticked to show popup without reload
			YAHOO.util.Event.onDOMReady(Venda.Widget.MinicartPopup.gatherAdds);
			}
		}
	}
};
 /**
 * Take the eta/release date of the attribute then compare to now date 
 * @param {string} the date of attribute product with "mm/dd/yyyy" format
 * return true if now date is greter than date param
 */
Venda.ProductDetail.checkDate = function(date){
		var chkDate = new Date(date);
		var nowDate = new Date();
		return (nowDate > chkDate);
};

Venda.ProductDetail.ListAttributes = [];
Venda.ProductDetail.attributesUUID = "";
Venda.ProductDetail.availList= [];
Venda.ProductDetail.createList = function(attrColumn){ 
	var attrColumnObj = jQuery("#"+attrColumn +"_"+Venda.ProductDetail.attributesUUID +" option:not(option:first)");
	var attrColumnStr = [];	  
	jQuery("#"+attrColumn +"_"+Venda.ProductDetail.attributesUUID+" option:not(option:first)").each(function(index){
	    attrColumnStr[index] = jQuery(this).text();
	});
	Venda.ProductDetail.ListAttributes[attrColumn] = attrColumnStr;
};

/**
*Use in ProductdetailSwatch template
*Add new functions to get colour
**/
function GetColourSwatchChangeSelect(){
	var curUrl = document.location.href; 
	if(Venda.Platform.getUrlParam(curUrl,"colour")){
		var preColur = Venda.Platform.getUrlParam(curUrl,"colour");	
			jQuery("#productdetail-att1 a.att1"+preColur).click();			
	}else{
	  if(jQuery("#productdetail-att1 a").length == 1){
		jQuery("#productdetail-att1 a:first").click();
	  }else{
		var color = jQuery("#productdetail-att1 a:first").attr("title");
		Venda.ProductDetail.changeSetForGrid(color);
	  }
	  if(jQuery("#productdetail-att2 a").length == 1){
		jQuery("#productdetail-att2 a:first").click();		
		}	
	}	
};
function GetColourSwatchChangeSet(){
	var curUrl = document.location.href; 
	if(Venda.Platform.getUrlParam(curUrl,"colour")){
		var preColur = Venda.Platform.getUrlParam(curUrl,"colour");
			Venda.ProductDetail.changeSet(preColur);						
	}
};


Venda.ProductDetail.loadSwatchForImg = function(attrColumn,divId,attType,isMultiAdd) {
	var attValue = [];
	var swatchObj ="";
	var swatchType = "";
	Venda.ProductDetail.createList(attrColumn);
	attValue = Venda.ProductDetail.ListAttributes[attrColumn]; 
	for(var i=0; i < attValue.length; i++){
		if(attType == "img"){
			swatchType = "<img src=\""+ Venda.ProductDetail.ImageSwatch[attValue[i]]+"\" alt=\""+ attValue[i]+"\" >";
		}
		else if(attType == "text"){
			swatchType = "<span class=\"swatch\">"+ attValue[i] +" </span>";
		}
		if(isMultiAdd == "1"){
				doFunc = "Venda.ProductDetail.changeAttrSet('"+attrColumn+ "','"+attValue[i] +"');";
		}else{
				doFunc = "Venda.ProductDetail.changeSelect('"+attrColumn+ "','"+attValue[i] +"');";		
		}	
		
		swatchObj = swatchObj + "<a href=\"#\" class=\""+ attrColumn+attValue[i] +"\" onClick=\""+doFunc +" return false;\" title=\""+ attValue[i] +"\">"+swatchType+"</a>";

		firstOption = attrColumn+attValue[0];
	}
	document.getElementById(divId).innerHTML = swatchObj;
	if(firstOption.indexOf(" ")!=-1){
		firstOption = firstOption.substring(0, firstOption.indexOf(" "));
	}	
	jQuery("."+firstOption).bind('custom', function() {
		Venda.ProductDetail.changeAttrSet (attrColumn, attValue[0]);	
    });	
};

Venda.ProductDetail.changeSelect = function(attrColumn,attrValue) {
var selectObj = jQuery("#"+attrColumn +"_"+Venda.ProductDetail.attributesUUID);
selectObj.val(attrValue);
selectObj.trigger('change'); 
	var existingAttributes = [];
	var availAttributes = [];
	var attrColumnSelect="";
	var swatchDivId = "#productdetail-";
	/* Display each attribute when selected */	
	jQuery(swatchDivId+attrColumn +" a").each( function(){jQuery(this).removeClass("selected");});         
	jQuery(swatchDivId+attrColumn +" a[title='" +attrValue + "']").addClass("selected");     	
	switch(attrColumn){
		case "att1": attrColumn="att2";attrColumnSelect="att1";break;
		case "att2": attrColumn="att1";attrColumnSelect="att2";break;
	}
	var a=0;	
	for (var eachAttrSet in product.attributeValues) {
		if(product.attributeValues[eachAttrSet].values[attrColumnSelect]==attrValue ){
			existingAttributes[a]=product.attributeValues[eachAttrSet].values[attrColumn]; 
			a++;
		}
	}		
	jQuery(swatchDivId+attrColumn +" a").each(function(){jQuery(this).addClass("hide");});				
	for(var i=0; i < existingAttributes.length; i++){			
		jQuery("a[title='" + existingAttributes[i] + "']").removeClass("hide");
	}
};

Venda.ProductDetail.changeAttrSet = function(attrColumn,attrValue) {
	Venda.ProductDetail.changeSetForGrid(attrValue);
	var existingAttributes = [];
	var availAttributes = [];
	var attrColumnSelect="";
	var swatchDivId = "#productdetail-";
	jQuery(swatchDivId+attrColumn +" a").each( function(){jQuery(this).removeClass("selected");});
	if(attrValue.indexOf(" ")!=-1){
		attrValue = attrValue.substring(0, attrValue.indexOf(" "));
	}
	jQuery("."+attrColumn+attrValue).addClass("selected");
	jQuery(".multiList .prodMulti .qty").val("0");
	jQuery(".multiList div.prodMulti").each(function(){jQuery(this).addClass("hide");});			
	jQuery(".multiList ."+attrValue).removeClass("hide");
	if(jQuery("#productdetail .itemdetail #substitute .featprods ul li .multiList ."+attrValue).length>0){ jQuery("#productdetail .itemdetail #substitute").show();	} else {jQuery("#productdetail .itemdetail #substitute").hide();}
	jQuery("#tag-itemsAdded").remove();
};
/* RT153854 */

Venda.ProductDetail.stockStatus = function(stockthreshold){
	
	
	if (stockthreshold == undefined) { 
		 
		stockthreshold = 0.5; 
		
	}
	
	var etawks = jQuery("div#checkMulti div#checketa_" + this.sku).text();
	etawks = etawks*1;
	
	

	
	var embisMessage = jQuery("div#productdetail form#addproductform #embisMessage");
	var stockstatus = jQuery("div#productdetail form#addproductform .stockstatus");
	var addproductform = jQuery("div#productdetail form#addproductform .qtyprod label, div#productdetail form#addproductform .qtyprod input#qty, div#productdetail form#addproductform div.btproddetail");
	
	jQuery("div#productdetail form#addproductform p.nostock").hide();
	addproductform.show();
	embisMessage.hide();
	
	if (this.onhandvalue == "default") {
		stockstatus.hide();
		
	} else if (this.onhandvalue >= stockthreshold) {
		stockstatus.text("In Stock").show();
		
	} else if (this.onhandvalue > 0 && this.onhandvalue < stockthreshold) {
		stockstatus.text("Low Stock").show();
	
	} else if (this.onhandvalue <= 0 && etawks <= 0 ) {
		stockstatus.text("Out of stock").show();	
		embisMessage.show();
		addproductform.hide();
	} else if (this.onhandvalue <= 0 && etawks <= 3 ) {
		/* if (etawks > 1) { s = "s"} else { s = ""}
		stockstatus.text("Due in " + etawks + " week" + s).show(); */
		stockstatus.text("Out of stock, expected " + this.etaday + "/" + this.etamonth + "/" + this.etayear).show();
		embisMessage.show();
		addproductform.hide();
		
	} else if (this.onhandvalue <= 0 && etawks > 3 ) {
		stockstatus.text("Out of stock, expected " + this.etaday + "/" + this.etamonth + "/" + this.etayear).show();
		embisMessage.show();
		addproductform.hide();
	}


};

/* endof RT153854 */
