/**
 * Tooltips
 * Nicholas Wright (http://www.nicholaswright.org)
 * Created 10 / 8 / 2006
 *
 * new Tooltip(this, 'this is sample usage', {opacity: 5, className: 'some_class'});
 */

/**
 *  objParent 
 *    * type: string
 *    * The node which the new tooltip will be binded. In most cases you can simply past 'this'.
 *  strMessage
 *    * type: string
 *    * The message to be displayed in the tooltip.
 *  hashOptions
 *    * type: hash
 *    * Customization options - currently available options are:
 *      opacity
 *        * Takes a value in the range of 0 to 100, with 100 being no opacity. Unless provided 50 is assumed.
 *      className
 *        * An alternative classname to use for the tooltip. Unless provided 'tooltip' is used.
 */
function Tooltip(objParent, strMessage, hashOptions) {
    this.hashOptions = hashOptions;
    this.strMessage = strMessage;
    this.objParentDiv = objParent;
    this.drawToolTip();
    this.objParentDiv.onmouseout = this.destroyToolTip.bindAsEventListener(this);
}

Tooltip.prototype = {
    drawToolTip: function() {
        this.objToolTipDiv = document.createElement('div');
        this.objToolTipDiv.setAttribute('id', 'tool_tip_' + this.objParentDiv.id);
        this.objToolTipDiv.style.position = 'absolute';
        this.objToolTipDiv.className = this.hashOptions.className || 'tooltip';  
        this.objToolTipDiv.style.filter = "alpha(opacity=" + (this.hashOptions.opacity || 50) + ")";
        this.objToolTipDiv.style.opacity = ((this.hashOptions.opacity  || 50) * 0.01);
        this.objToolTipDiv.style.MozOpacity = ((this.hashOptions.opacity || 50) * 0.01);
        this.objToolTipDiv.style.KhtmlOpacity = ((this.hashOptions.opacity || 50) * 0.01);
	this.objToolTipDiv.appendChild(document.createTextNode(this.strMessage));
        this.objParentDiv.appendChild(this.objToolTipDiv);
    },
    destroyToolTip: function(e) {
        this.objParentDiv.removeChild(this.objToolTipDiv);
    },
    mouseX: function(e) {
        var intX = e.pageX || (event.clientX + document.body.scrollLeft)
        return (intX <= 0) ? 0 : intX; 
    },
    mouseY: function(e) {
        var intY = e.pageY || (event.clientY + document.body.scrollTop)
        return  (intY <= 0) ? 0 : intY; 
    },
    observeMouse: function(e) {
        this.objToolTipDiv.style.top = this.mouseY(e) - 10 + 'px';
        this.objToolTipDiv.style.left = this.mouseX(e) + 10 + 'px'; 
    }
};
