var menu_showCounter=0;

function menu_onFirstMenuShow()
{
}

function menu_onLastMenuHide()
{
}

function Menu(caption)
{
  if(!window.menus)
    window.menus = new Array();

  this.items = new Array();

  this.id='m'+window.menus.length;
  this.caption=caption;

  this.addMenuItem = menu_addMenuItem;
  this.writeMenus = menu_writeMenus;
  this.show = menu_show;
  this.hide = menu_hide;
  this.hideSubMenus= menu_hideSubMenus;
  this.layout=menu_layout;
  this.hideAll=menu_hideAll;
  this.restartInactivityTimer=menu_restartInactivityTimer;


  this.activeMenu=null;
  this.setActiveMenu=menu_setActiveMenu;
  this.fullRedraw=menu_fullRedraw;

  this.index=window.menus.length;
  this.group='';

  this.hideTimer=null;
  this.urlRewriter=null;

  this.root=this;
  this.inactivityTimer=null;

  this.itemBackgroundColor=menu_itemBackgroundColor;
  this.itemBackgroundHighlightColor=menu_itemBackgroundHighlightColor;
  this.itemColor=menu_itemColor;
  this.itemHighlightColor=menu_itemHighlightColor;

  if(!window.menus)
    window.menus = new Array();

    window.menus[window.menus.length] = this;
    window.menus[this.id] = this;
}

function menu_addMenuItem(label,href,target)
{
  mi = new MenuItem(label);

  mi.href=href;
  mi.target=target?target:'';

  mi.backgroundColor=this.itemBackgroundColor;
  mi.backgroundHighlightColor=this.itemBackgroundHighlightColor;
  mi.color=this.itemColor;
  mi.highlightColor=this.itemHighlightColor;

  mi.id=this.id+'i'+this.items.length;
  mi.index=this.items.length;
  mi.parent=this;

  this.items[this.items.length] = mi;


  return(mi);
}

function menu_setActiveMenu(menu)
{

  if(this.root.activeMenu!=menu)
  {
    if(this.root.activeMenu)
      this.root.activeMenu.fullRedraw(false);
    this.root.activeMenu=menu;
    this.root.activeMenu.fullRedraw(true);
  }
}

function menu_fullRedraw(active)
{
  for(var i=0;i<this.items.length;i++)
  {
    if(this.items[i].subMenu)
    {
      if(active)
        this.items[i].draw(false);
      else
        this.items[i].draw(this.items[i].subMenu.visible?true:false);
    }
  }
}

function menu_writeMenus()
{
  document.write('<style type="text/css">');
  document.write('.menu_itemtext {'+menu_textStyle+'}');
  document.write('</style>');
  for(var i=0;i<window.menus.length;i++)
    window.menus[i].layout();

  document.onclick = menu_hideAll;

  var i1=new Image();
  i1.src=menu_imgSubMenu;

  var i2=new Image();
  i2.src=menu_imgSubMenuOver;
}

function menu_layout()
{
  var width = eval(menu_menuWidth+2*menu_borderWidth);
  var height = eval(menu_borderWidth);
  for(var i=0;i<this.items.length;i++)
  {
    this.items[i].top=height;
    height+=eval(menu_innerBorderWidth+this.items[i].height*menu_itemHeight);
  }
  height+=eval(menu_borderWidth-menu_innerBorderWidth);

  var color = menu_menuBackgroundColor;

  if(document.layers)
  {
    document.write('<layer id="'+this.id+'" width="'+width+'" height="'+height+'" bgcolor="'+color+'" visibility="hide">abc');
    for(var i=0;i<this.items.length;i++)
      document.write(this.items[i].layout());
    document.write('</layer>');
    this.layer=document.layers[this.id];
  }
  else
  {
    document.write('<div id="'+this.id+'" style="position:absolute;left:0px;top:0px;width:2px;height:2px;background-color:'+color+';visibility:hidden;z-index:10;">');
    document.write('</div>');
    this.layer=document.getElementById(this.id);
  }

  this.layouted=true;
}

function menu_hideAll()
{
  for(var i=0;i<window.menus.length;i++)
    window.menus[i].hide();
}

