var IE4 = document.all;
var NN4 = document.layers;
var NN7 = !(NN4 || IE4);
// this won't work in NN4 cos there's no innerHTML property and cos the reference will fail cos of layers within layers
// either make a vanilla version that will work in NN4, write completely different version of this code and integrate or make a "this won't work on your browser" page
// If I don't use innerHTML to write the sub nav items then it might be worth making it work in NN4

function getStyleObject(obj) {
	if (IE4)
		return eval("document.all." + obj + ".style");
	else if (NN7)
		return (document.getElementById(obj)).style;
	else if (NN4)
		return eval("document." + obj);
}

function getObject(obj) {
	if (IE4)
		return eval("document.all." + obj);
	else if (NN7)
		return (document.getElementById(obj));
	else if (NN4)
		return eval("document." + obj);
}

function hideAllFaqs(faq) {
	var faqlength = { ozone:9, veneers:3, bleaching:7 };
	var faqtype = faq.replace(/\d+/g,"");
	var obj;
	for (var i = 1; i <= faqlength[faqtype]; i++) {
		if (faq == faqtype + i)
			continue;
		obj = getStyleObject(faqtype + i);
		obj.display = "none";
	}
}

function getFaq(faq) {
	hideAllFaqs(faq);
	var obj = getStyleObject(faq);
	obj.display = (obj.display == "block"?"none":"block");
}

var timerID1;
function showPic(n) {
	n = (n?n:2);
	if (!transformPic.transition) {
		if (IE4) {
			timerID1 = setInterval("transformPic("+ transformPic.visible[n] + "," + (n?n:null) + ")",25);
			transformPic.transition = true;
		} else {
			var obj = getStyleObject("pic" + n);
			obj.visibility = (transformPic.visible[n]?"hidden":"visible");
			var obj2 = getStyleObject("button" + n);
			obj2.visibility = (transformPic.visible[n]?"hidden":"visible");
			transformPic.visible[n] = (transformPic.visible[n]?false:true);
		}
	} else
		alert("Please wait for the current animation to finish before pressing the button again.");
}

function transformPic(vis,n) {
	var obj = getObject("pic" + n);
	var obj2 = getObject("button" + n);
	if (obj.filters.alpha.opacity < 100 && !vis) {
		obj.filters.alpha.opacity += 1;
		obj2.filters.alpha.opacity += 1;
	} else if (obj.filters.alpha.opacity > 0 && vis) {
		obj.filters.alpha.opacity -= 1;
		obj2.filters.alpha.opacity -= 1;
	} else {
		transformPic.visible[n] = (transformPic.visible[n]?false:true);
		clearInterval(timerID1);
		transformPic.transition = false;
	}
}
transformPic.visible = [];
transformPic.visible[2] = false;
transformPic.visible[4] = false;
transformPic.transition = false;

function subMenu(arr,y,w) {
	this.state = false;
	this.links = arr;
	this.y = y;
	this.width = w;
}

function menuState() {
	this.treatments = new subMenu(["Ozone","Implants","Modern fillings","Crowns and bridges","Tooth whitening","Veneers","Children","Prices"],"110px","150");
	this.care = new subMenu(["Nervous patients","Prevention","Hygienist","Your smile"],"150px","130");
	this.faq = new subMenu(["Tooth whitening","Veneers","Ozone"],"190px","125");
	this.about = new subMenu(["Our philosophy","About us",["Prices","../treatments/prices.htm"],"How to find us"],"230px","122");
	this.contact = new subMenu(["Contact details",["How to find us","../about/howtofindus.htm"]],"270px","122");
	this.opened = false;
	this.oversubmenu = false;
}

function checkMenuStatus(item) {
	status = (status != item?item:"");
	for (var prop in menu) {
		if (typeof menu[prop] == "object" && menu[prop].state && item != prop)
			menu[prop].state = false;
	}
	var obj = getStyleObject(item + "bg");
	if (menu[item].state) {
		menu[item].state = false;
		if (menu.opened != item && item)
			obj.backgroundColor = "#faf6ef";
	} else {
		menu[item].state = true;
		if (obj.backgroundColor != "#ebf3ff" && obj.backgroundColor != "rgb(235,243,255)")
			obj.backgroundColor = "transparent";
	}
	if (menu.opened && menu.opened != item && menu[item].state)
		openSubMenu(item);
}

function subMenuHandler() {
	if (!menu.oversubmenu) {
		if (menu.opened) {
			closeSubMenu(menu.opened);
			return;
		} else {
			for (var prop in menu) {
				if (typeof menu[prop] == "object" && menu[prop].state) {
					openSubMenu(prop);
					break;
				}
			}
		}
	}
}

function closeSubMenu(item) {
	var obj = getStyleObject(item + "bg");
	obj.backgroundColor = "#faf6ef"
	obj = getStyleObject(item);
	obj.visibility = "visible";
	obj = getObject("submenu");
	obj.innerHTML = "";
	obj.style.visibility = "hidden";
	menu.opened = false;
}

function openSubMenu(item) {
	resetAllBg(item);
	var obj = getStyleObject(item);
	obj.visibility = "hidden";
	obj = getObject("submenu");
	obj.innerHTML = getSubMenuHTML(item);
	obj.style.top = menu[item].y;
	obj.style.visibility = "visible";
	obj = getStyleObject("transbg");
	obj.height = (menu[item].links.length * (IE4?20:20)) + "px";
	menu.opened = item;
}

function resetAllBg(item) {
	var obj;
	for (var prop in menu) {
		if (prop != item && typeof menu[prop] == "object") {
			obj = getStyleObject(prop + "bg");
			obj.backgroundColor = "#faf6ef";
			obj = getStyleObject(prop);
			obj.visibility = "visible";
		}
	}
}

