/*
 * Changes
 *
 * 02/21/06 -- tooltip is appended directly to body to avoid any relatively
 * positioned divs messing up the position calculations
 */

/*
 * The Class.create() function is provided by the library. All it does is create an
 * object and add an initialize() function which gets passed any arguments in the
 * constructor.
 */
var ZToolTip = Class.create();

/* 
 * In JavaScript, everything is an object (even functions are objects). And every
 * object has a prototype, which is a "fresh" version of that object which gets cloned
 * everytime a new instance of the object is created. Essentially, then, the prototype
 * is the same as class.
 */
ZToolTip.prototype = {
	
	initialize: function( oRoot ) {
		
		var oParent = oRoot.parentNode;
		
		/*
		 * Hide the tooltip, and allow us to place it anywhere outside the document flow.
		 */
		var oRoot = oParent.removeChild( oRoot );
		oRoot.style.position = 'absolute';
		oRoot.style.display = 'none';
		document.body.appendChild( oRoot );
		
		/*
		 * Event.observe() provides a way of registering a new event on an object without
		 * clobbering any events that were already there.
		 *
		 * The first argument is who is doing the observing, the second is the event he's
		 * looking out for, the third is what function he runs when he sees it happen, and the fourth
		 * is whether he responds to that event during the capturing or the bubbling phase.
		 *
		 * The bindAsEventListener() function is provided by the prototype library. It does
		 * two things:
		 *  1 -- it returns an instance of the function with "this" set to oRoot.
		 *  2 -- it passes the event object as an argument to the function.
		 *
		 * The boolean determines whether to observe in capture mode (true; when events are firing
		 * from the outside in) or bubbling mode  (false; when events are firing from the inside out).
		 * Internet Explorer only supports bubbling.
		 *
		 * For more info on capturing vs bubbling, see http://www.quirksmode.org/js/events_order.html
		 *
		 */
		if ( oParent ) {
			Event.observe( oParent, 'mouseover', this.show.bindAsEventListener( oRoot ), false );
			Event.observe( oParent, 'mouseout', this.hide.bindAsEventListener( oRoot ), false );
			Event.observe( oParent, 'mousemove', this.move.bindAsEventListener( oRoot ), false );
		}
	},
	
	hide: function( oEvent ) {
		this.style.display = 'none';
		$A( document.getElementsByTagName( 'select' ) ).each(
			function( oElem, iIdx ) {
				oElem.style.visibility = 'visible';
			}
		);
	},
	
	move: function( oEvent ) {
		/* 
		 * Push the tooltip about 5 px to the southeast so Firefox and Opera will
		 * fire the mouse events in the right order (edge case).
		 */
		this.style.left = ( oEvent.pageX ? oEvent.pageX : oEvent.clientX ) + 5 + 'px';
		
		/*
		 * #$#$ IE will move the scrollTop around depending on the doctype
		 */
		var iScrollTop = 0;
		if (document.documentElement && document.documentElement.scrollTop) {
			iScrollTop = document.documentElement.scrollTop;
		}
		else if (document.body) {
			iScrollTop = document.body.scrollTop;
		}
		
		this.style.top = ( oEvent.pageY ? oEvent.pageY : oEvent.clientY + iScrollTop ) + 5 + 'px';
	},
	
	show: function( oEvent ) {
		this.style.display = 'block';
		$A( document.getElementsByTagName( 'select' ) ).each(
			function( oElem, iIdx ) {
				oElem.style.visibility = 'hidden';
			}
		);
	}
	
};

/* 
 * This is the function which finds all the tooltips on page load.
 */
Event.observe(
	window,
	'load',
	function() {
		$A( document.getElementsByClassName( 'replace-with-ztooltip' ) ).each(
			function( oElem, iIdx ) {
				new ZToolTip( oElem );
			}
		);
	},
	false
);