var siteprofile = new Array();

siteprofile['gp'] = { propThumbSize: 180, propDivHeight: 240, propDivWidth: 188, propWidthArrow: 30,
					  pvShadow: null, propShadow: null,
					  pvDivHeight: 55, pvDivWidth: 55,
					  propShadowh: null, propShadoww: null, p2photodim: 265,
					  gvShadow: null,
					  ident: 'gp' };
siteprofile['fl'] = { propThumbSize: 153, propDivHeight: 212, propDivWidth: 161, propWidthArrow: 30,
					  pvShadow: '/images/pvshadow.png', propShadow: '/images/propshadow.png',
					  pvDivHeight: 56, pvDivWidth: 56, 
					  propShadowh: typeof(sh) == 'undefined' ? 0 : sh, propShadoww: typeof(sw) == 'undefined' ? 0 : sw,
					  p2photodim: 218,
					  gvShadow: '/images/gvshadow.png',
					  ident: 'fl' };

function pagePrev()
{
	if(! docReady)
	{
		return;
	}
	
	history.back();
}

function pageNext()
{
	if(! docReady)
	{
		return;
	}
	
	history.back();
}

// position of the tooltip relative to the mouse in pixel //
var offsetx = 12;
var offsety =  8;

function newelement(newid)
{ 
    if(document.createElement)
    { 
        var el = document.createElement('div'); 
        el.id = newid;     
		el.setAttribute('class', "tooltip");
        with(el.style)
        { 
            display = 'none';
            position = 'absolute';
			padding = '8px';
    		width = '120px';
    		background = '#FF1CAC';
    		border = '1px solid #FFE805';
			fontFamily = 'arial, helvetica, sans-serif; font-size: 10pt';
    		fontSize = '9pt';
    		color = '#FFE805';
    		zIndex = '960000';
        } 
        el.innerHTML = '&nbsp;'; 
        document.body.appendChild(el); 
    } 
} 

function pageInitCustomer()
{
	if(customerType == 2 || customerType == 4)
	{
		BagGenerateWholesalePrices();
	}
}

function changePhotoDisplay(tv, imgno)
{
	var itemid = tv.itemid;
	var img = ge('pvphoto' + itemid);
	img.src = sURIs[itemid][imgno];
	curimgno[itemid] = imgno;
}

function changeCoordItemDisplay(tv, imgno)
{
	var i = tv.i;
	var itemid = tv.itemid;
	curitemid[i] = citemids[itemid][imgno];
	currentcoordno[i] = imgno;

	setCoordItemDisplay(tv, imgno);
}

function setCoordItemDisplay(tv, imgno)
{	
	var itemid = tv.itemid;
	var i = tv.i;
	var img = ge('cvphoto' + i);
	img.src = csURIs[itemid][imgno];
	
	var div = ge("reg" + i);
	div.innerHTML = "<b>" + ciprice[itemid][imgno] + "</b>" + "&nbsp;" + cidesc[itemid][imgno];
	coorditemid[i] = itemid;
	coordimgno[i] = imgno;
}


var lastAddTime = 0;

function addtobag(item)
{
	if(! docReady)
	{
		return;
	}
	
	var now = new Date();
	if(now.getTime() - lastAddTime > 2000)
	{
		var d = new Date();
		document.bagadd.itemid.value = item;
		document.bagadd.quantity.value = 1;
		document.bagadd.tzoff.value = d.getTimezoneOffset();
		document.bagadd.submit();
		lastAddTime = now.getTime();
	}
}

var pindexsave;

function showPhotoViewer()
{
	var pindex = pindexsave;
	dispstate[pindex] = 'pv';
	unhighlightall(pindex);
	highlight(pindex, "p2cpv");
	ge('pv' + pindex).style.display = "block";
	ge('rv' + pindex).style.display = "none";
	ge('cv' + pindex).style.display = "none";

	var div = ge("reg" + pindex);
	div.innerHTML = "<b>" + iprice[itemnos[pindex]] + "</b>" + "&nbsp;" + idesc[itemnos[pindex]];
	curitemid[pindex] = itemnos[pindex];
}

