/*======================================
	Author : Fason 
	Emal	: fason_pfx@hotmail.com
	HomePage : http://fason.nease.net
======================================*/

var MenuConfig = {
	imagePath:					"skins/default/images/",
	moreRightIcon:				"",
	moreDownIcon:				"",
	defaultMenuCss:				"MenuItem",
	defaultMenuHoverCss:		"hover",
	defaultTopMenuCss:			"TopMenuItem",
	defaultTopMenuHoverCss:		"TopHover",
	defaultText:				"New",
	defaultTarget:				"_self",
	defaultDirection:			"vertical", //"horizontal"
	defaultWidth:				100,
	defaultHeight:				22,
	showDelay:					0,
	hideDelay:					0,
	useMask:					true
};

var MenuHandler = {
	callOverItem:		function(id) { this[id].overItem(); },
	callOutItem:		function(id) { this[id].outItem(); },
	callClickItem:		function(id) { return this[id].clickItem(); }
}

var MenuItem = (function () {
	//**
	var ua = window.navigator.userAgent;
	var ie = /msie/ig.test(ua);
	var ie55up = /msie (5\.5|6)/ig.test(ua);
	var index = 0, sMenuPrefix = "MenuItem", sFrame = "Frame";

	function getId(o) {
		MenuHandler[++index] = o;
		return index;
	};

	function HTMLtoText(s) {
		return s.replace(/&/g, '&amp;').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	};

	function isFolder(oThis) {
		return oThis.childNodes.length > 0;
	};

	function isHorizontal(oThis) {
		return oThis.direction == "horizontal";
	};

	function getRoot(oThis) {
		var o = oThis;
		while (o.parentNode) {
			if (o.parentNode.constructor != MenuItem) break;
			o = o.parentNode;
		}
		return o;
	};

	function setCss(el, sCss) {
		el.className = sCss;
	};

	function setVisible(oThis, v) {
		var o = oThis.getItem();
		var f = oThis.getFrame();
		oThis.visible = v;
		if (ie55up) {
			if (o.filters[0]) {
				o.filters[0].Stop();
				o.filters[0].Apply();
				o.style.visibility = v ? "visible" : "hidden";
				o.filters[0].Play();
			} else {
				o.style.visibility = v ? "visible" : "hidden";
			}
		} else {
			o.style.visibility = v ? "visible" : "hidden";
		}
		if (f) f.style.display = v ? "" : "none";
	};

	function getFunctionHandler(oThis, fnc) {
		var argu = arguments;
		for (var i=2,tmp=[]; i<argu.length; i++) 
			tmp[i-2] = "argu[" + i + "]";
		return (function(){ 
			if (typeof(fnc.apply) == 'function') return fnc.apply(oThis, tmp);
			else {
				oThis.constructor.prototype._caller = fnc;
				eval("var r=oThis._caller(" + tmp.join(",") + ");");
				return r;
			}
		});
	};

	//** Class Start **//
	function xItem(sText, sHref, sWidth, isTop, sTarget, sIcon, sHeight, sMenuCss, sMenuHoverCss) {
		this.id = getId(this);
		this.level = 0;
		this.order = 0;
		this.text = sText || MenuConfig.defaultText;
		this.href = sHref;
		this.target = sHref ? (sTarget ? sTarget : MenuConfig.defaultTarget) : "_self";
		this.icon = sIcon;
		this.isTop = isTop ? isTop : false;
		if(isTop){
			this.css = sMenuCss || MenuConfig.defaultTopMenuCss;
			this.hoverCss = sMenuHoverCss || MenuConfig.defaultTopMenuHoverCss;
		}
		else{
			this.css = sMenuCss || MenuConfig.defaultMenuCss;
			this.hoverCss = sMenuHoverCss || MenuConfig.defaultMenuHoverCss;
		}
		this.visible = false;
		this.direction = MenuConfig.defaultDirection;
		this.width = sWidth || MenuConfig.defaultWidth;
		this.height = sHeight || MenuConfig.defaultHeight;
		this.parentNode = null;
		this.childNodes = new Array();
	};

	xItem.prototype.add = function (oItem) {
		var len = this.childNodes.length;
		this.childNodes[len] = oItem;
		oItem.parentNode = this;
		oItem.order = len;
		return oItem;
	};

	xItem.prototype.overItem = function () {
		if (!this.visible) return;
		window.clearTimeout(this.bar._hideTimer);
		for (var i=0; i<this.parentNode.childNodes.length; i++) {
			var ch = this.parentNode.childNodes[i].childNodes;
			if (ch.length > 0 && i != this.order) {
				if (ch[0].visible) {
					this.parentNode.childNodes[i].hide(this.level);break;
				}
			}
		}
		this.hide(this.level + 1);
		var showHandler = getFunctionHandler(this, this.show);
		this.bar._showTimer = window.setTimeout(showHandler, MenuConfig.showDelay);
		setCss(this.getItem(), this.css + " " + this.hoverCss);
	};

	xItem.prototype.outItem = function () {
		window.clearTimeout(this.bar._showTimer);
		var hideHandler = getFunctionHandler(this,this.hideAll);
		this.bar._hideTimer = window.setTimeout(hideHandler, MenuConfig.hideDelay);		
		if (isFolder(this)){
			if (! this.childNodes[0].visible) {
				setCss(this.getItem(), this.css);
			}
		} else {
			setCss(this.getItem(), this.css);
		}
	};

	xItem.prototype.clickItem = function () {
		if (typeof this.href == "function") {
			this.href();
			return  true;
		} else {
			if (this.href) return true;
		}
		return false;
	};

	xItem.prototype.getItem = function () {
		return document.getElementById(sMenuPrefix + this.id);
	};
	xItem.prototype.getFrame = function () {
		return document.getElementById(sFrame + this.id);
	};
	xItem.prototype.getBoundary = function () {
		var el = this.getItem();
		return {
			"left":		el.offsetLeft,
			"top":		el.offsetTop,
			"width":	el.offsetWidth,
			"height":	el.offsetHeight
		}
	};

	xItem.prototype.show = function () {
		var docwidth = ie ? document.body.clientWidth : window.innerWidth;
		var scrollLeft = ie ? document.body.scrollLeft : window.pageXOffset;
		for (var j=0; j<this.childNodes.length; j++) {
			var oThis = this.childNodes[j], pBoundary = this.getBoundary(), cBoundary = oThis.getBoundary();
			var f = oThis.getFrame();
			if (j) var fBoundary = this.childNodes[j - 1].getBoundary();
			var w = (oThis.level > 1 || isHorizontal(oThis)) ? (pBoundary.width -5): 0;
			var h = (oThis.level > 1 || isHorizontal(oThis)) ? 0 : (pBoundary.height);
			var x= oThis.level ? ((pBoundary.left + w)) : (oThis.order ? (fBoundary.left + fBoundary.width) : 0);
			var y = oThis.level ? (oThis.order ? (fBoundary.top + fBoundary.height) : (pBoundary.top + h)) : (oThis.order ? fBoundary.top : 0);
			if (cBoundary.width + x - scrollLeft - docwidth > 0) {
				x = oThis.level > 1 ? (pBoundary.left - cBoundary.width + 5) : (docwidth + scrollLeft - cBoundary.width);
				if (x - scrollLeft<0) { x = scrollLeft;}
			}
			oThis.getItem().style.left = x + "px";
			oThis.getItem().style.top = y + "px";
			if (f) {
				f.style.left = x + "px";
				f.style.top = y + "px";
			}
			setVisible(oThis, true);
		}
	};

	xItem.prototype.hide = function (v) {
		var ch =  this.childNodes;
		for (var i=0; i<ch.length; i++) { ch[i].hide(v); }
		if (this.level > v && this.visible) {
			setVisible(this, false);
		}
		if (this.level > -1) setCss(this.getItem(), this.css);	
	};

	xItem.prototype.hideAll = function () {
		var o = getRoot(this);
		o.hide(0);
	};

	xItem.prototype.toString = function () {
		this.visible = ! this.level;
		this.direction = this.parentNode.direction;
		if (this.level && this.order || isHorizontal(this)) 
			this.width = this.parentNode.childNodes[0].width;
		if (this.level) this.bar = this.parentNode.bar;
		var sHtml = '<a id="' +
					sMenuPrefix + this.id + '" class="' + 
					this.css + '" href="' +
					this.href + '" target="' +
					this.target + '" unselectable="on" tabindex="-1" onmouseover="MenuHandler.callOverItem(' +
					this.id + ')" onmouseout="MenuHandler.callOutItem(' +
					this.id + ')"  onclick="return MenuHandler.callClickItem(' +
					this.id + ')" oncontextmenu="return true" hideFocus="true" style="position:absolute;width:' + 
					this.width + 'px;height:' +
					this.height + 'px;z-index:' +
					(this.level + 9999) + ';visibility:' +
					(this.visible ? "visible" : "hidden") +'"><div id="MenuItem-text' +
					this.id + '" style="padding:3px 5px 3px 5px" nowrap>' +
					HTMLtoText(this.text) + '</div>' +
					(this.icon ? ('<img src="' + this.icon + '" style="position:absolute;border:0px;width:16px;height:16px;top:' +
					Math.floor((this.height - 16)/2) + ';left:3px;" align="absmiddle">') : '' ) +
					(isFolder(this) ? ('<img src="' + (MenuConfig.imagePath + 
					(isHorizontal(this) ? MenuConfig.moreRightIcon : (! this.level ? MenuConfig.moreDownIcon : MenuConfig.moreRightIcon))) + 
					'" style="position:absolute;border:0px;width:10px;height:7px;top:' +
					Math.floor((this.height - 7)/2) + 'px;left:' +
					(this.width - 15) +'px;">') : '') + '</a>' +
					(ie55up && MenuConfig.useMask ? '<iframe id="' +
					sFrame + this.id + '" frameborder=0 style="position:absolute;width:' +
					this.width + 'px;height:' +
					this.height + 'px;filter:alpha(opacity=0);z-index:0"></iframe>' : '');	
		for (var i=0; i<this.childNodes.length; i++) {
			this.childNodes[i].level = this.level +1;
			sHtml += this.childNodes[i].toString();
		};
		return sHtml;
	};

	return xItem;
})();

