﻿IE = (document.all)?true:false;
IE6 = IE && (navigator.appVersion.indexOf("6.")!=-1);
IE5 = IE && (navigator.appVersion.indexOf("5.")!=-1);
IE55 = IE && (navigator.appVersion.indexOf("5.5")!=-1);
IE4 = IE && (navigator.appVersion.indexOf("4.")!=-1);
IE5 = IE5 && !IE6;
NS6 = (!document.layers && !document.all)
IE = IE || NS6;
IE4 = IE && !IE5 && !IE6;
Macintosh = navigator.userAgent.indexOf('Mac')>0;
flaks = new Array();
current = null;
topMenu = null;
onPane = false;
onButton = false;
openmenu = null;
initialized = false;
menuPos = -1;

function writeCookie(name, value) {
		var strCookie = "";
		strCookie = name + "=" + value;
		document.cookie = strCookie;
}

function getCookie(SCookieName)
{
	var sCookies = "";
	var sCookie = null;
	var iStart;
	var iStop
	sCookies = document.cookie;
	if (sCookies!="")
	{
		iStart = sCookies.indexOf(SCookieName + '=',0);
		if (iStart!=-1)
		{
			iStart = iStart+SCookieName.length+1;
			if (sCookies.indexOf(';',iStart)==-1)
			{
				iStop = sCookies.length;
			}
			else
			{
				iStop = sCookies.indexOf(';',iStart);
			}
			sCookie = sCookies.substring(iStart,iStop);
		}
	}
	return sCookie;
}

function localInit() {
	if(!IE) {
		init(); 
	}
	initialized = true;
	if(menuPos != -1) {
		writeCookie("menupos", menuPos);
	}
	showActive();
}

function showActive() {
	if(menuPos == -1) 
		menuPos = getCookie("menupos");
	if(menuPos != null) {
		rollActive(menuPos);
	} else {
		menuPos = -1;
	}
}

function goThere(ref,i) {
	writeCookie("menupos", i);
	document.location = ref;
}

function rollActive(i) {
	if(UKMENU == 1) 
		setImage(i, "media/menu_uk_"+ (17+(2*i)) +".gif")
	else if(UKMENU == 0)
		setImage(i, "media/menu_"+ (17+(2*i)) +".gif")
	else // Intraweb
		setImage(i, "media/menu_intra_"+ i +"b.gif")
}

function rollOver(i) {
	if(UKMENU == 1) 
		setImage(i, "media/menu_uk_"+ (17+(2*i)) +".gif")
	else if(UKMENU == 0)
		setImage(i, "media/menu_"+ (17+(2*i)) +".gif")
	else // Intraweb
		setImage(i, "media/menu_intra_"+ i +"b.gif")
}

function rollOut(i) {
	if(UKMENU == 1) 
		setImage(i, "media/menu_uk_"+ (17+(2*i)) +"_green.gif")
	else if(UKMENU == 0)
		setImage(i, "media/menu_"+ (17+(2*i)) +"_green.gif")
	else
		setImage(i, "media/menu_29_green.gif")

}

function setImage(i, img) {
o = document.images["menu"+i]
	if(o && o != null)
		o.src = img
}
			
			
function mOver() {
 this.bgColor = MenuItemMouseOverColor;
 current = topMenu;
}

function mOverPane() {
 onPane = true;
}

function mOutPane() {
 onPane = false;
}

function hide(i) {
 onButton = false;
 setTimeout("mouseLeftMenu()", 100);
}

function subOver() {
 onButton = true;
}

function subOut() {
 onButton = false;
 setTimeout("mouseLeftMenu()", 100);
}

function mouseLeftMenu() {
 if(onButton == true) return;
 if(onPane) { setTimeout("mouseLeftMenu()", 100); return; }
 if(current == null && topMenu != null) {
    topMenu.hideChildren();
 }
}

function mOut() {
 this.bgColor = MenuItemBackgroundColor;
 current = null;
 setTimeout("mouseLeftMenu()", 100);
}

MHEIGHT = 38;

function MenuItem(f, parent, prev, sub, icon, tekst, lnk) {
var l = this.l = new Layer(defWidth, f.l);
this.f = f;
l.m = this;
this.sub = sub;
var subt = (sub==0)?'':'<img src="'+arrowImg+'">';
var icont = (icon=="")?'<img src="images/icons/empty.gif">':'<img src="images/icons/'+icon+'">';
//var href = (lnk=="")?tekst:('<a class="menu" href="'+lnk+'">'+tekst+'</a>');
var href = (lnk=="")?tekst:('<a class="menu" href="javascript:goThere(\''+lnk+'\','+f.i+');">'+tekst+'</a>');
var txt2 = '<span class="menuitem">Hello</span>'

var txt = "&nbsp;" + "&nbsp;" + href
// Below for old functionality with images, foldout arrows etc.
l.document.write(txt);
l.document.close();
l.visibility = "inherit";
l.onmouseover = mOver;
l.onmouseout = mOut;
l.bgColor = MenuItemBackgroundColor;
l.moveBy(1,1);
l.oldwidth = l.clip.width
l.resizeTo(defWidth-2,l.clip.height); //l.clip.height-1);
}