function PhotoViewer(pindex)
{
	pindexsave = pindex;
	if(prevstate[pindex] === 'pv')
	{
		var imgsrc;

		if(prevstate[pindex] === 'pv')
		{
			imgsrc = sURIs[itemnos[pindex]][curimgno[itemid]];
		}
		else
		{
			imgsrc = csURIs[itemnos[pindex]][currentcoordno[pindex]];
		}
		var profile = siteprofile[siteid];
		var itemid = curitemid[pindex];
		var html = new Array();
		html.push('<img class="p2photo" style="float: left; margin-left: 5px; margin-right: 15px; ');
		html.push('width: 50px; height: 50px; margin-top: 5px;" ');
		html.push('src="' + imgsrc + '" id="rvimg' + pindex + '" />');
		$('#rv' + pindex).html(html.join(''));
		$("#rvimg" + pindex).animate({ top: "0px", width: profile.p2photodim + "px", height: profile.p2photodim + "px", marginLeft: "24px" }, 150, "swing", showPhotoViewer);
	}
	else
	{
		showPhotoViewer();
	}
	prevstate[pindex] = dispstate[pindex];
	dispstate[pindex] = 'pv';
}

function MatchingItems(pindex)
{
	prevstate[pindex] = dispstate[pindex];
	dispstate[pindex] = 'mi';
	unhighlightall(pindex);
	highlight(pindex, "p2cmi");
	ge('pv' + pindex).style.display = "none";
	ge('rv' + pindex).style.display = "none";
	ge('cv' + pindex).style.display = "block";

	coorditemid[pindex] = itemnos[pindex];
	curitemid[pindex] = citemids[itemnos[pindex]][coordimgno[pindex]];

	var div = ge("reg" + pindex);
	div.innerHTML = "<b>" + ciprice[itemnos[pindex]][coordimgno[pindex]] + "</b>" + "&nbsp;" +
					cidesc[itemnos[pindex]][coordimgno[pindex]];

	ge("mag2" + pindex).style.display = "block";
	el_fnFixPng(ge("mag2" + pindex));
}

function CustomerReviews(pindex)
{
	if(dispstate[pindex] === 'rv') { return; }
	
	var profile = siteprofile[siteid];
	var itemid = curitemid[pindex];
	var html = new Array();
	var imgsrc;

	if(dispstate[pindex] === 'pv')
	{
		imgsrc = sURIs[itemnos[pindex]][curimgno[itemid]];
	}
	else
	{
		imgsrc = csURIs[itemnos[pindex]][currentcoordno[pindex]];
	}
	
	prevstate[pindex] = dispstate[pindex];
	dispstate[pindex] = 'rv';
	unhighlightall(pindex);
	highlight(pindex, "p2ccr");
	$('#rv' + pindex).html("");

	html.push('<div style="z-index: 500000; position: absolute; top: 0px; left: 0px;">')
	html.push('<img class="p2photo"');
	html.push('style="z-index: 500000; float: left; margin-left: 24px; margin-right: 10px; ');
	html.push('width: ' + profile.p2photodim + 'px; height: ' + profile.p2photodim + 'px;" ');
	html.push('src="' + imgsrc + '" id="rvimg' + pindex + '" />');
	html.push('</div>');
	$('#rv' + pindex).html(html.join(''));

	$('#pv' + pindex).css({ 'display' : "none" });
	$('#rv' + pindex).css({ 'display' : "block" });
	$('#cv' + pindex).css({ 'display' : "none" });

	$("#rvimg" + pindex).animate({ width: "50px", height: "50px", marginTop: "5px", marginLeft: "5px" }, 150, "swing",
	function ()
	{
		var profile = siteprofile[siteid];
		if(profile.ident == 'fl')
		{
			$('#rv' + pindex).append('<div style="position: absolute; top: 0px; left: 0px;"><img style="position: absolute; top: 5px; left: 5px; z-index: 400000;" src="/images/pvshadow.png"/></div>');
		}
		$.get("/RetailScience/review/?reqid=" + pindex + "&itemid=" + curitemid[pindex] + "&itemindex=" + pindex,
			  renderReview);
	});
}

