/**
* @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
var ebiz = Venda.namespace("Ebiz");

 /**
 * 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>');
};
/**
 * example function below
 * ebiz.myFunctionname = function(param1, param2) {
 *	..... Place function code here ....
 * }; 
 */

//2. declare function
ebiz.getIt = function (p1) {
	return p1 + 'TV.';
};

/**
* @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.
*/


var ebizProductDetail = Venda.namespace("Ebiz.ProductDetail");


// new function
ebizProductDetail.allImages = new Array();
ebizProductDetail.config = {
	objDropdown: null,
	objDefaultImage: null,
	objMediumArea: null,
	objLinkArea: null,	
	objSwatchArea: null,
	objSideviewArea: null,
	objProduct: null,
	swatchTitle: "",
	sideviewTitle: "",
	mediumTitle: "",
	viewLargeTitle: "",
	viewLargeText: "",
	loadingHeader: "",
	loadingImage: "",	
	popupHeader: ""
};
ebizProductDetail.init = function(settings) {
	for (var eachProp in settings) {
		this.config[eachProp] = settings[eachProp];
	}
};
ebizProductDetail.loadImage = function(attValue, imgSources) {
	this.allImages[attValue] = imgSources;
};
ebizProductDetail.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;
}
ebizProductDetail.displaySwatch = function() {	

	var allSwatch = "";

	for (var eachData in this.allImages) {		
		if (this.isClickable(this.allImages[eachData].settsideview) && eachData != "" && this.allImages[eachData].setswatch != "") {
			allSwatch = allSwatch + "<div class='swatchwrapper'><a href=\"#\" onclick=\"Venda.Ebiz.ProductDetail.changeSet('"+eachData+"'); return false;\" title=\""+this.config["swatchTitle"]+" - "+eachData+"\"><img src=\""+this.allImages[eachData].setswatch+"\" alt=\" "+eachData+"\"></a></div>";
			
		} else if(eachData!="" && this.allImages[eachData].setswatch) {
			allSwatch = allSwatch + "<div class='swatchwrapper'><img src=\""+this.allImages[eachData].setswatch+"\"></div>";		
		}else if(eachData=="" && this.allImages[eachData].setswatch ==""){
			allSwatch = allSwatch + "<div class='' style='display:none;'></div>";	
		}
	}
	
	this.config["objSwatchArea"].innerHTML = allSwatch;
};

ebizProductDetail.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+"}); return false;\" title=\""+mappingData.imgTitle+"\">"+ imgTag + "</a>";
	} 		

	if (mappingData.imgPopup) {
		imgTag = "<a href=\""+mappingData.imgPopup+"\" onclick=\"Venda.Ebiz.ProductDetail.doPopup('"+mappingData.attValue+"',this.href, "+mappingData.currentImage+"); return false;\" title=\""+mappingData.imgTitle+"\">"+ imgTag + "</a>";
	} 			
	
	return imgTag;
};

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

ebizProductDetail.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"]+"\">"+ this.config["viewLargeText"] + "</a></div>";
	} 
	this.config["objLinkArea"].innerHTML = viewLarge;		
};

ebizProductDetail.changeSideviewSet = function(attValue) {
	var allSideview = "";
	var n = 0;
	for (var i = 0; i < this.allImages[attValue].settsideview.length; i++) {
		if (this.allImages[attValue].settsideview[i] !="") {
			if (this.allImages[attValue].setmsideview[i] !="") {
				n++;
				allSideview = allSideview + this.getImageTag({
																					currentImage: n,
																					imgSource: this.allImages[attValue].settsideview[i],
																					imgChange: this.allImages[attValue].setmsideview[i],																				
																					attValue: attValue,
																					alt: "sideview - " +attValue,
																					imgTitle: this.config["sideviewTitle"]
																				});

			} else {
				allSideview = allSideview + this.getImageTag({
																					imgSource: this.allImages[attValue].settsideview[i],
																					imgChange: "",
																					attValue: attValue,
																					alt: attValue,
																					imgTitle: this.config["sideviewTitle"]																																						
																				});				
			}

		}
	}
	this.config["objSideviewArea"].innerHTML = allSideview;	
};

