$(document).ready(function () {
	if (typeof window.Eaccounts != 'object') {
		window.Eaccounts = new Eaccounts();
		}
	window.Eaccounts.init();
	});

function popwin(sParam,iSecure)
	{
	sBaseURL="https://www3.eaccounts.co.nz";
	if (iSecure) sBaseURL="https://www3.eaccounts.co.nz";
	sShopCode="ADVANCE";
	sTarget="basket";
	//sSettings="top=10,left=10,width=830,height=675,location=no,menubar=no,status=no,toolbar=yes,scrollbars=yes";
	//sSettings="top=10,left=10,width=830,height=675";
	sGoURL=(sBaseURL+"/oCart.asp?shop="+sShopCode+"%7C1%7C"+sParam);

	//window.open(sGoURL,sTarget,sSettings);


	$.fancybox({
		'width': 600,
		'height': 500,
		'autoScale': true,
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'type': 'iframe',
		'href': sGoURL
		});
	}



function openCenteredWindow(url) {
	var width = 500;
	var height = 400;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	myWindow = window.open(url, "windowID", windowFeatures);
	}

var Eaccounts = {
	baseURL: "https://www3.eaccounts.co.nz",
	shopCode: "ADVANCE",
	cartFrame: "cartItems",
	hiddenFrame: "hiddeniframe",
	catalog: "catalog",
	search: "search",
	
	init: function () {
		//Eaccounts.setConfig();
		Eaccounts.autoRefresh();
		Eaccounts.refreshCart();
		
		//setTimeout(Eaccounts.setConfig,500);
	
	},

	setConfig: function () {
		if (window.frames[Eaccounts.hiddenFrame] != null) {
			window.frames[Eaccounts.hiddenFrame].location.href = Eaccounts.buildURL('oCart.asp', 'SETCONFIG=SF NH CB FB ON');
		}
	
	},
	
	buyProduct: function (productCode, productQty) {
		if (productQty ==  undefined) {
			var addToCart = "<a href=\"#buy\" onclick=\"window.Eaccounts.buyProduct('"+productCode+"', document.getElementById('productQty').value)\">+ Add to Cart</a>";
			
			
			var qtyInput = '<input type="text" name="productQty" id="productQty" value="1" />';
			var qtyDialog = '<div class="productQty form"><span class="title">Quantity: </span>'+qtyInput+' '+addToCart+'</div>';
			
			// show qty frame
			jQuery.fancybox(qtyDialog, {overlayShow:false});
			
		} else {
			
			openCenteredWindow(Eaccounts.buildURL('oCart.asp', 'ADDITEMDIRECT='+productCode+"%7C"+productQty));
			//below doesn't work in most modern browsers
			//window.frames[Eaccounts.hiddenFrame].location.href = Eaccounts.buildURL('oCart.asp', 'ADDITEMDIRECT='+productCode+"%7C"+productQty);
			
			jQuery.fancybox('<div class="productQty success"><h3>'+productQty+' product(s) added to your cart</h3></div>', {overlayShow:false});
			setTimeout(function () { jQuery.fancybox.close() },1100);
				
			setTimeout(Eaccounts.refreshCart,150);
		}
	},
	
	refreshCart: function() {
		if (frames[Eaccounts.cartFrame]) {
			frames[Eaccounts.cartFrame].location.href= Eaccounts.buildURL('oTotal.asp', 'CARTSUMMARY=YES');
		}
	},
	
	setCatalog: function(oSearch) {
		if (oSearch && document.getElementById(Eaccounts.catalog)) {
			document.getElementById(Eaccounts.catalog).src = Eaccounts.buildURL('oCart.asp', "ipage=" + oSearch);
		} else if( document.getElementById(Eaccounts.catalog) ) {
			document.getElementById(Eaccounts.catalog).src = Eaccounts.buildURL('oCart.asp', "ALLPAGES=YES");
		}
	},
	
	setSearch: function(keywords) {
		if (keywords && document.getElementById(Eaccounts.search)) {
			document.getElementById(Eaccounts.search).src = Eaccounts.buildURL('oCart.asp', 'key='+keywords);
		}
	},
	
	autoRefresh: function() {
		setInterval(window.Eaccounts.refreshCart, 10000);
	},
	
	buildURL: function (script, vars) {
		//return Eaccounts.baseURL+'/'+script+'?shop='+Eaccounts.shopCode+'|1|'+vars;
		return Eaccounts.baseURL+'/'+script+'?shop=ADVANCE|1|'+vars;
	}
}