function renderReview(data)
{
	var response = $.secureEvalJSON(data);

	var html = new Array();
	var pindex = response.itemindex;
	var itemid = curitemid[pindex];

	var imgsrc;
	imgsrc = prevstate[pindex];
	if(imgsrc === 'pv')
	{
		imgsrc = tURIs[itemnos[pindex]][curimgno[itemid]];
	}
	else
	{
		imgsrc = ctURIs[itemnos[pindex]][currentcoordno[pindex]];
	}
	$("#rvimg" + pindex).attr("src", imgsrc)
	
	if(response.count === 0)
	{
		html.push('<div style="position: absolute; top: 0px; left: 65px;"><p class="review">There are no reviews for this item. <a href="javascript:WriteReview(' + itemid + ');">Click here</a> if you would like to write the first review.</p></div><p class="review" style="bottom: 0; width: 90%; position: absolute; text-align: right; padding-right: 36px; margin-bottom: 3px;"><a href="javascript:PhotoViewer(' + pindex + ')" class="captionlink">Close X</a></p>');
		$("#rv" + pindex).append(html.join(''));
		return;
	}
	
	var r;
	html.push('<div style="height: 60px; padding-top: 5px; padding-left: 65px;">');
//	<p class="review">A' + response.avgrating.toFixed(2) + '<br />');
	html.push('<p class="review"><a href="javascript:WriteReview(' + itemid + ');">Click here</a> to write your own review</p></div>');
	html.push('<div class="reviewlist">');
	var i;
	for(i = 0; i < response.count; i++)
	{
		var review = response.review[i];
		for(r = 0; r < 5; r++)
		{
			html.push('<div class="heartrating' + (r < review.rating ? 1 : 0) + '" />');
		}
		
		html.push('<br />');
		if(InternetExploderSix)
		{
			html.push('&nbsp;<br />');
		}
		
		html.push('<p class="review"><b>by ' + review.nickname + ' on ' + review.datestring + '</b>');

		if(review.isowned)
		{
			html.push('<br /><span style="font-size: 9pt;"><i>this user purchased this product from us</i></span>');
		}
		
		html.push('</p>');
		html.push('<p class="review" style="margin-bottom: 15px;">' + unescape(review.reviewtext) + '</p>');
	}

	html.push('</div>');

	$("#rv" + pindex).append(html.join(''));
}

function Review(pindex)
{
	WriteReview(curitemid[pindex]);
}

function WriteReview(itemid)
{
	document.location.href = '/RetailScience/writereview?itemid=' + itemid;
}

function unhighlightall(itemid)
{
	unhighlight(itemid, "p2cpv");
	unhighlight(itemid, "p2cmi");
	unhighlight(itemid, "p2ccr");
}

function UploadPhoto(pindex)
{
	document.location.href = '/RetailScience/choosephoto?itemid=' + curitemid[pindex];
}

function getToolbarLeft(e)
{
	var left = "-91px";
	if(e == "p2cad")
	{
		left = "0px";
	}
	if(e == "p2cmi")
	{
		left = "-143px";
	}
	
	if(e == "p2ccr")
	{
		left = "-206px";
	}
	return(left);
}

function highlight(itemid, e)
{
	if(disabledItems[itemid])
	{
		if(disabledItems[itemid][e])
		{
			return;
		}
	}

	$("#" + e + itemid).attr("class", "p2ch");
}

function unhighlight(itemid, e)
{
	if(disabledItems[itemid])
	{
		if(disabledItems[itemid][e])
		{
			return;
		}
	}
	
	$("#" + e + itemid).attr("class", "p2c");
}

function activate(itemid, e)
{
	if(disabledItems[itemid])
	{
		if(disabledItems[itemid][e])
		{
			return;
		}
	}
	
	setBackgroundPosition(e + itemid, "-53px", getToolbarLeft(e));
}

function deactivate(itemid, e)
{
	if(disabledItems[itemid])
	{
		if(disabledItems[itemid][e])
		{
			return;
		}
	}
	
	setBackgroundPosition(e + itemid, "0px", getToolbarLeft(e));
}

function disable(itemid, e)
{
	var href = "javascript:void(0);";
	$("#" + e + itemid + ": a").attr("href", href);
	if(disabledItems[itemid] === null)
	{
		disabledItems[itemid] = new Array();
	}
		
	disabledItems[itemid][e] = 1;
}

function enable(itemid, e)
{
	var action = "MatchingItems";
	if(e == "p2ccr")
	{
		action = "CustomerReviews";
	}

	var href = "javascript:" + action + "(" + itemid + ");";
	$("#" + e + itemid + ": a").attr("href", href);

	if(disabledItems[itemid] === null)
	{
		disabledItems[itemid] = new Array();
	}
		
	disabledItems[itemid][e] = 0;
}

