var menuHTML = "";

// menu format

var menuOffsetLeft = 0;
var menuOffsetTop = 20;
var dropMenuWidth = 175;
if (typeof layoutWidth == 'undefined') {
	var layoutWidth = 989; //default width if not set in html header
}
var maxOffsetLeft = layoutWidth -dropMenuWidth; // ajusted in setDim
var maxOffsetLeftDef = layoutWidth -dropMenuWidth; 

var menuStart = "<div id  class=\"menuDrop\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"" + dropMenuWidth + "\" >"; // remember to include "id" in the top block element
var menuEnd = "</table></div>";
var itemStart = "<tr><td class=\"menuDropItem\" nav>"; // remember to include nav in the container
var itemEnd = "</td></tr>";
var activeTopLevel;


// Make the menus

var z = 0;
// ValidUrl=0;
// txtValue = "";
ValidUrl="0";
for (var i=0; i < menuItems.length; i++) {

	if (menuHTML != "" && menuItems[i][0] == 0) menuHTML += menuEnd;
	if (menuItems[i][0] == 0) {
		menuHTML += menuStart.replace("id","id=\"menuID" + z + "\"");
		z++
	}
	else {
		ValidUrl=0;
		if(menuItems[i][1]=="Log af") ValidUrl="1"; else ValidUrl="0";
		// txtValue += menuItems[i][1] + " -- " + ValidUrl;
		if(menuItems[i][3]!="1"){
			if (document.all) menuHTML += itemStart.replace("nav","onmouseover=\"delayCloseMenu();this.style.backgroundColor=currentHoverColor;\" onmouseout=\"startCloseMenu();this.style.backgroundColor=currentTableColor;\" onclick=\"ok=" + ValidUrl + ";document.location.href='" + menuItems[i][2] + "'\""); 
			else menuHTML += itemStart.replace("nav","onmouseover=\"delayCloseMenu();this.style.backgroundColor=currentHoverColor;this.style.cursor = 'pointer';\" onmouseout=\"startCloseMenu();this.style.backgroundColor=currentTableColor;\" onclick=\"ok=" + ValidUrl + ";document.location.href='" + menuItems[i][2] + "'\"");
		}else{
			if (document.all) menuHTML += itemStart.replace("nav","onmouseover=\"delayCloseMenu();this.style.backgroundColor=currentHoverColor;\" onmouseout=\"startCloseMenu();this.style.backgroundColor=currentTableColor;\" onclick=\"ok=" + ValidUrl + ";window.open('" + menuItems[i][2] + "')\""); 
			else menuHTML += itemStart.replace("nav","onmouseover=\"delayCloseMenu();this.style.backgroundColor=currentHoverColor;this.style.cursor = 'pointer';\" onmouseout=\"startCloseMenu();this.style.backgroundColor=currentTableColor;\" onclick=\"ok=" + ValidUrl + ";window.open('" + menuItems[i][2] + "')\"");
		}

		//menuHTML += "<a href=\"" + menuItems[i][2] + "\">";
		menuHTML += menuItems[i][1];
		//menuHTML += "</a>";
		menuHTML += itemEnd;
	}
}
menuHTML += menuEnd;
// Menu controls
var delayClose = null;
var openMenuID= null;
var openPlaceID = null;
var currentHoverColor;
var currentTableColor;

function getObj(id) {
	var obj;
	if (document.all) obj = document.all[id];	
	else obj = document.getElementById(id);
	return obj;
}

function allwaysOnMenu(){
	if (allwaysOn!=""){
		if (allwaysOn.substring(0,1)=="0")
			ao = allwaysOn.substring(1,2)
		else
			ao = allwaysOn
		menunum = parseInt(ao) - 1;
		currentHoverColor = menuColors[ menunum ][1];
		findObj("m" + menunum).style.backgroundColor = currentHoverColor;
		findObj("Mlink" + menunum).style.color = "rgb(255,255,255)";
		}
	}
