// page object
var Page = new function(){
	this._menucontent = false;
	this._sitemapcontent = false;
	// page identification handlers
	this._trail = new Array();
	this._trail_success = false;

	this._menu = false;
	this._ul0 = false;
	this._ul1 = false;
	this._sel = new Array();

	this._selects = false;
	this._objects = false;
	this._applets = false;
}
Page.loadXML = function(file){
	Page.send(file);
}
Page.createMenu = function(){
	var menuitem, i, item, children, li, lis_count, lis_width, lis, margin, a, link;
	this.identifyPage();
	this._menu = document.getElementById("menuWrap");
	this._ul0 = document.createElement("ul");
	this._ul0.id = "menulevel0";
	this._menu.appendChild(this._ul0);

	lis_count = 0;
	lis_width = 0;
	lis = new Array()

	if(this._menucontent){
		for(i = 0; menuitem = this._menucontent.childNodes[i]; i++){
			if(menuitem.nodeName.toLowerCase() == "menuitem")
			{
				lis_count++;
				li = document.createElement("li");
				this._ul0.appendChild(li);
				item = Page.getChildNodeValue(menuitem, "item");
				li.childitems = Page.getChildren(menuitem);
				li.onclick = Page.selectLevel0;
				if(Page._trail.length > 0 && Page._trail[0] == menuitem){
					this._sel[0] = li;
					li.className = "selected";
				}
				li.innerHTML = '<span>'+item+'</span>';
				lis_width += li.offsetWidth;
				lis[lis.length] = li;
			}
            if(menuitem.nodeName.toLowerCase() == "menuitemdirect")
            {
    			lis_count++;
				li = document.createElement("li");
                this._ul0.appendChild(li); 
//                var link = document.createElement('a');
//                link.setAttribute("href", "http://www.eb.dk");
//                link.setAttribute("title", "Min side");
//                link.setAttribute("target", "_blank");
//                var linkText=document.createTextNode('Min side');
//                link.appendChild(linkText);
//                this._ul0.appendChild(link); 
                li.innerHTML = '<span>Element</span>';
				lis_width += li.offsetWidth;
				lis[lis.length] = li;
//				li.onclick = window.open('http://www.eb.dk/','BL');
    		}
		}

		// balance level0 item margin
		margin = Math.floor((this._menu.offsetWidth - lis_width) / (lis_count-1));
		for(i = 0; menuitem = lis[i]; i++){
			if(i < lis.length-1){
				menuitem.style.marginRight = margin+"px";
			}
		}
		if(this._sel[0]){
			Page.setLevel1();
		}
	}
	this.activate(this._ul0);
}