function menu_show(left,top)
{
  if(!this.layouted)
    this.writeMenus();

  if(!this.itemsLayouted)
  {
    if(!document.layers)
    {
      var content="";
      for(var i=0;i<this.items.length;i++)
        content=content+this.items[i].layout();
      this.layer.innerHTML=content;
    }

    for(i=0;i<this.items.length;i++)
    {
      if(document.layers)
      {
        this.items[i].layer=document.layers[this.id].layers[this.items[i].id];
        this.items[i].layer.captureEvents(Event.MOUSEUP);
      }
      else
      {
        this.items[i].layer=document.getElementById(this.items[i].id);
      }
      this.items[i].layer.menuitem=this.items[i];
      this.items[i].layer.onmouseover=menuitem_mouseOver;
      this.items[i].layer.onmouseout=menuitem_mouseOut;
      this.items[i].layer.onmouseup=menuitem_mouseUp;

      this.items[i].draw(false);
    }
    this.itemsLayouted=true;
  }

  if(this.group!='')
  {
    for(var i=0;i<window.menus.length;i++)
    {
      if(window.menus[i].root!=this.root&&window.menus[i].root.group==this.root.group)
        window.menus[i].hide();
    }
  }

  if(!this.visible)
  {
    menu_showCounter++;
    if(menu_showCounter==1)
      menu_onFirstMenuShow();

    if(document.layers)
    {
      this.layer.left=left;
      this.layer.top=top;
      this.layer.visibility="visible";
    }
    else
    {
      var width = eval(menu_menuWidth+2*menu_borderWidth);
      var height = eval(2*menu_borderWidth-menu_innerBorderWidth);
      for(var i=0;i<this.items.length;i++)
        height+=eval(menu_innerBorderWidth+this.items[i].height*menu_itemHeight);

      this.layer.style.left=left;
      this.layer.style.top=top;
      this.layer.style.width=width;
      this.layer.style.height=height;
      this.layer.style.visibility="visible";
    }

    this.visible=true;
    this.root.restartInactivityTimer();
  }
}

function menu_hide()
{
  if(this.visible)
  {
    if(document.layers)
    {
      this.layer.visibility="hidden";
    }
    else
    {
      this.layer.style.visibility="hidden";
    }
    this.hideSubMenus(true);

    this.visible=false;

    if(this.parent)
      this.parent.draw(false);

    menu_showCounter--;
    if(menu_showCounter==0)
      menu_onLastMenuHide();
  }
}

function menu_hideSubMenus(immediately)
{
  for(var i=0;i<this.items.length;i++)
  {
    if(this.items[i].subMenu&&this.items[i].subMenu.visible)
    {
      this.items[i].subMenu.hideTimer=window.setTimeout('menu_hideSubMenuTimed('+this.items[i].subMenu.index+')',immediately?0:menu_timerSubMenuHide);

      if(this.items[i].subMenuOpenTimer)
      {
        clearTimeout(this.items[i].subMenuOpenTimer);
        this.items[i].subMenuOpenTimer=0;
      }
    }
  }
}

function menu_hideSubMenuTimed(menuIndex)
{
  var menu=window.menus[menuIndex];

  if(menu.hideTimer)
  {
    menu.hide();
    menu.hideTimer=null;
  }
}

function menu_restartInactivityTimer()
{
  if(this.inactivityTimer)
  {
    clearTimeout(this.inactivityTimer);
    this.inactivityTimer=null;
  }

  this.inactivityTimer=setTimeout('window.menus['+this.index+'].hide()',menu_timerInactivity);
}

function MenuItem(caption)
{
  this.layout=menuitem_layout;
  this.caption=caption;
  this.source=menuitem_getSource;
  this.addSubMenu=menuitem_addSubMenu;
  this.showSubMenu=menuitem_showSubMenu;
  this.setHeight=menuitem_setHeight;
  this.draw=menuitem_draw;
  this.htmlPrefix=menuitem_getHtmlPrefix;
  this.htmlSuffix=menuitem_getHtmlSuffix;

  this.height=1;
}

function menuitem_setHeight(height)
{
  this.height=height;
}

function menuitem_mouseOver()
{
  if(this.menuitem.parent.hideTimer)
    this.menuitem.parent.hideTimer=null;

  var mi=this.menuitem;
  mi.parent.root.setActiveMenu(mi.parent);

  mi.draw(true);

  mi.parent.hideSubMenus(false);

  if(mi.subMenu)
  {
    mi.subMenuOpenTimer=setTimeout('menuitem_showSubMenu('+mi.parent.index+','+mi.index+')',menu_timerSubMenuShow);
  }

}

function menuitem_showSubMenu(menuIndex,itemIndex)
{
  mi=window.menus[menuIndex].items[itemIndex];
  mi.subMenuOpenTimer=0;

  if(mi.subMenu)
  {
    var left,top;

    if(document.layers)
    {
      left=parseInt(mi.parent.layer.left)+parseInt(mi.layer.left)+parseInt(mi.layer.clip.width)-menu_itemPadding-menu_imgSubMenuWidth;
      top=parseInt(mi.parent.layer.top)+parseInt(mi.layer.top)-menu_borderWidth;
    }
    else
    {
      left=parseInt(mi.parent.layer.style.left)+parseInt(mi.layer.style.left)+parseInt(mi.layer.style.width)-menu_itemPadding-menu_imgSubMenuWidth;
      top=parseInt(mi.parent.layer.style.top)+parseInt(mi.layer.style.top)-menu_borderWidth;
    }

    mi.subMenu.show(left,top);
  }
}

function menuitem_mouseOut()
{
  var mi=this.menuitem;

  if(mi.subMenuOpenTimer)
  {
    clearTimeout(mi.subMenuOpenTimer);
    mi.subMenuOpenTimer=0;
  }

  mi.draw(false);
}

