/*----------------------------------------------
	
	SmartMenu 1.1
	Author     : Christian Palm (cpa@1508.dk)
	Company    : 1508 A/S (http://www.1508.dk/)
	Copyright  : (c) 2004 - 2005, 1508 A/S
	Modified   : 2005-11-04
	
	The above copyright notice shall be included in
	all copies or substantial portions of this file

------------------------------------------------*/

var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
var ie6 = ie && /MSIE [6789]/.test(ua);
var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
var moz = !opera && /gecko/i.test(ua);
var nn6 = !opera && /netscape.*6\./i.test(ua);

var SmartMenuHandler = {
	idCounter		:	0,
	idPrefix		:	"smart-menu-object-",
	all				:	{},
	allViaPageId	:	{},
	activeMainMenuItem : null,
	hideTimer		: null,
	getId			:	function () { return this.idPrefix + this.idCounter++; },
	OverMainMenuItem	:	function (pageId) {
		var menuItem = this.allViaPageId[pageId];
		if (menuItem) {
			menuItem.Over();
		}
	},
	OutMainMenuItem	:	function () {
		SmartMenuHandler.hideTimer = window.setTimeout("SmartMenuHandler.HideActiveMenu()", 1000);
	},
	OverDropDown : function () {
		this.activeMainMenuItem.Over();
	},
	OutDropDown : function () {
		SmartMenuHandler.hideTimer = window.setTimeout("SmartMenuHandler.HideActiveMenu()", 1000);
	},
	OverExplorerDropDown : function () {
		clearTimeout(SmartMenuHandler.hideTimer)
		SmartMenuHandler.hideTimer = null;
	},
	HideActiveMenu : function () {
		if (SmartMenuHandler.activeMainMenuItem != null) {
			SmartMenuHandler.activeMainMenuItem.Hide();
		}
	}
}

function SmartMenu() {
	this.Id = SmartMenuHandler.getId();
	SmartMenuHandler.all[this.Id] = this;
	this.childNodes  = [];
	this.ActiveMenu = null;
	this.HideTimer = null;
	this.PlaceHolderID = "";
	this.CssFile = "";
	this.ExplorerMacBugExtraTopPixel = 0;
}

SmartMenu.prototype.Add = function (menuItem) {
	menuItem.Menu = this;
	this.childNodes[this.childNodes.length] = menuItem;
}

function SmartMenuItem(text, link) {
	this.Id = SmartMenuHandler.getId();
	SmartMenuHandler.all[this.Id] = this;
	this.SubGifId = this.Id + "-placerGif" 
	this.PageId = 0;
	this.ImageId = "";
	this.ImageSrc = "";
	this.ImageOverSrc = "";
	this.ImageHeight = 0;
	this.ImageWidth = 0;
	this.Text = text;
	this.Class = "";
	this.Link = link;
	this.IsThisMainMenuItem = false;
	this.ChildNodes  = [];
	this.HasChilds = false;
	this.Menu = null;
	this.BgColor = "#ffffff";
}

SmartMenuItem.prototype.Add = function (menuItem) {
	this.HasChilds = true;
	this.ChildNodes[this.ChildNodes.length] = menuItem;
}

SmartMenuItem.prototype.RegisterMainMenuItem = function (pageId) {
	this.PageId = pageId;
	this.IsThisMainMenuItem = true;
	SmartMenuHandler.allViaPageId[this.PageId] = this;
}

SmartMenuItem.prototype.Over = function () {
	if (SmartMenuHandler.activeMainMenuItem != this) {
		if (SmartMenuHandler.activeMainMenuItem) {
			SmartMenuHandler.activeMainMenuItem.Hide();
		}
		this.SwapImageOver();
		this.ShowSub();
	}
	SmartMenuHandler.activeMainMenuItem = this;
	if (SmartMenuHandler.hideTimer != null) {
		clearTimeout(SmartMenuHandler.hideTimer)
	}
}

SmartMenuItem.prototype.ShowSub = function () {
	var html = this.BuildHtml();
	var el = document.getElementById(this.Menu.PlaceHolderID);
	el.innerHTML = html;
	el.style.backgroundColor = this.BgColor;
	this.Position();
	el.style.left = opera ? this.Left  : this.Left  + "px";
	el.style.top = opera ? this.Top : this.Top + "px";
	el.style.visibility = "visible";
}

SmartMenuItem.prototype.BuildHtml = function () {
	return "<a class=\"SmartMenuLink\" href=\"" + this.Link + "\">" + this.Text + "</a>";
}

SmartMenuItem.prototype.Position = function () {
	var el = document.getElementById(this.ImageId);
	var l = getAbsX(el);
	var t = getAbsY(el);
	this.Left = l + (-208);
	this.Top = t;
}

SmartMenuItem.prototype.SwapImageOver = function () {
	if ((this.ImageId != "") && (this.ImageOverSrc) != "") {
		document.getElementById(this.ImageId).src = this.ImageOverSrc;
	}
}

SmartMenuItem.prototype.SwapImageOut = function () {
	if ((this.ImageId != "") && (this.ImageSrc) != "") {
		document.getElementById(this.ImageId).src = this.ImageSrc;
	}
}

SmartMenuItem.prototype.Hide = function () {
	if (this.IsThisMainMenuItem) {
		this.SwapImageOut();
		var el = document.getElementById(this.Menu.PlaceHolderID);
		el.style.visibility = "hidden";
		if (this.popup) {
			this.popup.hide();
		}
		SmartMenuHandler.activeMainMenuItem = null;
	}
}

function getAbsX(elt) {
	return (elt.x) ? elt.x : getAbsPos(elt, "Left");
}

function getAbsY(elt) {
	return (elt.y) ? elt.y : getAbsPos(elt, "Top");
}

function getAbsPos(elt, which) {
	iPos = 0;
	while (elt != null) {
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}