var globIdx = null;

function browserOs() {
	userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf("linux") != -1) return "Linux"
	else if (userAgent.indexOf("x11") != -1) return "Unix"
	else if (userAgent.indexOf("mac") != -1) return "Mac"
	else if (userAgent.indexOf("win") != -1) return "Windows"
	else return "Unknown";
}

function browserCheck(str) {
	userAgent = navigator.userAgent.toLowerCase();
	idx = userAgent.indexOf(str);
	if (idx != -1) {
		globVersion = parseFloat(userAgent.charAt(str.length + idx + 1));
		return true;
	}
	return false;
}

function browserObject(str) {
	this.os = browserOs();
	this.appName = navigator.appName;
	this.appCodeName = navigator.appCodeName;
	this.appVersion = navigator.appVersion;
	this.userAgent = navigator.userAgent;

	if (browserCheck("opera")) {
		this.version = globVersion;
		this.name = "Opera";
		this.alias = this.name;
	}
	else if (browserCheck("msie")) {
		this.version = globVersion;
		this.name = "Internet Explorer";
		this.alias = "IE";
	}
	else if (navigator.appName == "Netscape") {
		this.version = parseFloat(navigator.appVersion);
		this.name = "Netscape Navigator";
		this.alias = "NS";
	}
	else {
		this.version = 0;
		this.name = "Unknown";
		this.alias = "Unknown";
	}
}

function writeBrowser() {
	if (((b.alias == "IE") && (b.version >= 4)) || (b.alias == "Opera")) {
		var s = "";
		s += "<style>";
		s += ".divDropMenu .dropItem {padding: 0px;}";
		s += ".divDropMenu A {padding-top: 5px; padding-bottom: 5px; padding-left: 12px; padding-right: 12px; width: 100%;}";
		s += "</style>";
		document.writeln(s);
	}
	else if ((b.alias == "NS") && (b.version >= 5)) {
		var s = "";
		s += "<style>";
		s += "</style>";
		document.writeln(s);
	}
	else if ((b.alias == "NS") && (b.version >= 4)) {
	}
	else if ((b.alias == "Opera") && (b.version >= 5)) {
	}
}

function getReal(el, type, value) {
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return null;
}

function getRealEl(el, type, value, n) {
	i = 0;
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY") && (i < n)) {
		if (eval("temp." + type + ".indexOf('" + value + "')") != -1) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
		i++;
	}
	return null;
}

function getRealNS(tempEl, typ, value, n) {
	i = 0;
	while ((tempEl != null) && (tempEl.tagName != "BODY") && (i < n)) {
		if (tempEl.className != null) {
			if ((tempEl.className.indexOf(value) != -1) || (tempEl.id == 'divMenu')) return tempEl;
		}
		tempEl = tempEl.parentNode;
		i++;
	}
	return null;
}

function getRealOffset(el, str) {
	temp = el;
	i = 0;
	while ((temp != null) && (temp.tagName != "BODY")) {
		i += eval("temp." + str);
		temp = temp.offsetParent;
	}
	return i;
}

function handleOver(e) {
	if ((b.alias == "IE") && (b.version >= 4)) {
	}
	else if ((b.alias == "NS") && (b.version >= 5)) {
	}
}

function handleOut() {
}

function handleClick() {
	hideMenu();
	bMenuActive = false;
	toggleSelect();
}

function handleScroll() {
}

function handleResize() {
	hideMenu();
}

function hideMenu() {
	if (globIdx == null) return;
	if ((b.alias == "IE" && b.version >= 4) || (b.alias == "Opera")) {
		topEl = eval("document.all.divTop" + globIdx);
		dropEl = eval("document.all.divDrop" + globIdx);
		if ((topEl != null) && (dropEl != null)) {
			topEl.style.visibility = "hidden";
			dropEl.style.visibility = "hidden";
			globIdx = null;
			bMenuActive = false;
			toggleSelect();
		}
	}
	else if (b.alias == "NS" && b.version >= 5) {
		topEl = document.getElementById("divTop" + globIdx);
		dropEl = document.getElementById("divDrop" + globIdx);
		if ((topEl != null) && (dropEl != null)) {
			topEl.style.visibility = "hidden";
			dropEl.style.visibility = "hidden";
			globIdx = null;
			bMenuActive = false;
			toggleSelect();
		}
	}
}

