/**
 * Itomic's Suckerfish
 *
 * @copyright Itomic Pty Ltd
 * @link http://www.itomic.com.au/
 * @param HTMLULElement menuElement
 * @param Object opts
 * 
 * Options are:
 *    int timeout (in milliseconds)
 */
SuckerFish = function(menu, opts) { this.SuckerFish(menu, opts); }
SuckerFish.prototype = {
	timeout: 0,
	lastElement: null,
	
	SuckerFish: function(menuElm, opts) {
		var sfEls = menuElm.getElementsByTagName("LI");
		var me = this;
		
		// Apply our options
		if (opts) {
			if (opts.timeout) this.timeout = opts.timeout;
		}
		
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function(e) { me.hover(this, e); };
			sfEls[i].onmouseout=function(e) { me.unhover(this, e); };
		}
	},
	
	hover: function(elm, e) {
		var me = this;
		// Clear any timeouts on this element
		var timeout = elm.getAttribute('timed');
		if (timeout) clearTimeout(timeout);
		elm.getAttribute('timed', false);
		
		// Clear any last elements that may be active
		if (me.lastElement) this.hide(me.lastElement);
		me.lastElement = elm;
		// Show this element
		this.show(elm);
	},
	
	unhover: function(elm, e) {
		// Set a timeout on this element to hide it
		var me = this;
		var timeout = setTimeout(function(e) {me.hide(elm);}, me.timeout);
		elm.setAttribute('timed', timeout);
	},
	
	show: function(elm) {
		elm.className += ' sfhover';
	},
	
	hide: function(elm) {
		elm.className = elm.className.replace(/(^| )sfhover/igm, "");
		//alert("'"+elm.className+"'");
	}
}

if (window.attachEvent) window.attachEvent("onload", function(e){ new SuckerFish(document.getElementById("nav")); });
	else window.addEventListener('load', function(e){ new SuckerFish(document.getElementById("nav")); }, false);
