function HelpHover()
{
	this._mousePosX = 0;
	this._mousePosY = 0;
	this._hoverItem = null;
	this._hoverContents = null;
}

HelpHover.prototype.init = function()
{
	var hh = this;
	var helpItems = document.getElementsByClassName('hasHelp');
	for (var i=0; i<helpItems.length; i++)
	{
		helpItems[i].onmousemove = function(e)
		{
			if (!e) var e = window.event;
			if (e.pageX || e.pageY)
			{
				hh.mousePosX = e.pageX;
				hh.mousePosY = e.pageY;

			}
			else if (e.clientX || e.clientY)
			{
				hh.mousePosX = e.clientX + document.body.scrollLeft;
				hh.mousePosY = e.clientY + document.body.scrollTop;
			}
				hh.mousePosX2 = e.clientX;
				hh.mousePosY2 = e.clientY;
			hh._hoverItem = this;
			hh._hoverContents = document.getElementById(this.id+'Help');
			//hh._hoverText = document.getElementById(this.id+'text');
			//alert (hh._hoverContents.clientHeight);
			//
			//alert(hh._hoverContents.clientWidth);
			hh.numchars =  hh._hoverContents.innerHTML.length;
			//alert(hh.numchars);
			hh.move();
		}
		helpItems[i].onmouseout = function (e)
		{
			hh.out();
		}
	}
}

HelpHover.prototype.out = function()
{
	this._hoverContents.style.top = -10000+'px';
	this._hoverContents.style.left = -10000+'px';
	this._hoverItem = null;
	this._hoverContents = null;
}

HelpHover.prototype.move = function()
{
	var winWidth = getSize('width');
    var winHeight = getSize('height');


        var newXPos = this.mousePosX+10;
	var winWidthDiff = winWidth-this.mousePosX2;
	var popWidth = this._hoverContents.clientWidth;
        if (this.mousePosX2>winWidthDiff) {
           newXPos = this.mousePosX-popWidth-10;
        }


	var winHeightDiff = winHeight-this.mousePosY2;
	//alert ("winHeight: " + winHeight + " mousePosY: "+this.mousePosY + "test: "+this.mousePosY2);
	//alert (winHeightDiff);
	var popHeight = this._hoverContents.clientHeight;
	var spaceNeeded = popHeight+20;
    var moveHeight = 0;
	if (winHeightDiff<spaceNeeded) {
		moveHeight = spaceNeeded-winHeightDiff;
	}
	newYPos = this.mousePosY+10-moveHeight;
	if (newYPos<=(document.body.scrollTop+3)) { newYPos=document.body.scrollTop+3; }
	//alert (newYPos);


	

	this._hoverContents.style.top = newYPos+'px';
//	this._hoverContents.style.left = this.mousePosX+10+'px';
	this._hoverContents.style.left = newXPos+'px';
}

addEvent(window, 'load', function()
{
	var hh = new HelpHover();
	hh.init();
});

function getSize(myType) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (myType == 'height') {
	  return (myHeight);
  } else {
	  return (myWidth);
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}