ebizProductDetail.changeDropdown = function(attValue) {
	for (var i = 0; i < this.config["objDropdown"].options.length; i++) {
		if (this.config["objDropdown"].options[i].value == attValue) {
			this.config["objDropdown"].selectedIndex = i;
		}
	}
	this.config["objProduct"].changeAttributes(this.config["objDropdown"]);
};
ebizProductDetail.changeSet = function(attValue) {
	this.changeMainImage({attValue:attValue});
	this.changeViewLargeLink({attValue:attValue});	
	this.changeSideviewSet(attValue);
	this.changeDropdown(attValue);		
};
ebizProductDetail.changePopup = function(number,objLink) {
//	$(
	$("#enlargedpopup").attr({src:objLink.href});
	$("#page-popup a").removeClass("selected");
	$(objLink).addClass("selected");	
};

ebizProductDetail.loadingPanel = new YAHOO.widget.Panel("loading_panel",  
														{ 
															width:"240px", 
															fixedcenter:true, 
															close:false, 
															draggable:false, 
															zindex:3,
															modal:true,
															visible:false
														} 
													);	

ebizProductDetail.enlargePanel = new YAHOO.widget.Panel("enlarge_panel",  
														{ 
															fade: 0.24,															
															fixedcenter:true,
															draggable: true,
															zindex:4,
															modal:true,
															visible:false
														} 
													);	


ebizProductDetail.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;
};
ebizProductDetail.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";			
};
ebizProductDetail.doPopup = function(attValue,sLink,number) {
	var pageNumber = this.createPopupPage(attValue,number);
	
	ebizProductDetail.loadingPanel.setHeader('<div class="tl"></div><span>'+ebizProductDetail.config.loadingHeader+'</span><div class="tr"></div>');
	ebizProductDetail.loadingPanel.setBody('<img src="'+ebizProductDetail.config.loadingImage+'" />');														
	ebizProductDetail.loadingPanel.render(document.body);
	ebizProductDetail.loadingPanel.show();

	ebizProductDetail.enlargePanel.setHeader('<div class="tl"></div><span>'+ebizProductDetail.config.popupHeader+'</span><div class="tr"></div>');		
	ebizProductDetail.enlargePanel.setBody( "<div id=\"enlarged\"><img src=\""+sLink+"\" id=\"enlargedpopup\" name=\"enlargedpopup\" onload=\"ebizProductDetail.setWidthPanel(ebizProductDetail.enlargePanel, this);ebizProductDetail.loadingPanel.hide();ebizProductDetail.enlargePanel.show();\">"+pageNumber+"</div>");
	ebizProductDetail.enlargePanel.render(document.body);	
		
};

ebizProductDetail.preloadImage = function(imgSource) {
	if (imgSource != "") {
		new Image().src = imgSource;
	}
};

ebizProductDetail.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]);			
		}		
	}
};

/** Function to select the swatch associated with dropdown **/
Venda.Ebiz.selectSwatch = function(index){
	if(index==0){
		Venda.Ebiz.resetSwatch('','swatchwrapper','');
	}else{
		Venda.Ebiz.resetSwatch(index,'swatchwrapper','swatchwrapperselected');
	}
};
Venda.Ebiz.resetSwatch = function(setSwatch,defaultClass,selectedClass){
	var swatchDiv = document.getElementById("productdetail-swatch").getElementsByTagName("div");
	for(var i=0; i<swatchDiv.length;i++){
		if(swatchDiv[i].className!=""){
			swatchDiv[i].className =defaultClass;
		}
	};
	if(setSwatch!='' && swatchDiv[setSwatch]){
		if(swatchDiv[setSwatch].className==defaultClass){
			swatchDiv[setSwatch].className =selectedClass;
		}
	}
}; 

