function Browser()
{
	this.dom = (!document.all && document.getElementsByTagName && document.getElementById);
	this.ie5up = (document.all && document.getElementById && navigator.userAgent.toLowerCase().indexOf("mac") == -1);
	this.macie5up = (document.all && document.getElementById && navigator.userAgent.toLowerCase().indexOf("mac") != -1);
	this.ns6up = (document.getElementById && navigator.appName.toLowerCase().indexOf("netscape") != -1);
}

// write stylesheet if browser = ie5.0 or ie5.5
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
						&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
						&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
											(agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);


var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

if (is_ie5 || (is_ie5_5up && !is_ie6up))
{
	var strStyle =	"<style>" +
										"tr.Dropdownmenu-submenuItem { cursor: hand; } \n" +
										"tr.Dropdownmenu-submenuItem-active { cursor: hand; } \n" +
										"td.Dropdownmenu-Menubar-item { cursor: hand; } \n" +
									"</style>";
	
	document.write(strStyle);
}

function getPosX(obj)
{
	var intLeft = 0;

	while (obj.offsetParent)
	{
		intLeft += obj.offsetLeft
		obj = obj.offsetParent;
	}

	return intLeft;
}

function getPosY(obj)
{
	var intTop = 0;

	while (obj.offsetParent)
	{
		intTop += obj.offsetTop
		obj = obj.offsetParent;
	}

	return intTop;
}

function getParent(objElement, strTagName)
{
	if (objElement.tagName == strTagName)
		return objElement;

	var objTemp = objElement;
	var blnStop = false;

	while (objTemp.tagName != "BODY")
	{
		if (objTemp.tagName == strTagName)
			return objTemp;
		else
			objTemp = objTemp.parentNode;
	}

	return null;
}

/* handles all mousemovement and clicks - do not change this name ! */
var MenuHandler = {
	is: new Browser(),
	all: [],
	menus: [],
	barHtml: [],
	menuHtml: [],
	flashDiv: null,
	items: [],
	nextId: 1,
	getNextId: function() { return "Dropdownmenu-id-" + this.nextId++; },
	nextShow: 0,
	getNextShow: function() { return this.nextShow++; },
	getCurrentShow: function() { return this.nextShow; },
	imagePath: "/images/dropdownmenu/",

	itemHover: function(event, id) { this.items[id].mHover(event); },

	itemOut: function(event, id) { this.items[id].mOut(event); },

	itemClick: function(event, id) { this.items[id].click(event);	},

	showMenu: function(event, id)
	{
		if (this.flashDiv)
			this.flashDiv.style.display = "none";

		var menuItem = this.items[id];
		var submenu = menuItem.subMenu;

		// the menu and all its parents get a new unique nr - after this all menus with an other nr than this gets hidden
		var nextShowing = this.getNextShow();

		if (submenu)
		{
			submenu.showing = nextShowing;
			submenu.show();
		}

		var parentMenu = menuItem.parent;

		while (parentMenu)
		{
			parentMenu.showing = nextShowing;
			if (parentMenu.parent)
			{
				parentMenu.parent.mHover();
				parentMenu = parentMenu.parent.parent;
			}
			else
				parentMenu = null;
		}

		this.hideAllOld(nextShowing);
	},

	hideMenu: function(dropoutmenu)	{ dropoutmenu.hide();	},

	setup: function()
	{
		var me = this;

		if (this.is.ie5up)
		{
			document.attachEvent("onclick", function() { me.hideAllMenus(); });
			window.attachEvent("onload", function() { me.getFlashDivRef(); });
			window.attachEvent("onresize", function() { me.hideAllMenus(); });
		}
		else if (this.is.macie5up)
		{
			document.onclick = function() { me.hideAllMenus(); };
			window.onload = function() { me.getFlashDivRef(); };
			window.onresize = function() { me.hideAllMenus(); };
		}
		else if (this.is.dom)
		{
			document.addEventListener("click", function() { me.hideAllMenus(); }, false);
			window.addEventListener("load", function() { me.getFlashDivRef(); }, false);
			window.addEventListener("resize", function() { me.hideAllMenus(); }, false);
		}
	},

	getFlashDivRef: function()
	{
		this.flashDiv = document.getElementById("top-flash-div");
	},

	hideAllOld: function(showing)
	{
		for (var menu in this.menus)
		{
			var tmp = this.menus[menu];
			
			if (tmp.showing < showing)
				tmp.hide();
		}
	},

	hideAllMenus: function()
	{
		for (var menu in this.menus)
			this.menus[menu].hide();

		if (this.flashDiv)
			this.flashDiv.style.display = "block";
	}
};

MenuHandler.setup();