function menuitem_mouseUp()
{
  var mi=this.menuitem;
  if(mi.href)
  {
    if(mi.parent.urlRewriter)
      loc=mi.parent.urlRewriter(mi.href)
    else
      loc=mi.href;

    if(mi.target=='self')
      self.location.href=loc;
  }
}

function menuitem_getHtmlPrefix()
{
  return('<table border="0" cellpadding="0" cellspacing="0" width="'+menu_menuWidth+'" height="'+eval(this.height*menu_itemHeight)+'"><tr valign="middle"><td width="'+menu_itemPadding+'"><img src="/img/clearpixel.gif" width="'+menu_itemPadding+'" height="1"></td><td class="menu_itemtext" width="'+eval(menu_menuWidth-menu_imgSubMenuWidth-2*menu_itemPadding)+'" id="'+this.id+'_content">');
}

function menuitem_getHtmlSuffix()
{
  if(this.subMenu)
    add='<img name="'+this.id+'_image" id="'+this.id+'_image" src="'+menu_imgSubMenu+'" width="'+menu_imgSubMenuWidth+'" height="'+menu_imgSubMenuWidth+'">';
  else
    add='<img src="/img/clearpixel.gif" width="'+menu_imgSubMenuWidth+'" height="1">';

  return('</td><td width="'+menu_imgSubMenuWidth+'" align="center" valign="middle">'+add+'</td><td width="'+menu_itemPadding+'"><img src="/img/clearpixel.gif" width="'+menu_itemPadding+'" height="1"></td></tr></table>');
}

function menuitem_getSource(active)
{
  if(document.layers)
  {
    if(active)
      return(this.htmlPrefix()+'<font color="'+this.highlightColor+'">'+this.caption+'</font>'+this.htmlSuffix());
    else
      return(this.htmlPrefix()+'<font color="'+this.color+'">'+this.caption+'</font>'+this.htmlSuffix());
  }
  else
  {
    return(this.caption);
  }
}

function menuitem_addSubMenu()
{
  this.subMenu=new Menu();
  this.subMenu.parent=this;
  this.subMenu.root=this.parent.root;

  this.subMenu.itemBackgroundColor=this.backgroundColor;
  this.subMenu.itemBackgroundHighlightColor=this.backgroundHighlightColor;
  this.subMenu.itemColor=this.color;
  this.subMenu.itemHighlightColor=this.highlightColor;

  return(this.subMenu);
}

function menuitem_draw(active)
{
  this.parent.root.restartInactivityTimer();

  stateActive=active;

  if(document.layers)
  {
    this.layer.document.open();
    this.layer.document.write(this.source(stateActive));
    this.layer.document.close();
    this.layer.bgColor=stateActive?this.backgroundHighlightColor:this.backgroundColor;

    if(this.subMenu)
      eval('document.layers[this.parent.id].layers[this.id].document.'+this.id+'_image').src=stateActive?menu_imgSubMenuOver:menu_imgSubMenu;
  }
  else
  {
    this.layer.style.backgroundColor=stateActive?this.backgroundHighlightColor:this.backgroundColor;
    document.getElementById(this.id+'_content').style.color=stateActive?this.highlightColor:this.color;

    if(this.subMenu)
      document.getElementById(this.id+'_image').src=stateActive?menu_imgSubMenuOver:menu_imgSubMenu;
  }
}

function menuitem_layout()
{
  var left = menu_borderWidth;

  var width = eval(menu_menuWidth);
  var height = eval(this.height*menu_itemHeight);
  var color = this.backgroundColor;
  if(document.layers)
    return('<layer id="'+this.id+'" left="'+left+'" top="'+this.top+'" width="'+width+'" height="'+height+'" bgcolor="'+color+'">'+this.source(false)+'</layer>');
  else
    return('<div id="'+this.id+'" style="position:absolute;left:'+left+'px;top:'+this.top+'px;width:'+width+'px;height:'+height+'px;background-color:'+color+'">'+this.htmlPrefix()+this.source(false)+this.htmlSuffix()+'</div>');
}

menu_menuWidth=120;
menu_itemHeight=15;
menu_menuBackgroundColor='#EFF0F1';
menu_itemBackgroundColor='#372E7A';
menu_itemBackgroundHighlightColor='#5A5392';
menu_itemColor='#FFFFFF';
menu_itemHighlightColor='#FFFFFF';
menu_borderWidth=0;
menu_innerBorderWidth=1;
menu_textStyle='font-family:Verdana,Helvetica,sans-serif;font-size:10px;cursor:hand';
menu_itemPadding=5;
menu_imgSubMenu='images/nav.gif';
menu_imgSubMenuOver='images/navover.gif';
menu_imgSubMenuWidth=10;
menu_imgSubMenuHeight=10;
menu_timerSubMenuShow=250;
menu_timerSubMenuHide=250;
menu_timerInactivity=5000;