function displayAddedMessage(itemindex)
{
	var req = new Object;
	req.itemid = additemid[itemindex];
	req.itemindex = itemindex;
	req.success = queryRecommendationsNotification;
	AjaxQueryRecommendations(req);
}

var recommended = null;

function displayRecommendedItem(tv, imgno)
{
	for(i in recommended.list)
	{
		if(i == parseInt(i, 10))
		{
			var array = recommended.list[i];
			var itemid = array[imgno].itemid;
			document.location.href = '/RetailScience/page2/recommend/' + curitemid[recommended.itemindex] + '#' + itemid;
		}
	}
}

var aqrcount = 0;
var _recommend;

function queryRecommendationsNotification(recommend)
{
	_recommend = recommend;
	renderRecommendations();
}

function renderRecommendations()
{
	var modal = modalDiv();
	
	var recommend = _recommend;
	aqrcount += 1;
	modalBackdrop();
	$("#p2cad" + recommend.itemindex).html('');
	recommended = recommend;
	
	deactivate(recommend.itemindex, "p2cad");
	
	var html = new Array();

	html.push('<table class="rc" id="reccomend' + aqrcount + '" cellpadding="0" cellspacing="0">');
	html.push('<tr>');
	html.push('<td class="rctl"></td>');
	html.push('<td class="rctop" style="width: 627px;"><table class="bare" cellspacing="0" cellpadding="0"><tr><td style="width: 600px;">Shopping Bag</td><td style="text-align: right; width: 27px;"><a class="titlelink"  href="javascript:modalClose()">X</a></td></tr></table></td>');
	html.push('<td class="rctr"></td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td class="rcl"></td>');
	html.push('<td class="rcmid" style="width: 627px;">');

	var itemid = additemid[recommend.itemindex];
	var imguri = mainsURIs[itemid];
	html.push('<table cellpadding="0" cellspacing="0"><tr>');
	html.push('<td><img class="photo" style="margin-top: 3px; width: 107px; height: 107px; margin-right: 15px; margin-bottom: 15px;" src="' + imguri + '" /><td>');
	html.push('<td style="font-size: 9pt; width: 505px;"><p class="bigmsg" style="margin-top: 15px; margin-bottom: 5px;">This item was added to your shopping bag</p><span style="width: 500px">');
	
	if(customerType == 2 || customerType == 4)
	{
		html.push(idesc[itemid]);
	}
	else
	{
		html.push(iprice[itemid] + "&nbsp;" + idesc[itemid]);
	}
	
	html.push('</span>');
	
	if(recommend.listempty === 0)
	{
		html.push('<p class="bigmsg" style="margin-top: 15px; margin-bottom: 15px;">People who bought this item also bought:</p>');
	}
	
	html.push('</td></tr></table>');
	
	if(recommend.listempty === 0)
	{
		html.push('<div style="height: 120px; width: 631px;"><div id="rtv" style="width: 631px;"></div></div>');
	}
	
	html.push('<table cellpadding="0" cellspacing="0"><tr>');
	html.push('<td style="width: 480px;">');
	
	if(recommend.listempty === 0)
	{
		html.push('<a class="grey" style="font-weight: normal; font-size: 10pt;" href="/RetailScience/page2/recommend/' + itemid + '">View all of these items...</a>');
	}
	
	html.push('</td><td><div class="button" style="float: right; display: inline; width: 478px;">');
	var profile = siteprofile[siteid];
	if(profile.ident == 'gp')
		html.push('<a href="javascript:expressCheckout();" class="ord"><span><img class="photo" src="/images/btn_xpressCheckout.gif"></span></a>');
	html.push('<a class="button" href="https://' + thishost + '/RetailScience/checkout"><span>Checkout</span></a><a class="button" href="javascript:modalClose()"><span>Continue Shopping</span></a></div></td></tr></table>');

//	html.push('</td><td><div class="button" style="float: right; display: inline; width: 478px;"><a class="button" href="https://' + thishost + '/RetailScience/checkout"><span>Checkout</span></a><a class="button" href="javascript:modalClose()"><span>Continue Shopping</span></a></div></td></tr></table>');

	html.push('</td>');
	html.push('<td class="rcr"></td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td class="rcbl"></td>');
	html.push('<td class="rcbot"></td>');
	html.push('<td class="rcbr"></td>');
	html.push('</tr>');
	html.push('</table>');

	modalReplaceContent(html.join(''));
	pngifyTable($("#reccomend" + aqrcount));

	if(recommend.listempty === 0)
	{
		var i;
		var j;
		var rnum = new Array();
		var rimg = new Array();
		var ralt = new Array();
		for(i in recommend.list)
		{
			if(i == parseInt(i, 10))
			{
				var array = recommend.list[i];
				for(j = 0; j < array.length; j++)
				{
					rnum[rnum.length] = array[j].itemid;
					rimg[rimg.length] = array[j].imguri;
					ralt[ralt.length] = "$" + array[j].price.toFixed(2) + " " + array[j].description;
				}
			}
		}
		
		var loadimg = new Array();
		for(i = 0; i < rimg.length; i++)
		{
			loadimg[i] = '/images/photoloading.gif';
		}

		tvrec = new ThumbViewer('rtv');
		tvrec.setImageURIs(loadimg);
		tvrec.setAlt(ralt);
		tvrec.realimages = rimg;
		tvrec.displayCallback = displayRecommendedItem;
		tvrec.itemid = rnum[i];
		tvrec.thumbSize = 107;
		tvrec.divwidth = 113;
		tvrec.divheight = 107;
		tvrec.thumbSlots = 5;
		tvrec.widthArrow = 27;
		tvrec.arrowPrevOff = '/images/larrowprevoff.png';
		tvrec.arrowPrevOn =  '/images/larrowprevon.png';
		tvrec.arrowNextOff = '/images/larrownextoff.png';
		tvrec.arrowNextOn =  '/images/larrownexton.png';
		tvrec.css = 'tvalsobought';
		tvrec.generate();
		window.setTimeout("flipimages()", 1);
	}
	
	modalCenter();
	BagDisplaySummary();
}

