// Copyright (C) 2000 Mats Edlund, Gravity e-learning AB
// new emExplorerMenu("ExampleMenu")

function emExplorerMenu(id) {
  this.id = id + ""
  
  this.contentDiv = null
  this.items = new Array()
  this.all = new Array()
  this.content = ''
  this.activeItem = null
  this.changed = false

  // Default layout values (can be altered manually in .initLayout())
  this.width        = '100%'
  this.liteMode     = true
  this.preHtml      = ''
  this.postHtml     = ''
  this.hSpaceImages = '6'
  this.vSpaceImages = '0'
  this.vSpaceDepth1 = '0'
  this.startHtml    = '<table width="' + this.width + '" border="0" cellspacing="0" cellpadding="0">'
  this.endHtml      = '</table>'
  
  // Default image values (can be altered manually in .initImages())
  this.bkgArr   = new Array('menu_bkg.gif',  '21', '2')
  this.pixelArr = new Array('menu_pixel.gif', '1', '1')
  this.noneArr  = new Array('menu_none.gif',  '9', '9')
  this.plusArr  = new Array('menu_plus.gif',  '9', '9')
  this.minusArr = new Array('menu_minus.gif', '9', '9')
  this.lastArr  = new Array('menu_last.gif',  '9', '9')
  
  // Setup object methods
  this.initLayout = emInitLayout
  this.initImages = emInitImages
  this.initContentObj = emInitContentObj
  this.addItem = emAddItem
  this.build = emBuild
  this.getItem = emGetItem
  this.showItem = emShowItem
  this.toggleItem = emToggleItem
  this.moveItem = emMoveItem
  this.writeContent = emWriteContent
  this.exportItems = emExportItems
  
  // Make a global reference to this instance
  eval(this.id + "=this")
}

// Init layout
function emInitLayout(width, liteMode, preHtml, postHtml, hSpaceImages, vSpaceImages, vSpaceDepth1) {
  if (width != null)        this.width        = width
  if (liteMode != null)     this.liteMode     = liteMode
  if (preHtml != null)      this.preHtml      = preHtml
  if (postHtml != null)     this.postHtml     = postHtml
  if (hSpaceImages != null) this.hSpaceImages = hSpaceImages
  if (vSpaceImages != null) this.vSpaceImages = vSpaceImages
  if (vSpaceDepth1 != null) this.vSpaceDepth1 = vSpaceDepth1
  this.startHtml = '<table width="' + this.width + '" border="0" cellspacing="0" cellpadding="0">'
  this.endHtml   = '</table>'
}

// Init images
function emInitImages(bkgArr, pixelArr, noneArr, plusArr, minusArr, lastArr) {
  if (bkgArr != null)   this.bkgArr   = bkgArr
  if (pixelArr != null) this.pixelArr = pixelArr
  if (noneArr != null)  this.noneArr  = noneArr
  if (plusArr != null)  this.plusArr  = plusArr
  if (minusArr != null) this.minusArr = minusArr
  if (lastArr != null)  this.lastArr  = lastArr  
}

// Init ContentDiv
function emInitContentObj(contentDiv) {
  // Set content div to .write() to 
  this.contentDiv = contentDiv
}

// Add Item
// ExampleMenu.addItem("strId", "strParentId_or_null", "strName", "strAltText", "strOnClickScript_or_null", "strOnDblClickScript_or_null", arrImage_or_null)
function emAddItem(strId, strParentId, strName, strAltText, strOnClick, strOnDblClick, arrImage) {  
  if (strParentId == null || strParentId == '' || strParentId == strId || strParentId == 0) {
    // New main item - create new item
    this.items[this.items.length] = new emiExplorerMenuItem(strId, strParentId, strName, strAltText, strOnClick, strOnDblClick, arrImage, this, true)
  } else {
    //New sub item - call the items .addItem()
    parentObj = this.getItem(strParentId)
    if (parentObj != null) parentObj.addItem(strId, strName, strAltText, strOnClick, strOnDblClick, arrImage)
  }
}

function emBuild(emItem) {
  var outStr, i
  
  outStr = this.preHtml + this.startHtml

  for (i=0; i<this.items.length; i++) {
    if (this.liteMode && (emItem != null)) {
      if (this.items[i] != emItem.top) this.items[i].showChildren = false
    }
    outStr += this.items[i].build()
  }

  outStr += this.endHtml + this.postHtml
  return outStr
}

