function primeToolTipListeners(elements) {
  elements.each( function(element) {
		primeToolTipListener(element);
	});
  
  function tipFor(element) {
		return $(element.readAttribute('id') + 'ToolTip');
	}

	function primeToolTipListener(element, tip) {
		if (element) {
			element.observe('mouseover', function(e) {
				var toolTip = tip || tipFor(element);
				if (toolTip) {
                    alert(element.viewportOffset());
					toolTip.style.left = (e.pointerX() + 1) + "px";
					toolTip.style.top = (element.viewportOffset().top + 1) + "px";
					toolTip.show();
				}
			});
			element.observe('mouseout', function() {
				var toolTip = tip || tipFor(element);
				if (toolTip) {
					toolTip.hide();
				}
			});
		}
	}
}

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

var action = {
    show: function(event) {
        var elementId = Event.element(event).id
        var textId = elementId + "_text"
        ShowPopupDiv(textId)
    },
    hide: function(event) {
        var elementId = Event.element(event).id
        var textId = elementId + "_text"
        HidePopupDiv(textId)
    },
    modify_element: function(event, method) {
        elementId = Event.element(event).id
        eval("$('"+ elementId + "_text')." + method + ";")
    }
};


var g_PopupIFrame;

function IsIE()
{

  return ( navigator.appName=="Microsoft Internet Explorer" );

}

function HidePopupDiv(divID)
{
  var divPopup;
  divPopup=document.getElementById(divID);
  divPopup.style.visibility = "hidden";

  if (IsIE())
  {
    if (g_PopupIFrame!=null) {
      divPopup.parentNode.removeChild(g_PopupIFrame);
    }
    g_PopupIFrame=null;
  }

}

function ShowPopupDiv(divID)
{
  var divPopup=document.getElementById(divID);

  if (!IsIE())
  {
    //Just display the div
    divPopup.style.visibility ="visible";
    return;
  }

  var iFrame = document.createElement("iframe");
  
  iFrame.setAttribute("src", '/blank');
  
  //Match IFrame position with divPopup
  iFrame.style.position="absolute";
  iFrame.style.left = divPopup.offsetLeft + 'px';
  iFrame.style.top = divPopup.offsetTop + 'px';
  iFrame.style.width = divPopup.offsetWidth + 'px';
  iFrame.style.height = divPopup.offsetHeight + 'px';
//  iFrame.style.zIndex = divPopup.style.zIndex - 1;
  
  divPopup.parentNode.appendChild(iFrame);

  //Store iFrame in global variable, so it can get removed when divPopup is hidden 
  g_PopupIFrame=iFrame;
  divPopup.style.visibility = "visible";
}

// Add hide/show functionality to floating help texts
function registerToolTipEvents(){
    var elements = $$('.floating_help_button');
    elements.each(function(el) {
        Event.observe(el, 'mouseover', action.show);
        Event.observe(el, 'mouseout', action.hide);
    });
}

Event.observe(window, 'load', function() {
    registerToolTipEvents();
});
