var ProductPage = new function() {
	
	function customSort(a,b){
		return(a.seq-b.seq);
	}
	
	this.colorObj = function (seq,name,oprice,lprice,copy,image,skus){  
		this.seq = seq;  
		this.name = name; 
		this.oprice = oprice; 
		this.lprice = lprice; 
		this.copy = copy;
		this.image = image;
		this.skus = skus
	}
	
	this.skuObj = function (sku,catentry,size,seq,backdate,backordered){  
		this.sku = sku;  
		this.catentry = catentry; 
		this.size = size; 
		this.seq = seq;
		this.backdate = backdate;
		this.backordered = backordered;
	}
	
	this.customSort = function (a,b){
		return(a.seq-b.seq);
	}
	
	function togSwatch(e){
		var t = YUE.getTarget(e);
		if(YUD.hasClass(t, "colorSwatch")){
			if(!YUD.hasClass(t,"highlight") && !YUD.hasClass(t,"selected")){
				YUD.addClass(t, "highlight");
				t.src = t.src.replace("Off", "On");
			}else if(!YUD.hasClass(t,"selected")){
				YUD.removeClass(t,"highlight");
				t.src = t.src.replace("On", "Off");
			}
		}else{
			return true;
		}
	}
	
	function highlightSwatch(sw){
		var sibs = YUD.getElementsByClassName("colorSwatch", "img", sw.parentNode);
		for(var i=0; i < sibs.length; i++){
			YUD.removeClass(sibs[i],"selected");
			sibs[i].src = sibs[i].src.replace("On", "Off");
		}
		YUD.addClass(sw, "selected");
		YUD.removeClass(sw, "highlight");
		sw.src = sw.src.replace("Off", "On");
	}
	
	function setColor(seq){
		onesize = false;
		var seqPrefix = '';
		var swatch = $("swatch_" + seq);
		var xlZoomSwatch = $("xlZoomSwatch_" + seq);
		if(typeof seq != 'string' && seq < 10){
			seqPrefix = '0';
		}
		selectedColor = seq;
		
		if(swatch){
			highlightSwatch(swatch);
		}
		if(xlZoomSwatch){
			highlightSwatch(xlZoomSwatch);
		}
				
		for(var y = 0; y != colorArray.length; y++){
			if(colorArray[y].seq == seq){
				//Set Image
				if (template != 'v'){
					$("productImage").src = colorArray[y].image.src;
					$("productImagePrint").src = collPath + collNum + "_" + selectedColor + "_130_print.jpg";
				}
				//Set Color
				color = colorArray[y].name;
				colorStyle.value = color;
				//Set Price
				price = colorArray[y].oprice;
				//Set Copy
				if (colorArray[y].copy != 'null' && colorArray[y].copy != ''){
					copy = colorArray[y].copy;
				} else {
					copy = productCopy;
				}
				//Set Sizes
				listObjsize.length = 0;
				listObjsize.options[0] = new Option("Select Size");
				for(var t = 0; t != colorArray[y].skus.length; t++){
					if (colorArray[y].skus[t].backordered){
						shipDate = colorArray[y].skus[t].backdate;
						today = new Date();
						month = today.getMonth() +1;
						day = today.getDate();
						
						if(month <10){ 
							month = "0"+month;
						}
						if(day<10){
							day = "0"+day;
						}
						
						now = today.getFullYear() +""+ month +""+ day;
											
						if(shipDate <= now){
							listObjsize.options[t+1] = new Option((colorArray[y].skus[t].size)+" - Backordered",colorArray[y].skus[t].catentry);
						}else{
							shipStr = shipDate.substring(4,6) + "/" + shipDate.substring(6,8) + "/" + shipDate.substring(2,4)
							listObjsize.options[t+1] = new Option((colorArray[y].skus[t].size)+" - Will Ship " + shipStr,colorArray[y].skus[t].catentry);
						}
					} else {
						listObjsize.options[t+1] = new Option((colorArray[y].skus[t].size),colorArray[y].skus[t].catentry);
					}
					if (colorArray[y].skus[t].size == 'ONE SIZE'){
						onesize = true;
						var html = '<input type="hidden" name="size" value="'+colorArray[y].skus[t].catentry+'" />';
				        if(colorArray[y].skus[t].backordered == true){
				        	if(shipDate <= now) html += 'Backordered<br/>';
							else html += 'Will Ship ' + shipStr + '<br/>';
						}
						$('sizeBox').innerHTML = html;   
					}
				}
				$('prodCopy').innerHTML = copy;
				$('prodColor').innerHTML = color;
				$('prodOff').innerHTML = price;
			}	
		}
	}
	
	function setBagOn(submit){
		var oiuForm = document.forms.OrderItemUpdate;
		var currItemBackordered = false;
		if((!onesize) && (listObjsize.selectedIndex == 0)) {
			alert('Please select a size.');
			if (submit == 'submit'){
				return false;
			}
		} else if((template == 'v') && (oiuForm.sizelist.value == '')){
			alert("Please select a style.");
			if (submit == 'submit'){
				return false;
			}
		} else if (YUD.hasClass($("AddToBag"), "processingBtn")) {
			return;
		} else {
			catentry = OrderItemUpdateObj.size.value;
	
			for(var y = 0; y != colorArray.length; y++){
				if(colorArray[y].name == colorStyle.value){
					for(var t = 0; t != colorArray[y].skus.length; t++){
						if (colorArray[y].skus[t].catentry == catentry){
							OrderItemUpdateObj.partnumber.value = colorArray[y].skus[t].sku;
							currItemBackordered = colorArray[y].skus[t].backordered;
							break;
						}
					}
				}
			}
	        
			YUD.setStyle($("AddToBag"),"cursor", "wait");
	        
	        OrderItemUpdateObj.catEntryId.value = catentry;
	        OrderItemUpdateObj.URL.value = 'OrderCalculate?calculationUsageId=-1&URL=ShopRestrict';
	
	       	/* prepare to bag item */
	        YUD.replaceClass($("AddToBag"), "addToBagBtn", "processingBtn");// Processing ...
	        HTB.fixPngs();
	        OrderItemUpdateObj.action = '/webapp/wcs/stores/servlet/OrderItemAdd';
	        var oiuCB = {
	        	success: function(o){
					if(o.responseXML.getElementsByTagName('wasItemAdded')[0].firstChild.nodeValue === "true") {	 
	        			YUD.replaceClass($("AddToBag"), "processingBtn", "itemAddedBtn");       			
		        		HTB.fixPngs();
		        		HOL.miniCart.getCart('add');
		        		setTimeout(function(){
		        			YUD.replaceClass($("AddToBag"), "itemAddedBtn", "addToBagBtn");
							if($("size") && $("size").tagName.toUpperCase() == "SELECT"){
								$("size").selectedIndex = 0;
							}else if($("sizelist") && $("sizelist").tagName.toUpperCase() == "SELECT"){
								setColor(initColor);
		        				for(var i=0; i< $("sizelist").options.length; i++){
		        					if($("sizelist").options[i].value == initColor){
		        						$("sizelist").selectedIndex = i;
		        					}
		        				}
							}
							getVBImg();
		        		}, 2500);
		        	} else {
		        		YUD.replaceClass($("AddToBag"), "processingBtn", "addToBagBtn");
		        		alert(o.responseXML.getElementsByTagName('statusMsg')[0].firstChild.nodeValue);
		        	}
		    
		        	YUD.setStyle($("AddToBag"), "cursor", "pointer");
			   	},
	        	failure: function(o){
	        		YUD.replaceClass($("AddToBag"), "processingBtn", "addToBagBtn");
	        		HTB.fixPngs();
	        	}
	        }
	        
			//var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
			YUC.setForm(oiuForm);
			var transaction = YUC.asyncRequest('GET', oiuForm.action, oiuCB, null);
	        
	        //OrderItemUpdateObj.submit(); /* submit request */
		}
	}
	
	function getVBImg(){
		var cartNum = HTB.readCookie('myCart');
		var cartParts = cartNum ? cartNum.split(",") : null;
		var num = cartParts ? cartParts[0] : null;
		if(num != '0' && cartNum != null){
			YUD.setStyle($('viewBag'), "display", "inline");
		}else{
			return;
		}
	}
	
	function swatchClickHandler(e){
		var t = YUE.getTarget(e);
		function swSeq(swId){ return swId.split("_")[1]; }
		if(YUD.hasClass(t,"colorSwatch")){
			var seq = swSeq(t.id);
			setColor(seq);
		}else if(YUD.hasClass(t, "cycleSwatch")){
			//add behavior for cycling thru colors
			var c = t.parentNode, 
				h = YUD.getElementsByClassName("selected", "img", c)[0],
				sibs = YUD.getElementsByClassName("colorSwatch", "img", c),
				fs = sibs[0], 
				ls = sibs[sibs.length - 1], 
				ns;
			if(t.id.indexOf("Prev") > -1){
				var sib = YUD.getPreviousSibling(h);
				ns = YUD.hasClass(sib, "colorSwatch") ? sib : ls;
			}else{
				var sib = YUD.getNextSibling(h);
				ns = YUD.hasClass(sib, "colorSwatch") ? sib : fs;
			}
			setColor(swSeq(ns.id));
		}else{
			return true;
		}
	}
	
	function displayProdUtil(pu){		
		HTB.toggleSelects("hidden");
		YUD.setStyle(pu, "opacity", "0");
		YUD.setStyle(pu, "display", "block");
		var showPU = new YUA(pu, { opacity:{ to:1 } },.25);
		showPU.onComplete.subscribe(function(){
			HTB.fixPngs();
		});
		showPU.animate();
	}
	
	function closeProdUtil(pu){
		var hidePU = new YUA(pu, { opacity:{ to:0 } },.25);
		hidePU.onComplete.subscribe(function(){
			YUD.setStyle(pu, "display", "none");
			if(pu.id == 'shareUtil'){
				YUD.setStyle("EmailFriendForm", "display", "block");
				YUD.setStyle("eafResponse", "display", "none");
			}else if(pu.id == 'wlUtil'){
				YUD.setStyle($("wishListForm"), "display", "block");
				YUD.setStyle("wlResponse", "display", "none");
			}
		});
		hidePU.animate();
		HTB.toggleSelects("visible");
		YUD.setStyle($("sizelist"), "visibility", "visible");
		YUD.setStyle($("size"), "visibility", "visible");
		
	}
	
	function utilIconMouseOnOff(e){
		var t = YUE.getTarget(e);
		HTB.toggle.onoff(t);
		$("utilMessage").innerHTML = t.alt || '';
		HTB.toggle.visibility($("utilMessage"));
	}
	
	function utilIconClick(e){
		var t = YUE.getTarget(e);
		if(YUD.hasClass(t, "utilIcon")){
			switch(t.id){
				case "sizeChartImg":
					openSizeChart();
					break;
				case "wishlistImg":
					//HTB.getFadeUp($("wlFDC"));
					if((!onesize) && (listObjsize.selectedIndex == 0)){
						alert('Please select a size.');
					}else if((template == 'v') && (document.forms.OrderItemUpdate.sizelist.value == '')){
						alert("Please select a style.");
					}else{
						prepWishlist();
						displayProdUtil($("wlUtil"));
					}
					break;
				case "eafImg":
					//HTB.getFadeUp($("eafFDC"));
					pageTracker._trackEvent('ProductShare','ShareIt');
					displayProdUtil($("shareUtil"));
					break;
				case "printImg":
					print();
					//alert("printable version of product page");
					break;
				default:
					break;
			}
		}
	}
	function eafSend(){
		var f = $("EmailFriendForm"), r = $("eafResponse");
		var cb = {
			success: function(o){
				$("eafLastRecipient").innerHTML = f.toEmail.value;
				YUD.setStyle(f.parentNode, "cursor", "default");
				YUD.setStyle(f, "display", "none");
				f.toEmail.value = "";
				f.fromEmail.value = "";
				f.bodyEmail.value = "";
				YUD.setStyle(r, "display", "block");
				$("shareUtil").closeTO = setTimeout(function(){ closeProdUtil($("shareUtil")); }, 1000);
			},
			failure: function(o){
				//failed...
				YUD.setStyle(f.parentNode, "cursor", "default");
			}
		}
		YUD.setStyle(f.parentNode, "cursor", "wait");
		if(HTB.validateEmail(f.toEmail) && HTB.validateEmail(f.fromEmail)){
			f.collection.value = collNum;
			f.price.value = HTB.sanitizeEuro( $("prodOff").innerHTML );
			f.name.value = $("prodName").innerHTML;
			f.color.value = $("prodColor").innerHTML;
			f.productId.value = productId;
			f.storeId.value = globalStoreId;
			f.catalogId.value = globalCatalogId;
			f.langId.value = -1;
			f.cseq.value = parseInt(selectedColor, 10);
			$("eafMsg").innerHTML = "";
			YUC.setForm(f);
			YUC.asyncRequest("POST", f.action, cb);
		}else{
			YUD.setStyle(f.parentNode, "cursor", "default");
			$("eafMsg").innerHTML = "Please provide valid e-mail addresses.";
		}
	}
	
	function wlSubmit(){
		var f = $("wishListForm"), cookie = HTB.readCookie('wishList');
		var cb = {
			success: function(o){
				YUD.setStyle($("wlSubmit"), "cursor", "pointer");
				YUD.setStyle(f, "display", "none");
				f.reset();
			
				if(o.responseXML.getElementsByTagName('wasItemAdded')[0].firstChild.nodeValue === "true") {	 
		        	if(!cookie || cookie == "false"){
						HTB.createCookie('wishList', true);
					}
					YUD.setStyle($("wishListLink"), "display", "inline");
				} else {
					$('wlResponse').innerHTML = o.responseXML.getElementsByTagName('statusMsg')[0].firstChild.nodeValue;
				}
		
				$("wlUtil").closeTO = setTimeout(function(){ closeProdUtil($("wlUtil")); }, 1000);
				YUD.setStyle($("wlResponse"), "display", "block");
			},
			failure: function(o){
				YUD.setStyle($("wlSubmit"), "cursor", "pointer");
			}
		}
		YUD.setStyle($("wlSubmit"), "cursor", "wait");
		YUC.setForm(f);
		YUC.asyncRequest("GET", f.action, cb);
	}
	
	function prepWishlist(){
		var f = $("wishListForm");
		var size;
		var writeColor = true;
		var sizeOptions = onesize && template == 'v' ? document.forms.OrderItemUpdate.sizelist.getElementsByTagName('option') : listObjsize.getElementsByTagName('option');
		if (onesize){
			var catentry = OrderItemUpdateObj.size.value;
			if(template == 'v'){
				writeColor = false;
	    		 for(i = 0; i < sizeOptions.length; i++){
	    	 		if(sizeOptions[i].selected){
	    	 			size = sizeOptions[i].innerHTML;
	    	 		}
	    	 	}
	    	}
		}else{
	    	 var catentry = listObjsize.value;
	    	 for(i = 0; i < sizeOptions.length; i++){
	    	 	if (sizeOptions[i].selected){
	    	 		size = sizeOptions[i].innerHTML;
	    	 	}
	    	 }
	    }
	    f.catEntryId.value = catentry;
	    if(size){
			$("wlSize").innerHTML = size;
		}else{
			YUD.setStyle($("wlSize"), "display", "none");
		}
	    if(writeColor){
			$("wlColor").innerHTML = $("prodColor").innerHTML;
		}
		$("wlPrice").innerHTML = $("prodOff").innerHTML;
		YUD.setStyle($("wlResponse"), "display", "none");
	}
	
	function prepEAF(){
		var f = $("EmailFriendForm");
		$("eafColor").innerHTML = $("prodColor").innerHTML;
		$("eafPrice").innerHTML = $("prodOff").innerHTML;
	}
	
	this.init = function(){
			
		YUE.on($("swatches"),"click",swatchClickHandler);
		YUE.on($("swatches"),"mouseover",togSwatch);
		YUE.on($("swatches"),"mouseout",togSwatch);
		
		if($("size") && $("size").tagName == "SELECT"){
			YUE.on($("size"),"change",getVBImg);
		}
		
		var newString = new String(colorArray[0].skus[0].sku)
		var skuDept = parseInt(newString.substr(0,2))
		if((skuDept == '63') || (skuDept == '64')){
			$("sizeChartImg").style.display = "none";
		}
		
		YUE.on($("prodUtils"), "mouseover", utilIconMouseOnOff);
		YUE.on($("prodUtils"), "mouseout", utilIconMouseOnOff);
		YUE.on($("prodUtils"), "click", utilIconClick);
		YUE.on($("eafImg"), "click", utilIconClick);
		YUD.getElementsByClassName("closeProdUtil", "img", null, function(el){
			YUE.on(el, "click", function(e){
				var t = YUE.getTarget(e);
				clearTimeout(t.parentNode.closeTO);
				closeProdUtil(t.parentNode);
			});
		});
		
		getVBImg();
		
		setColor(initColor);
		
		
		if($("sizelist")){
			YUE.on($("sizelist"), "change", function(e){
				setColor($("sizelist").options[$("sizelist").selectedIndex].value);
			});
		}
		
		if($("xlZoomBtn")){
			YUE.on($("xlZoomBtn"), "click", openXLZoom);
			YUE.on($("xlZoomBtn"), "mouseover", function(e, el){
				HTB.toggle.onoff(el);
			}, $("xlZoomBtn"));
			YUE.on($("xlZoomBtn"), "mouseout", function(e, el){
				HTB.toggle.onoff(el);
			}, $("xlZoomBtn"));
		}
		
		switch(view){
			case "2":
				prepFB();
				break;
			case "3":
				prepIO();
				break;
			default:
				break;
		}
		
		if($("crossProds")){
			var xProds = YUD.getElementsByClassName("crossProdCntnr", "div", $("crossProds"));
			for(var i = 0; i < xProds.length; i++){
				YUE.on(xProds[i], 'mouseover', function(e,el){ HTB.toggle.visibility(el); },YUD.getElementsByClassName("crossProdInfo", "p", xProds[i])[0]);
				YUE.on(xProds[i], 'mouseout', function(e,el){ HTB.toggle.visibility(el); },YUD.getElementsByClassName("crossProdInfo", "p", xProds[i])[0]);
				
			}
			//YUE.on($("crossProds"), "mouseover", crossProdHandler);
			//YUE.on($("crossProds"), "mouseout", crossProdHandler);
		}
		YUE.on($("AddToBag"), "click", setBagOn);
		
		YUE.on($("eafSend"), "click", eafSend);
		YUE.on($("wlSubmit"), "click", wlSubmit);
	}
	
	if(($('nav_link_12634') || $('nav_link_12635')) && (YUD.hasClass($('nav_link_12634'), 'highlight') || YUD.hasClass($('nav_link_12634'), 'highlight'))){
		if(HTB.readCookie('hol-country') != 'US' && HTB.readCookie('hol-country') != 'CA'){
			window.document.location = '/hol/homepage.html';
		}
	}
	
	YUE.on(window, 'load' , this.init);
}