var MenuBar = (function () {
	//**
	var dom = document.getElementById ? true : false;
	var ie = /msie/ig.test(window.navigator.userAgent);

	if (!dom) {
		alert('The Menu needs DOM support!');
		return null;
	}

	function insertHTMLAfterBegin(oElement, sHTML) {
		if (ie) {
			oElement.insertAdjacentHTML("afterBegin", sHTML);
		}
		else {
			var r = oElement.ownerDocument.createRange();
			r.selectNodeContents(oElement);
			r.collapse(true);
			var df = r.createContextualFragment(sHTML);
			oElement.insertBefore(df, oElement.firstChild);
		}
	};

	function addEventListener(el, sHandler, fnc) {
		if (document.all && el.attachEvent) {
			el.attachEvent(sHandler, fnc);
		} else {
			el.addEventListener(sHandler.replace(/on/i, ''), fnc);
		}
	};

	function initialize(oThis) {
		for (var i=0; i<oThis.childNodes.length; i++){
			var c = oThis.childNodes[i], cBoundary = oThis.childNodes[( i ? i -1 : 0)].getBoundary();
			c.getItem().style.left =  oThis.direction == "horizontal" ? "" : (c.order ? (cBoundary.left + cBoundary.width - 1) : "");
			c.getItem().style.top = oThis.direction == "horizontal" ? (c.order ? (cBoundary.top + cBoundary.height - 1) : "") : "";
		}
	};

	// ** bar Return **//
	function bar() {
		this.left = 0;
		this.top = 0;
		this.direction = MenuConfig.defaultDirection;
		this.childNodes = new Array();
		this._showTimeout = null;
		this._hideTimeout = null;
	};

	bar.prototype.add = function (oItem) {
		var len = this.childNodes.length;
		this.childNodes[len] = oItem;
		oItem.parentNode = this;
		oItem.order = len;
		oItem.bar = this;
		return oItem;
	};

	bar.prototype.write = function (oTarget) {
		var sHtml = '';
		for (var i=0; i<this.childNodes.length; i++)
			sHtml += this.childNodes[i].toString();
		//insertHTMLAfterBegin(document.body, sHtml);
		insertHTMLAfterBegin(oTarget, sHtml);
		initialize(this);
	};

	return bar;
})();

