document.onmousemove=document.all ?
	     function ()
	     {
		window.posX=window.event.clientX+document.body.scrollLeft;
		window.posY=window.event.clientY+document.body.scrollTop;
	     }:
	     function (e)
	     {
		window.posX=e.pageX;
		window.posY=e.pageY;
	     }

var a_menus=[];

function menus(a_items,p_tpl)
{
 this.a_items=a_items;
 this.p_tpl=p_tpl;
 this.o_root=this;
 this.n_depth=-1;
 this.a_index=[];
 this.n_id=a_menus.length;
 this.x=p_tpl.pos.x;
 this.y=p_tpl.pos.y;
 var i_a=new Image();
 i_a.src=p_tpl.arrow.normal;
 var i_ao=new Image();
 i_ao.src=p_tpl.arrow.over;
 var i_snl=new Image();
 i_snl.src=p_tpl.sel.empty;
 var i_sn=new Image();
 i_sn.src=p_tpl.sel.normal;
 var i_so=new Image();
 i_so.src=p_tpl.sel.over;
 a_menus[this.n_id]=this;
 this.get_styleText=get_styleText;
 new menu(this,a_items);
}

function menu(o_parent,a_items,n_order)
{
 this.n_depth=o_parent.n_depth+1;
 this.a_items=a_items;
 if(!this.a_items) return;
 this.o_parent=o_parent;
 this.o_root=o_parent.o_root;
 this.n_id=this.o_root.a_index.length;
 this.o_root.a_index[this.n_id]=this;
 this.a_children=[];
 this.x=o_parent.x;
 this.y=o_parent.y;
 this.n_cItem=null;
 this.b_visible=false; 
 var n_id,b_tmBorder=false;
 if(o_parent.a_children){
    n_id=this.o_root.n_id+'_'+o_parent.n_id;
    o_parent.a_children[n_order]=this;
    if(!o_parent.n_depth){
       this.x+=document.getElementById('i_'+n_id+'_'+n_order).offsetLeft;
       this.y+=document.getElementById('mn_'+n_id).offsetHeight-1; 
      }
     else{
        var elm=document.getElementById('i_'+n_id+'_'+n_order);
        this.x+=elm.offsetWidth-10;
        this.y+=elm.offsetTop;
       }
   }
 n_id=this.o_root.n_id+'_'+this.n_id;
 if(this.o_root.p_tpl.top_menu_border_on==true) b_tmBorder=true;  
 var str='<div id="mn_'+n_id+'" style="position:absolute;left:'
  +this.x+';top:'+this.y+';z-index:'+(this.n_depth+10)+';visibility:hidden;'+(this.n_depth || b_tmBorder ? 'border:'+this.o_root.p_tpl.top_menu_border : '')+';"><table cellspacing="0" cellpadding="0">';
// var str='<table id="mn_'+n_id+'" cellspacing="0" cellpadding="0" style="position:absolute;left:'
//        +this.x+';top:'+this.y+';z-index:'+(this.n_depth+10)+';visibility:hidden;'+(this.n_depth || b_tmBorder ? 'border:'+this.o_root.p_tpl.top_menu_border : '')+';">';
 var n=0;
 this.n_depth ? n=3 : str+='<tr>';
 this.n_check=-1;
 for(n_order=n;n_order<a_items.length;n_order++){
    if(this.n_depth) str+='<tr>';
    if(this.a_items[n_order][2])
       this.n_check=n_order-n;
    str+='<td id="i_'+n_id+'_'+(n_order-n)+'" nowrap style="'+a_menus[this.o_root.n_id].get_styleText(this,false)+'"'
         +(!this.a_items[n_order][3]?(' onclick="'+(this.n_depth ? 'hide_menu('+this.o_root.n_id+','+this.n_id+','+(n_order-n)+');':'')+(this.a_items[n_order][1]? this.a_items[n_order][1] : 'alert(\'Feature is not implemented yet!\')')+';"'):'')
	 +'>'+(this.n_depth ? '<img id="sel"'+(this.o_root.p_tpl.sel.w ? ' width="'+this.o_root.p_tpl.sel.w+'" ':' ')+(this.o_root.p_tpl.sel.h ? ' height="'+this.o_root.p_tpl.sel.h+'" ':'')+'src="'+(this.a_items[n_order][2] ? this.o_root.p_tpl.sel.normal : this.o_root.p_tpl.sel.empty )+'">':'')
	 +this.a_items[n_order][0];
    if(this.n_depth && this.a_items[n_order][3])
       str+='&nbsp;&nbsp;&nbsp;<img id="arrow"'+(this.o_root.p_tpl.arrow.w ? ' width="'+this.o_root.p_tpl.arrow.w+'" ':' ')+(this.o_root.p_tpl.arrow.h?'height="'+this.o_root.p_tpl.arrow.h+'" ':'')+'src="'+this.o_root.p_tpl.arrow.normal+'">';
     else{
       if(this.n_depth)
          str+='&nbsp;&nbsp;&nbsp;';
      }
     str+='</td>';
     if(this.n_depth) str+='</tr>';
    }
 if(!this.n_depth) str+='</tr>';
 str+='</table></div>';
 document.write(str);
 var elm=document.getElementById('mn_'+n_id);
 if(!this.n_depth){
    this.b_visible=true;
    elm.style.visibility='visible';
   }
 var elm=document.getElementById('mn_'+n_id);
 var x=elm.offsetWidth-10;
 for(n_order=n;n_order<a_items.length;n_order++){
     elm=document.getElementById('i_'+n_id+'_'+(n_order-n));
     elm.o_parent=this;
     elm.onmouseout=item_mouse_out;
     elm.onmouseover=item_mouse_over;
     elm.n_id=n_order-n;
     if(this.a_items[n_order][3]){
        if(this.n_depth){
           elm=elm.getElementsByTagName('img')['arrow'];
           elm.style.visibility='inherit';
           elm.style.position='relative';
           elm.style.left=elm.parentNode.offsetWidth-elm.offsetLeft-elm.offsetWidth-6; 
	  }
        new menu(this,this.a_items[n_order],n_order-n);
       }
    }  
 elm=document.getElementById('mn_'+n_id);
 this.rt=this.x+elm.offsetWidth;
 this.bt=this.y+elm.offsetHeight;
}
function hide_menu(rid,id,c_id)
{
 var menu=a_menus[rid].a_index[id];
 if(menu.n_check!=-1 && c_id!=menu.n_check){
    var tpl=menu.o_root.p_tpl;
    document.getElementById('i_'+rid+'_'+id+'_'+menu.n_check).getElementsByTagName('img')['sel'].src=tpl.sel.empty;
    document.getElementById('i_'+rid+'_'+id+'_'+c_id).getElementsByTagName('img')['sel'].src=tpl.sel.over;
    menu.n_check=c_id;
   }
 while(menu && menu.n_depth>0){
       document.getElementById('mn_'+rid+'_'+menu.n_id).style.visibility='hidden';
       menu=menu.o_parent;               
      }
}
function set_class(n_id,cName)
{
 var elm= document.getElementById(n_id);
 elm.className=cName;
}
function get_styleText(menu,b_over)
{
  var tpl=this.p_tpl;
  var str='font:normal normal normal '+tpl.size+' '+tpl.font+';cursor:'+tpl.cursor
 +';background-color:'+(b_over ? (menu.n_depth ? tpl.bgcolor.pop.over : tpl.bgcolor.main.over):
  (menu.n_depth ? tpl.bgcolor.pop.normal : tpl.bgcolor.main.normal))
 +';color:'+(b_over ? (menu.n_depth ? tpl.color.pop.over : tpl.color.main.over ) :
  (menu.n_depth ? tpl.color.pop.normal : tpl.color.main.normal ))
 +';padding:'+ (b_over ? (menu.n_depth ? tpl.padding.pop.over : tpl.padding.main.over):
  (menu.n_depth ? tpl.padding.pop.normal : tpl.padding.main.normal))
 +';border:'+(b_over ? (menu.n_depth  ? tpl.border.pop.over :  tpl.border.main.over):
  (menu.n_depth  ? tpl.border.pop.normal :  tpl.border.main.normal)); 
  return str;
} 
function set_style(elm,b_over)
{
  var tpl=elm.o_parent.o_root.p_tpl;
  var menu=elm.o_parent;
  var padding=menu.n_depth ? tpl.padding.pop : tpl.padding.main;
  var border=menu.n_depth ? tpl.border.pop : tpl.border.main;
  var bgcolor=menu.n_depth ? tpl.bgcolor.pop : tpl.bgcolor.main;
  var color=menu.n_depth ? tpl.color.pop : tpl.color.main;
  if(b_over){
     elm.style.padding=padding.over;
     elm.style.border=border.over;
     elm.style.backgroundColor=bgcolor.over;
     elm.style.color=color.over;
    }
   else{
      elm.style.padding=padding.normal;
      elm.style.border=border.normal;
      elm.style.backgroundColor=bgcolor.normal;
      elm.style.color=color.normal;
     }
  var elm1;
  if((elm1=elm.getElementsByTagName('img')['arrow']))
     b_over ? elm1.src=tpl.arrow.over:elm1.src=tpl.arrow.normal;
  if((elm1=elm.getElementsByTagName('img')['sel'])){
      if(elm1.src.indexOf('selnull')==-1)
         b_over ? elm1.src=tpl.sel.over:elm1.src=tpl.sel.normal;
}    }
function Continue(e)
{
  if(document.all){
     if(window.event.fromElement)
        if(window.event.fromElement.tagName.toLowerCase()=='img'){
           window.event.cancelBubble=true;
           return false;
          }
     if(window.event.toElement)
        if(window.event.toElement.tagName.toLowerCase()=='img'){
           window.event.cancelBubble=true;
           return false;
          }
    }
  var elm;
  document.all ? elm=window.event.srcElement:elm=e.currentTarget
  if(elm.tagName.toLowerCase()=='img')
     return false;
  return elm;  
}

