/*--------------------------------------------------------\
|                                        SmartMenu 1.34    |
\--------------------------------------------------------*/
var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var mac = navigator.appVersion.indexOf('Mac') != -1;

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];
		menuItem.Over();
	},
	OutMainMenuItem	:	function () {
		this.hideTimer = window.setTimeout("SmartMenuHandler.activeMainMenuItem.Hide()", this.activeMainMenuItem.Menu.HideTime);
	},
	OverMenuItem	:	function (oItem) {
		var menuItem = this.all[oItem.id];
		oItem.style.backgroundColor = this.activeMainMenuItem.Menu.MenuPageOverColor;
		if (menuItem.Level == 1) {
			document.getElementById(this.activeMainMenuItem.Menu.SubPlaceHolderID).style.visibility = "hidden";
			if (ie || mac) {
				document.getElementById(this.activeMainMenuItem.Menu.SubFrameHolderID).style.visibility = "hidden";
			}
		}
		menuItem.Over();
	},
	OutMenuItem	:	function (oItem) {
		var menuItem = this.all[oItem.id];
		oItem.style.backgroundColor = this.activeMainMenuItem.Menu.MenuPageOutColor;
	},
	OverDropDown : function () {
		this.activeMainMenuItem.Over();
	}
}

function SmartMenu() {
	this.Id = SmartMenuHandler.getId();
	SmartMenuHandler.all[this.Id] = this;
	this.ShowArrow = false;
	this.ArrowSrc = "";
	this.ArrowSrcOver = "";
	this.childNodes  = [];
	this.ActiveMenu = null;
	this.HideTimer = null;
	this.Spacer = "";
	this.Arrow = "";
	this.ArrowWidth = 0;
	this.ArrowHeight = 0;
	this.PlaceHolderID = "";
	this.SubPlaceHolderID = "";
	this.FrameHolderID = "";
	this.SubFrameHolderID = "";
	this.FrameID = "";
	this.SubFrameID = "";
	this.MenuPageOverColor = "";
	this.MenuPageOutColor = "";
	this.HideTime = 500;
	this.MainMenuAddPixelInTop = 0;
	this.MainMenuAddPixelInLeft = 0;
	this.SubMenuAddPixelInTop = 0;
	this.SubMenuAddPixelInLeft = 0;
}

SmartMenu.prototype.Add = function (menuItem) {
	menuItem.Menu = this;
	this.childNodes[this.childNodes.length] = menuItem;
}

function SmartMenuItem(text, link, level) {
	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.Level = level;
	this.MainMenuSwapId = null;
}

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 (this.IsThisMainMenuItem) {
		if (SmartMenuHandler.activeMainMenuItem != this) {
			if (SmartMenuHandler.activeMainMenuItem) {
				SmartMenuHandler.activeMainMenuItem.Hide();
			}
			SmartMenuHandler.activeMainMenuItem = this;
			this.SwapImageOver();
			this.SwapMainBackground();
			this.ShowSub();
		}
		else {
		}
	}
	else {
		this.ShowSub();
	}
	if (SmartMenuHandler.hideTimer != null) {
		clearTimeout(SmartMenuHandler.hideTimer)
	}
}

SmartMenuItem.prototype.ShowSub = function () {
	if (this.HasChilds) {
		this.Position();
		var el = (this.IsThisMainMenuItem) ? document.getElementById(this.Menu.PlaceHolderID) : document.getElementById(SmartMenuHandler.activeMainMenuItem.Menu.SubPlaceHolderID);
		el.innerHTML = this.BuildHtml();
		el.style.left = opera ? this.Left : this.Left  + "px";
		el.style.top = opera ? this.Top : this.Top + "px";
		el.style.visibility = "visible";
		if (ie && (mac == false)) {
			var elFrameHolder = (this.IsThisMainMenuItem) ? document.getElementById(this.Menu.FrameHolderID) : document.getElementById(SmartMenuHandler.activeMainMenuItem.Menu.SubFrameHolderID);
			elFrameHolder.style.left = this.Left;
			elFrameHolder.style.top = this.Top;
			elFrameHolder.style.width = el.offsetWidth;
			elFrameHolder.style.height = el.offsetHeight;
			elFrameHolder.style.visibility = "visible";
			var elFrame = (this.IsThisMainMenuItem) ? document.getElementById(this.Menu.FrameID) : document.getElementById(SmartMenuHandler.activeMainMenuItem.Menu.SubFrameID);
			elFrame.style.width = el.offsetWidth;
			elFrame.style.height = el.offsetHeight;
		}
	}
}

