// JavaScript Document
if(!window.ess) window.ess={};
ess.showEnhancedImage = function() {
	remote = open("/largeImage/index.jsp?LargeImageURL="+ess.product.getCurrentView().enhancedImageURL, "largeImage", "width=630,height=545,left=0,top=0,scrollbars");
}
ess.validateProduct = function(pForm, cartAction) {
	var isCartable = true;
	
	//clear errors
	$$('#prodErr-color, #prodErr-size, #prodErr-qty').invoke('setStyle', { display:'none'});
	
	//check color
	if (ess.product.currentView.colorId == '' && ess.product.colorSliceValues.length != 0) {
		$$('#prodErr-color')
			.invoke('update','please select a color')
			.invoke('setStyle',{display:'block'});
		isCartable = false;
	}
	
	//check size
	if (ess.product.skuId == '') {
		$$('#prodErr-size')
			.invoke('update','please select a size')
			.invoke('setStyle',{display: 'block'});
		isCartable = false;
	}
	
	//check quantity
	var qty = pForm.qty_0.value;
	if (qty == 'undefined' || !ess.validateQty(qty) || qty < 1) {
		$$('#prodErr-qty')
			.invoke('update','please enter a valid quantity')
			.invoke('setStyle',{display:'block'});
		isCartable = false;
	}
	
	//add to cart
	if (isCartable) {
		if (cartAction == 'wishList') {
			pForm.wlName.value = 'default';
			pForm.async.value = '0';
			pForm.submit();
		} else if (ess.miniCartActive) {
			ajaxAddToCart(pForm);
			hideUnderElements();
		} else {
			pForm.submit();
		}
	}
}

ess.validateQty = function(strString) { return /^\d+$/.test(ess.trimQty(strString)); }
ess.trimQty = function(str){	 return str.replace(/^[\r\n	 ]+|[\r\n	 ]+$/,''); }	
document.observe('dom:loaded',function(e){
	var i, sku;
	if(!ess.domLoadedFired) {
		ess.domLoadedFired = true;
		ess.product = new ess.Product(ess.productJSON);
		if(ess.product.colorSliceValues.length>0) 
			ess.product.skuId = ess.product.colorSliceValues[0].availableSkuIds[0];
		else for(sku in ess.product.skus) if(i++==0) {
			ess.product.changeSku(ess.product.skus[sku].sku_id);
			break;
		}
	
		/* Color Swatches*/
		$$('#mainProductSwatches').each(function (mainProductSwatches) {
			mainProductSwatches.select('li').each(function(v) {
				v.colorId = (v.id).substring((v.id).lastIndexOf('_')+1);
				v.down("a").href = "javascript:ess.product.changeColor("+v.colorId+");";
				v.observeProductColorChange = function(product) {
					if(this.colorId == product.colorId) {
						this.selected = true;
						this.className='selected'; 
					} else {
						this.selected = false;
						this.className='';
					}
				}
				ess.product.addColorChangeListener(swatch);
			});
		});
		
		/* Alternate Views */
		$$('#moreViews').each(function (moreViews) {
			moreViews.select("li").each(function(v) {
				v.baseImageURL = v.down("img").src;
				v.down("a").href="javascript:ess.product.changeView('"+v.id+"');";
			});

			moreViews.observeProductColorChange = function(product) {
				var currentSlice = product.getCurrentColorSliceValue();
				var sliceViews = currentSlice.alternateViews;
				this.select("li").each(function(v) {
					if(sliceViews[v.id])
						v.down("img").src = sliceViews[v.id].thumbnailImageURL;
					else
						v.down("img").src = v.baseImageURL;
				});
			}

			moreViews.observeProductViewChange = function(product) {
				var currentView = product.viewId;
				if(currentView!=null && currentView!='') this.select("li").each(function(v) {
					if(v.id == currentView) {
						if(!v.hasClassName('selected')) 
							v.addClassName('selected');
					} else {
						if(v.hasClassName('selected'))
							v.removeClassName('selected');
					}
				});
			}
			ess.product.addViewChangeListener(moreViews);
		});
		
	
		/* Main Product Image */
		$$('#mainProductImage').each(function (mainProductImage) {
			mainProductImage.observeProductViewChange = function(product) {
				var currentView = product.getCurrentView();
				if(currentView!=null && currentView.mainImageURL!='')
					this.src = currentView.mainImageURL;
				else
					this.src = product.mainImageURL;
			}
			ess.product.addViewChangeListener(mainProductImage);
		});
	
		// HTML Color Select
		$$('#color').each(function (color) {
			color.observe('change', function(e){ ess.product.changeColor(this.value); });
			color.observeProductColorChange = function(product) { color.value = product.colorId; }
			ess.product.addColorChangeListener(color);
		});
		
		//Availability messaging for SKU change.
		$$('#availMsg').each(function (availMsg) {
			availMsg.observeProductSkuChange = function(product) {
				if (product.skuId != null && product.skuId != '' && availMsg) {
					var skuFlag = product.getSkuById(product.skuId).avail;
					if(skuFlag == 'ADVANCE_SALE' || skuFlag== 'ADVANCED_SALE_LIMITED') {
						this.innerHTML = 'Advance Sale';
						this.style.display = 'block';
					} else {
						this.style.display = 'none';
					}
				} else {
					this.style.display = 'none';
				}
			}
			ess.product.addSkuChangeListener(availMsg);
		});
		$$('#size').each(function (size) {
			size.observe('change', function(e){ ess.product.changeSku(this.value); });
			size.observeProductColorChange = function(product) {
				var currentColorSlice = product.getCurrentColorSliceValue();
				this.value = '';
			};
			size.observeProductColorChange = function(product) {
				var currentColorSlice = product.getCurrentColorSliceValue();
				var str = '';
				for(var i = 0; i < currentColorSlice.availableSkuIds.length; i++) {
					var sku = product.getSkuById(currentColorSlice.availableSkuIds[i]);
					str += '<option value="'+ product.productId + '|' + sku.sku_id +'">' + sku.size + '<\/option>';
				}
				size.update(str);
			};
			ess.product.addColorChangeListener(size);
			size.observeProductSkuChange = function(product) { this.value = product.productId+"|"+product.skuId; }
			ess.product.addSkuChangeListener(size);
		});
		
	
	
		// Qty html select
		$$('#quantity').each(function (qty) {
			qty.observe('change', function(e){ ess.product.setQuantity(this.value); });
			qty.observeProductQtyChange = function(product) { this.value = product.quantity; };
			ess.product.addQtyChangeListener(qty);
		});
		
		// Related Items custom checkbox
		$$('#rel_item_container').each(function (related) {
			related.select("ol.products li div input[type='checkbox']").each(function(n){
				if(!n.getAttribute('disabled')) {
					var custCheckbox = new Element('span', {'class': 'customCheckbox'});
					custCheckbox.observe('click', function(e){
						if(e.target.hasClassName('customCheckbox')){
							e.target.removeClassName('customCheckbox');
							e.target.addClassName('customCheckboxChecked');
							n.checked = true;
						}else{
							e.target.removeClassName('customCheckboxChecked');
							e.target.addClassName('customCheckbox');
							n.checked = false;
						}
						submitCBEnter(n, e);
					});
				}
				n.addClassName('hideCheckbox');
				n.insert({before:custCheckbox});
			});
		});
	}
});