function subMenuHighlight(item) {
	var obj = getStyleObject(item);
//	alert(obj.backgroundColor);
	obj.backgroundColor = (obj.backgroundColor == "#bb795d" || obj.backgroundColor == "rgb(187,121,93)" || obj.backgroundColor == "rgb(187, 121, 93)"?"transparent":"#bb795d");
	obj = getStyleObject(item + "link");
	obj.color = (obj.color == "#ffffff" || obj.color == "rgb(255,255,255)" || obj.color == "rgb(255, 255, 255)"?"#647382":"#ffffff");
	obj = getStyleObject(item + "bullet");
	obj.color = (obj.color == "#ffffff" || obj.color == "rgb(255,255,255)" || obj.color == "rgb(255, 255, 255)"?"#bb795d":"#ffffff");
	menu.oversubmenu = (menu.oversubmenu?false:true);
}

// following function checks to see if menu[item].links[i] is a string or array (object) to allow for pages with different link text and page names unlike e.g. "Ozone" and "ozone.htm" but like faq e.g. "How do I whiten my teeth?" and "?q=1"
function getSubMenuHTML(item) {
	var loco = getSubDir();
	var txt = "";
	txt += (IE4?"":"<div id=\"transbg\" class=\"submenupanelNN\" style=\"width:" + menu[item].width + "px\">");
	for (var i = 0; i < menu[item].links.length; i++) {
		txt += "<div id=\"" + (typeof menu[item].links[i] == "string"?removeSpace(menu[item].links[i]):removeSpace(menu[item].links[i][0])) + "\" style=\"width:" + (menu[item].width-(IE4?5:4)) + "px\" class=\"submenuitem\" onclick=\"javascript:location='" + (loco?(loco == item?"":"../"):"") + (typeof menu[item].links[i] == "string"?(loco == item?"":item + "/") + removeSpace(menu[item].links[i]) + ".htm":(loco == item?"":item + "/") + menu[item].links[i][1]) + "'\" onmouseover=\"subMenuHighlight('" + (typeof menu[item].links[i] == "string"?removeSpace(menu[item].links[i]):removeSpace(menu[item].links[i][0])) + "');this.style.cursor='hand'\" onmouseout=\"subMenuHighlight('" + (typeof menu[item].links[i] == "string"?removeSpace(menu[item].links[i]):removeSpace(menu[item].links[i][0])) + "')\"><a id=\"" + (typeof menu[item].links[i] == "string"?removeSpace(menu[item].links[i]):removeSpace(menu[item].links[i][0])) + "link\" href=\"" + (loco?(loco == item?"":"../"):"") + (typeof menu[item].links[i] == "string"?(loco == item?"":item + "/") + removeSpace(menu[item].links[i]) + ".htm":(loco == item?"":item + "/") + menu[item].links[i][1]) + "\"><span id=\"" + (typeof menu[item].links[i] == "string"?removeSpace(menu[item].links[i]):removeSpace(menu[item].links[i][0])) + "bullet\" class=\"gum\">&#149;</span>&nbsp;" + (typeof menu[item].links[i] == "string"?menu[item].links[i]:menu[item].links[i][0]) + "</a></div>";
	}
	txt += (IE4?"<div id=\"transbg\" class=\"submenupanel\" style=\"width:" + (menu[item].width-5) + "px\"></div>":"</div>");
	return txt;
}

function getSubDir() {
	var loco;
	for (var prop in menu) {
		if (location.href.indexOf(prop) != -1) {
			loco = prop;
			break;
		}
	}
	return loco;
}

function removeSpace(str) {
	while(str.indexOf(" ") != -1) {
		str = str.substring(0,str.indexOf(" ")) + str.substring(str.indexOf(" ")+1);
	}
	while(str.indexOf("?") != -1) {
		str = str.substring(0,str.indexOf("?")) + str.substring(str.indexOf("?")+1);
	}
	return str.toLowerCase();
}

function renderMainMenu() {
	var loco = getSubDir();
	var txt = "<div class=\"navigation\">";
	var n = 0;
	for (var prop in menu) {
		if (typeof(menu[prop]) == "object") {
			txt += "<div id=\"" + prop + "bg\" style=\"position:absolute; top:" + parseInt(n * 40) + "px; left:0px; height:17px; width:130px; background-color:#FAF6EF\"></div><div style=\"position:absolute; top:" + parseInt((n * 40) + 4) + "px; left:28px\"><img src=\"" + (loco?"../":"") + "images/bb_chdp_nav_word_" + prop + ".gif\" width=\"97\" height=\"9\" border=\"0\" alt=\"\"></div><div id=\"" + prop + "\" style=\"position:absolute; top:" + parseInt(n * 40) + "px; left:0px\"><img src=\"" + (loco?"../":"") + "images/bb_chdp_nav_single.gif\" width=\"447\" height=\"17\" border=\"0\" alt=\"\" usemap=\"#navmap" + n + "\"><map name=\"navmap" + n + "\"><area alt=\"\" shape=\"poly\" coords=\"0,0,1,0,123,0,127,2,130,5,130,10,128,14,123,16,0,16\" href=\"javascript:void(0)\" onmouseover=\"checkMenuStatus('" + prop + "'); return true\" onmouseout=\"checkMenuStatus('" + prop + "'); return true\" onFocus=\"if(IE4) this.blur()\"></map></div>";

		}
		n++;
	}
	txt += "</div>";
	txt += "<div id=\"submenu\" class=\"submenuholder" + (NN7?"NN":"") + "\"></div>";
	document.writeln(txt);
}

function highlightHome(state) {
	var obj = getStyleObject("headerbg");
	obj.backgroundColor = state;
}

document.onmousedown = subMenuHandler;
var menu = new menuState();
