var timer;
var mtimer;
var visibl;
var menu;

var sm;
var mp;
var mph;

var ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

window.onload = function(){
	sm = document.getElementById('SideMenu');
	mp = document.getElementById("PopMenu");
	mph = document.getElementById("PopMenuHeader");
	
	if(sm!="undefined" && sm)
	{	var y = sm.childNodes.length + 100;
		
		for (var i=0; i < sm.childNodes.length; i++)
		{	elm = sm.childNodes[i];
			if (elm && elm.tagName && elm.tagName=='LI')
			{	for (var j=0; j < elm.childNodes.length; j++)
				{	ell = elm.childNodes[j];
					if (ell.tagName=='UL')
					{	//ell.style.zIndex = y;
						for (var k=0; k < ell.childNodes.length; k++)
						{	eli = ell.childNodes[k];
							//eli.style.position = "relative";
							//eli.style.zIndex = y;
						}
					}
				}

				if (y > 1)
				{	elm.style.borderBottom = "1px solid #eeeeee";
				}	else
				{	elm.style.borderBottom = "none";
				}
				
				elm.style.position = "relative";
				elm.style.zIndex = y--;
				
				if (elm.className=='non' || elm.className=='leaf')
				{
					elm.onmouseover = function()
					{	if(mp) clearTimeout(mtimer);
					}
					
					elm.onmouseout = function()
					{	this.style.backgroundColor = "white";
						this.style.color = "#515151";
						if(mp) mtimer = window.setTimeout("hide_menu();", 500);
					}
				} else
				{	
					elm.onmouseover = function()
					{	clear_visible();
						if(mp) clearTimeout(mtimer);
						if (this.childNodes.length > 0)
						{//	alert('Child nodes: '+this.childNodes.length);
							for (var m=0; m < this.childNodes.length; m++)
							{	ell = this.childNodes[m];
								if (ell.tagName=='UL')
								{	ell.style.display='block';
									visibl = ell;
								}
								else if (ell.tagName=="A")
								{	ell.style.backgroundColor = "rgb(61, 75, 150)";
									ell.style.color = "white";
								}
							}
						}
					}
					
					elm.onmouseout = function()
					{	timer = window.setTimeout("clear_visible();", 200);
						this.style.backgroundColor = "white";
						this.style.color = "#515151";
						if(mp) mtimer = window.setTimeout("hide_menu();", 500);
						if (this.childNodes.length > 0)
						{//	alert('Child nodes: '+this.childNodes.length);
							for (var m=0; m < this.childNodes.length; m++)
							{	ell = this.childNodes[m];
								if (ell.tagName=="A")
								{	ell.style.backgroundColor = "white";
									ell.style.color = "#515151";
								}
							}
						}
					}
				}
			}
		}
	}
	
	if(mp && ie6)
	{	mp.style.top = "-10px";
	}
	
	if(mph)
	{	for (var i=0; i < mph.childNodes.length; i++)
		{	elm = mph.childNodes[i];
			if (elm && elm.tagName && elm.tagName=='H1')
			{	elm.innerHTML = "<img src=\"http://images.thesafeshop.co.uk/tabs/wider/down-arrow.gif\"/>" + elm.innerHTML;
				for (var i=0; i < elm.childNodes.length; i++)
				{	ell = elm.childNodes[i];
					if (ell && ell.tagName && ell.tagName=='IMG')
					{	ell.onmouseover = function ()
						{	if(mtimer) clearTimeout(mtimer);
							this.parentNode.style.backgroundImage = "url('http://images.thesafeshop.co.uk/tabs/sideblue-header.jpg')";
							mp.style.display = "block";
						}
						ell.onmouseout = function()
						{	mtimer = window.setTimeout("hide_menu();", 500);
						}
					}
				}
			}
		}
	}
}

function hide_menu()
{	//sbp.className = sbp.className.replace(/sideboxblue sideboxpopup/, "sideboxwhite");
	mp.style.display = "none";
	if(mph) for(var i=0; i < mph.childNodes.length; i++){
		elm = mph.childNodes[i];
		if(elm && elm.tagName && elm.tagName=='H1')
			elm.style.backgroundImage = "url('http://images.thesafeshop.co.uk/tabs/sideblue-header-closed.jpg')";
	}
	if(mtimer) clearTimeout(mtimer);
}

function clear_visible()
{	if (visibl)
	{	visibl.style.display='none'; visibl = null;
		if (timer)
		{	clearTimeout(timer);
		}
	}
}