SmartMenuItem.prototype.BuildHtml = function () {
	var html = "";
	var pagesHtml = new Array(this.ChildNodes.length);
	for (var i = 0; i < this.ChildNodes.length; i++) {
		pagesHtml[i] = this.ChildNodes[i].GetRowHtml();
	}
	html +=	"<table class=\"SmartMenuTable\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" bgcolor=\"#808080\" width=\"100%\">" +
			pagesHtml.join("") +
			"</table>";
	return html;
}

SmartMenuItem.prototype.GetRowHtml = function () {
	var html = "";
	html =	"<tr id=\"" + this.Id + "\" style=\"background-color: " + SmartMenuHandler.activeMainMenuItem.Menu.MenuPageOutColor + ";\" onmouseover=\"SmartMenuHandler.OverMenuItem(this)\" onmouseout=\"SmartMenuHandler.OutMenuItem(this)\">" +
			this.GetTextCellHtml() + 
			this.GetSubMenuArrowCellHtml() +
			"</tr>";
	return html;
}

SmartMenuItem.prototype.GetTextCellHtml = function () {
	return "<td nowrap=\"nowrap\" width=\"100%\" class=\"SmartMenuTd\">" +
			"<a class=\"SmartMenuLink\" href=\"" + this.Link + "\">" + this.Text + "</a>" +
			"</td>";
};

SmartMenuItem.prototype.GetSubMenuArrowCellHtml = function () {
	var imageStr = this.HasChilds ? SmartMenuHandler.activeMainMenuItem.Menu.Arrow : SmartMenuHandler.activeMainMenuItem.Menu.Space
	var strValign = this.HasChilds ? "middle" : "top";
	var w = this.HasChilds ? SmartMenuHandler.activeMainMenuItem.Menu.ArrowWidth : "1";
	var h = this.HasChilds ? SmartMenuHandler.activeMainMenuItem.Menu.ArrowHeight : "1";
	return "<td valign=\"" + strValign + "\"><img id=\"" + this.SubGifId +"\" src=\"" + imageStr + "\" alt=\"\" border=\"0\" width=\"" + w + "\" height=\"" + h + "\"></td>";
};

SmartMenuItem.prototype.Position = function () {
	var imgX = (this.IsThisMainMenuItem) ? document.getElementById(this.ImageId) : document.getElementById(this.SubGifId);
	var imgY = imgX;
	var l = imgX.offsetLeft || 0;
	var t = imgY.offsetTop || 0;
	while (imgX = imgX.offsetParent) {
		l += imgX.offsetLeft
	}
	while (imgY = imgY.offsetParent) {
		t += imgY.offsetTop
	}
	t += this.IsThisMainMenuItem ? this.ImageHeight : 0;
	t += this.IsThisMainMenuItem ? SmartMenuHandler.activeMainMenuItem.Menu.MainMenuAddPixelInTop : SmartMenuHandler.activeMainMenuItem.Menu.SubMenuAddPixelInTop;
	l += this.IsThisMainMenuItem ? SmartMenuHandler.activeMainMenuItem.Menu.MainMenuAddPixelInLeft : SmartMenuHandler.activeMainMenuItem.Menu.SubMenuAddPixelInLeft;
	this.Left = l;
	this.Top = t;
}

SmartMenuItem.prototype.SwapImageOver = function () {
	if ((this.ImageId != "") && (this.ImageOverSrc) != "") {
		document.getElementById(this.ImageId).src = this.ImageOverSrc;
	}
}

SmartMenuItem.prototype.SwapMainBackground = function () {
	if ((this.MainMenuSwapId != null) && (SmartMenuHandler.activeMainMenuItem.Menu.MenuPageOverColor) != "") {
		document.getElementById(this.MainMenuSwapId).style.backgroundColor = SmartMenuHandler.activeMainMenuItem.Menu.MenuPageOverColor;
	}
}

SmartMenuItem.prototype.SwapOutMainBackground = function () {
	if ((this.MainMenuSwapId != null) && (SmartMenuHandler.activeMainMenuItem.Menu.MenuPageOutColor) != "") {
		document.getElementById(this.MainMenuSwapId).style.backgroundColor = SmartMenuHandler.activeMainMenuItem.Menu.MenuPageOutColor;
	}
}

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();
		this.SwapOutMainBackground();
		this.HideAllSubs();
		SmartMenuHandler.activeMainMenuItem = null;
	}
}

SmartMenuItem.prototype.HideAllSubs = function () {
	document.getElementById(this.Menu.PlaceHolderID).style.visibility = "hidden";
	document.getElementById(this.Menu.SubPlaceHolderID).style.visibility = "hidden";
	if (ie || mac) {
		document.getElementById(this.Menu.FrameHolderID).style.visibility = "hidden";
		document.getElementById(this.Menu.SubFrameHolderID).style.visibility = "hidden";
	}
}