function emGetItem(id) {
  for (var i = 0; i < this.all.length; i++) {
    if (this.all[i].id == id) return this.all[i]
  }
  return null
}

function emShowItem(id, showTf, dblClick) {
  var emItem = this.getItem(id)
  this.toggleItem(id, emItem, showTf)
  if (emItem != null) emItem.goLocation(dblClick)
  return void(0)
}

function emToggleItem(id, emItem, showTf) {
  if (emItem == null) emItem = this.getItem(id)
  if (emItem == null) {
    this.activeItem = null
    this.writeContent(this.build())
  } else {
    this.activeItem = emItem
    if (emItem.isParent) emItem.showChildren = (showTf == null) ? !emItem.showChildren : showTf
    if (emItem.depth > 1) emItem.parent.toggleShowChildren(true, true)    
    this.writeContent(this.build(emItem))
    window.focus()
    if (is.ie && is.win && document.getElementById) {
      tmpHrefObj = document.getElementById('href_' + this.id + '_' + emItem.id)
      if (tmpHrefObj != null) tmpHrefObj.focus()
    }
  }
  return void(0)
}

function emMoveItem(id, moveType) {
  var emItem = this.getItem(id)
  if (emItem != null) {

    if (moveType == "orderUp") {
      itemsArr = (emItem.depth == 1) ? this.items : emItem.parent.items
      if (emItem == itemsArr[0]) {
        if (window.playSound != null) playSound('beep')
        return void(0)
      }
      for (i=1; i<itemsArr.length; i++) {
        if (emItem == itemsArr[i]) {
          itemsArr.splice(i, 1)
          itemsArr.splice(i-1, 0, emItem)
          break
        }
      }
          
    } else if (moveType == "orderDown") {
      itemsArr = (emItem.depth == 1) ? this.items : emItem.parent.items
      if (emItem == itemsArr[itemsArr.length-1]) {
        if (window.playSound != null) playSound('beep')
        return void(0)
      }
      for (i=0; i<itemsArr.length-1; i++) {
        if (emItem == itemsArr[i]) {
          itemsArr.splice(i, 1)
          itemsArr.splice(i+1, 0, emItem)
          break
        }
      }
  
    } else if (moveType == "levelUp") {
      if (emItem.depth == 1) {
        if (window.playSound != null) playSound('beep')
        return void(0)
      }
      itemsArrOld = emItem.parent.items
      itemsArrNew = (emItem.depth == 2) ? this.items : emItem.parent.parent.items
      for (i=0; i<itemsArrOld.length; i++) {
        if (emItem == itemsArrOld[i]) {
          itemsArrOld.splice(i, 1)
          if (itemsArrOld.length == 0) {
            emItem.parent.isParent = false
            emItem.parent.items = null
          }
          emItem.parent = (emItem.depth == 2) ? emItem : emItem.parent.parent
          emItem.changeDepth(-1)
          itemsArrNew[itemsArrNew.length] = emItem
          break
        }
      }
  
    } else if (moveType == "levelDown") {
      itemsArrOld = (emItem.depth == 1) ? this.items : emItem.parent.items
      if (emItem == itemsArrOld[0]) {
        if (window.playSound != null) playSound('beep')
        return void(0)
      }
      for (i=1; i<itemsArrOld.length; i++) {
        if (emItem == itemsArrOld[i]) {
          itemsArrOld.splice(i, 1)
          if (itemsArrOld.length == 0) {
            emItem.parent.isParent = false
            emItem.parent.items = null
          }
          emItem.parent = itemsArrOld[i-1]
          emItem.changeDepth(1)
          if (itemsArrOld[i-1].isParent) {
            itemsArrOld[i-1].items[itemsArrOld[i-1].items.length] = emItem
          } else {
            itemsArrOld[i-1].items = new Array(emItem)
            itemsArrOld[i-1].isParent = true
          }
          itemsArrOld[i-1].showChildren = true
          break
        }
      }
    }
    this.changed = true
    this.toggleItem(id, emItem, emItem.showChildren)
  }
  return void(0)
}

function emWriteContent(newContent) {
  if (this.content != newContent) {
    this.content = newContent
    //alert(newContent)
    if (this.contentDiv != null) this.contentDiv.write(newContent)
  }
}

