WestgateChapel.Website.Menu = Class.create();
WestgateChapel.Website.Menu.prototype = {

	// variables
	itemTagName: "li",
	activeClassName: "active",
	activationDelayMS: 250,
	deactivationDelayMS: 500,

	initialize: function(elem, activationDelayMS, deactivationDelayMS, itemTagName) {
	
		this.activeItem = null;
		this.element = $(elem);
		
		if(activationDelayMS)
			this.activationDelayMS = activationDelayMS;
			
		if(deactivationDelayMS)
			this.deactivationDelayMS = deactivationDelayMS;
			
		if(itemTagName)
			this.itemTagName = itemTagName;
			
		//this.delayQueue = new DelayQueue();
		
		for(var i = 0; i < this.element.childNodes.length; i++) {
		
			if(this.element.childNodes.item(i).nodeName.toUpperCase() == this.itemTagName.toUpperCase())
				this.initItem(this.element.childNodes[i]);
		
		}
		
		// init timer property
		this.currentTimer = new WestgateChapel.Website.Timer();
	
	},
	
	initItem: function(item) {
		if(item) {
			item.menu = this;
			//Event.observe(item, "mouseover", this.getActivateHandler(item));
			item.onmouseover = item.menu.getActivateHandler(item);
			//Event.observe(item, "mouseout", this.getDeactivateHandler(item));
			item.onmouseout = item.menu.getDeactivateHandler(item);
			//item.style.display = "none";
			item.subMenu = item.getElementsByTagName(item.menu.element.nodeName)[0];
			if(firstItem = item.getElementsByTagName(item.menu.itemTagName)[0])
				Element.addClassName(firstItem, "first");
		}	
	},
	
	showItem: function(item) {
		if(item.subMenu)
			item.subMenu.style.display = "block";
		Element.addClassName(item, item.menu.activeClassName);
	},
	
	hideItem: function(item) {
		if(item.subMenu)
			item.subMenu.style.display = "none";
		Element.removeClassName(item, item.menu.activeClassName);
	},
	
	activateItem: function(item) {
		if(item) {

			if(item.getElementsByTagName(item.menu.element.nodeName).length == 0) {
				// no sub menu
				// imediately finish any timed hide
				item.menu.currentTimer.stop(true);
					
				// imediately activate this item without delay
				item.menu.showItem(item);
			}
			if(item.menu.activeItem == null) {
			
				// no active menu
			
				// show item after interval
				item.menu.currentTimer.stop();
					
				// create a new timer new EverettClinic.Extranet.Timer(, 
				item.menu.currentTimer.callback = function() {
				
					// this is scoped to item
					this.menu.showItem(this);
				
				}.bind(item);
				item.menu.currentTimer.interval = item.menu.activationDelayMS;
				item.menu.currentTimer.start();
			
			}
			else if(item != item.menu.activeItem) {
			
				// menu active, but different
				
				// imediately finish any timed hide
				item.menu.currentTimer.stop(true);
					
				// imediately activate this item without delay
				item.menu.showItem(item);
			
			}
			else
			{
				item.menu.currentTimer.stop();
			}
		
			item.menu.activeItem = item;
		
		}
	},
	
	deactivateItem: function(item) {
		if(item) {
			
			// cancel the timer
			item.menu.currentTimer.stop();
				
			// create a new timer
			item.menu.currentTimer.interval = item.menu.deactivationDelayMS;
			item.menu.currentTimer.callback = function() {
			
				// this is scoped to item
				this.menu.activeItem = null;
				this.menu.hideItem(this);
			
			}.bind(item);
			item.menu.currentTimer.start();
			
		}
	},
	
	activateHandler: function(e, item) {
		//if(!item) item = Event.element(e);
		if(!item) item = this;
		item.menu.activateItem.call(item.menu, item); // change scope of this from item to menu
		//item.menu.activateItem(item);
	},
	
	deactivateHandler: function(e, item) {
		//if(!item) item = Event.element(e);
		if(!item) item = this;
		if(item.menu._checkEventTarget(e, item))
			item.menu.deactivateItem.call(item.menu, item); // change scope of this from item to menu
			//item.menu.deactivateItem(item);
	},
	
	getActivateHandler: function(item) {
		return item.menu.activateHandler.bindAsEventListener(item);
	},
	
	getDeactivateHandler: function(item) {
		return item.menu.deactivateHandler.bindAsEventListener(item);
	},
	
	_truncateText: function(item, chars) {
	
		// must have item and only do once
		if(item) {
		
			if(anchor = item.getElementsByTagName("a").item(0))
			{
				if(anchor.textContent)
				{
					if(anchor.textContent.length > chars)
						anchor.textContent = anchor.textContent.substr(0, chars).replace(/\s+$/g,'') + "...";
				}
				else if(anchor.innerText)
					if(anchor.innerText.length > chars)
						anchor.innerText = anchor.innerText.substr(0, chars).replace(/\s+$/g,'') + "...";
						
				//item.isTextTruncated = true;
			}
		
		}
		
	},
	
	_checkEventTarget: function(e, target) {
	
		var ret = true;
		if (!e) var e = window.event;
		//var tg = (window.event) ? e.srcElement : e.target;
		var tg = target;
		//if (tg == target) return false;
		//if (tg.nodeName != 'DIV') return;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if(!reltg)
			return true;
		while (reltg != tg && reltg.nodeName != 'HTML')
			reltg = reltg.parentNode
		if (reltg == tg) return false;
		
		return true;
		
		//return ret;
	
	}


}

WestgateChapel.Website.Timer = Class.create();
WestgateChapel.Website.Timer.prototype = {

	initialize: function(interval, callback, autoReset) {
	
		this.callback = null;
		this.interval = 0;
		this.autoReset = false;
		
		if(interval && typeof(interval) == "number")
			this.interval = interval;
			
		if(callback && typeof(callback) == "function")
			this.callback = callback;
			
		if(autoReset && typeof(autoReset) == "boolean")
			this.autoReset = autoReset;
			
		this._started = false;
	
	},
	
	start: function() {
	
		if(!this._started) {
	
			this._started = true;
		
			if(this.autoReset)
				this._clearID = window.setInterval(this.callback, this.interval);
			else
				this._clearID = window.setTimeout(this.callback, this.interval);
			
		}
	
	},
	
	stop: function(execute) {
	
		if(this._started) {
	
			this._started = false;
		
			if(this.autoReset)
				window.clearInterval(this._clearID);
			else
				window.clearTimeout(this._clearID);
				
			if(execute)
				this.callback();
			
		}
	
	},
	
	reset: function() {
		this.stop();
		this.start();
	}
	
}
new WestgateChapel.Website.Menu($("primaryMenu"));
//Event.observe(window, "load", function() {new WestgateChapel.Website.Menu(document.getElementById("primaryMenu"));});