/*
	http://www.askthecssguy.com/2007/03/form_field_hints_with_css_and.html
	
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
     window.onload = func;
  } 
  else 
  {
      window.onload = function() {
      func();
      oldonload();
    }
  }
}

//**************************************
// _prepareInputForHints
//**************************************
function _prepareInputForHints(sType)
{
	/*
	var inputs = document.getElementsByTagName(sType);
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				//this.parentNode.getElementsByTagName("span")[0].style.display = "inline"; 
				this.parentNode.getElementsByTagName("span")[0].style.visibility = "visible"; 
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				//this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				this.parentNode.getElementsByTagName("span")[0].style.visibility = "hidden";
			}
		}
	}
	*/
	
	// for each element type passed (i.e. 'input', 'select', etc)
	$(sType).each(function(){
						   // get the parent
						   var objParent = $(this).parent();
						   if (objParent.length)
						   {
							  // are there any spans of class hint_text within the parent (sibling of the passed type)
							  if (objParent.find("span.hint_text").length > 0)
							  {
								  // yes, when the input element gets the focus, turn on the span's visibility
							  	  $(this).focus(function(){
													   $(this).parent().find("span.hint_text").css("visibility","visible");
													   });
								  
								  // yes, when the input element loses the focus, turn off the span's visibility
							  	  $(this).blur(function(){
													   $(this).parent().find("span.hint_text").css("visibility","hidden");
													   });
								  
							  } // if (objParent.find("span.hint_text").length > 0)
							  
						   } //  if (objParent.length)
						   
						   }); //$(sType).each
	
} // function _prepareInputForHints(sType)

//**************************************
// prepareInputForHints: MJF
//**************************************
function prepareInputsForHints() 
{
	//***********************************************************************
	//return;  // un-comment this when _prepareInputForHints above is fixed.
	//***********************************************************************
	
	_prepareInputForHints("input");
	_prepareInputForHints("select");
	_prepareInputForHints("textarea");
		
	// MJF use this to set the left offset and width of the hints to 
	//     always appear to the right of the owning input control.
	var nExtraBuffer = 45;
	var nRadioExtraBuffer = 100;
	
	$(".hint_text").each(function() {
		// get the input control that this hint is assigned to. We will use "siblings" instead of 
		// prev in case there are other dynamic elements on the page such as char counters on 
		// textareas, etc.
		
		//var objElement = $(this).prev();
		var objElement = $(this).siblings("input,textarea,select");
		//alert(objElement.attr("name"));
		
		// get the current width of the control
		var nWidth = objElement.width();
		// get the control's 'left' offset from the left edge of the page
		var nLeftOffset = objElement.offset().left;
		var nTopOffset = objElement.offset().top;
		var nTotalLeft = nWidth+nLeftOffset+nExtraBuffer;
		// if a radio button, add more width to account for the text to the right of the radio
		if (objElement.attr("type")=="radio")
		{
			nTotalLeft += nRadioExtraBuffer;
		}
		
		//alert("nWidth=" + nWidth + " nLeftOffset=" + nLeftOffset);	
		
		// set the hint text to appear just to the right of the owning input control
		// for now, don't try to show hints for elements that are currently hidden
		if (nWidth > 0)
		{
			$(this).css("left",nTotalLeft.toString() + "px");
			$(this).css("top",nTopOffset.toString() + "px");
		}
		
		// set the hint width
		//$(this).css("width","200px");
	}); // $(".hint_text").each(function() 
	
	//return true;

} // function prepareInputsForHints()
//**************************************
// RefreshInputHints: MJF
// Called by pages when their display offsets have changed
// due to dynamic page content. We will set a setTimeout for 
// this to allow any asynchronous animations to complete and the new
// page dimensions to be established.
//**************************************
function RefreshInputHints()
{
	setTimeout('prepareInputsForHints()',250);
}

//addLoadEvent(prepareInputsForHints);