function Menubar()
{
	this.id = MenuHandler.getNextId();
	this.parent = null;
	this.children = [];

	MenuHandler.all[this.id] = this;
	//MenuHandler.menus[this.id] = this;
}

Menubar.prototype.add = function(item)
{
	this.children[this.children.length] = item;
	item.parent = this;
}

Menubar.prototype.getBarHtml = function()
{
	var c = this.children;
	var rHtml = "";

	MenuHandler.barHtml[this.id] = "<div class=\"Dropdownmenu-bar\">" +
															"  <table cellpadding='0' cellspacing='0' border='0' height='21'>" +
															"    <tr>";

	for (var i = 0; i < c.length; i++)
	{
		MenuHandler.barHtml[this.id] += c[i].getHtml();

		// spacer
		if (i < c.length - 1)
			MenuHandler.barHtml[this.id] += c[i].getSpacer();
	}

	MenuHandler.barHtml[this.id] += "    </tr>" +
															 "  </table>" +
															 "</div>";

	for (i in MenuHandler.barHtml)
		rHtml += MenuHandler.barHtml[i];

	return rHtml;
}

Menubar.prototype.getMenuHtml = function()
{
	var rHtml = "";

	for (i in MenuHandler.menuHtml)
	{
		rHtml += MenuHandler.menuHtml[i];
	}

	return rHtml;
}

/* General menuitem */
function Menuitem(title, link, subMenu)
{
	this.id = MenuHandler.getNextId();
	this.title = title;
	
	this.link = link;
	if ((this.link != "" || typeof link == "function") && this.link != null)
		this.validLink = true;
	else
		this.validLink = false;

	this.subMenu = subMenu;
	this.parent = null;
	this.locked = false;	// A menuitem is locked when its submenu (if any) is showing. 
												// This insurace that mOut() is not run when the cursor moves toward the submenu, while leaving this menuitem.
												// If not locked it would cause a short flash, because of mOut() being run and mHover() a breif second later in MenuHandler.showMenu()

	if (subMenu)
		subMenu.parent = this;

	MenuHandler.all[this.id] = this;
	MenuHandler.items[this.id] = this;
}

// should be defined in sub "classes"
Menuitem.prototype.mHover			= function() { alert("not implemented in sub"); }
Menuitem.prototype.mOut				= function() { alert("not implemented in sub"); }
Menuitem.prototype.getHtml		= function() { alert("not implemented in sub"); }
Menuitem.prototype.getOffsetX = function() { alert("not implemented in sub"); }
Menuitem.prototype.getOffsetY = function() { alert("not implemented in sub"); }