var zoomedimg;

var Z = null;

function zchangePhotoDisplay(tv, imgno)
{
	if(tv)
	{
		changePhotoDisplay(tv.original, imgno);
	}
}

function zoom(i, itemid)
{
	if(pageNotLoaded("zoom(" + i + "," + itemid + ")"))
	{
		return;
	}

	var n = itemid;
	var a = cwURIs;
	if(Z === null)
	{
		Z = new Zoomer();
	}

	Z.setGalleryCallback(zchangePhotoDisplay);
	Z.cbtv = null;

	Z.zoomgallery(thumbviewer[i], "#pvphoto" + itemid, curimgno[itemid], sURIs[itemid], wURIs[itemid],
				  500, 500, note[itemid], desc[itemid]);
}

function zchangeCoordItemDisplay(tv, imgno)
{
	if(tv)
	{
		changeCoordItemDisplay(tv.original, imgno);
	}
}

function zoomSimilar(i)
{
	var n = coorditemid;
	var o = coordimgno;
	var a = cwURIs;
	if(Z === null)
	{
		Z = new Zoomer();
	}

	Z.setGalleryCallback(zchangeCoordItemDisplay);
	Z.cbtv = null;
	
	var m = citemnos[i];
	Z.zoomgallery(sthumbviewer[i], "#cvphoto" + i, coordimgno[i],
				  csURIs[m], cwURIs[m],
				  500, 500, null, null);
}

function flipimages()
{
	tvrec.setImageURIs(tvrec.realimages);
}

var addBagTimer;
var addBagRetries;

function BagAdd(itemindex)
{
	if(pageNotLoaded("BagAdd(" + itemindex + ")"))
	{
		return;
	}

	var item = curitemid[itemindex];

	var hascookies = CookiesTest();
	if(hascookies === false)
	{
		modalMessage("Cookies Required", '<p class="body" style="margin-top: 15px;">Our website requires the use of &ldquo;Cookies&rdquo; and your browser currently has Cookies turned off.</p><p class="body">Please turn cookies on and try to add the item to your bag again.</p>');
		return;
	}

	if(idiscon[item] == 1)
	{
		modalMessage("Discontinued Item", '<p class="body" style="margin-top: 15px;">We\'re sorry, that item has been discontinued, so you cannot add it to your bag</p>', 325);
		return;
	}
	
	if(ajax)
	{
		ajaxadd(itemindex, item, 1, null);
	}
	else
	{
		addtobag(item);
	}
}

