﻿// creates a new drop-down menu
DropMenu = function(id)
{
	this.element = $("#" + id);
	this.fixHover = true;
	this.items = new Array();
	
	this.initialize();
}

// initializes the menu
DropMenu.prototype.initialize = function()
{
	var me = this;
	var list = me.element.children("LI");

	list.each(function(i) { me.items.push(new DropMenuItem(this, me)) });

	// fix menu z-order when popping over form elements in IE
	if (document.all)
	{
		me.element.find("UL").each(function(i)
		{
			var frameMenu = this;
			var frame = frameMenu.appendChild(document.createElement("iframe"));

			frame.frameBorder = "0";
			frame.scrolling = "no";
			frame.src = "about:blank";
			frame.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
			frame.style.left = "0px";
			frame.style.height = frameMenu.offsetHeight + "px";
			frame.style.position = "absolute";
			frame.style.top = "0px";
			frame.style.visibility = "hidden";
			frame.style.width = frameMenu.offsetWidth + "px";
			frame.style.zIndex = "-1";
			frameMenu.style.zIndex = "101";
		});
	}

	return this;
}

// creates a new item
DropMenuItem = function(element, menu, parent)
{
	var me = this;

	this.element = $(element);
	this.items = new Array();
	this.menu = menu;
	this.parent = parent;
	this.size = 0;

	// hover fix for IE6
	if (this.menu.fixHover)
	{
		this.element.hover(
			function() { me.show(); },
			function() { me.timeout = window.setTimeout(function() { me.hide(); }, 500); });
	}

	this.initialize();
}

// gets the width of the item
DropMenuItem.prototype.getWidth = function(item)
{
	var size = 0;

	if (item != null)
	{
		item.size = 0;

		$(item).children("A").each(function(i)
		{
			item.size = Math.max($(this).outerWidth(), item.size);
		});

		size = item.size;
	}

	return size;
}

// initializes the item
DropMenuItem.prototype.initialize = function()
{
	var li = this.element.children("UL").children("LI");
	var me = this;

	me.size = this.element.innerWidth();

	li.each(function(i)
	{
		if ($(this).children("UL").length > 0)
		{
			me.items.push(new DropMenuItem(this, me.menu, me));
		}

		me.size = Math.max(me.getWidth(this), me.size);
	});

	me.element.children("UL").css("width", me.size + "px");
	me.element.children("UL").children("LI").css("width", "100%");

	if (me.parent != null)
	{
		me.element.css("width", "100%");
	}
}

DropMenuItem.prototype.hide = function()
{
	if (this.element)
	{
		this.element.removeClass("hover");
		this.toggleSubmenus();
	}
}

DropMenuItem.prototype.show = function()
{
	var items = this.parent != null ? this.parent.items : this.menu.items;

	// set the classname and toggle iframe fix
	if (this.element)
	{
		this.element.addClass("hover");
		this.toggleSubmenus();
	}

	// clear the hide timer	
	if (this.timeout)
	{
		window.clearTimeout(this.timeout);
	}

	// hide any other open menus
	for (var i = 0; i < items.length; i++)
	{
		if (items[i] != this)
		{
			items[i].hide();
		}
	}
}

DropMenuItem.prototype.toggleSubmenus = function()
{
	this.element.find("IFRAME").each(function(i)
	{
		var parent = $(this.parentNode);
		
		this.style.height = parent.height() + "px";
		this.style.visibility = parent.css("visibility");
		this.style.width = parent.width() + "px";
	});
}