var sectormenu = z-1;
function showMenu(menu,offsetTopUp,place) {
    var mID = menu;
    if (menu==-1) mID = sectormenu;
    var placeID = place || menu;
    if (openMenuID != null) hideMenu();

    currentTableColor = menuColors[mID][0];
    currentHoverColor = menuColors[mID][1];

    findObj("m" + placeID).style.backgroundColor = currentHoverColor;
    findObj("Mlink" + placeID).style.color = "rgb(255,255,255)";

    var oLeft = getOffsetLeft("m" + placeID) + (menuOffsetLeft-1);
    var oTop = getOffsetTop("m" + placeID) + (menuOffsetTop-offsetTopUp);

    if (maxOffsetLeft < oLeft) oLeft = maxOffsetLeft;

   
    var mObj = findObj("menuID"+mID);

    with (mObj) {

        style.backgroundColor = currentTableColor;   
        style.left = oLeft;
        style.top = oTop;
        style.visibility = "visible";
    }

    openMenuID = mID;
    openPlaceID = placeID;
}

function setActiveTopLevel(id) {
	currentHoverColor = menuColors[id][1];
	findObj("m" + id).style.backgroundColor = currentHoverColor;
	findObj("Mlink" + id).style.color = "rgb(255,255,255)";
	activeTopLevel = id;
}

function hideMenu() {

	if (delayClose || delayClose == 0) clearTimeout(delayClose);

	findObj("menuID"+openMenuID).style.visibility = "hidden";

	var offColor;

	if (activeTopLevel != openPlaceID){
		offColor = menuColors[openPlaceID][2];
		if (typeof(allwaysOn)=="undefined") {
			findObj("m" + openPlaceID).style.backgroundColor = offColor;
			if (openPlaceID < menuCount)
				findObj("Mlink" + openPlaceID).style.color = "rgb(97,97,97)";
			else
				findObj("Mlink" + openPlaceID).style.color = "rgb(102,102,102)";
		} else {
		if (allwaysOn.substring(0,1)=="0")
			ao = allwaysOn.substring(1,2)
		else
			ao = allwaysOn

		if (parseInt(openPlaceID) != (parseInt(ao)-1))
			{
			findObj("m" + openPlaceID).style.backgroundColor = offColor;
			if (openPlaceID < menuCount)
				findObj("Mlink" + openPlaceID).style.color = "rgb(97,97,97)";	
			else
				findObj("Mlink" + openPlaceID).style.color = "rgb(102,102,102)";
			}
		}
	}

	openMenuID = null;
	openPlaceID = null;
}

function startCloseMenu() {
	delayClose = setTimeout("hideMenu()",200);
}

function delayCloseMenu() {
	if (delayClose || delayClose == 0) clearTimeout(delayClose);
}

function findObj(n, d) { // version 1.0, soren@araneum.dk, Litra B.

	// n: name or object id
	// d: document if left empty, can be used for finding objects in other frames etc.
 
	  var p,i,x;  

	if (!d) d=document; 

	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
    		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}

	  if (!(x=d[n])&&d.all) x=d.all[n]; 

	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

	  for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
 
	  if (!x && document.getElementById) x=document.getElementById(n);

	return x;
}

function getHeight(id) { // version 1.0, soren@araneum.dk, Litra C

	// id: Obejct id

	// findObj Litra A, B, or C must be included.

	var el = findObj(id);
	var oh = el.offsetHeight;
	return oh;

}

function getWidth(id) { // version 1.0, soren@araneum.dk, Litra C

	// id: Object id

	// findObj Litra A, B, or C must be included.

	var el = findObj(id);
	var ow = el.offsetWidth;
	return ow;

}

function getOffsetTop(id) { // version 1.0, soren@araneum.dk, Litra C.

	// id: object id

	// findObj Litra A, B, or C must be included

	var el = findObj(id);
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null) ot += el.offsetTop;
	return ot;
}

function getOffsetLeft(id) { // version 1.0, soren@araneum.dk, Litra C.

	// id: object id

	// findObj Litra A, B, or C must be included

	var el = findObj(id);
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null) ol += el.offsetLeft;
	return ol;
}

function moveItem(id,x,y) { // version: 1.0, soren@araneum.dk, Litra B.

	// findObj must be included
	// id: object id
	// x: left value - leave empty if no movement is required
	// y: top value - leave empty if no movement is required

	var obj = findObj(id); // findObj Litra A or Litra B.

	if (!document.layers) {
		if (x !="") obj.style.left = x;
		if (y !="") obj.style.top = y;	
	}
	else {
		if (x !="") obj.left = x;
		if (y !="") obj.top = y;
	}
}



