/**
 * dropnav.js
 * eFashionSolutions' Meteor JavaScript Library
 * 2008.11.28
 * TJ Eastmond <tj@efashionsolutions.com>
 */

// Encapsulate
(function() {

	// Don't edit anything unless I say you can!

	var dropDownLiSelector = 'ul#dropDownNav li'; // I guess you can change this, but why?
	//var dropDownLiSelector = 'ul.navGroup li'; // I guess you can change this, but why?
	//var dropDownLiSelector = 'li#panetop-nav_full-28 ul.navList li'; // I guess you can change this, but why?

	function setUpDropDowns() {

		// If the right markup is in place, lets start attaching events
		$A($$(dropDownLiSelector)).each(function(element) {
			var ul = element.getElementsByTagName('ul');
			if (ul.length > 0) {
				Event.observe(element, 'mouseover', function(e) {
					if (mouseLeaveEnter(e, this)) { dropNavOpening(e); $(ul[0]).show(); element.addClassName('open'); dropNavOpened(e); }
				});

				Event.observe(element, 'mouseout', function(e) {
					if (mouseLeaveEnter(e, this)) { dropNavClosing(e); $(ul[0]).hide(); element.removeClassName('open'); dropNavClosed(e); }
				});
			}
		});
	}

	// Incase the on DOM Ready method isn't set
	if (typeof Event.onReady != 'function') {
		Object.extend(Event, {
			onReady : function(f) {
				if(document.body) f();
				else document.observe('dom:loaded', f);
			}
		});
	}

	// True check of mouse coming and going; fixes IE flicker bug
	var mouseLeaveEnter = function(e, handler) {
		var t = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
		while (t && t != handler) { t = t.parentNode; }
		return (t != handler);
	};

	// You can edit now...but only by added something special to the next four functions

	var dropNavOpening = function(event) {
		// do something special
	};

	var dropNavClosing = function(event) {
		// do something special
	};

	var dropNavOpened = function(event) {
		// do something special
	};

	var dropNavClosed = function(event) {
		// do something special
	};

	// OK, stop now

	// Attach events as soon as the DOM is loaded
	Event.onReady(setUpDropDowns);

})();
