var CgMenu = {

	delay: 100,
	menuArray: null, 
	menuOn: null,
	ie: document.all, 
	ff: document.getElementById && !document.all,
	
	
	getX: function(obj) {
		var offset = obj.offsetLeft;
		var parent = obj.offsetParent;
		while (parent != null) {
			offset += parent.offsetLeft;
			parent = parent.offsetParent;
		}
		return offset;
	},

	getY: function(obj) {
		var offset = obj.offsetTop;
		var parent = obj.offsetParent;
		while (parent != null) {
			offset += parent.offsetTop;
			parent = parent.offsetParent;
		}
		return offset;
	},
	
	getAttribute: function(obj, attr) {
		var attr = attr.toLowerCase();
		if (obj != null && obj.attributes && obj.attributes.length > 0) {
			for (var i = 0; i < obj.attributes.length; ++i)
				if (obj.attributes[i].nodeName.toLowerCase() == attr)
					return obj.attributes[i].nodeValue;
		}
		return null;		
	},
	
	getMenu: function(level) {
		if (this.menuArray != null && this.menuArray.length > level)
			return this.menuArray[level];
		else
			return null;
	},

	toggleVis: function(style, e) {
		if (this.ie || this.ff)
			this.menuOn.style.left = this.menuOn.style.top = "-500px";
		if (e.type == "click" && style.visibility == "hidden" || e.type == "mouseover")
			style.visibility = "visible";
		else if (e.type == "click")
			style.visibility = "hidden";
	},

	ieCompat: function() {
		return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	},

	clearEdge: function(obj, edge) {
		var offset = 0;
		if (edge == "right") {
			var winEdge = this.ie && !window.opera ? 
				this.ieCompat().scrollLeft + this.ieCompat().clientWidth - 15 : 
				window.pageXOffset + window.innerWidth - 15;
			this.menuOn.contentWidth = this.menuOn.offsetWidth;
			if (winEdge - this.menuOn.x < this.menuOn.contentWidth)
				offset = this.menuOn.contentWidth - obj.offsetWidth;
		}
		else {
			var topEdge = this.ie && !window.opera ? this.ieCompat().scrollTop : window.pageYOffset;
			var winEdge = this.ie && !window.opera ? this.ieCompat().scrollTop + this.ieCompat().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
			this.menuOn.contentWidth = this.menuOn.offsetHeight;
			if (winEdge - this.menuOn.y < this.menuOn.contentWidth) {
				offset = this.menuOn.contentWidth + obj.offsetHeight;
				if  (this.menuOn.y - topEdge < this.menuOn.contentWidth)
					offset = this.menuOn.y + obj.offsetHeight - topEdge;
			}
		}
		return offset;
	},
	
	contains: function(a, b) {
		if (a == null || b == null || a.nodeName.toLowerCase() == 'body')
			return false;
		if (a == b)
			return true;
		if (this.ie)
			return a.contains(b);
		while (b.parentNode && b.parentNode.nodeName.toLowerCase() != 'body') {
			if ( (b = b.parentNode) == a )
				return true;
		}
		return false;
	},

	show: function(obj, e, id, level) {
		if (this.menuArray == null)
			this.menuArray = new Array();
						
		this.clearDelayHide();

		// delayHide all the menus above level
		var len = this.menuArray.length;
		for (var i = level; i < len; ++i) {
			this.menuArray.pop().style.visibility = 'hidden';	
		}

		if (this.ie || this.ff) {
			obj.onmouseout = function() { CgMenu.menuMouseOut(level, e); };
			this.menuOn = document.getElementById(id);								

			this.toggleVis(this.menuOn.style, e);
			
			this.menuOn.onmouseover = function() { CgMenu.clearDelayHide(); };
			this.menuOn.onmouseout = function() { CgMenu.delayHide(0); };
			this.menuOn.onclick = function() { CgMenu.menuOnClick(e); };
			this.menuOn.x = this.getX(obj);
			this.menuOn.y = this.getY(obj);
			this.menuOn.style.left = this.menuOn.x - this.clearEdge(obj, "right") + (level > 0 ? obj.offsetWidth + 1 : 0) + "px";
			this.menuOn.style.top = this.menuOn.y - this.clearEdge(obj, "bottom") + (level == 0 ? obj.offsetHeight + 3 : 0) + "px";
			this.menuOn.style.zIndex = 100 + level;
			
			this.menuArray.push(this.menuOn);
		}
	},
	
	menuOnClick: function(e) {
		var e = window.event ? window.event : e;
		var from = e.srcElement ? e.srcElement : e.target;
	},

	menuMouseOut: function(level, e) {		
		var e = window.event ? window.event : e;
		var from = e.srcElement ? e.srcElement : e.target;
		var to = e.toElement ? e.toElement : e.relatedTarget;
		
		if ( from == null || to == null || this.contains(from, to) || this.contains(to, from) || this.contains(this.menuOn, to) )
			return;
			
		var attr = this.getAttribute(to, "onmouseover");
		if (attr != null && attr.indexOf("CgMenu") != -1)
			return;
			
		this.delayHide(level);
	},

	delayHide: function(level) {
		if (this.menuArray != null && this.menuArray.length > level)
		{
			var exp = "";
			for (var i = level; i < this.menuArray.length; ++i) {
				exp += "if (CgMenu.menuArray[" + i + "]) CgMenu.menuArray[" + i + "].style.visibility = 'hidden'; ";
			}
			this.menuDelay = setTimeout(exp, this.delay);
		}
	},

	clearDelayHide:function() {
		if (this.menuDelay != "undefined")
			clearTimeout(this.menuDelay)
	}
}