/*
 * Author : Michael Bosworth 2006
 *
 *
 * Note : This class is dependant on :
 *
 * These files must be included with this script to function.
*/
function ToolTip(){
    this.tooltip = null;            /* Element , root HTML Element of tooltip */
    this.timeout_id = null;         /* Number , stores the timeout id number */
    this.showDelay = 0;             /* Number , duration of delay before tooltip is appended to the page */
    this.hideDelay = 0;             /* Number , duration of delay before tooltip is removed from the page */
    this.relativeEl = null;         /* Element , stores an HTML element in which the tooltip can be postioned relative to */
    this.ready = false;             /* Boolean , specifies if the tooltip is ready to be rendered */
    this.trackMouse = false;        /* Boolean , enables or disables the capturing of mouse events */
    this.isIE = null;               /* Boolean , specifies if the browser is Microsoft Internet Explorer */
    this.mouseX = null;             /* Number , stores the x coordinate of the mouse */
    this.mouseY = null;             /* Number , stores the y coordinate of the mouse */
    this.debug = false;             /* Boolean , specifies if error messages are to be shown */
    this.parent = null;             /* Element , (optional) parent element to the tooltip */

    /* Initializes main variables */
    this.init = function(showDelay,hideDelay,trackMouse){
        if(showDelay != null){
            this.showDelay = showDelay;
        }
        if(hideDelay != null){
            this.hideDelay = hideDelay;
        }
        if(trackMouse != null){
            this.trackMouse = trackMouse;
        }
        this.isIE = document.all;
        this.parent = document.body;
    };

    /* Show error messages if in debug mode */
    this.throwError = function(message){
        if(this.debug){
            alert("Tooltip Error: " + message);
        }
    };

    /* Creates a tooltip on the page */
    this.create = function(element){
        this.relativeEl = element;
        if(this.trackMouse){
            this.trackMouseStart();
        }
        if(this.showDelay > 0){
            if(this.ready){
                this.remove();
                this.render();
                this.ready = false;
            }
            else{
                var self = this;
                this.timeout_id = setTimeout(function(){
                    self.ready = true;
                    self.create(self.relativeEl);
                },self.showDelay);
            }
        }
        else{
            this.render();
        }
    };

    /* This function is to be defined based on the content of items being shown */
    this.render = function(){ };

    /* Removes the tooltip from the page */
    this.remove = function(){
        if(this.trackMouse){
            this.trackMouseStop();
        }
        if(this.hideDelay > 0){
            if(this.ready){
                if(this.timeout_id != null){
                    this.clear_timeout();
                }
                if(this.tooltip != null){
                    this.parent.removeChild(this.tooltip);
                    this.tooltip = null;
                }
                this.ready = false;
            }
            else{
                var self = this;
                this.timeout_id = setTimeout(function(){
                    self.ready = true;
                    self.remove();
                },self.hideDelay);
            }
        }
        else{
            if(this.timeout_id != null){
                this.clear_timeout();
            }
            if(this.tooltip != null){
                this.parent.removeChild(this.tooltip);
                this.tooltip = null;
            }
        }
    };

    /* Clears the timeout */
    this.clear_timeout = function(){
        clearTimeout(this.timeout_id);
    };

    /* Finds the y coordinate of an element */
    this.findPosY = function(obj){
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
              curtop += obj.offsetTop;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.y)
            curtop += obj.y;
        return curtop;
    };

    /* Finds the x coordinate of an element */
    this.findPosX = function(obj){
        var curleft = 0;
        if(obj.offsetParent)
            while(1)
            {
              curleft += obj.offsetLeft;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.x)
            curleft += obj.x;
        return curleft;
    };

    /* Starts capturing mouse events */
    this.trackMouseStart = function(){
        var self = this;
        document.onmousemove = function(e){
            if (!self.isIE) {
                self.mouseX = e.pageX;
                self.mouseY = e.pageY;
            }
            else{
                if(this.parent == null){
                    self.mouseX = event.clientX + document.body.scrollLeft;
                    self.mouseY = event.clientY + document.body.scrollTop;
                }
                else{
                    self.mouseX = event.clientX + this.parent.scrollLeft;
                    self.mouseY = event.clientY + this.parent.scrollTop;
                }
            }
        };

    };

    /* Stops capturing mouse events */
    this.trackMouseStop = function(){
        document.onmousemove = null;
    };

    /* Returns the x coordinate of the mouse */
    this.findMousePosX = function(){
        if(this.mouseX == null)
            this.throwError("Could Not Find X position!");
        return this.mouseX;
    };

    /* Returns the y coordinate of the mouse */
    this.findMousePosY = function(){
        if(this.mouseY == null)
            this.throwError("Could Not Find Y position!");
        return this.mouseY;
    };

}
