// JavaScript Document

/*******************************
*
* NavMenus.js
* 
* DHTML Drop Down Navigation Menus
* Ver = 2.0 - Utilizes prototype.js - /js/extern/prototype.js
*
* Author: John Hutcheson, control.option
*
*******************************/
var NavMenus = Class.create();


NavMenus.prototype = {

	//NavMenus Object Class Constructor
	initialize: function(imgUtils, menuSuffix) {
		if (document.images) {
			//Create empty Array for menu objects
			// To be populated by addMenu method
			this.menus = $A(new Array());
			// Check to see if an instance of ImageUtils has been passed in, otherwise
			// create instance of the ImageUtils to handle image rollovers
			this.images = (imgUtils instanceof ImageUtils) ? imgUtils : new ImageUtils ();
				
			this.mSuffix = (menuSuffix && typeof menuSuffix =="string") ? menuSuffix : "_menu";
			
			
			//Initialize timer variable used to hold set Timeout function in request hide method
			this.timer;
			
			// Set up vars to hold the bound mousover and mouseout listeners
			this.boundOver;
			this.boundOut;
			
			// set the document event handlers
			this.setHandlers();
			
		}
	},
	
	addMenu: function(el) {
		var menuObj = {mBarId:el.id, mBarW:el.width, mBarH:el.height, mDivId:el.id + this.mSuffix}
		
		if (this.menus.indexOf(menuObj) != -1) {
			return;
		}
		this.menus.push(menuObj);
		
		this.images.addImage(el);
	},
	
	setHandlers: function () {
		this.boundOver = this.over.bindAsEventListener(this);
		this.boundOut = this.out.bindAsEventListener(this);
		Event.observe (document, 'mouseover', this.boundOver);
		Event.observe (document, 'mouseout', this.boundOut);
	},
	
	over: function (e) {
		Event.stop(e);
		var elt = $(Event.element(e));
		if (elt.tagName == "IMG" && elt.hasClassName("navMenuBar")) {
			this.showMenu (elt.id, elt.id + this.mSuffix);
		} else if (elt.hasClassName("menuTab") || elt.hasClassName("menuContent") || elt.up("div.menuTab") || elt.up("div.menuContent")) {
			this.keepMenu();
		}
	},
	
	out: function (e) {
		Event.stop(e);
		var elt = $(Event.element(e));
		if (elt.tagName == "IMG" && elt.hasClassName("navMenuBar")) {
			this.requestHide();
		} else if (elt.hasClassName("menuTab") || elt.hasClassName("menuContent") || elt.up("div.menuTab") || elt.up("div.menuContent")) {
			this.requestHide();
		}
	},
	
	
	requestHide: function() {
		var boundHide = this.hideMenus.bind(this);
		this.timer = setTimeout(boundHide, 80);
	},
	
	hideMenus: function() {
		var img = this.images;
		this.menus.each(function (m) {
			img.out(null, m.mBarId);
			$(m.mDivId).setHidden();
		});
	},
	
	showMenu: function(imgId, menuDivId) {
		var menu = $(menuDivId);
		this.keepMenu();
		this.hideMenus();
		this.images.over(null, imgId);
		menu.setVisible();
	},
	
	keepMenu: function () {
		clearTimeout(this.timer);
	},
	
	toggleBGColor: function(mItemId, color) {
		$(mItemId).setStyle({background: color});
	},
	
	toggleMenuItem: function(mItemId, cssClass) {
		$(mItemId).toggleClassName(cssClass);
	},
	
	goToUrl: function(href) {
		window.location.href = href;
	},
	
	blurField: function(fieldname) {
		document.getElementById(fieldname).blur();
	}
};