function changeHeight(id,h) { // version 1.0, soren@araneum.dk, Litra C

	// id: Obejct id
	// h: New height

	// findObj Litra A, B, or C must be included.

	var el = findObj(id);
	el.style.height = h;

}


function showHide(el,showHide) {
	var el = getObj(el);
	if (showHide == "show") el.style.visibility = "visible";
	if (showHide == "hide") el.style.visibility = "hidden";

}

var winHeight;
var winWidth;
		
function getWinDim() {

	if (document.all) {
		winHeight = document.body.clientHeight;
		winWidth = document.body.clientWidth;
	}
	else {
		winHeight = window.innerHeight;
		winWidth = window.innerWidth;
	}
}

function frontKalNyh(show) {

	if (show == "kal") {
		findObj("Calendar").innerHTML = findObj("kal").innerHTML;
	}
	else {
		findObj("kalNews").innerHTML = findObj("news").innerHTML;
	}
}

function setDim() {

	// ajust the left margin
	var marginVal;
//	if (findObj("leftPosTopTable")) {
//		marginVal = (Math.round((winWidth-getWidth("leftPosTopTable"))/2));
//	} else {
		marginVal = (Math.round((winWidth-getWidth("leftPosTop"))/2));
//	}

	if (marginVal < 20) marginVal = 5;

	findObj("leftPosTop").style.marginLeft = marginVal;
	findObj("leftPosScroll").style.marginLeft = marginVal;
	if (findObj("ContentShadowBottom")) findObj("ContentShadowBottom").style.width = layoutWidth - 253;

	maxOffsetLeft = maxOffsetLeftDef+marginVal;

	// ajust top and set kal/news divs if resizeTop


	if (resizeTop) {
		findObj("topContent").style.height = 97;

		findObj("kalNews").innerHTML = findObj("news").innerHTML;
		findObj("Calendar").innerHTML = findObj("kal").innerHTML;
		
	}

	// fit the height of scrollContent to the max height.
	var secHeight = (winHeight-getOffsetTop("scrollContent"));
	
	if (secHeight < 40) secHeight = 40;

	changeHeight("scrollContent",secHeight);
	
	// set shadow
	
	if (!shadowSet) {

		var shadowLeftH = getHeight("content");
		var shadowLeftM = getOffsetTop("colEnd")-getOffsetTop("scrollContent")+12;
		if (getHeight("rightContent") > shadowLeftH) shadowLeftH = getHeight("rightContent");

		with (findObj("ContentShadowRight")) {
			style.marginTop = -(shadowLeftM); 
			style.height = (shadowLeftH); 
			style.display = "block";
		}
		shadowSet = true;
	}

	//
	// If screen height is to small
	if ((winHeight < 400) || (navigator.userAgent.indexOf('iPad') != -1) || (navigator.userAgent.indexOf('iPhone') != -1)  || (navigator.userAgent.indexOf('iPod') != -1)) {
		with (findObj("body")) {
			style.overflow = "auto";
		}
		with (findObj("scrollContent")) {
			style.display = "inline";	
			style.height = "100%";
		}	
	}
	
	// Small height end

}

