﻿// PopUpMenu.js
// Inspired by a HoverHelp.js script (origin unknown)
// and a script found at http://javascript.internet.com original script at: http://elouai.com/scripts.php
// Designed by Jeremy Schneider, RentLinx LLC, 5/12/2008

document.getElementsByClassName = function (needle)
{
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}
function addEvent(obj, evType, fn) { if (obj.addEventListener) { obj.addEventListener(evType, fn, true); return true; } else if (obj.attachEvent) { var r = obj.attachEvent("on"+evType, fn); return r; } else { return false; } }

var menus = new Array();

function CloseMenus(e)
{
	for (var i=0; i<menus.length; i++)
	{
		if(menus[i]._menuUp && !menus[i]._isHover)
		{
			menus[i]._menuUp = false;
			menus[i]._isHover = false;
			menus[i]._menuContents.style.display = 'none';
		}
	}
}

document.onmouseup  = CloseMenus;

function PopUpMenu()
{
	//Initialize this pop up menu
	this._menuButton = null;		//The button/link that initiates the menu
	this._menuContents = null;		//The menu control itself
	this._menuUp = false;			//When true, the menu is active
	this._isHover = false;			//When true the mouse is hovering over the menu
}

function InitPopUpMenus()
{
	var popUpItems = document.getElementsByClassName('RL_HasMenu');
	for (var i=0; i<popUpItems.length; i++)
	{
		var p = new PopUpMenu();
		menus.push(p);

        var oldOnClick = popUpItems[i].attributes["onclick"].value;

		popUpItems[i].onclick = function(e)
		{	
			//Get the x and y coords of the click
			var x, y;
			if (!e) var e = window.event;
			if (e.pageX || e.pageY)
			{
				x = e.pageX;
				y = e.pageY;
			}
			else if (e.clientX || e.clientY)
			{
				x = e.clientX + document.body.scrollLeft;
				y = e.clientY + document.body.scrollTop;
			}
			
			//Save the elements related to this menu
			p._menuButton = this;
			p._menuContents = document.getElementById(this.id+'Menu');
			
			//Set the position of the menu and show it
			p._menuContents.style.left = x + 'px';
			p._menuContents.style.top = y + 'px';
			p._menuContents.style.display = 'block';
			p._menuUp = true;
			p._isHover = false;
			
			//Add an event handler to hide the menu when the mouse moves away
			p._menuContents.onmouseout = function(e)
			{
				p._isHover = false;
			}
			p._menuContents.onmouseover = function(e)
			{
				p._isHover = true;
			}
			
            //This runs whatever the inline onclick function was in case there is additional loading stuff to take place			
		    eval(oldOnClick);
			
			return false;	
		} 			
	}
}

addEvent(window, 'load', function()
{	
	InitPopUpMenus();
});