function DropdownMenu(param_prepend) {
  this.param_prepend = param_prepend
  this.links = []
  this.link_ids = []
  this.root_link_ids = []
  this.current_id=0
  this.mouse_on_menu=0
  this.hidden_elements = []
  this.level2_div = document.body.appendChild(document.createElement("div"))
  this.level2_div.id= this.param_prepend + "_level_2"
  this.level2_div.style.position="absolute";
  this.level2_div.style.visibility="hidden"
  addEventListener(this.level2_div,"mouseover", (function(dm) { return function(ev) {
    dm.menu2MouseOver(ev)
  }})(this))
  addEventListener(this.level2_div,"mouseout", (function(dm) { return function(ev) {
      dm.menu2MouseOut(ev)
    }})(this))
}
DropdownMenu.prototype.addLink = function(str_parent_id,str_id, str_link, str_label, 
    str_target, str_css_class, b_html_link) {
  var menu_link
  if(str_parent_id=="") {
    menu_link = new MenuLink(str_id, str_link, str_label,1, str_target, str_css_class, b_html_link)
    this.root_link_ids.push(str_id)
  } else {
    var _parent_id = this.getMenuLinkOffset(str_parent_id)
    if (b_html_link == null) {
      b_html_link = this.links[_parent_id].html_link
    }
    menu_link = new MenuLink(str_id, str_link, str_label,this.links[_parent_id].level+1, str_target, str_css_class, b_html_link)
    this.links[_parent_id].children.push(str_id)
  }
  this.links.push(menu_link)
  this.link_ids.push(str_id)
}
DropdownMenu.prototype.setLevel1Menu = function(dMenu1) {
  addEventListener(dMenu1,"mouseover", (function(dm) { return function(ev) {
      dm.menu1MouseOver(ev)
    }})(this))
  addEventListener(dMenu1,"mouseout", (function(dm) { return function(ev) {
      dm.menu1MouseOut(ev)
    }})(this))
}
DropdownMenu.prototype.menu1MouseOver = function(e) {
  var it_prep
  this.mouse_on_menu=1
  var tg = (e.srcElement) ? e.srcElement : e.target;
  var str_prepend_item = this.param_prepend + "_item_"
  while (tg.nodeName.toLowerCase() != "body") {
    if (("id" in tg) && (tg.id != null) && (tg.id != "")) {
      it_prep = tg.id.slice(0,str_prepend_item.length)
      if(it_prep == str_prepend_item) {
        var tid=tg.id.slice(str_prepend_item.length)
        if(tid.slice(tid.length-5)=="_link") {
          tid = tid.slice(0,tid.length-5)
        }
        this.setSubMenus(tid)
        break
      } else {
        tg = tg.parentNode
      }
    } else {
      tg = tg.parentNode
    }

  }
}
DropdownMenu.prototype.menu1MouseOut = function(e) {
  var tg = (e.srcElement) ? e.srcElement : e.target;
  var str_prepend_item = this.param_prepend + "_item_"
  if(tg.id==this.param_prepend +"_level_1") {
    this.mouse_on_menu=0
    this.current_id=""
    window.setTimeout("hideSubMenus('" + this.param_prepend + "');",1000);
  } 
}
DropdownMenu.prototype.menu2MouseOver = function(e) {
  this.mouse_on_menu=1
}
DropdownMenu.prototype.menu2MouseOut = function(e) {
  e = e ? e : window.event;
  tg = (window.event) ? e.srcElement : e.target;
  var str_prepend_item = this.param_prepend + "_item_"
  if(tg.id==this.param_prepend +"_level_2") {
    this.mouse_on_menu=0
    this.current_id=""
    window.setTimeout((function(dd) { return function() {
      dd.hideSubMenus()
    }})(this),1000);
  } 
}

DropdownMenu.prototype.setSubMenus = function(str_id) {
  var rootlink=this.links[this.getMenuLinkOffset(str_id)]
  if(rootlink.children.length < 1) {
    this.current_id = str_id
    this.level2_div.style.visibility='hidden'
  } else if (str_id != this.current_id) {
    var i
    var idiv
    var ilink
    var itxt
    var lev1_span=_(this.param_prepend + '_item_' + str_id)
    this.level2_div.style.left=getPosition(lev1_span).left + "px"
    this.level2_div.style.top=(getPosition(lev1_span).top + lev1_span.offsetHeight) + "px"
    this.level2_div.style.visibility='visible'
    while(this.level2_div.childNodes.length >0){
      this.level2_div.removeChild(this.level2_div.firstChild)
    }
    this.current_id = str_id
    for (i=0; i<rootlink.children.length; i++) {    
      lnk = this.links[this.getMenuLinkOffset(rootlink.children[i])]
      idiv = document.createElement("div")
      idiv.id = this.param_prepend + "_item_" + str_id
      ilink = document.createElement("a")
      ilink.href = lnk.link
      ilink.target = lnk.target
      idiv.className = lnk.css_class
      ilink.id = this.param_prepend + "_lnk_" + str_id
      if(lnk.html_link) {
        ilink.innerHTML = lnk.label
      } else {
        itxt = document.createTextNode(lnk.label)
        ilink.appendChild(itxt)
      }
      idiv.appendChild(ilink)
      this.level2_div.appendChild(idiv)
    }
    if (document.all && (this.hidden_elements.length==0)) {
      this.hideElements("select")
      this.hideElements("iframe")
    }
  }
}
DropdownMenu.prototype.hideElements = function(sElementName) {
  var flds = document.all.tags(sElementName);
  var fld
  for (var i=0, j=flds.length; i<j; i++) {
    fld = flds[i]
    if(fld.style.visibility!='hidden'){
      fld.style.visibility='hidden'
        this.hidden_elements.push(fld)
    }
  }  
}
DropdownMenu.prototype.getMenuLinkOffset = function(str_id) {
  var i
  ret = 0
  for(i=0; i<this.links.length; i++) {
    if (this.link_ids[i]==str_id) {
      ret=i
      break
    }
  }
  return ret
}
DropdownMenu.prototype.hideSubMenus = function() {
  if(this.mouse_on_menu==0) {
    this.level2_div.style.visibility='hidden'
    var fld
    while(this.hidden_elements.length>0) {
      fld=this.hidden_elements.pop()
      fld.style.visibility='visible'
    }
  }
}

function MenuLink(str_id, str_link, str_label, int_level, str_target, str_css_class, b_html_link) {
  this.level=int_level
  this.id=str_id
  this.link=str_link
  this.label=str_label
  this.target=str_target
  this.css_class=str_css_class
  this.active=0
  this.children = new Array()
  this.html_link = (b_html_link!=null) ? b_html_link : false;   
} 


