function WebsiteMenu() {
	this.skin=window.engine.getSkin();
	this.width=0;
	this.top=0;
	this.left=0;
	if(this.skin=="www.axaco.se") {
		this.menuCoverWidth=false;
		this.slopeWidth=70;
		this.color="white";
		this.skinPath="axaco";
		this.subMenuBgColor="#52729e";
		this.subMenuHighLightColor="#738fb6";
		this.subMenuHighLightTextColor="white";
	} else {
		this.skinPath="menu";
		this.menuCoverWidth=true;
		this.slopeWidth=15;
		this.color="black";
		this.top=5;
		this.subMenuBgColor="#f2ad00";
		this.subMenuHighLightColor="#f2ad00";
		this.subMenuHighLightTextColor="black";
	}
	this.height=30;
	this.horizontalMargins=10;
	this.verticalMargins=2;
	this.fontFace="Tahoma";
	this.fontSize="8pt";
	this.background="transparent";
	this.shadowed=true;
	this.glowed=false;
	this.doms=new LayerCollection();
	this.items=new Object();
}
WebsiteMenu.prototype.onResize=function() {
	this.move(this.top);
}
WebsiteMenu.prototype.load=function(menuNode) {
	this.terminate();
	var img=createImage(graphicsBasePath+this.skinPath+"/middle.png");
	img.style.position="absolute";
	img.style.width="100%";
	img.style.height="30px";
	img.style.top=this.top+"px";
	img.style.left=this.left+"px";

	document.body.appendChild(img);
	this.doms.add(img, "middle");

	img=createImage(graphicsBasePath+this.skinPath+"/leftslope.png");
	img.style.position="absolute";
	img.style.top=this.top+"px";
	img.style.width=this.slopeWidth+"px";
	img.style.height=this.height+"px";
	document.body.appendChild(img);
	this.doms.add(img, "left slope");

	img=createImage(graphicsBasePath+this.skinPath+"/rightslope.png");
	img.style.position="absolute";
	img.style.top=this.top+"px";
	img.style.width=this.slopeWidth+"px";
	img.style.height=this.height+"px";
	document.body.appendChild(img);
	this.doms.add(img, "right slope");
	if(this.menuCoverWidth) {
		img=createImage(graphicsBasePath+this.skinPath+"/low.png");
		img.style.position="absolute";
		img.style.top=this.top;
		img.style.height="30px";
		document.body.appendChild(img);
		this.doms.add(img, "left");

		img=createImage(graphicsBasePath+this.skinPath+"/low.png");
		img.style.position="absolute";
		img.style.top=this.top;
		img.style.height="30px";
		document.body.appendChild(img);
		this.doms.add(img, "right");

		img=createImage(graphicsBasePath+this.skinPath+"/menu_shadow_top.png");
		img.style.position="absolute";
		img.style.top=this.top;
		img.style.height="16px";
		img.style.width="100%";
		document.body.appendChild(img);
		this.doms.add(img, "top");
	}

	var body=createLayer();
	body.style.backgroundColor="red";
	document.body.appendChild(body);
	this.doms.add(body, "body");

	var children=menuNode.getChildren();
	for(var childID in children) {
		var child=children[childID];
		var menu=this.add(child);
		menu.load(child);
	}
}
WebsiteMenu.prototype.add=function(menuNode) {
	this.items[menuNode.id]=new WebsiteMenuRoot();
	this.items[menuNode.id].parentItem=this;
	return this.items[menuNode.id];
}
WebsiteMenu.prototype.getWidth=function() {
	var w=0;
	for(var i in this.items) {
		w+=this.items[i].width;
	}
	return w;
}
WebsiteMenu.prototype.move=function() {
	var dims=new PageDimensions();
	var width=this.getWidth();
	this.left=(dims.innerWidth/2)-(width/2);

	var body=this.doms.get("body");
	body.style.top=this.top+1;
	body.style.left=this.left;

	var vMargins=10;
	var middle=this.doms.get("middle");
	middle.style.width=width+(vMargins*2);
	middle.style.top=this.top;
	middle.style.left=this.left-vMargins;

	var leftSlope=this.doms.get("left slope");
	leftSlope.style.top=this.top;
	leftSlope.style.left=this.left-vMargins-this.slopeWidth;

	var rightSlope=this.doms.get("right slope");
	rightSlope.style.top=this.top;
	rightSlope.style.left=this.left+width+vMargins;

	var left=this.doms.get("left");
	if(left) {
		left.style.left=0;
		left.style.top=this.top;
		left.style.width=this.left-vMargins-this.slopeWidth;
	}
	var right=this.doms.get("right");
	if(right) {
		right.style.top=this.top;
		right.style.left=this.left+width+vMargins+this.slopeWidth;
		right.style.width=this.left-vMargins-this.slopeWidth;
	}
	var topShadow=this.doms.get("top");
	if(topShadow) {
		topShadow.style.top=this.top-16;
	}
}
WebsiteMenu.prototype.terminate=function() {
	for(var childID in this.items) {
		var child=this.items[childID];
		child.terminate();
		purge(child);
	}
	this.doms.terminate();
}