function closeOpen() {
  if(openmenu != null)
	topMenu.hideChildren();
}

function hideChildren() { 
  rollOut(openmenu);
  openmenu = null;
  if(IE) {
    this.l.style.visibility = "hidden";
  } else {
    this.l.visibility = "hidden";
  }
  showActive();
}

function getWidthIE() {
	if(NS6) {
		return this.l.offsetWidth;
	} else {
		return this.l.clientWidth;
	}
}

function getWidthNS() {
return this.l.clip.width;
	//return this.l.clip.width;
}

function Flak(id) {
  this.l = l = new Layer(defWidth,window);
  this.child = null;
  l.f = this;
  l.bgColor = layerBgColor;
  l.visibility = "hidden";
  l.onmouseover = mOverPane;
  l.onmouseout = mOutPane;
  this.subs = new Array();
  this.hideChildren = hideChildren;
  this.getWidth = getWidthNS;
}

function initIE() {
	loc = checkTop();


  // Read through the menu array and build the menus
  var i,f,m,l,h=0,mi,id=0,mid;
  for(i=1;i<=menus;i++) {
   m = eval("menu"+i);
   id = "f"+i;
   f = new FlakIE(id, m, loc, i);
   flaks[i] = f;
 }
  
}

function init() {
 // Read through the menu array and build the menus
	var i,f,m,x,y,l,h=0,mi,rw,mia,loc;
	flaks = new Array();
	current = null;
	topMenu = null;
	onPane = false;
	onButton = false;
	openmenu = null;
	loc = checkTop();

 rw = 0;
 for(i=1;i<=menus;i++) {
	   rw = 0;
	   m = eval("menu"+i);
	   if(m.length > 0) {
		   f = new Flak("men"+i);
		   f.i = i;
		   h = 1;
		   mia=new Array()
		   for(x=0,y=0;x<m.length;y++) {
				    title = m[x++];
				    link = m[x++];
				    if(menuPos == -1 && loc == link) {
				    	menuPos = i;  // Kjell
				    }
				    icon = m[x++];
				    sub = m[x++];
				    
				
					 if(title=="separator") {
								h++;
								mia[y] = null;
					 } else {
								 mi = new MenuItem(f, null,null,sub,icon,title,link);
								 f.subs.push(m);
								 mia[y] = mi
								 mi.l.top = h;
								 h += mi.l.clip.height+0;
								 rw = Math.max(rw, mi.l.oldwidth)
					  }				
              }
				  h+=1
				  rw += spaceRight;
				  for(l=0;l<mia.length;l++) {
				    if(mia[l] != null) {
					    mia[l].l.resizeTo(rw-2, mia[l].l.clip.height);
				    }
				  }
		
		 f.l.resizeTo(rw, h);
		 flaks[i] = f;
	  }
 }
}

function checkTop() {
	var i;
	var loc;
	loc  = document.location.href.split("/");
   loc = loc[loc.length-1];

	if(menuPos == -1) {
		for(i=0;i<topMenus.length;i++) {
			if(topMenus[i] == loc) {
				menuPos = i;
				break;
			}
		}
	}
	return loc;
}

function showMenu(i, x, y) {
	 onButton = true;
	 closeOpen()
	 if(IE) {
		  var f = flaks[i];
		  if(NS6) {
				f.l.style.left = x;
				f.l.style.top = y;		  
		  } else {
		  	  //alert("Layer:"+f.l);
			  f.l.style.pixelLeft = x;
			  f.l.style.pixelTop = y;
		 }
		 f.l.style.visibility = "visible";
		  topMenu = f;
	 } else {
		  flaks[i].l.moveToAbsolute(x,y);
		  flaks[i].l.visibility = "visible";
		  topMenu = flaks[i];
	 }
}

function klik(i) {
	if(!initialized) return;
	rollOver(i)
	if(openmenu != i) {
		o = document.images["menu"+i]
		if(IE) {
			 //x = -10;
			 x = -1;
			 y = 83; //85; //81; // o.offsetHeight;
			 if(NS6) {
				  x = o.offsetLeft - 1;
				  y = 81;
			 } else {
			 	 bo = o;
			 	 /*
				 while(bo.tagName != "BODY") {
					   x += bo.offsetLeft;
					   //y += o.offsetTop;
					   bo=bo.parentElement;
				 }
				 */
				 while(bo.offsetParent) {
				 	x += bo.offsetLeft;
				 	bo = bo.offsetParent;
				 }
				/*
				 if(Macintosh) {
					  		x += 20;
				  } else {
				  		if(IE5 && !IE55) {
				  			x -= 105;
				  		}
				  	}
				  */
			}
		} else {
			 x=o.x
			 y=o.y+o.height
		}
		// Correct for right side overflow  TODO: also nr. 7 for uk
		if(i == 8 || (i >= 7)) {
				x = x - flaks[i].getWidth() + o.width;
				if(IE) {
					x+=2;
				}
		}
		showMenu(i,x,y);
		openmenu = i;
	}
}