function expand(n_rid,n_mid,n_cid)
{
  var menu=a_menus[n_rid].a_index[n_mid];
  if(menu.a_children[n_cid]){
     menu.a_children[n_cid].b_visible=true; 
     document.getElementById('mn_'+n_rid+'_'+menu.a_children[n_cid].n_id).style.visibility='visible';
     menu=menu.a_children[n_cid];
     for(var i=0;i<menu.a_items.length-3;i++)
         document.getElementById('i_'+n_rid+'_'+menu.n_id+'_'+i).style.visibility='inherit';
    }
} 

function item_mouse_over(e)
{
  var elm;
  if(!(elm=Continue(e)))
     return;
  var o_parent=elm.o_parent; 
  set_style(elm,true);
  o_parent.n_cItem=elm.n_id;
  setTimeout('expand('+o_parent.o_root.n_id+','+o_parent.n_id+','+o_parent.n_cItem+')',100);
}
function collapse(n_rid,n_mid,n_cid)
{
  var a_menu=a_menus[n_rid];
  var posx=window.posX,posy=window.posY;
  var b_onsubmenu=false,b_onmenu=false,b_onparentmenu=false;
  var n_level=a_menu.a_index[n_mid].n_depth;
  menu=a_menu.a_index[n_mid].a_children[n_cid];
  if(menu){
     if(menu.b_visible){
        if(posx>=menu.x && posx<=menu.rt && posy>=menu.y && posy<=menu.bt)
           b_onsubmenu=true;
       }
    }
 menu=a_menu.a_index[n_mid];
 if(posx>=menu.x && posx<=menu.rt && posy>=menu.y && posy<=menu.bt)
    b_onmenu=true;
 menu=a_menu.a_index[n_mid].o_parent;
 if(menu){
    if(posx>=menu.x && posx<=menu.rt && posy>=menu.y && posy<=menu.bt)
       b_onparentmenu=true;
   }
 var i;
 if(!b_onsubmenu){
    if(!b_onmenu && !b_onparentmenu)
       n_level=0;
    for(i=0;i<a_menu.a_index.length;i++)
        if(a_menu.a_index[i].n_depth > n_level && a_menu.a_index[i].b_visible){
           a_menu.a_index[i].b_visible=false;  
           document.getElementById('mn_'+n_rid+'_'+a_menu.a_index[i].n_id).style.visibility='hidden';
	  }
    if(a_menu.a_index[n_mid].n_depth && !b_onmenu){
       a_menu.a_index[n_mid].b_visible=false;  
       document.getElementById('mn_'+n_rid+'_'+a_menu.a_index[n_mid].n_id).style.visibility='hidden';
      }
   }
}
function item_mouse_out(e)
{
  var elm;
  if(!(elm=Continue(e)))
     return;
  set_style(elm,false);
  setTimeout('collapse('+elm.o_parent.o_root.n_id+','+elm.o_parent.n_id+','+elm.o_parent.n_cItem+')',100);
}

