//-----------------------------------------------------------------------
// Module name   : lmenu.js
// Author        :
//
//   AnyLink Drop Down Menu- © Dynamic Drive (www.dynamicdrive.com)
//   This notice MUST stay intact for legal use
//   Visit http://www.dynamicdrive.com/ for full source code
//
//   Made into a Class by Paul Battersby
//
// Creation Date : Sep 21/06
// Description   :
//  This module contains routines to implement a drop down menu and/or
//  a side menu. Because this menu is a class, multiple menus are possible
//  within the same page. Also note that the drop down menu automatically
//  adjusts itself to respect the page borders. In othwords, the drop down
//  menu will shift itself to the left if there is not enough room for it
//  to the right without venturing off the edge of the browser window.
//
//  To use this menu, include the following in the header of your page:
//
//  <link href="lmenu.css" rel="stylesheet" type="text/css">
//  <script type="text/javascript" src="lmenu.js"></script>
//  <script type="text/javascript">
//    // Contents for menu 1
//    var menu1=new Array()
//    menu1[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a>'
//    menu1[1]='<a href="http://www.freewarejava.com">Freewarejava.com</a>'
//    menu1[2]='<a href="http://codingforums.com">Coding Forums</a>'
//    menu1[3]='<a href="http://www.cssdrive.com">CSS Drive</a>'
//
//    // Contents for menu 2, and so on
//    var menu2=new Array()
//    menu2[0]='<a href="http://cnn.com">CNN</a>'
//    menu2[1]='<a href="http://msnbc.com">MSNBC</a>'
//    menu2[2]='<a href="http://news.bbc.co.uk">BBC News</a>'
//
//    var myMenu = new Lmenu("myMenu");
//  </script>
//
//  In the body, include this line:
//
//    <div id="dropmenudiv_myMenu" onMouseover="myMenu.clearhidemenu()" onMouseout="myMenu.dynamichide(event)"></div>
//
//  Note: in the above "myMenu" MUST match the name given to the menu object
//        in the "new Lmenu()" declaration
//
//
//  Place the main menu links anywhere you like and fill them in like this:
//
//    <a href="#" onClick="return myMenu.clickreturnvalue()" onMouseover="myMenu.dropdownmenu(this, event, menu1, '150px',0,0)" onMouseout="myMenu.delayhidemenu()">Web Design</a> |
//    <a href="#" onClick="return myMenu.clickreturnvalue()" onMouseover="myMenu.dropdownmenu(this, event, menu2, '150px',0,0)" onMouseout="myMenu.delayhidemenu()">Newest Sites</a> |
//    <a href="#" onClick="return myMenu.dropdownmenu(this, event, menu3, '200px',0,0)" onMouseout="myMenu.delayhidemenu()">News Sites</a> (onclick)
//
//  The first two links will display the drop down menu when the mouse hovers above the main menu
//  title.
//
//  The third link provides an example that will only display the drop down menu when the
//  user clicks on the title.
//
//  By default, the drop down menu will appear direcly below, and aligned to the left edge
//  of the menu title. Since "dropdownmenu" accepts an x,y offset it is possible, by using the
//  offset values to reposition the menu to appear beside the menu title, thus creating popout
//  side menu (will probably want a negative y offset and an x offset equal to the width of the
//  menu.
//
//  The width, colour, font of the menu can be configured by editing Lmenu.css
//
// $Log: lmenu.js $
// Revision 1.1  2006-09-22 11:35:29-04  Battersby
// Initial revision
//
//
//------------------------------------------------------------------------*/

/*---------------------------- INCLUDE FILES ----------------------------*/


/*----------------------------- CONSTANTS -------------------------------*/
Lmenu.disappeardelay = 250;  //menu disappear speed onMouseout (in miliseconds)

/*----------------------------- VARIABLES -------------------------------*/


/*----------------------------- FUNCTIONS -------------------------------*/
function Lmenu(menuName) {
  this.objName = menuName;

  Lmenu.ie4 = document.all;
  Lmenu.ns6 = document.getElementById&&!document.all;

  document.onclick=this.hidemenu;
}


/////No further editting needed


Lmenu.getposOffset = function(what, offsettype) {
  var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
  var parentEl=what.offsetParent;
  while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}


Lmenu.prototype.showhide = function(obj, e, visible, hidden, menuwidth){
  if (Lmenu.ie4||Lmenu.ns6)
    this.dropmenuobj.style.left=this.dropmenuobj.style.top="-500px"

  if (menuwidth!=""){
    this.dropmenuobj.widthobj=this.dropmenuobj.style
    this.dropmenuobj.widthobj.width=menuwidth
  }
  if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
    obj.visibility=visible
  else if (e.type=="click")
    obj.visibility=hidden
}

