
var PPSCHelpPopup = {

	currentPopups : [],
	
	isHelpPopupActive : 0,
	
	popupIframe : null,

	
	attachFormHelpHandlers : function() {
		
		var aTags = document.getElementsByTagName("a");
		
		for (var i = 0; i < aTags.length; i++)
		{
			if (  (aTags[i].getAttribute("class") && 
			       aTags[i].getAttribute("class").indexOf("form-help") >= 0)
			      ||     // moz
			      (aTags[i].getAttribute("className") && 
			       aTags[i].getAttribute("className").indexOf("form-help") >= 0 ))  // ie
			{
				// attach the event handlers.
				if (aTags[i].addEventListener)
				{
					aTags[i].addEventListener("mouseover", PPSCHelpPopup.formHelpMouseOver, false);
					aTags[i].addEventListener("mouseout", PPSCHelpPopup.formHelpMouseOut, false);
				}
				if (!aTags[i].addEventListener)
				{
					aTags[i].onmouseover = PPSCHelpPopup.formHelpMouseOver;
					aTags[i].onmouseout = PPSCHelpPopup.formHelpMouseOut;
				}
				
				aTags[i].setAttribute("tabIndex", 99);
			}
		}	
	},
	
	formHelpMouseOver : function (e) {
		
		var evt = e ? e : window.event;
		
		if (PPSCHelpPopup.isHelpPopupActive)
			return;
		
		for (var i = 0; i < this.childNodes.length; i++)
		{
			if (this.childNodes[i].tagName == "DIV")
			{
				var helpText = this.childNodes[i].innerHTML;
				var pageY = PPSCUtilities.getEventPageY(evt);
				var pageX = PPSCUtilities.getEventPageX(evt);

				//alert(pageX + " " + pageY);
				
				var positionLeft = null, positionRight = null, positionTop = null, positionBottom = null;
				
				// Have to add the DIV to the page before we can work out the position.
				var newDiv = document.createElement("div");
				
				newDiv.innerHTML = helpText;
				newDiv.style.width = "220px";
				newDiv.style.border = "1px solid darkgray";
				newDiv.style.backgroundColor = "#FFFF33";
				newDiv.style.padding = "4px";
				newDiv.style.textAlign = "left";
				newDiv.style.fontWeight = "normal";
				newDiv.style.zIndex = 100000;
				newDiv.style.position = "absolute";	
				
				var body = document.getElementsByTagName("body")[0];
				body.appendChild(newDiv);				

				// IE needs the IFRAME to get around the z-index'ing bug.
				if (window.event)
				{
				    if (PPSCHelpPopup.popupIframe == null)
				    {
						PPSCHelpPopup.popupIframe = document.createElement("iframe"); 
						                                           // why the heck we need an IFRAME?  by putting this just under the DIV, 
																   // it solves the problem of the SELECT's or other DHTML windowed controls 
																   // showing through
					}
					
					//alert(evt.clientY + " scr: " + evt.screenY + " off: " + evt.offsetY + " y: " + evt.y);
					PPSCHelpPopup.popupIframe.id = "helpPopup";
					PPSCHelpPopup.popupIframe.style.position = "absolute";                            
					PPSCHelpPopup.popupIframe.style.width = "220px";
					PPSCHelpPopup.popupIframe.style.zIndex = 99999;
					PPSCHelpPopup.popupIframe.style.height = newDiv.clientHeight;
					PPSCHelpPopup.popupIframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
				}
								
				//
				// first thing is to calculate where we need the help popup to appear
				// 
				var testWidth = document.body.clientWidth - 35; //window.innerWidth ? window.innerWidth  :
				if (pageX + newDiv.clientWidth > testWidth)
				{
					positionRight = (testWidth - pageX + 45) + "px";
					newDiv.style.right = positionRight;
					if (window.event)
						PPSCHelpPopup.popupIframe.style.right = positionRight;
				}
				else
				{
					newDiv.style.left = pageX + 20 + "px";
					if (window.event)
						PPSCHelpPopup.popupIframe.style.left = pageX + 20 + "px";
				}
				
				//alert(document.body.clientHeight);
				var testHeight = window.innerHeight ? window.innerHeight + window.scrollY : document.body.clientHeight + document.body.scrollTop;
				if (pageY + 20 + newDiv.clientHeight > testHeight)
				{	
					newDiv.style.bottom = "10px";
					if (window.event)
						PPSCHelpPopup.popupIframe.style.bottom = "10px";
				}
				else
				{
					newDiv.style.top = pageY + 20 +  "px";
					if (window.event)
						PPSCHelpPopup.popupIframe.style.top = pageY + 20 + "px";
				}
				
				PPSCHelpPopup.currentPopups.push(newDiv);
			
				if (window.event)
				{
					body.appendChild(PPSCHelpPopup.popupIframe);
					PPSCHelpPopup.currentPopups.push(PPSCHelpPopup.popupIframe);					
				}
				
				PPSCHelpPopup.isHelpPopupActive = 1;
				
				i = Number.POSITIVE_INFINITY;		
			}		
		}
	},
	
	formHelpMouseOut : function (e) {
	
		var evt = e ? e : window.event;

		var body = document.getElementsByTagName("body")[0];
		
		// remove all every time, cos onMouseOver + onMouseOut aren't 100% 
		// reliable.
		for (var i = 0; i < PPSCHelpPopup.currentPopups.length; )
		{
			body.removeChild(PPSCHelpPopup.currentPopups[i]);
			PPSCHelpPopup.currentPopups.splice(i, 1);
		}
		
		PPSCHelpPopup.isHelpPopupActive = 0;
	}

};