function emExportItems() {
  var outStr, i, delimStr
  if (!this.changed) {
    if (window.playSound != null) playSound('beep')
    return void(0)
  }
  outStr = ''
  delimStr = ''
  for (i=0; i<this.items.length; i++) {
    outStr += delimStr + this.items[i].exportItems('')
    delimStr = ','
  }
  return outStr
}


// EXPLORER MENU ITEM /////////////////////////////////////////////////////////////

function emiExplorerMenuItem(id, parent, name, desc, onclick, ondblclick, imageArr, emObj, depth1) {
  this.id = id + ""
  this.name = name
  this.desc = desc
  this.onclick = (onclick == null) ? '' : onclick
  this.ondblclick = (ondblclick == null) ? '' : ondblclick
  this.imageArr = (imageArr == null) ? null : imageArr
  this.emObj = emObj

  if (depth1) {
    this.depth = 1
    this.parent = this
    this.top = this
  } else {
    this.parent = parent
    this.top = this.parent.top
    this.depth = this.parent.depth + 1
  }
  this.showChildren = false
  this.isParent = false
  this.items = null
  
  this.addItem = emiAddItem
  this.display = emiDisplay
  this.goLocation = emiGoLocation
  this.build = emiBuild
  this.isLastItem = emiIsLastItem
  this.getParent = emiGetParent
  this.toggleShowChildren = emiToggleShowChildren
  this.changeDepth = emiChangeDepth
  this.exportItems = emiExportItems

  emObj.all[emObj.all.length] = this

  this.obj = "emi_" + emObj.id + "_" + id
  eval(this.obj + "=this")
}

function emiAddItem(id, name, desc, onclick, ondblclick, imageArr) {
  if (this.isParent) {
    this.items[this.items.length] = new emiExplorerMenuItem(id, this, name, desc, onclick, ondblclick, imageArr, this.emObj, false)
  } else {
    this.items = new Array(new emiExplorerMenuItem(id, this, name, desc, onclick, ondblclick, imageArr, this.emObj, false))
    this.isParent = true
  }
}

function emiDisplay() {
  if (this.depth == 1) return true
  if (!this.parent.showChildren) return false
  return this.parent.display()
}

function emiBuild() {
  var outStr, cols, col, colItem, colStr
  var imgArr, imgLinkStart, imgLinkEnd, tdStr
  var depth1Str, classStr, classDepthStr
  var imgStr, imgWstr, imgHstr, hrefStr
  var itemCount

  cols = this.depth
  colStr = ''

  for (col=0; col<cols; col++) {
    if (col < cols-1) {
      colItem = this.getParent(cols-col-1)
      colStr += '<td' + getTdBkg(colItem) + '><img src="' + this.emObj.pixelArr[0] + '" width="1" height="1" hspace="' + this.emObj.hSpaceImages + '" border="0"></td>'
    } else {
      imgArr = this.emObj.noneArr
      imgLinkStart = ''
      tdStr = ''
      imgLinkEnd = ''
      if (this.isParent) {
        imgArr = (this.showChildren) ? this.emObj.minusArr : this.emObj.plusArr
        imgLinkStart = '<a href="javascript:' + this.emObj.id + '.toggleItem(\'' + this.id + '\')" onMouseOver="top.status=\' \';return true" onMouseOut="top.status=\'\';return true">'
        imgLinkEnd = '</a>'
      }

      if (this.isLastItem()) {
        if (!this.isParent) imgArr = this.emObj.lastArr
        tdStr = ' valign="top"'
      }
    
      colStr += '<td' + getTdBkg(this) + tdStr + '>' + 
                imgLinkStart + '<img src="' + imgArr[0] + '" width="' + imgArr[1] + '" height="' + imgArr[2] + 
                '" vspace="' + this.emObj.vSpaceImages + '" hspace="' + this.emObj.hSpaceImages + '" border="0">' + imgLinkEnd + '</td>'
    }
  }

  depth1Str = ''
  if ((this.emObj.vSpaceDepth1 != '0') && (this.depth == 1) && (this != this.emObj.items[0])) {
    depth1Str = '<tr><td background="' + this.emObj.bkgArr[0] + '">' +
                '<img src="' + this.emObj.pixelArr[0] + '" width="1" height="1" vspace="' + this.emObj.vSpaceDepth1 + '" border="0"></td>' +
                '<td class="' + classStr + '"><img src="' + this.emObj.pixelArr[0] + '" width="1" height="1" border="0"></td>' +
                '<td width="' + this.emObj.width + '" colspan="' + (cols-1) + '">' + 
                '<img src="' + this.emObj.pixelArr[0] + '" width="1" height="1" border="0"></td></tr>'
  }

  classDepthStr = 'emi_' + this.emObj.id + '_Depth' + this.depth
  
  classStr = 'emi_' + this.emObj.id
  if (this.emObj.activeItem != null) {
    if (this.id == this.emObj.activeItem.id) classStr += '_Active'
  }
  
  hrefStr = '<a href="javascript:' +
           this.emObj.id + '.showItem(\'' + this.id + '\',true)" onDblClick="' + 
           this.emObj.id + '.showItem(\'' + this.id + '\',true,true)" onMouseOver="top.status=\'' +
           this.desc + ' \';return true" onMouseOut="top.status=\'\';return true" title="' +
           this.desc + '" alt="' + this.desc + '"'
  
  if (this.imageArr == null) {  
    imgStr = '<img src="' + this.emObj.pixelArr[0] + '" width="2" height="1" border="0">'
  } else {
    imgWstr = ((this.imageArr[1] != null) && (this.imageArr[1] != '')) ? ' width="' + this.imageArr[1] + '"' : ''
    imgHstr = ((this.imageArr[2] != null) && (this.imageArr[2] != '')) ? ' height="' + this.imageArr[2] + '"' : ''
    imgStr = '<img src="' + this.imageArr[0] + '"' + imgWstr + imgHstr + ' hspace="2" border="0">'
  }
  
  outStr = this.emObj.startHtml + depth1Str + '<tr>' + colStr + '<td class="' + classStr +
           '">' + hrefStr + '>' + imgStr + '</a></td>' + 
           '<td class="' + classStr + '" width="' + this.emObj.width + '" xnowrap>' +
           hrefStr + ' id="href_' + this.emObj.id + '_' + this.id + '">' +
           '<span class="' + classDepthStr + '"><span class="' + classStr + '">' +  this.name + '</span></span></a>' +
           '</td></tr>' + this.emObj.endHtml + '\n'
  
  if (this.isParent && this.showChildren) {
    for (itemCount=0; itemCount<this.items.length; itemCount++) {
      outStr += this.items[itemCount].build()
    }
  }

  return outStr
}

