var p_features_on = new Image();
p_features_on.src = "/images/product-detail-features-onv2.gif";
var p_features_off = new Image();
p_features_off.src = "/images/product-detail-features-offv2.gif";

var p_desc_on = new Image();
p_desc_on.src = "/images/product-detail-desc-onv2.gif";
var p_desc_off = new Image();
p_desc_off.src = "/images/product-detail-desc-offv2.gif";

var p_ship_on = new Image();
p_ship_on.src = "/images/product-detail-ship-onv2.gif";
var p_ship_off = new Image();
p_ship_off.src = "/images/product-detail-ship-offv2.gif";

var p_other_on = new Image();
p_other_on.src = "/images/product-detail-other-onv2.gif";
var p_other_off = new Image();
p_other_off.src = "/images/product-detail-other-offv2.gif";

var p_suggestion_on = new Image();
p_suggestion_on.src = "/images/product-suggestion-onv2.gif";
var p_suggestion_off = new Image();
p_suggestion_off.src = "/images/product-suggestion-offv2.gif";

var p_reviews_on = new Image();
p_reviews_on.src = "/images/product-reviews-onv2.gif";
var p_reviews_off = new Image();
p_reviews_off.src = "/images/product-reviews-offv2.gif";

function switchTab(tabName) {
	document.images['p_features'].src=p_features_off.src;
	document.images['p_desc'].src=p_desc_off.src;
	document.images['p_ship'].src=p_ship_off.src;
	if(document.images['p_other'] != null) {
		document.images['p_other'].src=p_other_off.src;
	}
	document.images['p_suggestion'].src=p_suggestion_off.src;
	document.images['p_reviews'].src=p_reviews_off.src;
	document.images[tabName].src = eval(tabName+'_on.src');
	document.getElementById('dp_features').style.display="none";
	document.getElementById('dp_desc').style.display="none";
	document.getElementById('dp_ship').style.display="none";
	if(document.getElementById('dp_other') != null) {
		document.getElementById('dp_other').style.display="none";
	}
	document.getElementById('dp_suggestion').style.display="none";
	document.getElementById('dp_reviews').style.display="none";
	var node = document.getElementById('d'+tabName);
	if(tabName != 'p_suggestion') {
		node.style.display='block';
	} else {
		getPage('/product/suggestion.html',function() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
				node.innerHTML = xmlHttp.responseText;
				node.style.display='block';
			}			
		});
	}
}

function showBiggerImage(index) {
	var nextBack='';
	var  html='';
	if(index==0 && zooms.length > 1) {
		nextBack='<a href="#" onclick="return showBiggerImage(1)">NEXT &gt;</a>';
	} else if(index == zooms.length -1 && index > 0) {
		nextBack='<a href="#" onclick="return showBiggerImage('+(zooms.length-2)+')">&lt; BACK</a>';
	} else if(index > 0) {
		nextBack='<a href="#" onclick="return showBiggerImage('+(index-1)+')">&lt;BACK</a> | <a href="#" onclick="showBiggerImage('+(index+1)+')">NEXT &gt;</a>';
	}

	var pos = findPos(document.getElementById('MagicZoomDiv'));
	
	if(zooms[index].zoomText=='') {
		html = '<div style="float:left" class="default">#'+zooms[index].sku+'</div><div align="right" class="default">'+nextBack+'</div><img src="'+zooms[index].img.src+'"><div align="center" class="default"><a	href="JavaScript: void closePopup()"><b>[CLOSE]</b></a></div>';
		showPopup(pos[0], pos[1], zooms[index].img.width+20, zooms[index].img.height+70,html);
	} else {
		html = '<div style="float:left" class="default">#'+zooms[index].sku+'</div><div align="right" class="default">'+nextBack+'</div><img src="'+zooms[index].img.src+'"><div class="text">'+zooms[index].zoomText+'</div><div align="center" class="default"><a	href="JavaScript: void closePopup()"><b>[CLOSE]</b></a></div>';
		extraHeight = Math.ceil(zooms[index].zoomText.length*7/zooms[index].img.width)*20;
		showPopup(pos[0], pos[1], zooms[index].img.width+20, zooms[index].img.height+70+extraHeight,html);
	}
	return false;
} 

