//----------------------------------------------------------------------
// Chrome Drop Down Menu
//----------------------------------------------------------------------
//   Author: Dynamic Drive (http://www.dynamicdrive.com)
//   Last updated: June 14th, 2006
//   Modified by: Stanley Xu
//----------------------------------------------------------------------

var ddmHandler = {
	DisappearDelay: 250,    //set delay in miliseconds before menu disappears onmouseout
	DisableMenuClick: true, //when user clicks on a menu item with a drop down menu, disable menu item's link?
	EnableSwipe: 0,         //enable swipe effect? 1 for yes, 0 for no

	//No need to edit beyond here////////////////////////
	MenuObj: null,
	MSIE: document.all,
	FireFox: document.getElementById && !document.all,
	SwipeTimer: "undefined",
	CloseUpMenuHandler: "undefined",
	BottomClip: 0,

	getPosOffset: function(what, offsettype) {
		var totaloffset = (offsettype == "left") ? what.offsetLeft : what.offsetTop;
		var parentEl = what.offsetParent;
		while (parentEl != null) {
			totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
			parentEl = parentEl.offsetParent;
		}
		return totaloffset;
	},

	swipeEffect: function() {
		if (this.BottomClip < parseInt(this.MenuObj.offsetHeight)) {
			this.BottomClip += 10 + (this.BottomClip / 10); //unclip drop down menu visibility gradually
			this.MenuObj.style.clip = "rect(0 auto " + this.BottomClip + "px 0)";
		} else {
			return this.SwipeTimer = setTimeout("ddmHandler.swipeEffect()", 10);
		}
	},

	showHideObj: function(obj, e) {
		if (this.MSIE || this.FireFox) {
			this.MenuObj.style.left = this.MenuObj.style.top = "-500px";
		}
		if (e.type == "click" && obj.visibility == "hidden" || e.type == "mouseover") {
			if (this.EnableSwipe == 1) {
				if (typeof this.SwipeTimer != "undefined") {
					clearTimeout(this.SwipeTimer);
				}
				obj.clip = "rect(0 auto 0 0)"; //hide menu via clipping
				this.BottomClip = 0;
				this.swipeEffect();
			}
			obj.visibility = "visible";
		}
		else if (e.type == "click") {
			obj.visibility = "hidden";
		}
	},

	ieCompatTest: function() {
		return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	},

	clearBrowserEdge: function(obj, whichedge) {
		var edgeoffset = 0;
		if (whichedge == "rightedge") {
			var windowedge = this.MSIE && !window.opera? this.ieCompatTest().scrollLeft + this.ieCompatTest().clientWidth-15 : window.pageXOffset + window.innerWidth - 15;
			this.MenuObj.ContentMeasure = this.MenuObj.offsetWidth;
			if (windowedge - this.MenuObj.x < this.MenuObj.ContentMeasure) {  //move menu to the left?
				edgeoffset = this.MenuObj.ContentMeasure - obj.offsetWidth;
			}
		} else {
			var topedge = this.MSIE && !window.opera? this.ieCompatTest().scrollTop : window.pageYOffset;
			var windowedge = this.MSIE && !window.opera? this.ieCompatTest().scrollTop + this.ieCompatTest().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
			this.MenuObj.ContentMeasure = this.MenuObj.offsetHeight;
			if (windowedge - this.MenuObj.y < this.MenuObj.ContentMeasure) { //move up?
				edgeoffset = this.MenuObj.ContentMeasure + obj.offsetHeight;
				if ((this.MenuObj.y - topedge) < this.MenuObj.ContentMeasure) { //up no good either?
					edgeoffset = this.MenuObj.y + obj.offsetHeight - topedge;
				}
			}
		}
		return edgeoffset;
	},

	closeUpMenu: function(obj, e, dropmenuID) {
		if (this.MenuObj != null) { //hide previous menu
			this.MenuObj.style.visibility = "hidden"; //hide menu
		}
		this.cancelCloseUpMenu();
		if (this.MSIE || this.FireFox) {
			obj.onmouseout = function() {
				ddmHandler.delayCloseUpMenu();
			}
			obj.onclick = function() {
				return !ddmHandler.DisableMenuClick;
			} //disable main menu item link onclick?
			this.MenuObj = document.getElementById(dropmenuID);
			this.MenuObj.onmouseover = function() {
				ddmHandler.cancelCloseUpMenu();
			}
			this.MenuObj.onmouseout = function() {
				ddmHandler.dynamicHideObj(e);
			}
			this.MenuObj.onclick = function() {
				ddmHandler.delayCloseUpMenu();
			}
			this.showHideObj(this.MenuObj.style, e);
			this.MenuObj.x = this.getPosOffset(obj, "left");
			this.MenuObj.y = this.getPosOffset(obj, "top");
			this.MenuObj.style.left = this.MenuObj.x - this.clearBrowserEdge(obj, "rightedge") + "px";
			this.MenuObj.style.top = this.MenuObj.y - this.clearBrowserEdge(obj, "bottomedge") + obj.offsetHeight + 1 + "px";
		}
	},

	containsFireFox: function(a, b) {
		while (b.parentNode) {
			if ((b = b.parentNode) == a) {
				return true;
			}
		}
		return false;
	},

	dynamicHideObj: function(e) {
		var evtobj = window.event ? window.event : e;
		if (this.MSIE && !this.MenuObj.contains(evtobj.toElement)) {
			this.delayCloseUpMenu();
		}
		else if (this.FireFox && e.currentTarget != evtobj.relatedTarget && !this.containsFireFox(evtobj.currentTarget, evtobj.relatedTarget)) {
			this.delayCloseUpMenu();
		}
	},

	delayCloseUpMenu: function() {
		this.CloseUpMenuHandler = setTimeout("ddmHandler.MenuObj.style.visibility='hidden'", this.DisappearDelay); //hide menu
	},

	cancelCloseUpMenu: function() {
		if (this.CloseUpMenuHandler != "undefined") {
			clearTimeout(this.CloseUpMenuHandler);
		}
	},

	startChrome: function() {
		for (var ids = 0; ids < arguments.length; ids++) {
			var menuitems = document.getElementById(arguments[ids]).getElementsByTagName("a");
			for (var i = 0; i < menuitems.length; i++) {
				if (menuitems[i].getAttribute("rel")) {
					menuitems[i].onmouseover = function(e) {
						var event = typeof e != "undefined" ? e : window.event;
						ddmHandler.closeUpMenu(this, event, this.getAttribute("rel"));
					}
				}
			}
		}
	}
}