var itemcount = 0;

 /**
 * Hide next or previouse button on product detail page when the current product is the first or last item in a particular category
 * @param {string,string} invt ref and the id of div which holds the value of invtref arrays
 */
Venda.Ebiz.hideCyclingButton = function(invtref,target){
	if(invtref && document.getElementById(target)){
		//take the value of all invtref from the target element
		var productCyclingArray = document.getElementById(target).innerHTML;
		//split the value by / then keep them into array
		productCyclingArray = productCyclingArray.split("/");
		var currentProduct = invtref;
		// check current invtref with first and last item in array if match then hide the button(s)
		if(currentProduct == productCyclingArray[0]){
			document.getElementById("previousInvt").style.display="none";
		}
		if(currentProduct == productCyclingArray[productCyclingArray.length-2]){
			document.getElementById("nextInvt").style.display="none";
		}
	}
};

/**
 * Hide unwanted link on collate
 * @param {object} the object of the configuration which keeps the value of target element and the unwanted link(s)
 */
 Venda.Ebiz.hideUnwatedLink = function(obj){
 	//check the obj existing
 	if(obj && obj.target){
	//take all <a></a> tag within the target element
 	 var targetLinks = obj.target.getElementsByTagName("a");
		for(var i =0;i<targetLinks.length;i++){
			//make the value lowercased and remove the space
 			var transformLink = targetLinks[i].innerHTML.toLowerCase().replace(/\s/g,"");
				for(var count = 0; count < obj.matchLink.length;count++){
					//if the text of link matches the value in obj config then hide
					if(transformLink.indexOf(obj.matchLink[count])!=-1){
					targetLinks[i].style.display ="none";
					}
				}
 		}
	}
 };
 
 /**
 * showing saving price of attribute product
 * @param {object} configuration objects for showing saving price
 */
 Venda.Ebiz.showSavingPrice = function(setting){
 		//taking a configurations as initial
		Venda.Ebiz.showSavingPrice.setting = setting;
	};
	Venda.Ebiz.showSavingPrice.change = function(){
		//taking all dropdown of attribute on product detail page
		var attrDropdown = document.getElementsByTagName("select");
		//combine the value of each attribute dropdown
		var attrCombination ='';
		for(var i =0;i <attrDropdown.length;i++){
			attrCombination+=attrDropdown[i].value;
		}
		if(product.attributeValues[attrCombination]){
			var attrSellingPrice = product.attributeValues[attrCombination].data["atrsell"];
			var attrMsrp = product.attributeValues[attrCombination].data["atrmsrp"];
			var attrWas = product.attributeValues[attrCombination].data["atrwas"];
			//if msrp is not equal to selling price and was price is heigher than selling price then show saving price
			if((attrMsrp!= attrSellingPrice) && (attrWas - attrSellingPrice>0)){
				Venda.Ebiz.showSavingPrice.setting.priceSpan.innerHTML = Venda.Ebiz.showSavingPrice.setting.currency+attrSellingPrice;
				Venda.Ebiz.showSavingPrice.setting.priceSpanLabel.innerHTML = Venda.Ebiz.showSavingPrice.setting.ourPriceLabel;
				Venda.Ebiz.showSavingPrice.setting.savingSpan.innerHTML = Venda.Ebiz.showSavingPrice.setting.currency+""+(attrWas-attrSellingPrice).toFixed(2);
				Venda.Ebiz.showSavingPrice.setting.savingLabelSpan.innerHTML = Venda.Ebiz.showSavingPrice.setting.saveLabel;
			}else{
				Venda.Ebiz.showSavingPrice.setting.priceSpan.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.priceSpanLabel.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.savingSpan.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.savingLabelSpan.innerHTML = "";
			}
		}else{
				Venda.Ebiz.showSavingPrice.setting.priceSpan.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.priceSpanLabel.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.savingSpan.innerHTML = "";
				Venda.Ebiz.showSavingPrice.setting.savingLabelSpan.innerHTML = "";
		}
	};
	
function calculateSave(par1,par2){
                var v3 = (par2 - par1).toFixed(2);
				 return v3;
}