function popupISIS(secure){
	return window.open((secure?"https":"http")+"://isisaccreditation.imrg.org/8025745000669E37/certificateISIS?readform&unid=146DEDD0C7DF41CF8025746A005356A2", "ia", "toolbar=no,location=yes,width=500,height=420");
}

function popupIDIS(secure){
	return window.open((secure?"https":"http")+"://isisaccreditation.imrg.org/8025745000669E37/certificateIDIS?readform&unid=146DEDD0C7DF41CF8025746A005356A2", "ia", "toolbar=no,location=yes,width=500,height=420");
}


function rowon(callee, therow_name)
{	
	therow = document.getElementById(therow_name);
	
	if (callee && therow)
	{	
		if (therow.style.backgroundColor=="")
		{	therow.style.backgroundColor="#efefef";
			therow.style.backgroundImage='none';
		} else
		{	therow.style.backgroundColor="";
			therow.style.backgroundImage='url(http://images.thesafeshop.co.uk/dropshadow.jpg)';
		}
	}
}



function popOpen(id) {
	var topOffset = 160;
	setSize(topOffset);
	var overShade = document.getElementById(id);
	var topPad =  pos+topOffset;
	// height-padding = div height
	overShade.style.height = (ph - topPad) + 'px'; /* remove topPad from height */
	overShade.style.width = pw + 'px';
	overShade.style.paddingTop = topPad + 'px';
	overShade.style.display = 'block'; 	
	if (document.all && !navigator.appVersion.match(/MSIE 7.0/) && !navigator.appVersion.match(/MSIE 8.0/) && !navigator.userAgent.match('Opera')) { 
		coverSelects(overShade);
	}
	
}
function popClose(id) {
	
	var fObj = document.getElementById(id);
	fObj.style.display = "";
	//end Added by Sarun
	
	if (document.all && !navigator.appVersion.match(/MSIE 7.0/)  && !navigator.appVersion.match(/MSIE 8.0/) && !navigator.userAgent.match('Opera')) {
		document.getElementById("popup-cover").outerHTML = "";
	}
		
}

function setSize(topOffset) { /* sets size of shade */
	if (window.pageYOffset != null) { /* moz and safari */
		pos = window.pageYOffset;
		ph = document.documentElement.scrollHeight;
		pw = document.documentElement.scrollWidth;
		if (document.body.scrollHeight > document.documentElement.scrollHeight) {
			ph = document.body.scrollHeight;
			pw = document.body.scrollWidth;
		}
	} else if (document.documentElement.scrollTop > document.body.scrollTop) { /* ie, catch if Standards compliance mode */
		pos = document.documentElement.scrollTop;
		ph = document.documentElement.scrollHeight;
		pw = document.documentElement.scrollWidth;
		if (document.documentElement.clientHeight > document.documentElement.scrollHeight) {
			ph = document.documentElement.clientHeight;
		}
	} else if (document.body != null) { /* if IE 5.5 */
		pos = document.body.scrollTop;
		ph = document.body.scrollHeight;
		pw = document.body.scrollWidth;
		if (document.documentElement.scrollHeight > document.body.scrollHeight) {
			ph = document.documentElement.scrollHeight;
		}
		ph = ph + pos + topOffset; /* fix box model */
	}
}	

/* covers select form elements with iframe in IE 6&< so they do not show through the popup */
function coverSelects(par) {
		var covHeight = document.body.scrollHeight+"px"; // add the negative margin to the height
		var coverFrame = "<IFRAME id='pop-up-cover' style='";
		coverFrame = coverFrame + "height:"+covHeight+";' ";
		coverFrame = coverFrame + "src='javascript:false;' frameBorder='0' scrolling='no'></IFRAME>";
		par.insertAdjacentHTML("afterEnd",coverFrame); // create new iframe that is the size of the popup window
}


function change(which, small)
{
	var filename = "";
	if(small)
	{
		filename = "2.jpg";
	}
	else
	{
		filename = ".jpg";
	}
	switch(which)
	{
		case 'fone':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/red"+filename;
			break;
		case 'ftwo':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/blue"+filename;
			break;
		case 'fthree':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/yellow"+filename;
			break;
		case 'ffour':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/green"+filename;
			break;
		case 'ffive':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/silver"+filename;
			break;
		case 'fsix':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/white"+filename;
			break;
		case 'fseven':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/treswood"+filename;
			break;
		case 'feight':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/tresblue"+filename;
			break;
		case 'fnine':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/treswhite"+filename;
			break;
		case 'ften':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/tresblack"+filename;
			break;
		case 'feleven':
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/tresaqua"+filename;
			break;
		default:
			document.getElementById("fone").src="http://images.thesafeshop.co.uk/colour_chart/finishes/red"+filename;
			break;
	}
}
