// Copyright 1999-2006 eSolutions Limited
// Obtain written permission before using any part of this code from info@esol-group.com.

// Support: IE6, NS7.0, NS7.1, Safari, Mozilla
//   In order to support NS7.0 the Parent of menu should be a absolute DIV
// Not support: MAC IE

/*******************************************************************************
  PopMenu class
*******************************************************************************/
function PopMenu(parentId,MenuArray,cell_w,cell_h,cell_p,border){

  if (typeof cell_w == 'undefined' || cell_w==null) cell_w=200
  if (typeof cell_h == 'undefined' || cell_h==null) cell_h=19
  if (typeof cell_p == 'undefined' || cell_p==null) cell_p=8
  if (typeof border == 'undefined' || border==null) border=1

  this.cellWidth = cell_w
  this.cellHeight = cell_h
  this.cellPadding = cell_p
  this.border = border
  this.spacerImage = '/sys/1x1.gif'

  /*****************************************************************************
   * Create and return menu item
   * Style className used  
   *   menu0  - 1st level, no child
   *   menu0c - 1st level, have child
   *   menu1  - 2nd+ level, no child
   *   menu1c - 2nd+ level, have child
   *   ??????_on - same 4 styles above in mouseover state              
   ****************************************************************************/    
  this.createItem = function(menu_level,num,DivData,x,y,w,h,padding,haveChild,isLast){
    var tempObj = document.createElement('DIV')
    tempObj.className = 'menu' + (menu_level==0?'0':'1') + (haveChild?'c':'')
    tempObj.offclass = tempObj.className
    tempObj.onclass = tempObj.offclass + "_on"

    var tempArray = (''+DivData).split('|')
    
    //Set left padding and menu title
    tempObj.innerHTML = '<img src="'+this.spacerImage+'" align=absmiddle height='+(h-1-this.border)+' width='+padding+'>' + tempArray[0]
    //storing debugging msg, not neccessary
    //tempObj.value = tempArray[0] 

    if (menu_level==0){
      tempObj.style.width = w
      tempObj.style.height = h-1
      tempObj.style.position = 'static'
      if(tempArray.length>2 && tempArray[2]!='')
        tempObj.style.width = parseInt(tempArray[2])
    }else{
      tempObj.style.position = 'absolute'
      tempObj.style.width = w
      tempObj.style.top = num * (h-Math.max(this.border,1))
      if(tempArray.length>2 && tempArray[2]!='')
        tempObj.style.width = parseInt(tempArray[2])
      if(this.border>0){
        if(menu_level>1){
          tempObj.style.marginLeft = -this.border
        }
        tempObj.style.marginTop = -this.border
        tempObj.style.borderTopWidth = this.border
        tempObj.style.borderTopStyle = 'solid'
        tempObj.style.borderLeftWidth = this.border
        tempObj.style.borderLeftStyle = 'solid'
        tempObj.style.borderRightWidth = this.border
        tempObj.style.borderRightStyle = 'solid'
        if(isLast){
          tempObj.style.borderBottomWidth = this.border
          tempObj.style.borderBottomStyle ='solid'
        }
      }
    }

    // If a hyperlink is specified, set the onclick event
    if ((tempArray[1]!="") && (tempArray[1]!=null)) { 

      var agent = navigator.userAgent.toLowerCase()
      var is_ie5 = (agent.indexOf('msie 5')!=-1)
      if(typeof tempObj.style == 'undefined')
        tempObj.style.cursor = new Object()
      tempObj.style.cursor = is_ie5 ? 'hand' : 'pointer'

      // evt for firefox, window.event for ie, what about other browsers?
      tempObj.onclick = function(evt) {
        if ((tempArray[1].toLowerCase()).indexOf('javascript:')>=0){
          eval(tempArray[1])
        }else{
          window.location = tempArray[1]
        }
        if(typeof window.event != 'undefined') evt = window.event
        evt.cancelBubble = true 
      }

    }

    // mouseover change the style
    tempObj.onmouseover = function() {
      //msg.appendMessage("mouseover:" + this.value); 
      tempObj.className = tempObj.onclass 
      if (tempObj.lastChild.tagName=='DIV')
        tempObj.lastChild.style.visibility='visible'
    }

    // mouseout change the style
    tempObj.onmouseout = function() {
      //msg.appendMessage("mouseout:" + this.value);
      tempObj.className = tempObj.offclass
      if (tempObj.lastChild.tagName=='DIV')
        tempObj.lastChild.style.visibility='hidden'
    }
    
    return(tempObj) // return the HTML Obj created
  }


  /*****************************************************************************
   * Create 1 level of menu
   ****************************************************************************/     
  this.createMenu = function(ParentObj,Level,SubArray,cx){
    var divHolder = document.createElement('DIV')

    divHolder.style.position = Level==0 ? 'static' : 'absolute' 
    divHolder.style.visibility = 'hidden'
    if(Level==1)
      divHolder.style.top = ParentObj.offsetTop // the parent is static and have an offsetTop value
    else if(Level>1)
      divHolder.style.top = 0 // the parent is absolute this child should align top with the parent

    ParentObj.appendChild(divHolder)
    
    for (var i=0; i<SubArray.length; i++) {
      var fx = 0 // fx=cx
      var fy = 0 // fy=pos_y
      if(Level>0){
        fx = ParentObj.offsetWidth - (Level==1 ? 0 : 1) // minus border for Level > 1
        if (isNaN(fx))
          fx=this.cellWidth // cell_w
      }
      divHolder.style.left = fx
      if (SubArray[i].length>1) {  // With submenus
        sParentObj = divHolder.appendChild(this.createItem(Level,i,SubArray[i][0],fx,fy,this.cellWidth,this.cellHeight,this.cellPadding,true,(i==SubArray.length-1)))
        this.createMenu(sParentObj,Level+1,SubArray[i][1],fx)
      }else{   // Without submenus
        divHolder.appendChild(this.createItem(Level,i,SubArray[i],fx,fy,this.cellWidth,this.cellHeight,this.cellPadding,false,(i==SubArray.length-1)))
      }
    }
  }
  
  /*****************************************************************************
    Create the entire menu
  *****************************************************************************/  
  this.init = function(){
    var root = document.getElementById(parentId)
    if(root == null) return false
    this.createMenu(root, 0, MenuArray)
  }
}