function BagAddCustom(itemindex)
{
	var item = curitemid[itemindex];

	if(pageNotLoaded("BagAddCustom(" + itemindex + ")"))
	{
		return;
	}

	var hascookies = CookiesTest();
	if(hascookies === false)
	{
		modalMessage("Cookies Required", '<p class="body" style="margin-top: 15px;">Our website requires the use of &ldquo;Cookies&rdquo; and your browser currently has Cookies turned off.</p><p class="body">Please turn cookies on and try to add the item to your bag again.</p>');
		return;
	}

	if(idiscon[item] == 1)
	{
		modalMessage("Discontinued Item", '<p class="body" style="margin-top: 15px;">We\'re sorry, that item has been discontinued, so you cannot add it to your bag</p>', 325);
		return;
	}
	
	var s = $("#custom" + itemindex).val();
	if(s.length === 0)
	{
		modalMessage("Custom Item", '<p class="body" style="margin-top: 15px;">Unable to add item to your shopping bag.</p><p class="body">Please enter the text you want to use to customize this item, then click "Add to Bag" again.</p>', 325);
		return;
	}
	
	if(ajax)
	{
		ajaxadd(itemindex, item, 1, s);
	}
	else
	{
		addtobag(item);
	}
}

function BagAddQuantity(itemindex)
{
	if(pageNotLoaded("BagAddQuantity(" + itemindex + ")"))
	{
		return;
	}

	var quantity = $("#qty" + itemindex).val();
	if(quantity < 6)
	{
		alert("There is minimum quantity of 6 units for wholesale customers.\n\nPlease increase your quantity to 6 or more.");
		return;
	}

	var item = curitemid[itemindex];
	ajaxadd(itemindex, item, quantity, null);
}

var addreq;

function ajaxadd(itemindex, item, qty, custom)
{
	additemid[itemindex] = curitemid[itemindex];
	var now = new Date();
	addreq = new Object;
	addreq.success = pg2addBagSuccess;
	addreq.failure = pg2addBagFailure;
	addreq.itemindex = itemindex;
	addreq.itemid = item;
	addreq.quantity = qty;
	if(custom)
		addreq.custom = custom;
		
	if(AjaxAddToBag(addreq))
	{
		var html = '<p class="body" style="margin-top: 15px; text-align: center;">Adding item to bag</p><img src="/images/pinkprogressbar.gif" />';
		modalDialog("Shopping Bag", html, 220, 'addbagnotice', 'NoClOsE');
		addBagRetries = 0;
		addBagTimer = window.setTimeout("pg2retryAdd()", 10000);
	}
	else
	{
		modalMessage("Shopping Bag Busy", '<p class="body" style="margin-top: 15px;">Sorry, we cannot add an item to your shopping bag at the moment because your shopping bag is busy.</p><p class="body">Please try again in a few seconds.</p>', 500);
	}

	lastAddTime = now.getTime();
}


function pg2addBagSuccess(bagresponse, statusText)
{
	if(addBagTimer)
	{
		window.clearTimeout(addBagTimer);
		addBagTimer = null;
	}
	
	window.setTimeout("bagBusy = false;", 1500);
	bag = bagresponse;
	
	DeleteCookieInDomain("fabuless.com", "itemtracker");
	if(bag.count > 0)
	{
		SetCookie("itemtracker", $.toJSON(bag.itemtracker));
	}
	else
	{
		SetCookieInDomain(thishost, "itemtracker", "");
	}

	var e = ge("bag" + bagresponse.itemindex + "_adding");
	if(e) { e.style.display = "none"; }
	
	if(customerType == -1)
	{
		var req = new Object;
		AjaxQueryCustomerType(req);
	}
	
	var now = new Date();
	var currentTime = now.getTime();
	window.setTimeout("displayAddedMessage(" + bag['itemindex'] + ")", 500);
}

function pg2addBagFailure(p)
{
	if(addBagTimer)
	{
		window.clearTimeout(addBagTimer);
		addBagTimer = null;
	}
	
	window.setTimeout("bagBusy = false;", 1500);
}

function pg2retryAdd()
{
	addBagTimer = null;
	if(addBagRetries == 2)
	{
		addBagFailure();
		return;
	}
	
	addBagRetries += 1;
	addBagTimer = window.setTimeout("AjaxRetryAdd()", 10000);
	AjaxAddToBag(addreq);
}