function startZoom(largeImage, smallImage) {
	MagicZoom_stopZooms();
	var value = '<img src="'+smallImage+'" name="blowupimg" border="0" style="border:1px solid #cccccc">';
	var html = '<a href="'+largeImage+'" class="MagicZoom" rel="zoom-width: 530px; zoom-height:320px; zoom-position: right">'+value+'</a>' + '<img class="MagicZoomLoading" src="/images/ajax-loader.gif" alt="Loading Zoom, please wait"/>';	
	_el('MagicZoomDiv').innerHTML = html;
	setTimeout('MagicZoom_findZooms()', 100);	
}

function changeZoom(largeImage, smallImage) {
	var value;
	if(largeImage != '') {
		//document.getElementById('zoomMessage').innerHTML='When loading is complete, use mouse to see details.';
		value = '<img src="'+smallImage+'" name="blowupimg" border="0" style="border:1px solid #ffffff" >';
		document.getElementById('zoomingDiv').innerHTML='<a href="#" onClick="startZoom(\'/images/product/'+largeImage+'\',\''+smallImage+'\'); return false;"><img src="/images/zoomv2.gif" border="0"></a>';
	} else {
		value = '<img src="'+smallImage+'" name="blowupimg" border="0" style="border:1px solid #ffffff" >';
		document.getElementById('zoomingDiv').innerHTML='';
		//document.getElementById('zoomMessage').innerHTML='';	
	}
	document.getElementById('MagicZoomDiv').innerHTML = value;
}

function addToWishList(productId,sku,price,image) {
	var wlparams = "wlurl="+escape(document.location.href);
	wlparams +="^wlburl="+escape("http://www.stacksandstacks.com/add-to-cart.html?productId="+productId);
	wlparams +="^wlburlmeth=GET";
	wlparams +="^wliurl="+escape("http://www.stacksandstacks.com/image/"+image);
	wlparams +="^wlprice="+price;
	wlparams +="^wldesc="+escape(document.getElementById('dp_desc').innerHTML);
	wlparams +="^wlsku="+sku;
	var wlpop = window.open("http://gre.wishlist.com/gre/blank_addwish.htm","Wishlist_com_pop","height=550,width=350,scrollbars=1,resizable=1");
	document.wishlist.addtowishlistparams.value = wlparams + "^WLBURLPARAMS=^WLSELECTTEXT=";
	document.wishlist.submit();
}

var zooms = new Array();

function activateReadReviewLink() {
	reviewss = document.getElementById('reviews').getElementsByTagName('A');	
	for(var i=0;i<reviewss.length;i++) {
		if(reviewss[i].href.indexOf("#MyReviewHeader") > 0) {
			reviewss[i].onclick = function () {
				switchTab('p_reviews');
				return false;
			}
			break;
		}
	}
}