function AbstractWebsiteMenuItem() {
	this.id=null;
	this.linkType=null;
	this.link=null;
	this.items=null;
	this.doms=null;
	this.parentItem=null;
	this.childCount=0;
}
AbstractWebsiteMenuItem.prototype.getRoot=function() {
	var parent=this;
	while(parent.parentItem!=null)
		parent=parent.parentItem;
	return parent;
}
AbstractWebsiteMenuItem.prototype.getBodyLayer=function() {
	return this.getRoot().doms.get("body");
}
AbstractWebsiteMenuItem.prototype.load=function(menuNode) {
	this.doms=new LayerCollection();
	this.items=new Object();
	this.id=menuNode.id;
	this.caption=menuNode.caption;
	this.link=menuNode.link;
	this.linkType=menuNode.linkType
	this.createLayers();
	for(var c in menuNode.children) {
		var childNode=menuNode.children[c];
		this.add(childNode).load(childNode);
	}
}
AbstractWebsiteMenuItem.prototype.add=function(menuNode) {
	this.items[menuNode.id]=new WebsiteMenuChild();
	this.items[menuNode.id].parentItem=this;
	this.childCount++;
	return this.items[menuNode.id];
}
AbstractWebsiteMenuItem.prototype.getItems=function() {
	var array=new Array();
	for(var childID in this.items) {
		array[array.length]=this.items[childID];
	}
	return array;
}
AbstractWebsiteMenuItem.prototype.onClick=function() {
	if(this.link) {
		if(this.linkType==0) {
			window.engine.navigate(this.link);
		} else {
			window.open(this.link, "_blank", "resizable=yes,menubar=no,status=no");
		}
	}
}
AbstractWebsiteMenuItem.prototype.terminate=function() {
	for(var childID in this.items) {
		var child=this.items[childID];
		child.terminate();
		purge(child);
	}
	this.doms.terminate();
}
AbstractWebsiteMenuItem.prototype.createLayers=function() {

}

WebsiteMenuChild.inheritsFrom(AbstractWebsiteMenuItem);
function WebsiteMenuChild() {
	this.width=0;
	this.height=0;
	this.doms=new LayerCollection();
}
WebsiteMenuChild.prototype.create=function(parentBody) {
	var body=document.createElement("div");
	body.style.position="static"
	body.style.padding="2px";
	body.style.paddingLeft="5px";
	body.style.paddingRight="5px";
	body.style.margin="2px";
	body.style.cursor="pointer";
	body.style.border="1px solid transparent";
	body.Menu=this;
	body.onclick=function() { this.Menu.onClick(); }
	body.appendChild(document.createTextNode(this.caption));
	var highLightColor=this.parentItem.parentItem.subMenuHighLightColor;
	var highLightTextColor=this.parentItem.parentItem.subMenuHighLightTextColor;
	body.onmouseover=function() {
		this.style.border="1px solid black";
		this.style.backgroundColor=highLightColor;
		this.style.color=highLightTextColor;
	}
	body.onmouseout=function() {
		this.style.border="1px solid transparent";
		this.style.backgroundColor="transparent";
		this.style.color="black";
	}
	parentBody.appendChild(body);
	var dims=new LayerDimensions(body);
	this.width=dims.width;
	this.height=dims.height;
}

/**
 * Top menu item
 */
WebsiteMenuRoot.inheritsFrom(AbstractWebsiteMenuItem);
function WebsiteMenuRoot() {
	this.closeTimer=0;
	this.menuTrail=0;
	this.subMenuOpacity=10;
	this.subMenuOpacityStart=5;
	this.width=0;
	this.height=0;
	this.left=0;
	this.subMenuStatus=false;
	this.isHiding=false;
	this.doms=new LayerCollection();
}