function AddWishlist(itemindex)
{
	if(! docReady)
	{
		modalMessage("Page Loading", '<p class="body" style="margin-top: 15px;">Please wait for the page to finish loading, then click Add to Wishlist again</p>', 300);
		return;
	}

	var hascookies = CookiesTest();
	if(hascookies === false)
	{
		modalMessage("Cookies Required", '<p class="body" style="margin-top: 15px;">Our website requires the use of &ldquo;Cookies&rdquo; and your browser currently has Cookies turned off.</p><p class="body">Please turn cookies on and try to add the item to your Wish List again.</p>');
		return;
	}

	if(ajax)
	{
		var req = new Object;
		req.success = pg2addWishlistSuccess;
		req.failure = pg2addWishlistFailure;
		req.wishlistid = -1;
		req.itemid = curitemid[itemindex];
		req.quantity = 1;
		AjaxAddToWishlist(req);
	}
}

function pg2addWishlistSuccess(resp, statusText)
{
}

function pg2addWishlistFailure(resp, statusText)
{
}

function BagAddQuantity(itemindex)
{
	var quantity = $("#p2add" + itemindex).val();
	if(quantity < 6)
	{
		alert("There is minimum quantity of 6 units for wholesale customers.\n\nPlease increase your quantity to 6 or more.");
		return;
	}

	var itemid = curitemid[itemindex];
	var req = new Object;
	req.success = pg2addBagSuccess;
	req.failure = pg2addBagFailure;
	req.itemindex = itemindex;
	req.itemid = itemid;
	req.quantity = quantity;
	if(AjaxAddToBag(req))
	{
		BagQuantityClose(itemindex);
		ge("p2cad" + itemindex).innerHTML = '<img src="/images/ajaxpinkbusy.gif" style="border: 0px; margin-top: 8px; margin-left: 45px;" />';
		activate(itemindex, "p2cad");
	}
	else
	{
		modalMessage("Shopping Bag Busy", '<p class="body" style="margin-top: 15px;">Sorry, we cannot add an item to your shopping bag at the moment because your shopping bag is busy.</p><p class="body">Please try again in a few seconds.</p>', 500);
	}
}

function BagEnterQuantity(itemindex)
{
	var quantity = 12;
	$("#p2who" + itemindex).css("display", "block");
	$("#p2add" + itemindex).val(quantity);
	$("#p2wsl" + itemindex).animate({ left: "0px" }, 150, "swing",
		function()
		{
			$("#p2add" + itemindex).each(
				function()
				{
					this.focus();
					this.select();
				}
			);
		}
	);
}

function BagQuantityClose(itemindex)
{
	$("#p2wsl" + itemindex).animate({ left: "-92px" }, 150, "swing", function() { 	$("#p2who" + itemindex).css("display", "none"); } );
}

function displayPropaganda(tv, i)
{
	if(Z === null)
	{
		Z = new Zoomer();
	}
		
	Z.zoom(tv.getImage(i), thumb[i], photo[i], pw[i], ph[i]);
}

function pageInitPropaganda()
{
	curphoto = 0;

	var profile = siteprofile[siteid];
	var tvrec = new ThumbViewer('ptv');
	tvrec.setImageURIs(thumb);
	tvrec.displayCallback = displayPropaganda;
	tvrec.thumbPad = 8;
	tvrec.thumbSize = profile.propThumbSize;
	tvrec.divheight = profile.propDivHeight;
	tvrec.divwidth = profile.propDivWidth;
	tvrec.shadow = profile.propShadow;
	tvrec.shadowh = profile.propShadowh;
	tvrec.shadoww = profile.propShadoww;
	tvrec.thumbSlots = 3;
	tvrec.arrowPrevOff = '/images/tarrowprevoff.png';
	tvrec.arrowPrevOn =  '/images/tarrowprevon.png';
	tvrec.arrowNextOff = '/images/tarrownextoff.png';
	tvrec.arrowNextOn =  '/images/tarrownexton.png';
	tvrec.css = 'tvprop';
	tvrec.divcss = 'tvdivcss';
	tvrec.acss = 'ziphoto';
	tvrec.thumbcss = 'ziphoto';
	tvrec.widthArrow = profile.propWidthArrow;
	tvrec.generate();

	SetCookie('propagandafrom', '');
}

