

/* ------------------------------------------------------------------------------------------------------------------------------	START:	attachLargeImg()*/


function attachLargeImg(sAlt,sHref)//Attaches the large image to the top container
{
	var galleryTop = jQuery('#product-gallery .top'); 
	
	galleryTop.html('');
	
	jQuery('<img />').load(function(){//preload image
		jQuery('<img />',{//Attach image
			alt: sAlt,
			src: sHref,
			css:{
				opacity: 0	
			}
		}).appendTo(galleryTop).animate({opacity:1},200,'linear',function(){
			magnifier = addMagnifier();	
			popletImageLoading = null;	
		});
	}).attr('src',sHref);	
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	attachLargeImg()*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	checkSouldOut()*/

function checkSoldOut()//Checks if item is sold out and displays the sold out label
{
	var $inStock = jQuery('.price-holder .in-stock');
	var sVal = jQuery('.size-color select option:selected').text();
	var isDefaultProduct = (sVal && sVal.match(/please select/gi) != null);
	if(!isDefaultProduct && $inStock.hasClass('stock-quantity-0'))$inStock.css('display','block');
	
}

/* ------------------------------------------------------------------------------------------------------------------------------	END:	checkSouldOut()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	reformatPoplets()*/
	
function reformatPoplets()
{
	jQuery('.productPopletsItem a').removeAttr('onclick').removeAttr('rel').click(function($e){
		$e.preventDefault();
		
		if(popletImageLoading)return;
		popletImageLoading = true;
		
			
		var sHref = jQuery(this).attr('href');
		var sAlt = jQuery(this).attr('alt');
		
		if(jQuery('#product-gallery .top img').length > 0)//If there is an image remove it before adding the next one
		{
			jQuery('#product-gallery .top img').animate({
				opacity: 0	
			},200,'linear',function(){
				jQuery(this).remove();	
				attachLargeImg(sAlt,sHref);	
			});
		} else {//Otherwise just attach the new image
			attachLargeImg(sAlt,sHref);		
		}
	});
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	reformatPoplets()*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	addMagnifier()*/

function addMagnifier()
{
	var mag = jQuery('#product-gallery .top img');
	if(mag.length == 0)return null;
	 
	return mag.Magnifier({
		treckerColor: 'transparent',
		treckerCursor: 'pointer',
		imgStyle:{
			margin: '0 auto'	
		},
		magnifierStyle:{
			background: 'blue',
			width: 116,
			height: 116,
			position: 'relative'
		},
		afterCreateMagHolder: function(magHolder,mag){
			jQuery('<div></div>').attr('id','mag-img').appendTo(magHolder);	
		}
	});	
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	addMagnifier()*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	addAsSeenIn()*/

function addAsSeenIn()
{
	var ProductDetail_asSeenIn = jQuery('#ProductDetail_asSeenIn').text();
	
	//initLightbox();
	
	if(typeof ProductDetail_asSeenIn == 'string' && ProductDetail_asSeenIn.length > 0 
	   && ProductDetail_asSeenIn != '{tag_custom1}' 
	   && jQuery('#large-product-as-seen-in').length > 0)//If there are images paths for as seen in
	{
		jQuery('#large-product-as-seen-in').html('');
		
		jQuery('<h3>As Seen In</h3>').appendTo('#large-product-as-seen-in');
		
		
		ProductDetail_asSeenIn = ProductDetail_asSeenIn.split(/\s*\;\s*/gi);
		
		pdAsSeenInImg = null;
		pdAsSeenInImg = new ImageLoader({
			images: ProductDetail_asSeenIn,
			onLoad: function()
			{ 
				this.resize({height:150});
				for(var i = 0; i < ProductDetail_asSeenIn.length; i++)
				{
					var oImg = 	this.getImg(ProductDetail_asSeenIn[i]);
					
					if(oImg)
					{
						var oAnchor = jQuery('<a></a>').attr({//Create anchor tag and attach it to as seen in holder
							href: ProductDetail_asSeenIn[i],
							rel: 'lightbox',
							title: 'As Seen In - Image ' + (i + 1) 	
						}).click(function($e){
							
							if(typeof magnifier != 'undefined')magnifier.remove();//Remove magnifier
							myLightbox.start(this);
							return false;	
						}).appendTo('#large-product-as-seen-in');	
						jQuery(oImg).appendTo(oAnchor);
					}
				}
			}
		});
		
		
	}		
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	addAsSeenIn()*/


function checkSale()
{
	
	/*
	//Small product
	jQuery('#content')
		.find('.mini-product')
		.each(function(){
			var e = jQuery(this);
			if(e.find('.price').attr('rel') == '1')e.find('.sale-price').css('display','block');
		});
	*/
	//Large product
	jQuery('#content')
		.find('.sale-price')
		.each(function(){
			var e = jQuery(this);
			if(e.attr('rel') == '1')e.css('display','block');
		});	
}


/* ------------------------------------------------------------------------------------------------------------------------------	START:	updateCartCounter()*/

function updateCartCounter(x)
{
	if(x.match(/shopping cart is empty/gi) == null)
	{
		var nQuantity = jQuery.trim(jQuery('<div></div>').html(x).find('.cartSummaryTable').text().split(' ')[0]);
	} else {
		var nQuantity = 0;
	}
	jQuery('#header .util-nav .cart').text('My Cart (' + nQuantity + ' items)');		
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	updateCartCounter()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	updateCartLinks()*/

function updateCartLinks(x)
{
	if(x.match(/shopping cart is empty/gi) != null)
	{
		jQuery('#header .util-nav .checkout').parent().css('display','none');
		jQuery('#header .util-nav .cart').click(function($e){$e.preventDefault();}).text('Cart is empty').mouseover(function(){
			jQuery(this).css({
				color: '#888888',
				cursor: 'default',
				'background-position': '-2px -49px'
			});	
		}).parent().addClass('last');
	} else {
		var sHref = jQuery.trim(jQuery('<div></div>').html(x).find('.cartSummaryLink').attr('href'));
		jQuery('#header .util-nav .checkout').attr('href',sHref).parent().css('display','block');
		jQuery('#header .util-nav .cart').unbind('click mouseover').attr('href',sHref).parent().removeClass('last');
	}	
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	updateCartLinks()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	addBackProductButton()*/

function addBackProductButton()
{
	
	if(jQuery('.productdetail-right h2').length > 0 && jQuery('.productdetail-right #product-detail-previous').length == 0)//If there is a heading to be used a reference
	{
		jQuery('.productdetail-right h2').before(jQuery('<p></p>').addClass('hasFloat mb-20 mt-0'));
		
		jQuery('<a />').addClass("link-left f-left").attr({
			title: 'Previous',
			href: '#',
			id: 'product-detail-previous'	
		}).html('Previous').click(function($e){
			$e.preventDefault();
			window.history.back();	
		}).appendTo('.productdetail-right p:first');
			
	}	
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	addBackProductButton()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	addTagsToLargeProduct()*/

function addTagsToLargeProduct()
{
	if(typeof ProductDetail_tags != 'undefined' && ProductDetail_tags.length > 0 && ProductDetail_tags != '{tag_tags}' 
	   && jQuery('.product-detail-heading img').length == 0)
	{
		//Add tags to products
		jQuery('.product-detail-heading').each(function(){
			var sTags = ProductDetail_tags.toLowerCase().split(/\s*\;\s*/);
			for(var i = 0; i < sTags.length; i++)
			{
				switch(sTags[i])
				{
				  	case 'as seen in':
						  //var src = '/img/layout/tag-as-seen.gif';	
						  var src = '/images/goddess-collection/tag-as-seen.png';
						  break;
					  case 'shilla loves':
						  //var src = '/img/layout/tag-shila-loves.gif';
						  var src = '/images/goddess-collection/tag-shila-loves.png';
						  break;
					  case 'new':
						  //var src = '/img/layout/tag-new.gif';
						  var src = '/images/ss-2011-2012-maybe-ill-come-home-in-the-spring/tag-new.png';
						  break;	
				  }
				  
				  if(i == 0)
				  {
						  
				  }
				  
				  if(jQuery('.product-detail-heading').find('span').width() > 195)
				  {
						var left = 130;
						var top = -((i == 0) ? 25 : (i * 50)); 
				  } else {
					  var left = (jQuery('.product-detail-heading').find('span').width() < 130) 
					             ? 130 : jQuery('.product-detail-heading').find('span').width() + 20;
					  var top = -((i == 0) ? 0 : (i * 30));
					}
					
				  jQuery('<img />').attr({
					  src: src,
					  alt: sTags[i]
				  }).css({
					  position: 'absolute',
					  left: left,
					  top: top
					}).addClass('tag-' + (i + 1)).appendTo(this);	
			  }
				
		});	
	}	
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	addTagsToLargeProduct()*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	logg()*/

function logg(x)
{
	/*
	if(console && console.log)
	{
		console.log(x);
	} else {
		*/
		jQuery('<p></p>').attr('id','logg').css({
			color: 'white',
			padding: '20px 0 0 0'	
		}).text(x).appendTo(document.body);	
	//}
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	logg()*/





/* CART FUNCTION OVERRIDES -------------------------------------------------------------------------------------------------------------------------------------------*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	DrawProduct()*/

DrawProduct = function(b,j,g,d,a)
{
	var h=document.getElementById("Grouping_"+j);
	var c;
	var f=true;
	var i=h;
	
	while(f)
	{
		i=i.parentNode;
		if(i.id)
		{
			if(i.id.indexOf("catProdTd_")!=-1)
			{
				c=i.id.substring(10);
				f=false
			}
		}
	}
	
	var e=CMS.CatalogueRetrieve.ServerSideDrawProduct(b,g,a,d);
	i.innerHTML=e.value;
	ProcessJS(i);
	
	if(e.value.indexOf("lightbox")!=-1)
	{
		//initLightbox();
		reformatPoplets();
	}
	
	addAsSeenIn();
	checkSoldOut();
	magnifier = addMagnifier();
	
	checkSale();
	
	addBackProductButton();
	addTagsToLargeProduct();
};
/* ------------------------------------------------------------------------------------------------------------------------------	END:	DrawProduct()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	AddToCart()*/

AddToCart = function(j,w,t,B,l)
{
	var h=w;
	var q="";
	var f=document.getElementById("Units_"+w);
	
	var A=readCookie("CartID");
	var v=document.getElementById("Grouping_"+w);
	var F=document.getElementById("Related_"+w);
	var e=document.getElementById("catProdTd_"+w);
	var n=document.getElementById("catProdAttributes_"+w);
	var u=document.getElementById("catProdAttributes2_"+w);
	var s=document.getElementById("catProdInstructions_"+w);
	var c="";
	var p=new Array();
	var C=false;
	var a=false;
	var m;
	var g=0;
	var E;
	var d=true;
	if(f)
	{
		m=f.value;
		if(m<0)
		{
			alert(OshopLang.RemoveError);
			return false;
		}
	}else{
		m=1
	}
	
	if(A==null||A=="")
	{
		A=-1
	}
	
	E=document.getElementById("catCartSummary");
	if(v)
	{
		if(v.nodeName=="SELECT")
		{
			h=v.value
		} else {
			var r=v.getElementsByTagName("input");
			for(var z=0;z<r.length;z++)
			{
				if(r[z].checked)
				{
					h=r[z].value;
					break
				}
			}
		}
	}
	
	if(F)
	{
		q=GetCheckListValue(F);
		if(q.length>0)
		{
			g=q.split(",").length
		}
	}
	
	if(s)
	{
		c=s.value
	}
	
	if(n)
	{
		var o=n.getElementsByTagName("select");
		if(o)
		{
			if(c.length>0)
			{
				c+=";"
			}
			for(var z=0;z<o.length;z++)
			{
				if(o[z].value.length>0)
				{
					c+=o[z].value+";"
				}
			}
		}
	}
	
	if(u)
	{
		var D=0;
		var o=u.getElementsByTagName("select");
		var y;
		var x;
		var k="";
		if(o)
		{
			for(var z=0;z<o.length;z++)
			{
				if(o[z].value.length>0)
				{
					p[D]=o[z].value+"|1";
					D++
				} else {
					if(o[z].getAttribute("mandatory"))
					{
						alert(Oshoplang.ChooseAttribute);
						return;
					}
				}
			}
		}
		
		var o=u.getElementsByTagName("input");
		if(o)
		{
			for(var z=0;z<o.length;z++)
			{
				if(o[z].type=="checkbox"||o[z].type=="radio")
				{
					if(k!=o[z].getAttribute("name"))
					{
						if(z>0&&!x&&o[z-1].getAttribute("mandatory"))
						{
							alert(Oshoplang.ChooseAttribute);
							return;
						}
						x=false;
					}
					if(o[z].checked)
					{
						p[D]=o[z].id+"|1";D++;
						x=true;
					}
					k=o[z].getAttribute("name")
				} else {
					if(o[z].value.length>0)
					{
						p[D]=o[z].id+"|"+o[z].value;
						D++;
						x=true;
					} else {
						if(o[z].getAttribute("mandatory"))
						{
							alert(Oshoplang.ChooseAttribute);
							return;
						}
					}
				}
			}
			
			if(o.length>0&&(o[o.length-1].type=="checkbox"||o[o.length-1].type=="radio"))
			{
				if(!x&&o[z-1].getAttribute("mandatory"))
				{
					alert(Oshoplang.ChooseAttribute);
					return;
				}
			}
		}
	}
	if(E)
	{
		if(E.getAttribute("Vertical")=="True")
		{
			C=true
		}
		if(E.getAttribute("Quote")=="True")
		{
			a=true
		}
	}
	
	var b=CMS.CatalogueRetrieve.ServerSideAddItemToOrder(A,j,h,m,q,p,c,C,B,a,t,d);
	
	if(b.value[4])
	{
		g=b.value[4]
	}
	
	createCookie("CartID",b.value[0],2);
	
	if(E)
	{
		E.innerHTML=b.value[2]
	}
	
	switch(b.value[1])
	{
		case 0:
			if(!l)
			{
				if(e)
				{
					e.innerHTML=b.value[3];
					ProcessJS(e)
				}
				updateCartCounter(b.value[2]);
				updateCartLinks(b.value[2]);
				
				alert(g+Oshoplang.Added)
			
			} else {
				document.location="/OrderRetrievev2.aspx?CatalogueID="+j
			}
			break;
		case 1:
			alert(Oshoplang.OutOfStock);
			break;
		case 2:
			if(!l)
			{
				if(e)
				{
					e.innerHTML=b.value[3];
					ProcessJS(e)
				}
				alert(g+Oshoplang.PreOrder)
			} else {
				document.location="/OrderRetrievev2.aspx?CatalogueID="+j
			}
			break;
		case 3:
			alert(Oshoplang.MinLimit);
			break;
		case 4:
			alert(Oshoplang.MaxLimit);
			break;
	}
	if(typeof AddProductExtras=="function ")
	{
		AddProductExtras(j,h,b.value[1])
	}
	
	addBackProductButton();
	addTagsToLargeProduct();
};
/* ------------------------------------------------------------------------------------------------------------------------------	END:	AddToCart()*/


/* ------------------------------------------------------------------------------------------------------------------------------	START:	UpdateItemQuantity()*/

UpdateItemQuantity = function(d,i,f,e,j,l,g)
{
	var b;
	var h;
	var a=false;
	var m=false;
	var k=true;
	b=document.getElementById("catCartDetails");
	h=document.getElementById("catCartSummary");
	if(h)
	{
		if(h.getAttribute("Vertical")=="True")
		{
			a=true
		}
		
		if(h.getAttribute("Quote")=="True")
		{
			m=true
		}
	}
	
	if(!IsNumeric(d))
	{
		alert(Oshoplang.InvalidQuantity);
		return false;
	}
	
	if(b)
	{
		var c;
		c=CMS.OrderRetrievev2.ServerSideUpdateItemQuanity(i,f,e,j,d,l,g,a,m,k);
		switch(c.value[0])
		{
			case 0:
			case 2:
				b.innerHTML=c.value[1];
				if(h)
				{
					h.innerHTML=c.value[2];
					
					
				}
				updateCartCounter(c.value[2]);
				updateCartLinks(c.value[2]);
				break;
			case 1:
				alert(Oshoplang.OutOfStock);
				return;
			case 3:
				alert(Oshoplang.MinLimit);
				break;
			case 4:
				alert(Oshoplang.MaxLimit);
				break;
			case -1:
				b.innerHTML=c.value[1];
				if(h)
				{
					h.innerHTML=c.value[2];
					
				}
				updateCartCounter(c.value[2]);
				updateCartLinks(c.value[2]);
				alert(Oshoplang.CartEmpty);
				break;
		}
	}
	
	if(typeof UpdateProductExtras=="function")
	{
		UpdateProductExtras(f,e,c.value[0])
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	UpdateItemQuantity()*/



/* ------------------------------------------------------------------------------------------------------------------------------	START:	ClearCart()*/

ClearCart = function(e,c)
{
	var d=document.getElementById("catCartDetails");
	var a=document.getElementById("catCartSummary");
	
	if(d)
	{
		var b=CMS.OrderRetrievev2.ServerSideDrawCartEmpty(e,c);
		d.innerHTML=b.value;
		alert(Oshoplang.CartUpdateSuccess)
	}
	
	if(a)
	{
		a.innerHTML=Oshoplang.CartEmpty
	}
	
	updateCartCounter('shopping cart is empty');
	updateCartLinks('shopping cart is empty');
	
	if(typeof UpdateProductExtras=="function")
	{
		UpdateProductExtras(c,-1,-1)
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------	END:	ClearCart()*/

