
var currentNavID 		= 0;
var overNav 			= 0;

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

}

function checkForValidBrowser() {	

	if (readCookie('browserVersionCookie2')!='1') {
	// if cookies not set, check for browser version, see if it's too old.
		error = 0;
		BrowserDetect.init();
		createCookie('browserVersionCookie2',1,1);


		if (BrowserDetect.browser=='Firefox' && Number(BrowserDetect.version) < 3) error = 1;
		if (BrowserDetect.browser=='Opera' && Number(BrowserDetect.version) < 8) error = 1;
		if (BrowserDetect.browser=='Explorer' && Number(BrowserDetect.version) < 6) error = 1;

		if (error) {
			if (d = document.getElementById('info-feed-box')) {
			d.style.display = '';
			//d.innerHTML = "<p>We have detected that you are using <b>" + BrowserDetect.OS + " " + BrowserDetect.browser + " " + BrowserDetect.version + "</b> to browse the internet. As this is an old version of <b>" + BrowserDetect.browser + "</b> you may experience problems in browsing and as such we recommend you upgrade your browser, or try a different one: <a href=\"http://www.microsoft.com/uk/windows/internet-explorer/browse-with-confidence.aspx?WT.mc_id=Search&WT.srch=1\" onclick=\"window.open(this.href); return false\" title=\"Opens new page to download new browser\">Internet Explorer</a>, <a href=\"http://www.mozilla-europe.org/en/firefox/\" onclick=\"window.open(this.href); return false\" title=\"Opens new page to download new browser\">Firefox</a>, <a href=\"http://www.opera.com/\" onclick=\"window.open(this.href); return false\" title=\"Opens new page to download new browser\">Opera</a>, <a href=\"http://www.apple.com/safari/download/\" onclick=\"window.open(this.href); return false\" title=\"Opens new page to download new browser\">Safari</a></p><p style=\"text-align:right\"><a href=\"#\" onclick=\"document.getElementById('info-feed-box').style.display='none'; return false\" >Close X</a></p>";
			} // end if d
		} // end if error
	} // end if readCookie
}

function checkBrowser() {
		BrowserDetect.init();
		window.alert("We have detected that you are using " + BrowserDetect.OS + " " + BrowserDetect.browser + " " + BrowserDetect.version + ". " + (BrowserDetect.browser=='Explorer') +" " +  (Number(BrowserDetect.version) < 7) + " cookie=" + readCookie('browserVersionCookie2'));

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function getY( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}


function DivValueToNum(v) {

	return parseInt(v.replace("px",""));
}


function Element(id) { return document.getElementById(id); }


function displayDelayedNav(id) {
		
		if (overNav) {
		hideCurrent();
		if (d = document.getElementById('nav-m-' + id)) {

			d.style.display =  (id==currentNavID && d.style.display!='none')? 'none': d.style.display = '';	
			document.getElementById(id).style.backgroundPosition = "0 100%";
			currentNavID = id;
		}
		}	
	
	}
function displayNav(id, delay) {
	
	if (delay==undefined) delay = 300;
	hideCurrent();
	
	overNav = 1;
	if (delay>0) setTimeout("displayDelayedNav('" + id + "')",delay);
	else displayDelayedNav(id);
}


function hideNav() {
	t = 0;
	overNav = 0;
	setTimeout(function() {
		if (!overNav) hideCurrent();		
	},t);
	
}


function hideCurrent() { 
	if (d = document.getElementById("nav-m-"+currentNavID)) {
		d.style.display = 'none'; 
		document.getElementById(currentNavID).style.backgroundPosition = "0 0";
	}

}

	


function switchImage(a, b) {
	document.getElementById(a).style.backgroundImage ="url(" + b + ")";
	return false;
}

function submitSearchResults(f) {

	form[f].submit();
	return false;
}


function showHideAdvanced() {

	opts = Element('advanced-options');
	img = Element('b-advanced-search');
	title = Element('advanced-titleHolder');
	box = Element('advanced-box');
	
	
	if (opts.style.display=='none') {
		opts.style.display = "";
		img.src = '/images/shared/box_t_advanced_on.gif';		
		box.className = 'search-dropdown';
		box.style.width = '96.5%';
	}else{
		opts.style.display = "none";
		img.src = '/images/shared/box_t_advanced_off.gif';
		box.className = 'advanced';
	}
}

function openProductWindow(ref, w, h) {

	window.open(ref, 'sizeChart',"width=" + w + ", height=" + h + ", menubar=0");

	return false;

}

function sitemapOpen(id) {
	if (d = document.getElementById(id)) d.style.display = (d.style.display =='none') ? '' : 'none';
	return false;
}


function showMag(v) {

v.style.backgroundImage = "url(/images/products/mag_overlay.gif)";
}

function hideMag(v) {

v.style.background = "none";
}

function openMag(img) {
	imageAppear(img);
}


function imageAppear(img) {

	if (d = document.getElementById('image-popup')) {
		d.style.display = 'block';
		d.innerHTML = '<h3>Close Window</h3>';
		
		h = (browser.isIE) ? document.documentElement.scrollTop : window.scrollY;
		if (h==undefined) h = 0;
		
		d.style.top = h-120 + 'px';
		d.style.backgroundImage = "url(/" + img + ")";
		
	  	if (browser.isIE)
	  		d.attachEvent("onclick", closeImageAppear);
		else		
			d.addEventListener("click", closeImageAppear, true);
		// end if
	}

}



function closeImageAppear() {
	if (d = document.getElementById('image-popup')) {
		d.style.backgroundImage = "url(/images/filters/loading.gif)";
		d.style.display = "none";
	}
}


function MoveTo(id, newV, v) {
		moving = true;
		if (d = document.getElementById(id)) {
			//window.alert(d + " " + newV + " " + v);
			mt = parseInt(d.style.marginTop.replace("px",""));
			mt = (mt<0 || mt>0) ? mt : 0;
			sum = mt + (v/10);

			d.style.marginTop = sum + "px";

			check = (v < 0) ? (sum>newV) : (sum<newV);

			if (check) setTimeout("MoveTo('"+id+"','"+newV+"','"+v+"')",1);
			else moving = false;
		}

	}

	function scrollDiv(id, dir, parentOffset) {
		var newV;

		v = (dir=='down') ? -200 : 200;

		if (d = document.getElementById(id)) {

			if (d.maxH == undefined) d.maxH = ((d.clientHeight ? d.clientHeight : d.offsetHeight) - parentOffset) * -1;
			
			if (!moving) {
				moving = true;
				mt = parseInt(d.style.marginTop.replace("px",""));
				mt = (mt<0 || mt>0) ? mt : 0;
				
				newV = mt + v;

				if (dir=='reset') newV = 0;
				else newV = (newV >= 0 ) ? 0 : (newV <= d.maxH ? d.maxH : newV);

				
				check = (v < 0) ? (mt>newV) : (mt<newV);
				
				if (check) setTimeout("MoveTo('"+id+"','"+newV+"','"+v+"')",1);
				else moving = false;
			} // end if moving

		} // end if doc

	}
	
	function addLoadEvent(newonload) {
		if (typeof window.onload != 'function') {
			window.onload = newonload;
		} else {
			var oldonload = window.onload;
			window.onload = function() {
				oldonload();
				newonload();
			}
		}
	}	

	moving = false;
	
	
addLoadEvent(checkForValidBrowser);
