/***********************************************************************************************

Javascript vertical cascading menu class

(c) Tim Little 2004
************************************************************************************************/

function _mmAddItem (pDescription, pType, pDest) {
   var itemDesc = new Array ();
   
   itemDesc ['Description'] = pDescription;
   itemDesc ['Type'] = pType;
   itemDesc ['Dest'] = pDest;
   
   this.itemList[this.itemCount++] = itemDesc;
   }

function _mmShowSub (pSub, pParent) {
   var x;
   var y;

   y = getRealTop (pParent);
   x = getRealLeft(pParent) + pParent.offsetWidth;
   
   pSub.show ( y , x);
   
   this.childMenu = pSub;
   }

function _mmShow (pTop, pLeft) {

   objDiv = document.getElementById('div' + this.name);

   if (!this.drawn) {
      var sTable;
      var counter;
      var itemDesc;
      
      
      sTable = '<TABLE border=1 cellpadding=0 CLASS=MENUTABLE >'
      
      for (counter = 0; counter < this.itemCount; counter++) {
         
         itemDesc = this.itemList[counter];
         
         if (itemDesc ['Type'] == '') {
            sTable += '<TR><TD align=left >'
         }
         else {
            sTable += '<TR><TD align=left onMouseOver="javascript:' + this.name + '.showSub (' + itemDesc ["Type"].name + ', this)" onMouseOut="javascript:' + this.name + '.hide(event)" >';
            
         }

         sTable += '<A href=' + itemDesc['Dest'] + ' ><NOBR>' + itemDesc['Description'].replace( "'", "&#39;");
         
         if (itemDesc ['Type'] != '') {
            sTable += '<img src="images/foldoutmenu_arrow.gif" border=0/>';
         }
         sTable += '</A></TD></TR>';
      }
      
      sTable += '</TABLE>';
     
      //eval ('div' + this.name + '.style.posTop = ' + pTop );
      //eval ('div' + this.name + '.style.posLeft = ' + pLeft );

      
      objDiv .style.top = pTop;
      objDiv.style.left = pLeft;
      objDiv.innerHTML = sTable;

      
      this.drawn = true;
      
      //alert (pTop + ', ' + pLeft);
   }
   
   objDiv.style.visibility = 'visible';
   objDiv.style.display = ''
   
   /*
   eval ("div" + this.name + ".style.visibility = 'visible'" );
   eval ("div" + this.name + ".style.display = ''" );
   */
   }

function _mmMouseOff (e) {
   var hidden;
   var objDiv;
   var hiddenChild;
   var relTarg;
   
   
   hidden = false;
   hiddenChild = true;
   //objDiv = document.all['div' + this.name];
   objDiv = document.getElementById('div' + this.name);
 
 
 if (this.childMenu != '') {
 	hiddenChild = this.childMenu.hide (e);
 	}
  
   //alert ('top: ' + objDiv.offsetLeft + '\nleft ' + objDiv.offsetTop + '\nwidth '  + objDiv.offsetWidth + '\nheight '  + objDiv.offsetHeight);
   
   if (!checkCoords (objDiv, e.clientX, e.clientY) ) {
      hidden = hiddenChild;
//      alert (hidden);
      
      if (hidden) {
         objDiv.style.display = 'none';
         this.childMenu = ''
      }
   }
   
   return (hidden);
}


function MyMenu (pName) {
   this.name = pName;
   this.itemList = new Array();
   this.itemCount = 0;
   this.drawn = false;
   
   this.addItem = _mmAddItem;
   this.show = _mmShow;
   this.showSub = _mmShowSub;
   this.hide = _mmMouseOff;
   this.childMenu = '';
   
   document.writeln ('<DIV ALIGN="left" ID="div' + pName + '" CLASS="DROPDOWN" onmouseout="topObj.hide(event)" ></DIV>');
   
}

/****************************************************

Helper functions

****************************************************/

var topObj = null;

function dropDown (pMenu, pTop, pLeft) {
   topObj = pMenu;
   
   pMenu.show (pTop, pLeft)
}

function checkCoords (pObject, pX, pY) {
var ret;
var childObj;

   ret = false;

   //window.status = pObject.style.left + '\n' + pObject.offsetWidth + '\n' + pObject.style.top + '\n' + pObject.offsetHeight;

   if (document.layers && document.layers[pObject]) {
   	
   	  alert (document.layers[pObject].left + '\n' + document.layers[pObject].right + '\n' + document.layers[pObject].top + '\n' + document.layers[pObject].bottom);
      
      if (pX >= document.layers[pObject].left && pX <= document.layers[pObject].right && pY > document.layers[pObject].top && pY <= document.layers[pObject].bottom)
         ret = true;
      
    } else {
    	
    	childObj = document.elementFromPoint(pX, pY);
    	
    	while (!ret && childObj != null) {
    		ret = (childObj == pObject);
    		childObj = childObj.offsetParent;
    	}
    	
    	//ret = pObject.contains (childObj);
    }
      
    return ret;

}

/* 

NB the next two functions where borrowed with gratitude from
http://webreference.com/dhtml/diner/realpos_old/realpos4.html

*/

function getRealLeft(pItem) {
    xPos = pItem.offsetLeft;
    tempEl = pItem.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    
    return xPos;
}

function getRealTop(pItem) {
    yPos = pItem.offsetTop;
    tempEl = pItem.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

// Program:      document.elementFromPoint(int clientX, int clientY) in Gecko
// Author:      Jason Karl Davis (www.jasonkarldavis.com)
// Date:        15 June 2003
// Purpose:      Emulate Internet Explorer's document.elementFromPoint method as described here:
//              http://msdn.microsoft.com/workshop/a...tfrompoint.asp
// Requirements: A browser built off of the 1.4 branch of Mozilla (or better)
// Distribution: You may freely distribute and use this script as long as these comments remain intact

if (navigator.product == "Gecko") {
        Document.prototype.elementFromPoint = function(x, y) {
                this.addEventListener("mousemove", this.elementFromPoint__handler, false);
                var event = this.createEvent("MouseEvents");
                var box = this.getBoxObjectFor(this.documentElement);
                var screenDelta = { x: box.screenX, y: box.screenY };
                event.initMouseEvent("mousemove", true, false, this.defaultView, 0,
                                      x + screenDelta.x, y + screenDelta.y, x, y,
                                      false, false, false, false, 0, null);
                this.dispatchEvent(event);
                this.removeEventListener("mousemove", this.elementFromPoint__handler, false);
                return this.elementFromPoint__target;
        }
        Document.prototype.elementFromPoint__handler = function (event) {
                this.elementFromPoint__target = event.explicitOriginalTarget;
       
                // reparent target if it is a text node to emulate IE's behavior
                if (this.elementFromPoint__target.nodeType == Node.TEXT_NODE)
                        this.elementFromPoint__target = this.elementFromPoint__target.parentNode;
       
                // change an HTML target to a BODY target to emulate IE's behavior (if we are in an HTML document)
                if (this.elementFromPoint__target.nodeName.toUpperCase() == "HTML" && this.documentElement.nodeName.toUpperCase() == "HTML")
                        this.elementFromPoint__target = this.getElementsByTagName("BODY").item(0);

                event.preventDefault();
                event.stopPropagation();
        }
        Document.prototype.elementFromPoint__target = null;
}
