﻿function dge(id) {
	if (id != null)
		return document.getElementById(id) 
	else
		return null
}

function doSearch() {
	if (dge("topSearch")) {
		if (dge("topSearch").value != "Søg..." && dge("topSearch").value != "Søk..." && dge("topSearch").value != "") {
			if (location.href.indexOf('.dk') > 1) {
				location.href = "default.aspx?m=2&i=196&soegform=vis&soegeord=" + escape(dge("topSearch").value)
			} else {
				location.href = "default.aspx?m=2&i=211&soegform=vis&soegeord=" + escape(dge("topSearch").value)
			}
		} else {
			alert("Du skal angive et søgeord først")
		}
	}
}

function keycodeSearch(e) {
	if (e.keyCode == 13)
		doSearch()
}

function initiatePage() {
	var nodePos = 0 //mi ? 0 : 1;
	
	// show post info on post detail pages
	var elmDate = dge('postInfoTD').childNodes[nodePos]
	if (elmDate) {
		if (elmDate.tagName == 'DIV')
			dge('postInfoDiv').style.display = 'block'
	}
	
	// move the 4 top level item plus the active one
	var topLevelTD1 = dge("topLevelsDiv")
	var topLevelTD2 = dge("toplevelTD2")
	var topLevelTD3 = dge("toplevelTD3")
	for (var i = 0; i <= 3; i++) {
		var thisItem = topLevelTD1.childNodes[nodePos]
		thisItem.className = "TopLevelCspan"
		thisItem.childNodes[0].className = "TopLevelCAnchor"
		topLevelTD3.appendChild(thisItem)
	}

	var activeElm = dge("TopLevel" + CurrentN0Active + "Anchor")
	if (activeElm) {
		var actParent = activeElm.parentNode.cloneNode(true)
		actParent.className = "TopLevelCspanActive"
		actParent.style.width = "300px"
		actParent.childNodes[0].className = "TopLevelCAAnchor"
		topLevelTD2.appendChild(actParent)
	} else if (CurrentN0Active == 14) { //English pages
		var txtNode = document.createTextNode("English")
		var actNode = document.createElement("A")
		actNode.className = "TopLevelCAAnchor"
		actNode.appendChild(txtNode)
		var spanNode = document.createElement("SPAN")
		spanNode.className = "TopLevelCspanActive"
		spanNode.style.width = "300px"
		spanNode.appendChild(actNode)
		topLevelTD2.appendChild(spanNode)
	}
	
	// Enable hover effect on the last top level items
	for (var i = nodePos; i < topLevelTD1.childNodes.length; i++) {
		if (topLevelTD1.childNodes[i].childNodes.length > 0) {
			var thisID = topLevelTD1.childNodes[i].childNodes[0].id
			thisID = thisID.replace("TopLevel", "")
			thisID = thisID.replace("Anchor", "")
			if (dge("menuL0" + thisID)) {
				topLevelTD1.childNodes[i].onmouseover = function(event) { showTopMenu(this.childNodes[0].id, event) }
				topLevelTD1.childNodes[i].onmouseout = function() { hideTopMenu() }
			}
		}
	}
	dge("topLevelsDiv").style.display = "block"
}

var topMenuT
var topMenuLastID = ""
function showTopMenu(id,e) {
	if (typeof (topMenuT) != "undefined")
		clearTimeout(topMenuT)

	var thisID = id.replace("TopLevel", "")
	thisID = thisID.replace("Anchor", "")

	if (topMenuLastID != "" && topMenuLastID != "menuL0" + thisID)
		hideTopMenu2(topMenuLastID)

	if (topMenuLastID != "menuL0" + thisID) {
		topMenuLastID = "menuL0" + thisID
		var posLeft = dge(id).parentNode.offsetLeft - 260
		var winWidth = mi ? parseInt(document.body.clientWidth, 10) : parseInt(window.innerWidth, 10)
		var winMid = winWidth / 2
		var thisLeft = winMid + posLeft
		var elmWidth = (dge(topMenuLastID).currentStyle) ? parseInt(dge(topMenuLastID).currentStyle.width, 10) : parseInt(window.getComputedStyle(dge(topMenuLastID), null).getPropertyValue('width'), 10)
		if (thisLeft + elmWidth > winWidth)
			thisLeft = winWidth - elmWidth
		dge(topMenuLastID).style.left = thisLeft + "px"
		if (!mi)
			dge(topMenuLastID).style.top = "94px"
		dge(topMenuLastID).style.visibility = "visible"
	}
}
function hideTopMenu() {
	topMenuT = setTimeout("hideTopMenu2('" + topMenuLastID + "')", 1000)
}
function hideTopMenu2(id) {
	dge(id).style.visibility = "hidden"
	topMenuLastID = ""
}
function topMenuSubEnter() {
	if (typeof (topMenuT) != "undefined")
		clearTimeout(topMenuT)
}

function changeSite() {
	var goUrl = dge("siteSelector").value

	if (goUrl != "") {
		location.href = goUrl
	}
}

function drawAddThis() {
	var path = location.href
	if (path.indexOf('m=4') > 0) {
		document.write('<div class="addthis_toolbox addthis_default_style"><a class="addthis_button_facebook"></a><a class="addthis_button_twitter"></a><a class="addthis_button_email"></a><a class="addthis_button_compact"></a><a class="addthis_counter addthis_bubble_style"></a></div><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4dbfa2511381a450"></script><br />')
	}
}