// Here follows IE specific code

function mOverIE() {
 current = topMenu;
}

function mOutIE() {
 current = null;
 setTimeout("mouseLeftMenu()", 100);
}


function FlakIE(id, m, loc, i) {
   var mid, title, link, icon, h, mi,l,rw;
   //document.write('<div id="'+id+'" style="'+IEmenustyle+'" onmouseout="mOut()" onmouseover="mOver()">');
	//document.write('<table border="0" cellpadding="0" cellspacing="0"><tr height="1"><td bgcolor="#b3dc1f" width="1" height="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1"><img src="media/spacer_green.gif" width="auto" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1" width="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td></tr><tr><td bgcolor="#b3dc1f" width="1"><img src="media/spacer_green.gif" width="1" height="10" border="0"></td><td bgcolor="#000000" valign="top"><table border="0" cellpadding="0" cellspacing="0"><tr><td><table border="0" cellpadding="0" cellspacing="0">');
	//document.write('<table id="'+id+'" style="'+IEmenustyle+'" onmouseout="mOut()" onmouseover="mOver()" border="0" cellpadding="0" cellspacing="0"><tr height="1"><td bgcolor="#b3dc1f" width="1" height="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1"><img src="media/spacer_green.gif" width="auto" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1" width="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td></tr><tr><td bgcolor="#b3dc1f" width="1"><img src="media/spacer_green.gif" width="1" height="10" border="0"></td><td bgcolor="#000000" valign="top"><table border="0" cellpadding="0" cellspacing="0"><tr><td><table border="0" cellpadding="0" cellspacing="0">');
	document.write('<table id="'+id+'" style="'+IEmenustyle+'" onmouseout="mOut()" onmouseover="mOver()" border="0" cellpadding="0" cellspacing="0"><tr height="1"><td bgcolor="#b3dc1f" width="1" height="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1"><img src="media/spacer_green.gif" width="auto" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1" width="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td></tr><tr><td bgcolor="#b3dc1f" width="1"><img src="media/spacer_green.gif" width="1" height="10" border="0"></td><td bgcolor="#000000" valign="top"><table border="0" cellpadding="0" cellspacing="0"><tr><td><table border="0" cellpadding="0" cellspacing="0">');
   sep = false;
   this.subs = new Array();
   rw = 0;
   
   for(l=0;l<m.length;) {
	    mid = id + "_" +  l;
	    title = m[l++];
	    link = m[l++];
	    if(menuPos == -1 && link == loc) {
	    	menuPos = i;
	    }
	    icon = m[l++];
	    sub = m[l++];

    	document.write('<tr height="1" onmouseover="this.style.background=\''+MenuItemMouseOverColor+'\'" onmouseout="this.style.background=\''+MenuItemBackgroundColor+'\'"><td height="1" width="10"><img src="media/spacer_trans.gif" width="10" height="20" border="0"></td><td height="1"><a href="'+link+'" class="menu">'+title+'</a></td><td height="1" width="11"><img src="media/spacer_trans.gif" width="10" height="20" border="0"></td></tr>');
		if(l<m.length-1)
			document.write('<tr height="1"><td height="1" bgcolor="#b3dc1f" width="10"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td><td height="1" bgcolor="#b3dc1f"><img src="media/spacer_green.gif" width="10" height="1" border="0"></td><td height="1" bgcolor="#b3dc1f" width="11"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td></tr>');
	   }
	   document.write('</table></td></tr></table></td><td bgcolor="#b3dc1f" width="1"><img src="media/spacer_green.gif" width="1" border="0"></td></tr><tr><td bgcolor="#b3dc1f" width="1" height="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1" ><img src="media/spacer_green.gif" width="auto" height="1" border="0"></td><td bgcolor="#b3dc1f" height="1" width="1"><img src="media/spacer_green.gif" width="1" height="1" border="0"></td></tr></table><!-- /div -->');
	   if(NS6) {
	   	this.l = document.getElementById(id);
	   	if(this.l.firstChild.offsetWidth > 0) alert(this.l.firstChild.offsetWidth);
	   } else {
	   	this.l = document.all[id];
	   }
	   this.child = null;
	   this.hideChildren = hideChildren;
	   this.getWidth = getWidthIE;
}