Page.createTrail = function(strHomeUrl){
	var html, i, trail, item, url;
	html = 'Du er her: <span><a href="'+strHomeUrl+'">Forside</a></span>';
	if(this._trail_success){
		for(i = 0; trail = this._trail[i]; i++){
			item = Page.getChildNodeValue(trail, "item");			
			if( i == this._trail.length-1 ) item='<FONT color=red>'+item+'</FONT>'; 
			url = Page.getChildNodeValue(trail, "url");
			html += '&nbsp;&raquo;&nbsp;'
			if( trail = this._trail[i] ) url=null;  
			html += url ? '<span><a href="'+url+'">'+item+'</a></span>' : '<span>'+item+'</span>';
		}
	}
	document.write(html);
}
Page.selectLevel0 = function(){
	if(Page._sel[0]){
		Page._sel[0].className = Page._sel[0].className.replace(/ selected|selected/g, "");
	}
	this.className += this.className ? " selected" : "selected";
	Page._sel[0] = this;
	Page.setLevel1();
}
Page.selectLevel1 = function(event){
	Page.setSubLevel(this, 1, event);
}
Page.setLevel1 = function(){
	var menuitem, i, item, url, li, a, lis_width, target;
	lis_width = 0;

	document.getElementById("page").className = "bg_full";
	if(this._ul1){
		this._menu.removeChild(this._ul1);
	}
	this._ul1 = document.createElement("ul");
	this._ul1.id = "menulevel1";
	this._menu.appendChild(this._ul1);
	for(i = 0; menuitem = this._sel[0].childitems.childNodes[i]; i++){
		if(menuitem.nodeName.toLowerCase() == "menuitem"){
			li = document.createElement("li");
			this._ul1.appendChild(li);
			item = Page.getChildNodeValue(menuitem, "item");
			url = Page.getChildNodeValue(menuitem, "url");
			target = Page.getChildNodeValue(menuitem, "target");
			if(url){
				a = document.createElement("a");
				a.href = url;
                if(target)
                {
                a.setAttribute("target", target);
                }
				a.innerHTML = '<span>'+item+'</span>';				
				li.appendChild(a);
			}else{
				li.childitems = Page.getChildren(menuitem);
				li.onclick = Page.selectLevel1;
				li.innerHTML = '<span>'+item+'</span>';
			}
			lis_width += li.offsetWidth;
			if(Page._trail.length > 1 && Page._trail[1] == menuitem){
				li.className += li.className ? " selected" : "selected";
			}				
		}
	}
	this._ul1.style.width = lis_width+"px";
	this.activate(this._ul1);
}
Page.setSubLevel = function(element, level, event){
	var menuitem, i, item, url, div, innerspan, span, a, target;
	Page.closeMenu(false, level);
	element.className += element.className ? " selected" : "selected";
	this._sel[level] = element;
	if(typeof(this._sel[level].submenu) == "undefined"){
		div = document.createElement("div");
		div.className = "menusublevel";
		this._sel[level].submenu = div;
		for(i = 0; menuitem = this._sel[level].childitems.childNodes[i]; i++){
			if(menuitem.nodeName.toLowerCase() == "menuitem"){
				innerspan = document.createElement("span");
				innerspan.className = "innerspan";
				item = Page.getChildNodeValue(menuitem, "item");
				url = Page.getChildNodeValue(menuitem, "url");
				target = Page.getChildNodeValue(menuitem, "target");
				if(url){
					a = document.createElement("a");
					a.href = url;
                    if(target){
                    a.setAttribute("target", target);
                    }
					div.appendChild(a);
					a.appendChild(innerspan);
				}else{
				 	span = document.createElement("span");
					span.className = "outerspan";
					span.childitems = Page.getChildren(menuitem);
					div.appendChild(span);
					span.appendChild(innerspan);
					span.onclick = function(event){
						Page.setSubLevel(this, (level+1), event);
					}
				}
				innerspan.innerHTML = item;				
			}
		}
		this._menu.appendChild(div);
		this.activate(div);
	}
	if(level == 1){
		this._sel[level].submenu.style.top = Page.absTop(this._sel[level])+this._sel[level].offsetHeight+3-(Page.absTop(this._menu))+'px';
		this._sel[level].submenu.style.left = Page.absLeft(this._sel[level]) - Page.absLeft(this._menu) + 5 +'px';
	}else{
		this._sel[level].submenu.style.top = Page.absTop(this._sel[level])-(Page.absTop(this._menu))+'px';
		var left = Page.absLeft(this._sel[level]) + this._sel[level].offsetWidth + 4 - Page.absLeft(this._menu);
		this._sel[level].submenu.style.visibility = "hidden";
		this._sel[level].submenu.style.display = "inline";
		if(left+this._sel[level].submenu.offsetWidth > this._menu.offsetWidth){
			left = Page.absLeft(this._sel[level]) - this._sel[level].submenu.offsetWidth - 4 - Page.absLeft(this._menu);
		}
		this._sel[level].submenu.style.visibility = "visible";
		this._sel[level].submenu.style.left = left+'px';
	}
	this._sel[level].submenu.style.display = "inline";
	Page.hideObstructions(this._sel[level].submenu);
	Page.nonClick(event);
	document.onclick = Page.closeMenu;
}
Page.activate = function(content){
	var elements, e, i;
	elements = content.getElementsByTagName("span");
	for(i = 0; e = elements[i]; i++){
		e.onmouseover = function(){
			this.className += this.className ? " over" : "over";
		}
		e.onmouseout = function(){
			this.className = this.className.replace(/ over|over/, "");
		}
	}
}
Page.nonClick = function(event){
	event = event ? event : window.event;
	if(event.preventDefault){event.preventDefault();}
	if(event.stopPropagation){event.stopPropagation();}
	event.returnValue = false;
	event.cancelBubble = true;
}
Page.closeMenu = function(event, level){
	document.onclick = null;
	var i, item;
	level = typeof(level) != "undefined" ? level : 1;
	for(i = level; item = Page._sel[i]; i++){
		if(item.submenu){
			Page.restoreObstructions(item.submenu);
			item.submenu.style.display = "none";
		}
		item.className = item.className.replace(/ selected|selected/g, "");
	}
}
Page.identifyPage = function(){
	var node;
	this.iterate = function(element){
		if(element.parentNode.nodeName.toLowerCase() == "menuitem"){
			this._trail = this._trail.unshiftElement(element.parentNode);
			return this.iterate(element.parentNode);
		}else if(element.parentNode.nodeName.toLowerCase() == "children"){
			return this.iterate(element.parentNode);
		}else if(element.parentNode.nodeName.toLowerCase() == "menu"){
			if(this._trail.length){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	if(this._menucontent){
		node = Page.getElementByURI(location.pathname, this._menucontent);
		if(node){
			this._trail_success = this.iterate(node);
		}
	}
}

Page.hideObstructions = function(e){
	var i, select, object, applet;
	e.x = Page.absLeft(e);
	e.y = Page.absTop(e);
	e.obstructions = new Array();
	if(Page.explorer()){
		this._selects = this._selects ? this._selects : document.getElementsByTagName("select");
		for(i = 0; select = this._selects[i]; i++){
			this.resolveObstruction(select, e);
		}
		this._objects = this._objects ? this._objects : document.getElementsByTagName("object");
		for(i = 0; object = this._objects[i]; i++){
			this.resolveObstruction(object, e);
		}
	}			
	this._applets = this._applets ? this._applets : document.getElementsByTagName("applet");
	for(i = 0; applet = this._applets[i]; i++){
		this.resolveObstruction(applet, e);
	}
	var iframe = document.getElementById("IframeDiv");
	if (iframe) {
		this.resolveObstruction(iframe, e);
	}
}
Page.resolveObstruction = function(obs, e){
	obs.x = Page.absLeft(obs);
	obs.y = Page.absTop(obs);
	if(obs.x + obs.offsetWidth > e.x && e.x + e.offsetWidth > obs.x && obs.y + obs.offsetHeight > e.y && e.y + e.offsetHeight > obs.y){
		obs.style.visibility = "hidden";
		obs.obstructing = obs.obstructing ? ++obs.obstructing : 1;
		e.obstructions[e.obstructions.length] = obs;
	}
}
Page.restoreObstructions = function(e){
	var i, obs, select, object, applet;
	if(e){
		for(i = 0; obs = e.obstructions[i]; i++){
			obs.obstructing--;
			if(obs.obstructing < 1){
				obs.obstructing = 0;
				obs.style.visibility = "visible";
			}
		}
	}
}

Page.send = function(url){
	this.request = new Object();
	this.request.url = url;
	this.request.xmlHttp = this.createRequestObject();
	if(!this.request.xmlHttp || typeof(this.request.xmlHttp.send) == 'undefined'){
		this.responder(false);
		return;
	}
//	this.request.xmlHttp.open('POST', this.request.url, false);
// LS: POST is not cached - GET is
	this.request.xmlHttp.open('GET', this.request.url, false);
	try{
		this.request.xmlHttp.send("");
	}catch(e){
		this.responder(false);
		return;
	}
	Page.responder(true);
	return;
}

// LS
// function createXMLHttpRequest() {
//   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
//   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
//   try { return new XMLHttpRequest(); } catch(e) {}
//   alert("XMLHttpRequest not supported");
//   return null;
// }
// LS
Page.createRequestObject = function(){
	var requestObject = false;
	if(typeof(window.ActiveXObject) == "function"){
		try{
			requestObject = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			return false;
		}
	}else if(window.XMLHttpRequest){
		try{
			requestObject = new XMLHttpRequest();
		}catch(e){
			return false;
		}
	}
	return requestObject;
}		
Page.responder = function(state){
	if(!state){
		alert("XML error");
	}else{
		try{
			var test = this.request.xmlHttp.status;
			if(this.request.xmlHttp.status == 200){
				this.request.result = this.request.xmlHttp.responseXML;
				this.request.resultText = this.request.xmlHttp.responseText;
				this.request.xmlHttp = null;
				Page.parse(this.request);
			}else{
				alert("XML error");
			}
		}catch(e){
			if(this.request){
				alert("XML error");
			}
		}
	}
}

Page.parse = function(response){
	if(response){
		if(Page.safari()){
			var container = document.createElement("div");
			container.innerHTML = response.resultText;
		}else{
			container = response.result;
		}
		if(container.getElementsByTagName("menu").length){
			Page._menucontent = container.getElementsByTagName("menu")[0];
		}
		if(container.getElementsByTagName("sitemap").length){
			Page._sitemapcontent = container.getElementsByTagName("sitemap")[0];
		}
	}
}

Page.getElementByURI = function(url, content){
	var url_node, i, urls;
	urls = content.getElementsByTagName("url");
	for(i = 0; url_node = urls[i]; i++){
		if(this.getNodeValue(url_node) == url){
			return url_node;
		}
	}
	return false;
}
Page.getChildNode = function(node, type){
	var i, child;
	for(i = 0; child = node.childNodes[i]; i++){
		if(child.nodeName.toLowerCase() == type){
			return child;
		}
	}
	return false;
}
Page.getNodeValue = function(node){
	if(node.firstChild && node.firstChild.nodeValue){
		return node.firstChild.nodeValue;
	}else{
		return "";
	}
}
Page.getChildNodeValue = function(node, type){
	var child = this.getChildNode(node, type);
	if(child){
		return this.getNodeValue(child);			
	}
	return "";
}
Page.getChildren = function(node){
	var children = this.getChildNode(node, "children");
	if(children && children.childNodes.length){
		return children;
	}
	return false;
}

Page.explorer = function(){
	if(document.all){
		return true;
	}
	return false;
}
Page.safari = function(){
	if(navigator.appVersion.indexOf("Safari") >= 0){
		return true;
	}
	return false;
}
Page.firefox = function(){
	if(navigator.userAgent.indexOf("Firefox") >= 0){
		return true;
	}
	return false;
}
Page.absLeft = function(e){
	if(Page.safari() && e.nodeName == "TR"){
		return e.getElementsByTagName("TD")[0].offsetLeft + Page.absLeft(Page.getParentTag("TABLE",e));
	}else if(e.offsetParent){
		return e.offsetLeft + Page.absLeft(e.offsetParent);
	}
	return e.offsetLeft;
}
Page.absTop = function(e){
	if(Page.safari() && e.nodeName == "TR"){
		return e.getElementsByTagName("TD")[0].offsetTop + Page.absTop(Page.getParentTag("TABLE",e));
	}else if(e.offsetParent){
		return e.offsetTop + Page.absTop(e.offsetParent);
	}
	return e.offsetTop;
}
Page.getParentTag = function(which, e){
	if(e.nodeName != which && e.nodeName != "BODY"){
		e = Page.getParentTag(which, e.parentNode);
	} 
	return e;
}
// array.unshift for IE
Array.prototype.unshiftElement = function(e){
	var newArray = new Array();
	newArray[0] = e;
	for(var i = 0; i < this.length; i++){
		newArray[newArray.length] = this[i];
	}
	return newArray;
}
