﻿// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var util = {
	cacheVersion: null,
	cacheQuery: 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;
	},
	
	isVista: function() {	
		var result = false;
		var val = navigator.userAgent;
		var m = val.match(/NT \d+\.{0,1}\d*;/);
		if(m.length > 0) { if(((m[0].match(/\d+\.{0,1}\d*/)[0]) * 1.0) >= 6) result = true; }
		return result;
	},
	
	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;
	}, 
	
	setCookie: function(name, value, expires, path, domain, secure) {
		if(!expires) {
			expires = new Date();
			expires.setDate(expires.getDate()+1);
		}
		var curCookie = name + "=" + escape(value) +
	        ((expires		) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	},
	
	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 = '';
		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;
	},
	
	isVisible: function(obj) {
		var visible = true;
		if (obj.parentElement) {
			if(obj.style.display == 'none') visible = false;
			while (obj = obj.parentElement) {
				if(obj.style) if(obj.style.display == 'none') visible = false;
			}
		}
		return visible;
	},
	
	QueryString: function(name) {
		var result = '';
		if(util.cacheQuery == null) {
			util.cacheQuery = new Array();
			var url = document.location.href;
			if(url.indexOf('?') > -1) 
			{
				url = url.substring(url.indexOf('?')+1);
				queryVals = url.split(/&/);
				for(var i=0; i<queryVals.length; i++) 
				{
					util.cacheQuery[i] = new Object();
					util.cacheQuery[i].name = queryVals[i].substring(0, queryVals[i].indexOf('='))
					util.cacheQuery[i].value = queryVals[i].substring(queryVals[i].indexOf('=')+1)
				}
			}
		}
		for(var i=0; i<util.cacheQuery.length; i++)
		{
			if(util.cacheQuery[i].name.toLowerCase().trim() == name) {
				result = util.cacheQuery[i].value;
			}
		}
		return result;
	}
}

function setupAnimationStarts() {
	var img = document.getElementsByTagName('img');
	for(var i=0; i<img.length; i++) {
		if(/\banimateOnClick\b/.test(img[i].className)) {
			if(window.parent == window) 
			{
				img[i].onclick = function() {
					this.src = this.realName;
					this.style.cursor = 'default';
					this.onclick = function() {};
				}
				
				if(img[i].src.indexOf('v_') > -1 && !util.isVista()) {
					img[i].src = img[i].src.replace(/v_/, '');
				}
				
				img[i].style.cursor = 'pointer';
				img[i].realName = img[i].src.replace(/_start/,'') + '?r=' + Math.random();
			} 
			else 
			{
				util.addEvent(img[i], 'click', function() { alert('Cannot run anim inside the CMS.'); });
			}
		}
	}
}

util.addEvent(window, 'load', function() {
	SupportCookie();

	setupAnimationStarts();
	/*var howToGif = document.getElementById('howToGif');
	if(howToGif != null) {
		if(!util.isVista()) howToGif.src='images/howto/how-to-run.gif';
	}*/

	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 = '';
	}
}

function SupportCookie() {
	if(document.location.href.indexOf('&b=') > -1) 
	{
		util.setCookie('support_b', util.QueryString('b'));
		util.setCookie('support_a', util.QueryString('a'));
		util.setCookie('support_o', util.QueryString('o'));
		util.setCookie('support_p', util.QueryString('p'));
		util.setCookie('support_x', util.QueryString('x'));
	}
}
