﻿// 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;
	}
}

util.addEvent(window, 'load', function() {
	var url = document.location.href;
	if(url.match(/\&pv=\d*/i)) 
	if(url.match(/\&pv=\d*/i).length > 0) {
		var m = url.match(/\&pv=\d*/)[0].replace(/\&pv=/gi,'');
		var z = document.getElementById('proVer' + m);
		if(!z) return;
		var rad = 0.0, mag = 100.0, orig = 240.0, spd = 0.05;
		var f = function() {
			var v = (1.0 - Math.abs(Math.cos(rad))) * mag;
			var r = orig;var g = Math.floor(orig - v);var b = Math.floor(orig - v);
			z.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
			rad = rad + spd;
			if(rad < Math.PI) {
				z.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
				setTimeout(f,20);
			} else {z.style.background = 'none';}
		}
		f();
		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();
		}
	}
});

util.addEvent(window, 'load', function() {
	if(util.getMSIEVersion() > 0 && util.getMSIEVersion() < 7) {
		var c = document.getElementById('selectCountry');
		var element = c.getElementsByTagName('span')[0];
		element.onmouseover = function() {
			element.getElementsByTagName('div')[0].style.display = 'block';
		}
		element.onmouseout = function() {
			element.getElementsByTagName('div')[0].style.display = 'none';
		}
	}
});

function showLargeImage(element) {
	var f = element.src.match(/f=.*\.jpg/gi)[0].replace(/f=/gi,'').replace(/\.jpg/gi,'_large.jpg');
	
	var div = document.createElement('div');
	var img = document.createElement('img');
	var p = util.findPos(element);
	div.className = 'largeProductImage';
	img.src = 'im.aspx?f=' + f + '&w=700&m=2&q=80';
	div.style.left = ((p.left + element.offsetWidth) - 709) + 'px';
	div.style.top = (p.top - 11) + 'px';
	div.appendChild(img);
	document.body.appendChild(div);
	div.title = 'Click to close image';
	div.onclick = function() {
		document.body.removeChild(div);
		img.src = '';
	}
}