Menuitem.prototype.click = function(event)
{
	if (MenuHandler.is.ie5up || MenuHandler.is.macie5up)
	{
		if (typeof this.link == "string" && this.link != "")
			window.location = this.link;
		else if (typeof this.link == "function")
			this.link();
		else
		{
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
	else if (MenuHandler.is.dom)
	{
		if (typeof this.link == "string" && this.link != "")
			window.location = this.link;
		else if (typeof this.link == "function")
			this.link();
		else
			event.stopPropagation();
	}
}

/* Menubar menu item */

function MenubarItem(title, link, subMenu)
{
	this.base = Menuitem;
	this.base(title, link, subMenu);
}
MenubarItem.prototype = new Menuitem; // :: Menuitem

MenubarItem.prototype.mHover = function()
{
	if (!this.td)
		this.td = document.getElementById(this.id);
}

MenubarItem.prototype.mOut = function() {}

MenubarItem.prototype.getOffsetX = function()
{
	var offX = getPosX(this.td) - 4;

	return offX;
}

MenubarItem.prototype.getOffsetY = function()
{
	var menucontainer = document.getElementById("menucontainer");
	var tdOffY = getPosY(menucontainer) + menucontainer.offsetHeight;

	return tdOffY;
}

MenubarItem.prototype.getHtml = function()
{
	var html = "";

	if (this.subMenu)
		this.subMenu.appendHtml();

	html = "<td id=\"" + this.id + "\" class='Dropdownmenu-Menubar-item' onmouseover=\"MenuHandler.itemHover(event, '" + this.id + "'); " +
				 "MenuHandler.showMenu(event,'" + this.id + "')\" onmouseout=\"MenuHandler.itemOut(event, '" + this.id + "')\" " +
				 "onclick=\"MenuHandler.itemClick(event, '" + this.id + "');\"";
	
	/*if (this.validLink)
		html += " style=\"cursor: hand; cursor: pointer;\"";
	else
		html += " style=\"cursor: default;\"";*/
	
	if (!this.validLink)
		html += " style=\"cursor: default;\"";
	
	html += ">" + this.title + "</td>";

	return html;
}

MenubarItem.prototype.getSpacer = function()
{
	return "<td class='Dropdownmenu-menubar-item-spacer'>|</td>";
}

/* Submenu Menu item */

function SubmenuItem(title, link, subMenu)
{
	this.base = Menuitem;
	this.base(title, link, subMenu);

	this.tr = null;
	this.img = null;
}
SubmenuItem.prototype = new Menuitem; // :: Menuitem

SubmenuItem.prototype.mHover = function()
{
	if (!this.tr)
		this.tr = document.getElementById(this.id + "-tr");

	if (this.subMenu)
	{
		if (!this.img)
			this.img = document.getElementById(this.id + "-img");

		this.img.src = MenuHandler.imagePath + "sub_hover.gif";
	}

	this.tr.className = "Dropdownmenu-submenuItem-active";
}

SubmenuItem.prototype.mOut = function()
{
	if (this.locked)
		return;

	if (this.subMenu)
		this.img.src = MenuHandler.imagePath + "sub.gif";

	this.tr.className = "Dropdownmenu-submenuItem";
}

SubmenuItem.prototype.getHtml = function()
{
	var html = "";
	html = "<tr id=\"" + this.id + "-tr\" class='Dropdownmenu-submenuItem' onmouseover=\"MenuHandler.itemHover(event, '" + this.id + "'); " +
				 "MenuHandler.showMenu(event,'" + this.id + "');\" onmouseout=\"MenuHandler.itemOut(event, '" + this.id + "')\" " +
				 "onclick=\"MenuHandler.itemClick(event, '" + this.id + "');\" ";

	/*if (this.validLink)
		html += " style=\"cursor: hand; cursor: pointer;\"";
	else
		html += " style=\"cursor: default;\"";*/

	if (!this.validLink)
		html += " style=\"cursor: default;\"";
	
	html += ">" +
				  "  <td id=\"" + this.id + "-text\" class='Dropdownmenu-submenuItem-text'>" + this.title + "</td>" +
				  "	<td id=\"" + this.id + "-td\" class='Dropdownmenu-submenuItem-icon'>";

	if (this.subMenu)
	{
		this.subMenu.appendHtml();
		html += "<img id=\"" + this.id + "-img\" src='" + MenuHandler.imagePath + "sub.gif'>";
	}
	else
		html += "<img src='" + MenuHandler.imagePath +  "1x1.gif' width='1' height='1'>";
	
	html += "</td>" +
				  "</tr>";

	return html;
}

SubmenuItem.prototype.getSpacer = function()
{
	return "<tr>" +
				 "  <td colspan='2' class='Dropdownmenu-menu-item-spacer'><img src='" + MenuHandler.imagePath + "1x1.gif'></td>" +
				 "</tr>";
}

SubmenuItem.prototype.getOffsetX = function()
{
	if (!this.x)
	{
		var offX = getPosX(this.tr) + this.tr.offsetWidth;
		this.x = offX;
	}

	return this.x;
}

SubmenuItem.prototype.getOffsetY = function()
{
	if (!this.y)
	{
		var offY = getPosY(this.tr);
		this.y = offY - 1;
	}

	return this.y;
}

/* Menu */

function Menu()
{
	this.id = MenuHandler.getNextId();
	this.children = [];

	this.div = null;

	MenuHandler.all[this.id] = this;
	MenuHandler.menus[this.id] = this;
}

Menu.prototype.add = function(item)
{
	this.children[this.children.length] = item;
	item.parent = this;
}

var i = 0;

Menu.prototype.show = function(offX, offY)
{
	var parentX, parentY;
	
	parentX = this.parent.getOffsetX();
	parentY = this.parent.getOffsetY();

	if (!this.div)
		this.div = document.getElementById(this.id);

	this.parent.locked = true;

	with (this.div.style)
	{
		visibility = "visible";
		left = parentX;
		top = parentY;
	}
}

Menu.prototype.hide = function()
{
	if (!this.div)
		return;

	this.parent.locked = false;
	this.parent.mOut();

	this.div.style.visibility = "hidden";
}

Menu.prototype.appendHtml = function()
{
	var c = this.children;

	MenuHandler.menuHtml[this.id] = "<div class='Dropdownmenu-menu' id=\"" + this.id + "\">" +
																	"  <table cellpadding='0' cellspacing='0' border='0'>";

	for (var i = 0; i < c.length; i++)
	{
		MenuHandler.menuHtml[this.id] += c[i].getHtml()

		// spacer
		if (i < c.length - 1)
			MenuHandler.menuHtml[this.id] += c[i].getSpacer();
	}

	MenuHandler.menuHtml[this.id] += "  </table>" +
																	 "</div>";
}