Lmenu.iecompattest = function() {
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

Lmenu.prototype.clearbrowseredge = function(obj, whichedge) {
  var edgeoffset=0
  if (whichedge=="rightedge"){
    var windowedge=Lmenu.ie4 && !window.opera? Lmenu.iecompattest().scrollLeft+Lmenu.iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
    this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetWidth
    if (windowedge-this.dropmenuobj.x < this.dropmenuobj.contentmeasure)
      edgeoffset=this.dropmenuobj.contentmeasure-obj.offsetWidth
  } else {
    var topedge=Lmenu.ie4 && !window.opera? Lmenu.iecompattest().scrollTop : window.pageYOffset
    var windowedge=Lmenu.ie4 && !window.opera? Lmenu.iecompattest().scrollTop+Lmenu.iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    this.dropmenuobj.contentmeasure=this.dropmenuobj.offsetHeight
    if (windowedge-this.dropmenuobj.y < this.dropmenuobj.contentmeasure){ //move up?
      edgeoffset=this.dropmenuobj.contentmeasure+obj.offsetHeight
    if ((this.dropmenuobj.y-topedge)<this.dropmenuobj.contentmeasure) //up no good either?
      edgeoffset=this.dropmenuobj.y+obj.offsetHeight-topedge
    }
  }
  return edgeoffset
}

Lmenu.prototype.populatemenu = function(what) {
  if (Lmenu.ie4||Lmenu.ns6)
    this.dropmenuobj.innerHTML=what.join("")
}


//------------------------------------------------------------------------------
// obj          - always set to the keyword:  this
// e            - always set to the keyword:  event
// menuContents - an array containing <a href></a> strings for the dropdown menu
// menuwidth    - the width in pixels (example '150px') of this drop down menu
// xoffset      - how far right to shift the menu (0 = left aligned with title)
// yoffset      - how far down  to shift the menu (0 = directly below the title)
//------------------------------------------------------------------------------
Lmenu.prototype.dropdownmenu = function(obj, e, menucontents, menuwidth, xoffset,yoffset) {
  if (window.event) event.cancelBubble=true
  else if (e.stopPropagation) e.stopPropagation()
    this.clearhidemenu()

  this.dropmenuobj=document.getElementById? document.getElementById("dropmenudiv_" + this.objName) : dropmenudiv
  this.populatemenu(menucontents)

  if (Lmenu.ie4||Lmenu.ns6){
    this.showhide(this.dropmenuobj.style, e, "visible", "hidden", menuwidth)
    this.dropmenuobj.x=Lmenu.getposOffset(obj, "left") + xoffset;
    this.dropmenuobj.y=Lmenu.getposOffset(obj, "top") + yoffset;
    this.dropmenuobj.style.left=this.dropmenuobj.x-this.clearbrowseredge(obj, "rightedge")+"px"
    this.dropmenuobj.style.top=this.dropmenuobj.y-this.clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
  }

  return Lmenu.clickreturnvalue()
}

Lmenu.clickreturnvalue = function() {
  if (Lmenu.ie4||Lmenu.ns6) return false
  else return true
}

Lmenu.contains_ns6 = function(a, b) {
  while (b.parentNode)
    if ((b = b.parentNode) == a)
      return true;

  return false;
}

Lmenu.prototype.dynamichide =  function(e){
  if (Lmenu.ie4&&!this.dropmenuobj.contains(e.toElement))
    this.delayhidemenu()
  else if (Lmenu.ns6&&e.currentTarget!= e.relatedTarget&& !Lmenu.contains_ns6(e.currentTarget, e.relatedTarget))
    this.delayhidemenu()
}

Lmenu.prototype.hidemenu = function(e){
  if (typeof this.dropmenuobj!="undefined"){
    if (Lmenu.ie4||Lmenu.ns6)
      this.dropmenuobj.style.visibility="hidden"
  }
}

Lmenu.prototype.delayhidemenu = function(){
  if (Lmenu.ie4||Lmenu.ns6)
    this.delayhide=setTimeout(this.objName + ".hidemenu()",Lmenu.disappeardelay)
}

Lmenu.prototype.clearhidemenu = function() {
  if (typeof this.delayhide!="undefined")
    clearTimeout(this.delayhide)
}

