var activePopupMenu

function popup(p1, p2, p3, p4) {
  if (!this.menuDIV) {
    this.menuDIV = document.createElement('<div style="width:' + this.width + 'px; visibility:hidden; position:absolute; top:5px; left:5px"></div>')
    document.body.appendChild(this.menuDIV)
  }
  activePopupMenu = this
  var html = '<table width="100%" class="DropdownMain" cellspacing="0">'
  var gif, action
  closeOnClick = (this.disableClose) ? '' : '; parent.activePopupMenu.hide()'
  for (var id in this.item) if (!this.item[id].hidden) {
    switch(this.item[id].type) {
      case 'u':
      case 'f':
        action = this.item[id].action
        if (p1) action = action.replace(/%P1%/, p1)
        if (p2) action = action.replace(/%P2%/, p2)
        if (p3) action = action.replace(/%P3%/, p3)
        if (p4) action = action.replace(/%P4%/, p4)
        if (this.item[id].type == 'u') action = 'location.href=\'' + action + '\''
        
        if (this.item[id].gif.substring(0,1) != '/')
            gif = (this.item[id].gif == '') ? '&nbsp;' : '<img src="/WT1Skin/Graphics/' + this.item[id].gif + '">';
        else
            gif = '<img src="' + this.item[id].gif + '">';
            
        html +=
          '<tr' + this.item[id].disabled + ' class="DropdownRow"' +
          ' onmouseover="this.children[0].className=\'DropdownRowHoverLeft\';this.children[1].className=\'DropdownRowHoverRight\'" onmouseout="this.children[0].className=\'DropdownLeft\';this.children[1].className=\'DropdownRight\'"' +
          ' onclick="parent.' + action + closeOnClick + '">' +
          '<td class="DropdownLeft">' + gif + '</td>' +
          '<td nowrap' + this.item[id].emphasized + ' class="DropdownRight">' + this.item[id].text + '</td>' + 
          '</tr>'
        break
      case '-':
        html +=
          '<tr class="DropdownRowSeperator">' +
          '<td class="DropdownLeftSeperator"></td>' +
          '<td class="DropdownRightSeperator"></td>' +
          '</tr>'
        break
    }
  }
  html += '</table>'
  this.menuDIV.innerHTML = html
  var x
  if (this.position == 'right') x = window.event.clientX
  else if (this.position == 'left') x = window.event.clientX - this.width
  else if (this.position == 'center') x = window.event.clientX - this.width/2
  if (x < 0) x = 5;
  var y = window.event.clientY
  
  var ss;
  for (ss = 0; ss < document.styleSheets.length; ss++) {
     if (document.styleSheets[ss].href != '') {
       this.menu.document.createStyleSheet(document.styleSheets[ss].href)
     }
  }
  this.menu.document.body.innerHTML = html
  this.menu.show(x, y, this.width, this.menuDIV.clientHeight, document.body); // inc parm 4 with 25 for each new item
}
    
function disableItem(id) {
  this.item[id].disabled = ' disabled'
}
      
function enableItem(id) {
  this.item[id].disabled = ''
}
  
function hideItem(id) {
  this.item[id].hidden = true
}
  
function showItem(id) {
  this.item[id].hidden = false
}
  
function emphasizeItem(id) {
  this.item[id].emphasized = ' style="font-weight:bold"'
}
  
function normalizeItem(id) {
  this.item[id].emphasized = ''
}
  
function setGif(id, path) {
  this.item[id].gif = path
}

function setAction(id, action) {
  this.item[id].action = action.replace(/'/g, "\'")
}

function menuItem(type, gif, text, action){
  this.type = type
  this.gif = gif
  this.text = text
  this.action = action.replace(/'/g, "\'")
  this.disabled = ''
  this.hidden = false
  this.emphasized = ''
}

function addItem(id, type, gif, text, action) {
  this.item[id] = new menuItem(type, gif, text, action)
}

function disableCloseOnClick() {
  this.disableClose = true
}
  
function enableCloseOnClick() {
  this.disableClose = false
}

function popupMenu(position, width) {
  this.position = position
  this.width = width + 8
  this.disableClose = false
  this.item = new Array()
  this.addItem = addItem
  this.show = popup
  this.enableItem = enableItem
  this.disableItem = disableItem
  this.hideItem = hideItem
  this.showItem = showItem
  this.emphasizeItem = emphasizeItem
  this.normalizeItem = normalizeItem
  this.setGif = setGif
  this.enableCloseOnClick = enableCloseOnClick
  this.disableCloseOnClick = disableCloseOnClick
  this.setAction = setAction
  this.menu = window.createPopup()
  this.hide = this.menu.hide
  this.menuDIV = null
}

