/* 
 *  Copyright(c) 2008-2009, Y.T.Ho & Information Strategies
 *  
 *  All rights reserved. 
 *  Any unauthorized use of this script is strictly prohibited.
 *
 *  http://infst.com
 */
infst(function(){var a=this;this.menu=function(d,e){if(!e){e={}}if(!e.rootMenu){var b=e.parentNode||document.body;if(typeof b=="string"){b=document.getElementById(b)}var c=document.createElement("div");c.className="infstMenu container";if(e.zIndex){c.style.zIndex=e.zIndex}b.appendChild(c);e.menuContainer=c;e.rootMenu=this}this.param=e;this.parentMenu=null;this.parentItem=null;this.rootMenu=e.rootMenu;this.menuContainer=e.menuContainer;this.subMenu=[];this.closed=true;this.isEmpty=false;this.showSubMenuTimeout=null;this.dropShadow=(a.dropShadow)?new a.dropShadow():null;this.onClose=[];this.setData(d)};this.menu.prototype={setData:function(b){if(!b){b=[]}this.init(b)},init:function(b){if(!this.closed){this.close()}this.domObj=document.createElement("div");var c;if(b&&b.length){if(this.param.mainMenuDir=="H"){this.param.mainMenuDir="V";this.domObj.className="infstMenu H";c=this.hMenu(b)}else{this.domObj.className="infstMenu sub";c=this.vMenu(b)}this.domObj.appendChild(c)}else{this.isEmpty=true;this.domObj.className="infstMenu empty"}this._preloadIcon(b);return this.domObj},hMenu:function(e){var d=this;var f=document.createElement("div");for(var b=0;b<e.length;b++){var c=document.createElement("div");c.className="menuItem";f.appendChild(c);if(this.param.showIcon&&e[b].icon){c.appendChild(document.createElement("div"));c.lastChild.className="icon";c.lastChild.style.backgroundImage='url("'+e[b].icon+'")'}c.appendChild(document.createElement("span"));c.lastChild.className=e[b].className;c.lastChild.innerHTML=e[b].innerHTML||e[b].text;a.setUnselectable(c.lastChild);this.itemAddAction(c,e[b]);this.addSubMenu(e[b],c,"V")}return f},vMenu:function(c){var g=this;var k=document.createElement("table");k.setAttribute("cellSpacing",0);k.setAttribute("cellPadding",0);var e=document.createElement("tbody");k.appendChild(e);for(var d=0;d<c.length;d++){var f=document.createElement("tr");f.className="menuItem";e.appendChild(f);if(this.param.showIcon){f.appendChild(document.createElement("td"));f.lastChild.className="icon";f.lastChild.appendChild(document.createElement("div"));if(c[d].icon){f.lastChild.style.backgroundImage='url("'+c[d].icon+'")'}}else{f.appendChild(document.createElement("td"));f.lastChild.className="space";f.lastChild.appendChild(document.createElement("div"))}var j=document.createElement("td");j.className="text";f.appendChild(j);a.setUnselectable(j);var h=document.createElement("td");h.className="arrow";h.appendChild(document.createElement("div"));f.appendChild(h);if(c[d].innerHTML){j.innerHTML=c[d].innerHTML}else{if(c[d].text!=null){j.appendChild(document.createTextNode(c[d].text))}else{f.className="menuItem separator";j.innerHTML='<hr style="width:100%" />';continue}}this.itemAddAction(f,c[d]);if(c[d].disabled){j.className="text disabled"}var b=this.addSubMenu(c[d],f,"H");if(!b.isEmpty){h.className="arrow hasSubMenu"}}return k},itemAddAction:function(d,c){var b=this;if(c.onclick&&!c.disabled){d.onclick=function(){b.rootMenu.closeAll();d.className="menuItem";c.onclick.call(b,c)}}else{if(c.href&&!c.disabled){(function(){var e=c.href;var f=c.target;d.onclick=function(g){b.rootMenu.closeAll();d.className="menuItem";if(f){window.open(e,"_blank","")}else{window.location=e}}})()}else{a.regEvent(d,"click",a.cancelBubble)}}a.regEvent(d,"mousedown",function(e){a.cancelBubble(e)});a.regEvent(d,"mouseover",function(){d.className="menuItem mouseover"});a.regEvent(d,"mouseout",function(){d.className="menuItem"});d.oncontextmenu=function(e){return false}},addSubMenu:function(f,d,c){var e=this;var b=new a.menu(f.subMenu,this.param);b.parentMenu=this;b.parentItem=d;this.subMenu.push(b);(function(){if(e.param.expandOn=="click"){if(!b.isEmpty){d.onclick=null}a.regEvent(d,"click",function(g){b._show(d,c);a.cancelBubble(g)});a.regEvent(d,"mouseover",function(){e.showSubMenuTimeout=setTimeout(function(){b._show(d,c)},300)});a.regEvent(d,"mouseout",function(){clearTimeout(e.showSubMenuTimeout)})}else{a.regEvent(d,"mouseover",function(g){b._show(d,c);a.cancelBubble(g)})}})();return b},show:function(g,f,c,b){if(!this.closed){return}this.closed=false;var d=this;var e=this.menuContainer;e.style.display="block";e.appendChild(this.domObj);e.style.width=this.domObj.offsetWidth+"px";e.style.height=this.domObj.offsetHeight+"px";if(this.dropShadow){this.dropShadow.show(this.domObj)}if(this.param.type=="popup"){if(isNaN(g)){g=0}if(isNaN(f)){f=0}if(isNaN(c)){c=0}if(isNaN(b)){b=0}e.style.position="absolute";var h=this.checkOverflow({top:g,left:f,width:c,height:b},"V");e.style.top=(h.y>0)?g-this.domObj.offsetHeight+"px":g+b+"px";e.style.left=(h.x>0)?f-this.domObj.offsetWidth+c+"px":f+"px"}this.closeAll=function(){if(d.param.type=="popup"){d.close();d.menuContainer.style.display="none";a.delEvent(document,"mousedown",arguments.callee)}else{d.closeAllSubmenu()}};a.regEvent(document,"mousedown",this.closeAll);a.regEvent(window,"resize",this.closeAll)},showUnderCursor:function(b){var c=a.mouseCoords(b);this.show(c.y,c.x)},_show:function(d,b){this.parentMenu.closeAllSubmenu();this.menuContainer.appendChild(this.domObj);var c=this.parentMenu.domObj;var g=(c.nodeType)?{left:c.offsetLeft,top:c.offsetTop}:{left:0,top:0};var h=this.checkOverflow(d,b);var f,e;if(b=="V"){f=(h.x>0)?d.offsetLeft+d.offsetWidth-this.domObj.offsetWidth:d.offsetLeft;e=(h.y>0)?d.offsetTop-this.domObj.offsetHeight+2:d.offsetTop+d.offsetHeight}else{f=(h.x>0)?-(this.domObj.offsetWidth-g.left-4):g.left+d.offsetWidth-1;e=(h.y>0)?(g.top+d.offsetTop+d.offsetHeight-this.domObj.offsetHeight+6):g.top+d.offsetTop}this.domObj.style.left=f+"px";this.domObj.style.top=e+"px";if(this.dropShadow){this.dropShadow.show(this.domObj)}},checkOverflow:function(f,d){var g={};var e=a.getWindowSize();var c=(f.nodeType)?a.elmPos(f):{x:f.left,y:f.top};var b={width:f.offsetWidth||f.width||0,height:f.offsetHeight||f.height||0};if(d=="V"){g.x=(c.x+this.domObj.offsetWidth)-e.width;g.y=(c.y+b.height+this.domObj.offsetHeight)-e.height}else{g.x=(c.x+b.width+this.domObj.offsetWidth)-e.width;g.y=(c.y+this.domObj.offsetHeight)-e.height}return g},close:function(){this.closed=true;if(this.dropShadow){this.dropShadow.hide()}if(this.onClose.length){for(var b=0;b<this.onClose.length;b++){this.onClose[b].call(this)}}this.closeAllSubmenu();if(this.domObj.parentNode){this.domObj.parentNode.removeChild(this.domObj)}},closeAllSubmenu:function(){for(var b=0;b<this.subMenu.length;b++){this.subMenu[b].close()}},closeAll:function(){},appendToButton:function(d,b){var c=this;if(typeof d=="string"){d=document.getElementById(d)}a.regEvent(d,"mousedown",function(e){a.cancelBubble(e)});a.regEvent(d,"click",function(e){var f=a.elmPos(d);c.show(f.y,f.x,d.offsetWidth,d.offsetHeight);a.cancelBubble(e)})},_preloadIcon:function(c){if(!c){return}for(var b=0;b<c.length;b++){if(c[b].icon){a.preloadImg(c[b].icon)}}},regEvent:function(c,b){switch(c){case"close":this.onClose.push(b);break}return this},delEvent:function(d,b){var e=this.onClose;for(var c=0;c<e.length;c++){if(b==e[c]){e.splice(c,1);break}}return this}}});
