// JavaScript Document

if(ess) {
}else{
	var 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').style.display = 'none'
		$('prodErr-size').style.display = 'none'
		$('prodErr-qty').style.display = 'none'
		
		//check color
		if (ess.product.currentView.colorId == '' && ess.product.colorSliceValues.length != 0)
		{
			$('prodErr-color').innerHTML = 'please select a color'
			$('prodErr-color').style.display = 'block'
			isCartable = false;
			
		}
		//check size
		if (ess.product.skuId == '')
		{
			$('prodErr-size').innerHTML = 'please select a size'
			$('prodErr-size').style.display = 'block'
			isCartable = false;
		}
		//check quantity
		var qty = pForm.qty_0.value;
		if (qty == 'undefined' || !ess.validateQty(qty) || qty < 1)
		{
			$('prodErr-qty').innerHTML = 'please enter a valid quantity'
			$('prodErr-qty').style.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)
   //  check for valid numeric strings	
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   strString=ess.trimQty(strString);

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}
ess.trimQty = function(str){
	
	// trim off leading spaces
	while (str.charAt(0) == ' '){
		str = str.substring(1);
	}
	//trim off trailing spaces
	while (str.charAt(str.length-1) == ' '){
		str = str.substring(0,str.length-1);
	}

	return str;
}	

document.observe('dom:loaded',function(e){

	/*Create instance of Product*/
 	ess.product = new ess.Product(ess.productJSON);
	if(ess.product.colorSliceValues.length>0) {
		ess.product.skuId = ess.product.colorSliceValues[0].availableSkuIds[0];
	} else {
		var i = 0;
		for(sku in ess.product.skus) {
			if(i==0) {
				ess.product.changeSku(ess.product.skus[sku].sku_id);
				break;
			}
			i++;
		}
	}
	

	/* Color Swatches*/
	if($('mainProductSwatces')) {
		$('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 */
	var altViewContainer = $("moreViews");
	if (altViewContainer)
	{
		altViewContainer.select("li").each(function(v) {
			v.baseImageURL = v.down("img").src;
			v.down("a").href="javascript:ess.product.changeView('"+v.id+"');";
		});


		altViewContainer.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;
				}
			});
		}
		altViewContainer.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(altViewContainer);
	}
	

	/* Main Product Image */
	/*var mainProductImage = $("mainProductImage");
	if(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);
	}*/
	
	var loader =  Df.e('div').addClassName('loader')
	var zoomProductImage = $('main-img').e('div', 'top', {id: 'zoomProductImage'})
	var zoomViewer = new Df.EmbeddedZoom(zoomProductImage, {
		loader: loader
	})
	
	var imgObj = {base: ess.product.mainImageURL}
	if(ess.product.enhancedImageURL)
		imgObj.zoom = ess.product.enhancedImageURL
		
	zoomViewer.load(imgObj)

	if($$('div#zoomProductImage img.base')[0]){
		$$('div#zoomProductImage img.base')[0].alt = ess.product.productTitle;
	}
	if($$('div#zoomProductImage img.zoom')[0]){
		$$('div#zoomProductImage img.zoom')[0].alt = ess.product.productTitle;
	}

	zoomViewer.observeProductViewChange = function(product) {
		var currentView = product.getCurrentView();
		var imgObj = {}
		
		if(currentView!=null && currentView.mainImageURL!='') {
			imgObj.base = currentView.mainImageURL;
			if(currentView.enhancedImageURL)
				imgObj.zoom = currentView.enhancedImageURL
		}else{
			imgObj.base = product.mainImageURL;
			if(product.enhancedImageURL)
				imgObj.zoom = product.enhancedImageURL
		}
		this.load(imgObj)
	}
	ess.product.addViewChangeListener(zoomViewer);
	
	

	
	if($('enhImgLink')) {
		//$('enhImgLink').href="javascript:ess.showEnhancedImage()";
		$('enhImgLink').href="javascript:void(0)"
		$('enhImgLink').observe('click', function(e){
			zoomViewer.element.fire(':zoomTo', {decimal: Math.abs(zoomViewer.decimal - 1)})
		})
		
		
		
		$('zoom').observeProductViewChange = function(product) {
				if(ess.product.getCurrentView().enhancedImageURL=='') {
					this.style.display="none";
				} else {
					this.style.display="";
				}
		}
		
		if(ess.product.getCurrentView().enhancedImageURL=='') {
			$('zoom').style.display="none";
		} else {
			$('zoom').style.display="";
		}
		var enh = $('enhImgLink');
		$('enhImgLink').style.zIndex = 100;
		enh.style.display="block";
		enh.style.display="inline";
		enh.style.display="block";
		ess.product.addViewChangeListener($('enhImgLink'));
	}
	// HTML Color Select
	if ($('color'))
	{
		$('color').observe('change', function(e){
			ess.product.changeColor(this.value);
		});
		$('color').observeProductColorChange = function(product) {
			$('color').value = product.colorId;
		}
		ess.product.addColorChangeListener($('color'));
	}

	




	if ($('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';
			}
		}
	}
	if ($('availMsg'))
	{
		ess.product.addSkuChangeListener($('availMsg'));
	}


	



	// HTML size drop down
	if ($('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
	if ($('quantity'))
	{
		$('quantity').observe('change', function(e){
			ess.product.setQuantity(this.value);
		})
		$('quantity').observeProductQtyChange = function(product) {
			this.value = product.quantity;
		}
		ess.product.addQtyChangeListener($('quantity'));

	}
});


Event.observe(window,'load',function(e){
	$$('.slider').each(function(v) {
		v.calculatedWidth = 0;
		// Fix the width of the slider element
		v.select('li').each(function(vv) {
			v.calculatedWidth += vv.offsetWidth;
		});
		v.style.width = ""+v.calculatedWidth+"px";
		var sliderIns = new Df.Slider(v.up('th'));
		sliderIns.pars.animate = {time:100,pause:20};
		sliderIns.pars.iterateBy = "li";
		sliderIns.set();
	});
});