function ieHideMenu() {
	topEl = getReal((event.toElement), "className", "divDropTop");
	menuEl = getReal((event.toElement), "className", "divDropMenu");
	if ((topEl == null) && (menuEl == null)) {
		hideMenu();
	}
}

function initMenu() {
}

var bMenuActive = false;
var activeMenuId = 0;

function toggleSelect() {
	var dropCol = document.body.getElementsByTagName("SELECT");
	for (j=0; j<dropCol.length; j++) {
		if (bMenuActive) dropCol[j].style.visibility = "hidden"
		else dropCol[j].style.visibility = "visible";
	}
}

function dropMenu(idx, bClick) {
	if (((b.alias == "IE") && (b.version >= 4)) || (b.alias == "Opera")) {
		if (globIdx != null) hideMenu();
		menuEl = eval("document.all.divMenu" + idx);
		topEl = eval("document.all.divTop" + idx);
		dropEl = eval("document.all.divDrop" + idx);
		if ((topEl != null) && (menuEl != null) && (dropEl != null)) {
			oTop = getRealOffset(menuEl, "offsetTop");
			oLeft = getRealOffset(menuEl, "offsetLeft");
			if (b.os == "Mac") {
				oLeft = parseInt(oLeft) - 12;
			}

			topEl.style.top = oTop;
			topEl.style.left = oLeft;
			dropEl.style.top = parseInt(topEl.style.top) + topEl.offsetHeight;
			dropEl.style.left = parseInt(topEl.style.left);

			bMenuActive = true;
			toggleSelect();

			topEl.style.visibility = "visible";
			dropEl.style.visibility = "visible";

			globIdx = idx;
		}
	}
	else if ((b.alias == "NS") && (b.version >= 5)) {
		if (globIdx != null) hideMenu();

		menuEl = document.getElementById("divMenu" + idx);
		topEl = document.getElementById("divTop" + idx);
		dropEl = document.getElementById("divDrop" + idx);
		if ((topEl != null) && (menuEl != null) && (dropEl != null)) {
			oTop = menuEl.offsetTop;
			oLeft = menuEl.offsetLeft;

			topEl.style.top = oTop;
			topEl.style.left = oLeft;
			dropEl.style.top = parseInt(topEl.style.top) + topEl.offsetHeight;
			dropEl.style.left = parseInt(topEl.style.left);

			bMenuActive = true;
			toggleSelect();
	
			topEl.style.visibility = "visible";
			dropEl.style.visibility = "visible";

			globIdx = idx;
		}
	}
}

function getMenu() {
	if (m == null) return false;
	for (j=0; j<m.length; j++) {
		if (m[j].items.length > 0) {
			if (activeMenuId == m[j].id) idStr = ' id="topItem' + m[j].id + '"'
			else idStr = '';
			topId = m[j].id;
			var s = '';
			s += '<div class="divDropTop" id="divTop' + topId + '">';
			s += '<div class="topItem"' + idStr + '><a href="/?page=' + topId + '">' + m[j].title + '</a></div>';
			s += '</div>';
			s += '<div class="divDropMenu" id="divDrop' + topId + '">';
			for (i=0; i<m[j].items.length; i++) {
				var styleStr = "";
				var last = true;
				if (i < (m[j].items.length-1)) last = false;
				s += '<div class="dropItem"><a href="scripts/' + m[j].items[i].href + '" target=_self>' + m[j].items[i].title + '</a></div>';
			}
			s += '</div>';
			document.write(s);
			s = null;
		}
	}
}

var b = new browserObject();
writeBrowser();

function init() {
	if (((b.alias == "IE") && (b.version >= 4)) || (b.alias == "Opera")) {
		document.onmouseover = handleOver;
		document.onmouseout = handleOut;
		document.onclick = handleClick;
		window.onscroll = handleScroll;
		window.onresize = handleResize;
	}
	else if ((b.alias == "NS") && (b.version >= 5)) {
		document.onmouseover = handleOver;
		document.onclick = handleClick;
	}
	initMenu();
}
window.onload = init;


// Tillägg för Internet Explorer
	sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);