﻿// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var util = {
	cacheVersion: null,
	
	getMSIEVersion: function() {
		if(util.cacheVersion != null) return util.cacheVersion;
		msv = 0;
		if(navigator.appVersion.indexOf('MSIE') > -1) {
			msv = navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,10);
			msv = msv.substr(0, msv.indexOf(';'));
		}
		util.cacheVersion = msv * 1;
		return util.cacheVersion;
	},
	
	addEvent: function(object, eventName, fn) {
		if (document.addEventListener) object.addEventListener(eventName, fn, false);
		else object.attachEvent('on' + eventName, fn);
	},
	
	byId: function(id) {
		return document.getElementById(id);
	},
		
	findPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return {left:curleft,top:curtop};
	},
	
	createOption: function(ddlList, text, value, selected) {
		var option = document.createElement("OPTION");
		ddlList.options.add(option);
		option.text = text;
		option.innerText = text;
		option.value = value;
		if(selected) option.selected = true;
		return option;
	}, 
	
	toPrice: function(v) {
		v = (Math.round(v * 100) / 100) + '';
		if(v.indexOf('.') == -1) v = v + '.00';
		else if(v.indexOf('.') == v.length-2) v = v + '0';
		dp = v.substring(v.indexOf('.')+1);
		fp = v.substring(0, v.indexOf('.'));
		var nfp = '';
		var tester = document.getElementsByTagName('p');
		if(fp.length > 3) {
			var c = 0;
			for(var i=fp.length-1; i>=0; i--) {
				if(c == 3) { nfp = ',' + nfp; c = 0; }
				c++;
				nfp = fp.substr(i,1) + nfp;
			}
			fp = nfp;
		}
		return fp + '.' + dp;
	},
	
	setCookie: function(name, value, expires, path, domain, secure) {
		var curCookie = name + "=" + escape(value) +
	        ((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	},
	
	getCookie: function(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
	        begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	},
	
	delCookie: function(name) {
		if(util.getCookie(name)) document.cookie = name + '=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}

util.addEvent(window, 'load', function() {
	var url = document.location.href;
	var productCode = util.getCookie('product');
	if(productCode == null) productCode = '';
	util.delCookie('product', '/', '');
	
	if(productCode != '') {
		var z = document.getElementById('prod_' + productCode);
		var ztd = z.getElementsByTagName('td');
		for(var i=0; i<ztd.length; i++) ztd[i].className += ' highlightBasketRow';
		
		var callout = document.createElement('div');
		callout.id = 'calloutbox';
		var p = util.findPos(document.getElementById('shopBasket'));
		document.body.appendChild(callout);
		callout.style.left = (p.left - callout.offsetWidth + 60) + 'px';
		callout.l = (p.left - callout.offsetWidth + 60);
		callout.style.top = (p.top - 90) + 'px';
		callout.t = (p.top - 90)
		callout.innerHTML = '<div>Your item has been added to your basket</div>';
		callout.childNodes[0].style.backgroundImage = 'url(images/callout.' + ((util.getMSIEVersion() > 0 && util.getMSIEVersion() < 7) ? 'gif' : 'png') + ')';
		var height = 115;
		var h = function() {
			var width = (height / 115) * 181;
			callout.style.height = height + 'px';
			callout.style.width = width + 'px';
			callout.style.top = (callout.t + (115 - height)) + 'px';
			callout.style.left = (callout.l + (181 - width)) + 'px';
			callout.childNodes[0].style.marginLeft = -(181 - width) + 'px';
			callout.childNodes[0].style.marginTop = -(115 - height) + 'px';
			height = height - 10;
			if(height > 0) setTimeout(h,20);
			else document.body.removeChild(callout);
		}
		var c = setTimeout(h,5000);
		callout.onclick = function() {
			clearTimeout(c); h();
		}
	}
});

function navToLink(v) {
	document.location.replace(v + '&r=' + Math.random());
}

var TOthumbHover = null;
var thumbHoverIm = null;

util.addEvent(window, 'load', function() {
	if(typeof(expandableImageDiv) != 'undefined') {
		var im = document.createElement('div');
		im.className = 'expandimage';
		var element = document.getElementById(expandableImageDiv);
		element.appendChild(im);
		var imElement = element.getElementsByTagName('img')[0];
		im.onclick = imElement.onclick;
		imElement.title = 'Click to view larger image';
		imElement.style.cursor = 'pointer';
	}
	
	var el = null;
	var t = document.getElementsByTagName('table');
	if(t.length > 0) for(var i=0; i<t.length; i++) if(t[i].className == 'productListing') el = t[i];
	if(el != null) {
		var im = el.getElementsByTagName('img');
		for(var i=0; i<im.length; i++) {
			im[i].onmouseover = function() {
				clearTimeout(TOthumbHover);
				thumbHoverIm = this;
				TOthumbHover = setTimeout(thumbHover, 1000);
			}
			im[i].onmouseout = function() {
				thumbHoverIm = null;
				clearTimeout(TOthumbHover);
			}
		}
	}
});

function thumbHover() {
	var src = thumbHoverIm.src;
	src = src.replace(/w=40/,'w=80').replace(/h=30/,'h=60');
	var im = document.createElement('img');
	im.src = src;
	var pos = util.findPos(thumbHoverIm);
	im.className = 'thumbPopup';
	im.style.left = (pos.left-34) + 'px';
	im.style.top = (pos.top-20) + 'px';
	document.body.appendChild(im);
	im.url = thumbHoverIm.parentNode.href;

	im.onclick = function() {
		document.location = this.url;
	}
	
	im.onmouseout = function() {
		document.body.removeChild(im);
		clearTimeout(TOthumbHover);
		thumbHoverIm = null;
	}
}

function viewImage(width, height) {
	var divElement = document.getElementById(expandableImageDiv);
	var element = divElement.getElementsByTagName('img')[0];
	var e = element.src;
	var f = e.match(/[a-z0-9A-Z]*\.(j[e]?pg)/gi);
	if(f.length > 0) {
		f = f[0];
		var div = document.createElement('div');
		var im = document.createElement('img');
		if(width > 400) {
			height = height / (width / 400);
			width = 400;
		}
		im.src = 'im.aspx?f=productimages/' + f + '&w=' + width + '&m=2&q=90';
		im.style.width = width + 'px';
		im.style.height = height + 'px';
		div.appendChild(im);
		var p = util.findPos(element);

		div.className = 'largeProductImage';
		div.style.left = (p.left-6) + 'px';
		div.style.top = (p.top-8) + 'px';
		div.style.width = (width+2) + 'px';
		document.body.appendChild(div);
		
		div.innerHTML += "<p>Hide image</p>";
		
		div.title = 'Click to hide image';
		document.body.imageDiv = div;

		setTimeout(function() {
			document.body.onclick = function() {
				document.body.removeChild(document.body.imageDiv);
				document.body.imageDiv = null;
				document.body.onclick = null;
				im.src = '';
			}
		},100);
	}
}