function emiGoLocation(dblClick) {
  if (dblClick) {
    if (this.ondblclick != '') eval(this.ondblclick)
  } else {
    if (this.onclick != '') eval(this.onclick)
  }
  return void(0)
}

function emiIsLastItem() {
  if (this.depth == 1) return (this == this.emObj.items[this.emObj.items.length-1])
  return (this == this.parent.items[this.parent.items.length-1])
}

function emiGetParent(upLevel) {
  if (this.depth > upLevel) {
    if (upLevel <= 0) {
      return this
    } else if (upLevel == 1) {
      return this.parent
    } else {
      return this.parent.getParent(upLevel-1)  
    }
  }
  return null
}

function emiToggleShowChildren(bolShowChildren, bolUpRecursive) {
  if (bolUpRecursive && this.depth > 1) this.parent.toggleShowChildren(bolShowChildren, bolUpRecursive)
  this.showChildren = bolShowChildren
}

function emiChangeDepth(changeBy) {
  var itemCount
  this.depth = this.depth + changeBy
  if (this.isParent) {
    for (itemCount=0; itemCount<this.items.length; itemCount++) {
      this.items[itemCount].changeDepth(changeBy)
    }
  }
}

function emiExportItems(exportStr) {
  var delimStr, itemCount
  delimStr = (exportStr == '') ? '' : ','
  exportStr += delimStr + this.id + ':' + this.parent.id
  if (this.isParent) {
    for (itemCount=0; itemCount<this.items.length; itemCount++) {
      exportStr = this.items[itemCount].exportItems(exportStr)
    }
  }
  return exportStr
}


// INDEPENDENT FUNCTIONS //////////////////////////////////////////

function getTdBkg(tdItem) {
  if (tdItem == null) return ''
  if (tdItem.isLastItem()) return ''
  return ' background="' + tdItem.emObj.bkgArr[0] + '"'
}

function preloadImages() {
  if (document.images) {
    var imgFiles, i, j
    imgFiles = preloadImages.arguments
    if (document.preloadArray==null) document.preloadArray = new Array()
    i = document.preloadArray.length
    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#") {
      preloadArray[i] = new Image
      preloadArray[i++].src = imgFiles[j]
    }
  }
}