// Sitemap 
var topNo = 0;
function getConfigDbRepID() {
	//til erstatning for <Computed Value> : "/" + @ReplaceSubstring(@GetProfileField("profileDb";"ConfigDbRepId");":";"") +"/"
	return glCONFIGBREPID;
}
function getSitemap() {

	var sitemapHTML = "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" class=\"sitemap\">";
	var currentColor;
	ValidUrl="0";
	for (var i=0; i < sitemapItems.length; i++) {

		if (sitemapItems[i][0] == 0) {
			currentColor = menuColorsIndex[topNo][1];
			if (topNo > 0) sitemapHTML += "</td></tr>";
			if (document.all) sitemapHTML += "<tr valign=\"top\"><td><img src=\""+getConfigDbRepID()+"sitemapPlus.gif\" style=\"cursor: hand;\" onclick=\"ok=" + ValidUrl + ";showSitemap('" + topNo + "');\" width=\"10\" height=\"10\" border=\"0\" vspace=\"2\" id=\"smImg" + topNo + "\" alt=\"Vis/skjul\"></td><td><strong><a href=\"" + sitemapItems[i][2] + "\" style=\"color: " + currentColor + "; font-family: verdana; arial; helvetica; font-size: 11px;\">" + sitemapItems[i][1] + "</a></strong></td></tr><tr id=\"sitemapR" + topNo + "\" style=\"display: none;\"><td><br></td><td>"
			else sitemapHTML += "<tr valign=\"top\"><td><img src=\""+getConfigDbRepID()+"sitemapPlus.gif\" style=\"cursor: pointer;\" onclick=\"ok=" + ValidUrl + ";showSitemap('" + topNo + "');\" width=\"10\" height=\"10\" border=\"0\" vspace=\"2\" id=\"smImg" + topNo + "\" alt=\"Vis/skjul\"></td><td><strong><a href=\"" + sitemapItems[i][2] + "\" style=\"color: " + currentColor + "; font-family: verdana; arial; helvetica; font-size: 11px;\">" + sitemapItems[i][1] + "</a></strong></td></tr><tr id=\"sitemapR" + topNo + "\" style=\"display: none;\"><td><br></td><td>"
			topNo++
		}
		if (sitemapItems[i][0] == 1) {
			sitemapHTML += "<div style=\"margin-left: 12px;\"><a href=\"" + sitemapItems[i][2] + "\" style=\"color: " + currentColor + "; font-family: verdana; arial; helvetica; font-size: 9px;\">" + sitemapItems[i][1] + "</a></div>"
		}
					if (sitemapItems[i][0] == 2) {
			sitemapHTML += "<div style=\"margin-left: 24px;\"><a href=\"" + sitemapItems[i][2] + "\" style=\"color: " + currentColor + "; font-family: verdana; arial; helvetica; font-size: 9px;\">" + sitemapItems[i][1] + "</a></div>"
		}

					if (sitemapItems[i][0] == 3) {
			sitemapHTML += "<div style=\"margin-left: 36px;\"><a href=\"" + sitemapItems[i][2] + "\" style=\"color: " + currentColor + "; font-family: verdana; arial; helvetica; font-size: 9px;\">" + sitemapItems[i][1] + "</a></div>"
		}

	}

	sitemapHTML += "</td></tr>";
	sitemapHTML += "</table>";

	return sitemapHTML;
}

var rowShow = "table-row";
if (document.all) rowShow = "block"

function showSitemap(what) {

	if (what == "all") {
		for (var i=0; i < topNo; i++) {
			findObj("sitemapR" + i).style.display = rowShow;
			findObj("smImg" + i).src = getConfigDbRepID()+"sitemapMinus.gif";
		}

	}

	if (what == "none") {
		for (var i=0; i < topNo; i++) {
			findObj("sitemapR" + i).style.display = "none";
			findObj("smImg" + i).src = getConfigDbRepID()+"sitemapPlus.gif";
		}
	}

	if (what != "none" && what != "all") {

		if (findObj("sitemapR" + what).style.display == "none") findObj("sitemapR" + what).style.display = rowShow;
		else  findObj("sitemapR" + what).style.display = "none";
			obj = findObj("smImg" + what)
			if (obj.src.indexOf(getConfigDbRepID()+"sitemapMinus.gif") > 0) obj.src = getConfigDbRepID()+"sitemapPlus.gif";
			else obj.src = getConfigDbRepID()+"sitemapMinus.gif";
	}
}

function openpopwin(url,wname,winWidth,winHeight) {
	var popwin
	if (wname=='') wname='nyt';
	if (winWidth=='') winWidth=300;
	if (winHeight=='') winHeight=300;
	if (url=='') url='http:\/\/www.w3.org';

	popwin=window.open(url,wname,'toolbar=no,copyhistory=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=' + winWidth + ',height=' + winHeight);
	if (popwin.focus!=null) {
		popwin.focus();
	}
}

function toggleYear(chkObj) {

	var hideShow = rowShow;

	if (chkObj.checked) hideShow = "none";

	findObj("year1").style.display = hideShow;
	findObj("year2").style.display = hideShow;
}