WebsiteMenuRoot.prototype.closeSubMenu=function() {
	if(this.closeTimer!=0)
		window.clearTimeout(this.closeTimer);
	this.closeTimer=0;
	var body=this.doms.get("sub body");
	if(!body) return;
	this.isHiding=true;
	if(this.subMenuOpacity==0) {
		body=null;
		this.doms.remove("sub body");
		this.isHiding=false;
		return;
	}
	var timer=this.subMenuOpacity==10 ? 500 : 20;
	if(this.subMenuOpacity!=10) setOpacity(body, this.subMenuOpacity);
	this.subMenuOpacity--;
	if(this.subMenuOpacity==9)
		this.subMenuOpacity=this.subMenuOpacityStart;
	this.closeTimer=window.setTimeout("var menu=document.getElementById(\""+body.id+"\"); if(menu) {menu.RootMenu.closeSubMenu(); menu=null;} else alert(\""+body.id+"\")", timer);
}
WebsiteMenuRoot.prototype.openSubMenu=function() {
	if(this.closeTimer!=0)
		window.clearTimeout(this.closeTimer);
	this.closeTimer=0;


	var body=null;
	if(this.isHiding) {
		body=this.doms.get("sub body");
		if(body) {
			if(this.subMenuOpacity<this.subMenuOpacityStart-1) {
				setOpacity(body, 10);
			}
		}
		this.subMenuOpacity=10;
		this.isHiding=false;
		return;
	}
	this.subMenuOpacity=10;
	if(this.subMenuStatus==true) return;
	if(this.childCount==0) return;
	body=this.doms.get("sub body");
	this.doms.remove("sub body");
	body=createLayer();
	body.RootMenu=this;
	body.id="submenu_"+Math.random();
	body.style.background=this.parentItem.subMenuBgColor+" url("+graphicsBasePath+this.parentItem.skinPath+"/submenu_bg.jpg) repeat-x";
	body.style.color="black";
	var left=this.left+this.parentItem.left;
	var top=this.parentItem.height-10+this.parentItem.top;
	body.style.left=left;
	body.style.top=top;
	body.style.zIndex=10;
	body.style.border="1px solid black";
	body.onmouseover=function() {this.RootMenu.openSubMenu(); }
	body.onmouseout=function() {this.RootMenu.closeSubMenu(); }
	document.body.appendChild(body);
	this.doms.add(body, "sub body");
	var innerHeight=0;
	var innerWidth=0;
	for(var c in this.items) {
		var child=this.items[c];
		child.create(body);
		innerHeight+=child.height;
		if(innerWidth<child.width) innerWidth=child.width;
	}
}

WebsiteMenuRoot.prototype.createLayers=function() {
	var offsetLeft=this.parentItem.getWidth();
	this.left=offsetLeft;
	var menu=this.getRoot();
	var body=createLayer();
//	body.style.position="relative";
	body.style.background=menu.background;
//	body.style.width="200px";
	body.style.cursor="pointer";
	body.style.left=offsetLeft;
//	body.style.paddingBottom=menu.verticalMargins;
//	body.style.paddingTop=menu.verticalMargins;
	body.Menu=this;
	body.onclick=function() {this.Menu.onClick();}
	body.onmouseover=function() { this.Menu.openSubMenu();}
	body.onmouseout=function() { this.Menu.closeSubMenu();}

	this.getBodyLayer().appendChild(body);
	this.doms.add(body, "body");

	var caption=createLayer();
	caption.style.position="static";
	caption.style.marginBottom=menu.verticalMargins;
	caption.style.marginTop=menu.verticalMargins;
	caption.style.marginLeft=menu.horizontalMargins;
	caption.style.marginRight=menu.horizontalMargins;
	caption.style.fontSize=menu.fontSize;
	caption.style.fontFace=menu.fontFace;
	caption.style.color=menu.color;
	caption.style.whiteSpace="nowrap";
	caption.appendChild(document.createTextNode(this.caption));
	body.appendChild(caption);
	this.doms.add(caption, "caption");

	if(offsetLeft>0) {
		var separator=createImage(graphicsBasePath+"menu_separator.png");
		separator.style.position="absolute";
		separator.style.top="0px"
		body.appendChild(separator);
	}
	this.width=body.clientWidth;
	this.height=body.clientHeight;
}
