﻿// 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};
	},
	
	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;
	}
}

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() {
	return;
	var url = document.location.href;
	if(url.match(/\&terms=.*/i)) 
	if(url.match(/\&terms=.*/i).length > 0) {
		var terms = url.match(/\&terms=.*/)[0].replace(/\&terms=/gi,'');
		var t = terms.split(" ");
		if(t.length > 0) {
			html = document.getElementById('pageHtml');
			for(var i=0; i < t.length; i++) {
				//re = new RegExp("\\b(" + t[i].replace(/_/gi,' ') + ")\\b", "gi");
				//html.innerHTML = html.innerHTML.replace(re, '<span style="background-color:yellow;">$1</span>');
				htmlHighlight(t[i]);
			}
		}
	}
	//setupSearchBox();
});

function htmlHighlight(text) {
	re = new RegExp("\\b(" + text.replace(/_/gi,' ') + ")\\b", "gi");
	var html = document.getElementById('pageHtml');
	var result = '';
	var v = '';
	if(html) {
		h = html.innerHTML.split('<');
		for(var i=0; i<h.length; i++) {
			s = h[i].split('>');
			if(s.length > 1) {
				s[1] = s[1].replace(re, '<span class="highlight">$1</span>');
				result = result + '<' + s[0] + '>' + s[1];
			} 
		}
	}
	html.innerHTML = result;
}

// Disabled - cool buy not good end-user stufff!!!
function setupSearchBox() {
	var d = document.getElementById(searchTextId);
	d.normalWidth = d.offsetWidth;
	d.r = 0;
	d.add = 0;
	d.tout = null;
	
	d.setsize = function() {
		d.r = d.r + d.add;
		var stop = false;
		if(d.r >= Math.PI / 2) { d.r = Math.PI / 2; stop = true; }
		if(d.r <= 0) { d.r = 0; stop = true; }
		d.style.width = (d.normalWidth + Math.round(Math.sin(d.r) * d.normalWidth)) + 'px';
		if(!stop) setTimeout(d.setsize, 10);
	}
	
	d.onfocus = function() {
		clearTimeout(d.tout);
		d.add = 0.1;
		setTimeout(d.setsize, 10);
	}
	d.onblur = function() {
		clearTimeout(d.tout);
		d.add = -0.1;
		setTimeout(d.setsize, 10);
	}
}