/* submenu.js 

This script will provide dropdown menus
Requires prototype.js

need to:
* foreach submenu
* get coordinates of parent menu item
* find height of parent element
* set coordinates of child menu to: left value of parent, and top + height of parent.

*/

var pageMenu;
var defaultMenuID;

function Menu(button,menu) {
	this.button = button;
	this.menu = menu;
}

Menu.prototype.initialize = function() {
	var thisMenu = this;
		thisMenu.button.onmouseover = function () {
			thisMenu.showMenu();
		}
		thisMenu.button.onmouseout = function () {
			thisMenu.rollOff();
		}
		if (this.menu != null) {
			thisMenu.menu.onmouseover = function () {
				thisMenu.cancelTimer();
			}
			thisMenu.menu.onmouseout = function () {
				thisMenu.rollOff();
			}
		}
}

Menu.prototype.cancelTimer = function () {
	clearTimeout(this.timer);
}

Menu.prototype.rollOff = function () {
	if (pageMenu != undefined) {
		var closeMenu = "menuObjects["+this.id+"].defaultMenu()";
		this.timer = setTimeout(closeMenu,0);
	}
	else {
		var closeMenu = "menuObjects["+this.id+"].hideMenu()";
		this.timer = setTimeout(closeMenu,0);
	}
}

Menu.prototype.showMenu = function () {
	for (i=0;i<menuObjects.length;i++) {
		menuObjects[i].hideMenu();
		menuObjects[i].cancelTimer();
	}
	if (this.menu != null) {
	this.menu.style.display = "block";
	if (this.id == defaultMenuID) {
		this.button.className = "dropdown on";
		}
		else {
		this.button.className += " hover";
		}
	}
}

Menu.prototype.hideMenu = function () {
	if (this.menu != null) {
	this.menu.style.display = "none";
	this.button.className = "dropdown";
	if (this.id == defaultMenuID) {
		this.button.className = "dropdown on";
	}
	}
}

Menu.prototype.defaultMenu = function () {
	if (this.menu != null) {
	this.menu.style.display = "none";
	this.button.className = "dropdown";
		menuObjects[defaultMenuID].showMenu();
		menuObjects[defaultMenuID].button.className = "dropdown on";
	}
}


// initialize menus //

var menuObjects = new Array();

Event.observe(window, 'load', function() {
	var menus = $$("a.dropdown");
	for (var y=0; y < menus.length; y++) {
		var submenuID = "dropdown-"+menus[y].id;
		var submenu = $(submenuID);
		menuObjects[y] = new Menu(menus[y],submenu);
		menuObjects[y].id = y;
		menuObjects[y].initialize();
		if (pageMenu != undefined) {
			if (pageMenu == menus[y].id) {
					defaultMenuID = y;
				}
		}
	}
	if (pageMenu != undefined) {
		menuObjects[defaultMenuID].defaultMenu();
	}
});
	