function draw_menuBar(n_menu,n_width)
{
 var elm=document.getElementById('mn_'+n_menu+'_0');
 var x=elm.offsetLeft-2,h=elm.offsetHeight,h1,y=elm.offsetTop;
 var ww=elm.offsetWidth+2;
 if(n_width<ww)
    n_width=ww;
    
 var tpl=a_menus[n_menu].p_tpl;
 var n_id='mnBar_'+n_menu+'_0';
/* var icon;
 if(tpl.icon.src){
    icon=new Image();
    icon.src=tpl.icon.src;
   }   */
 var a_str=tpl.title.split('.');
 var str='<div id="'+n_id+'" style="position:absolute;padding:0px;'+
	 'left:'+x+'px;top:'+y+'px;"><table cellspacing="2" cellpadding="2" bgcolor="'+tpl.menu_bar_bgcolor+'" width="'+n_width+
	 '"><tr bgcolor="'+tpl.menu_bar_bgcolor+'" style="font-weight:bold;font-family:'+tpl.menu_bar_font+';font-size:'+tpl.menu_bar_font_size+';color:'+tpl.menu_bar_color+'">'+
	 (tpl.icon.src ?'<td bgcolor="'+tpl.menu_bar_bgcolor+'" align="left" valign="middle" width="'+(tpl.icon.w+4)+'"><img src="'+tpl.icon.src+'" width="'+tpl.icon.w+'" height="'+tpl.icon.h+'"></td>':'')+'<td nowrap bgcolor="'+tpl.menu_bar_bgcolor+'" align="center" valign="top">'+a_str[0];
 if(a_str.length>1){
    str+='<div style="font-weight:bold;font-size:'+tpl.menu_bar_sfont_size+';text-align:center">';
    for(var i=1;i<a_str.length;i++){
        str+=a_str[i];
        if(i!=a_str.length-1)
           str+='<br>';
       }
    str+='</div>';
   }
 str+='</td></tr></table></div>';
 document.write(str);
 elm=document.getElementById(n_id);
 y+=elm.offsetHeight+1;
 n_id='mnBar_'+n_menu+'_1';
 n_width=elm.offsetWidth;
 var str='<div id="'+n_id+'" style="position:absolute;top:'+(y-1)+'px;left:'+x+'px;"><table cellspacing="0" cellpadding="0" width="'+n_width+'" height="'+(h+4)+'"><tr><td bgcolor="'+tpl.bgcolor.main.normal+'">&nbsp;</td></tr></table></div>';
 document.write(str);
 str='';
 menus=a_menus[n_menu].a_index;
 y=y-document.getElementById('mn_'+n_menu+'_0').offsetTop;
 for(i=0;i<menus.length;i++){
     str+=i+' '+menus[i].x+" "+menus[i].y+"\n";
     elm=document.getElementById('mn_'+n_menu+'_'+i);
     menus[i].y+=y;
     elm.style.top=menus[i].y;
     menus[i].bt=menus[i].y+elm.offsetHeight;
    }
}