window.onload = function() {
	if(isLoadMenu) {
		cssdropdown.startchrome("chromemenu"); 
		if(isFirstTime) {
			showFirstTime();
		}
	}
	var reviewss = document.getElementById('reviews').getElementsByTagName('A');
	if(reviewss.length > 0) {
		activateReadReviewLink();
	} else {
		setTimeout("activateReadReviewLink()",1000);
	}
	registerOnClickPopup('/why-shop-at-stacks-a.html','whyshopatsas','whyshopatsas',500,300,-250,-150);
	document.getElementById('whyshopatsas').onmouseover=function () {
		this.style.cursor='pointer';
	}	
	document.getElementById('emailtofriend').onmouseover=function () {
		this.style.cursor='pointer';
	}
	registerOnClickPopup('/product/email-to-friend.html?categoryId='+categoryId+'&groupId='+groupId,'emailtofriend','emailtofriend',500,430,0,-350);

	for(var i=0;i<productData.length;i=i+6) {
		sku = productData[i];
		zoom = productData[i+1];
		reg = productData[i+2];
		mth = productData[i+3];
		zoomText = productData[i+4];
		zoomingImage=productData[i+5];
		if(document.getElementById('sku'+sku) != null) {
			registerOnClickPopup('/product/out-of-stock-notification.html?sku='+sku,'sku'+sku,'sku'+sku,500,300,-400,-50);
			document.getElementById('sku'+sku).onmouseover=function () {
				this.style.cursor='pointer';
			}
		}
		if(zoom != '') {
			zooms[zooms.length] = new Object();
			zooms[zooms.length-1].sku = sku;
			zooms[zooms.length-1].zoomText=zoomText;
			zooms[zooms.length-1].img = new Image();
			zooms[zooms.length-1].img.src="/images/product/"+zoom;
			zooms[zooms.length-1].imgReg = new Image();
			zooms[zooms.length-1].imgReg.src="/images/product/"+reg;
			zooms[zooms.length-1].imgTh = new Image();
			zooms[zooms.length-1].imgTh.src="/images/product/"+mth;
			zooms[zooms.length-1].zoomingImage=zoomingImage;
		}
	}

	var vli=null;
	var viewLargerImage=document.getElementById('viewLargerImage');
	
	if(zooms.length > 0) {
		vli = document.createElement('IMG');
		vli.src="/images/view-larger-imagev2.gif";
		vli.onclick = function() {
			showBiggerImage(0);
		}
		vli.onmouseover=function () {
			this.style.cursor='pointer';
		}
		viewLargerImage.appendChild(vli);
	} 

	if(zooms.length > 1) {
		for(var i=0;i<zooms.length;i++) {
			div = document.createElement('DIV');
			img = document.createElement('IMG');
			img.src=zooms[i].imgTh.src;
			img.title="#"+zooms[i].sku;
			img.style.marginRight="5px";
			img.style.border="1px solid #cccccc";
			img.imgReg = zooms[i].imgReg;
			img.magicZoom = zooms[i].zoomingImage;
			img.onmouseover = function() {
				//MagicZoom_stopZooms();
				//document.getElementById('ni_view_Large').src=this.imgReg.src;		
				changeZoom(this.magicZoom,this.imgReg.src);
			}	
			div.appendChild(img);
			document.getElementById('itemSku'+zooms[i].sku).appendChild(div);
		}
	}
	registerOnClickPopup('/product/metric-conversion.html','metricConversion','metricConversion',500,400,-200,-20);
	if(document.getElementById('commoncarrier') != null) {
		registerOnClickPopup('/shop/a/cc.html','commoncarrier','commoncarrier',500,360,-100,-200);
	}
	if(document.getElementById('whiteglove') != null) {
		registerOnClickPopup('/product/white-glove.html','whiteglove','whiteglove',400,300,-100,-200);
	}
	if(document.getElementById('wg') != null) {
		registerOnClickPopup('/product/white-glove.html','wg','wg',400,300,-100,-200);
	}
	registerOnClickPopup('/ebillme.html','ebillmep','ebillmep',830,730,-600,-380);
	if(document.getElementById('addthis') != null) {
		document.getElementById('addthis').style.display="block";
	}
}

function  showZipInput(obj,id) {
	obj.parentNode.innerHTML='<img src="/images/ship-to-zip.gif" align="absmiddle">&nbsp;<input type="text" name="zip'+id+'" id="zip'+id+'" class="yellow" style="width:40px"/> <a href="#" onclick="return getShippingCost(\''+id+'\',true);"><img src="/images/go-blue.gif" align="absmiddle" border="0"></a>';
	return false;
}

var isShippingCostPopup=false;
var shipCache=new Array();

function getShippingCost(id,flag) {
	var div = document.getElementById('shippingCostPopup');
	if(div == null) {
		div =document.createElement('DIV');
	}
	div.id="shippingCostPopup";
	div.style.width="170px";
	div.style.height="80px";
	div.style.position="absolute";
	div.style.border="2px solid #cccccc";
	div.style.backgroundColor="white";
	div.style.padding="2px";

	var position = findPos(document.getElementById('shipcalc'+id));

	div.style.left=(position[0]-50)+"px";
	div.style.top=(position[1]+25)+"px";

	div.innerHTML='<span class="default"><font color="red">Calculating.... Please wait...</font></span>';
	isShippingCostPopup=flag;
	document.body.appendChild(div);	
	var zip = 	document.getElementById('zip'+id).value;
	var qty = 	document.getElementById('qty'+id).value;
	if(shipCache[zip+'-'+id+'-'+qty] != null) {
		div.innerHTML=shipCache[zip+'-'+id+'-'+qty];
		return false;
	} else {
		return getPage('/shop/get-shipping-quote.html?id='+id+'&shipToZip='+zip+'&qty='+qty,function () {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
				div.innerHTML=xmlHttp.responseText;
				if(xmlHttp.responseText.indexOf('Error') == -1) {
					shipCache[zip+'-'+id+'-'+qty]	= xmlHttp.responseText;
				}
			}		
		});
	}
}

var rval=true;

document.onclick=function () {
	if(!isShippingCostPopup) {
		var node = document.getElementById('shippingCostPopup');
		if(node != null) {
			document.body.removeChild(node);	
		}	
	}
	isShippingCostPopup=false;
	if(!rval) {
		rval=true;
		return false;
	}
	return true;
}