function dealVerificationCallback(response)
{
	if(response.status == 0)
	{
		if(response.dealstatus != 0)
		{
			document.location.href = '/RetailScience/nodeal/' + response.deal.dealcode + '/' + response.dealstatus;
		}
	}
}

function pg2customerTypeCallback(resp, statusText)
{
	pageInitCustomer();
}	

function pg2Init()
{
	var profile = siteprofile[siteid];

	pageCommonInit();
	pageInitNavbar();
	if(haspropaganda)
	{
		pageInitPropaganda();
	}

	if(ajax)
	{
		var req = new Object();
		req.success = pg2customerTypeCallback;
		req.failure = pg2customerTypeCallback;
		AjaxQueryCustomerType(req);
	}
	
	docReady = true;

	var i;
	var tv;
	for(i = 0; i < itemnos.length; i++)
	{
		tv = new ThumbViewer('ptv' + i);
		thumbviewer[i] = tv;
		tv.setImageURIs(tURIs[itemnos[i]]);
		tv.displayCallback = changePhotoDisplay;
		tv.itemid = itemnos[i];
		tv.i = i;
		tv.css = 'tvbarwrapper';
		tv.divcss = 'tvdivcss';
		tv.arrowPrevOff = '/images/sarrowprevoff.png';
		tv.arrowPrevOn =  '/images/sarrowprevon.png';
		tv.arrowNextOff = '/images/sarrownextoff.png';
		tv.arrowNextOn =  '/images/sarrownexton.png';	
		tv.shadow = profile.pvShadow;
		tv.divheight = profile.pvDivHeight;
		tv.divwidth = profile.pvDivWidth;
		tv.title = note[itemnos[i]];
		tv.generate();

		ge("mag" + i).style.display = "block";
		if(InternetExploderFive || InternetExploderSix)
		{
			el_fnFixPng(ge("mag" + i));
		}

		$("#zl" + i).attr('href', "javascript:zoom(" + i + "," + itemnos[i] + ");");
	}

	for(i = 0; i < citemnos.length; i++)
	{
		enable(i, "p2ccr");
		var itemid = citemnos[i];
		if(citemids[itemid].length > 0)
		{
			currentcoordno[i] = 0;
			coordimgno[i] = 0;
			tv = new ThumbViewer('ctv' + i);
			sthumbviewer[i] = tv;
			var URIs = ctURIs[itemid];
			tv.setImageURIs(URIs);
			tv.displayCallback = changeCoordItemDisplay;
			tv.itemid = citemnos[i];
			tv.i = i;
			tv.css = 'tvbarwrapper';
			tv.divcss = 'tvdivcss';
			tv.arrowPrevOff = '/images/sarrowprevoff.png';
			tv.arrowPrevOn =  '/images/sarrowprevon.png';
			tv.arrowNextOff = '/images/sarrownextoff.png';
			tv.arrowNextOn =  '/images/sarrownexton.png';	
			tv.shadow = profile.pvShadow;
			tv.divheight = profile.pvDivHeight;
			tv.divwidth = profile.pvDivWidth;
			tv.generate();
		
			ge('cvphoto' + i).src = csURIs[citemnos[i]][0];
			enable(i, "p2cmi");
		}
		else
		{
			disable(i, "p2cmi");
		}
	}
	
	pngifyLabels();
	if(Gecko && Macintosh)
	{
		$("img.ziphoto").each(function(i) { this.style.cursor = "-moz-zoom-in"; });
		$(".pg2photo").each(function(i) { this.style.cursor = "-moz-zoom-in"; });
	}

	$("td.p2c: a").attr("onmouseover", "this.style.textDecoration='underline';"); 	
	$("td.p2c: a").attr("onmouseout", "this.style.textDecoration='none';");
	
	if(dealcode.length > 0)
	{
		var req = new Object;
		req.dealcode = dealcode;
		req.success = dealVerificationCallback;
		req.failure = null;
		AjaxVerifyDeal(req);
	}

	$("div.p2cadd").css({ 'display' : 'block'});
}

function pngifyLabels()
{
	if(InternetExploderSix)
	{
		$("img.p2discontinued").each(function(i) { el_fnFixPng(this); });
		$("img.p2soldout").each(function(i) { el_fnFixPng(this); });
	}
}

