/**
 * This file is part of WebWorx javascript widget library. All rights reserver.
 *
 * Tooltip control implementation. This class hooks document at global level and
 * displays tootip popup over elements with tooltipattribute.
 */
 
if (!wxLib) {
    throw Exception("Please include webworx.js first");
} else if (wxLib.version < 1.0) {
    throw Exception("wxLib version 1.0 required");
}

/**
 * Construct instance of tooltip control.
 */
function wxToolTipCtrl() {
    this.enabled = false;
    this.visible = false;
    this.currentElement = null;    
    
    var self = this;
    // idle timer handler, this is used when tooltips are set to nontracking
    this.idleHandler = function() {
        self.popup();     
    }
}

/**
 * Creates internal structure of tooltip control. This method may be called during onload event
 * handling
 */
wxToolTipCtrl.prototype.create = function() {
    this.enabled = true;
    this.tracking = true;
    this.timerId = -1;
    this.timeout = 750;
    
    // create tooltip popup element    
    with (this.toolTipPopup = document.body.appendChild(document.createElement("div"))) {
        className = "tool-tip-popup";
        style.position = "absolute";
        style.visibility = "hidden";
        style.zIndex = "1001";
        style.left = "0px";
        style.top = "0px";
    }
        
    var self = this;    
    
    // helper method that search DOM tree for elements with tooltip attribute
    function findToolTipElement(element) {
        var toolTipElement = null;
        for (var it = element; (null == toolTipElement) && (document != it); it = it.parentNode) {
            if (it.attributes && it.attributes.getNamedItem("tooltip")) {
                toolTipElement = it;
            }
        }
        
        return toolTipElement;
    }
    
    // calculate tooltip position
    function calculatePosition(x, y) {
        var anchorX = x + 3;
        var anchorY = y - 3 - self.toolTipPopup.clientHeight;
                    
        if (anchorX + self.toolTipPopup.clientWidth > document.body.clientWidth) {
            anchorX -= self.toolTipPopup.clientWidth;
        }
                                    
        self.toolTipPopup.style.left = anchorX + "px";
        self.toolTipPopup.style.top = anchorY + "px";
    }
    
    // event listener
    function MouseHoverListener() {
    }
    
    MouseHoverListener.prototype.handleEvent = function(event) {
        if (event.type == "mousemove") {        
            self.overElement = findToolTipElement(event.target);
        
            if (-1 != self.timerId) {
                clearTimeout(self.timerId);
                self.timerId = -1;
            }
        
            if (self.enabled) {                        
                with (self) {                                
                    if (null == overElement) {                    
                        pop();
                    } else {                    
                        if (currentElement != overElement) {
                            if (tracking) {
                                popup();
                            } else {
                                timerId = setTimeout(idleHandler, timeout);
                                calculatePosition(event.clientX, event.clientY);                            
                            }
                        }
                    }
                 
                    if (visible) {
                        if (tracking) {
                            calculatePosition(event.clientX, event.clientY);
                        }
                        else {
                            pop();
                        }                    
                    }
                }
            }
        } else {
            self.pop();
        }
    }
        
    wxLib.addEventListener(document, "mousemove", new MouseHoverListener());
    wxLib.addEventListener(document, "click", new MouseHoverListener());    
}

wxToolTipCtrl.prototype.popup = function() {
    if (null != this.overElement) {
    	if (this.overElement.attributes.getNamedItem("tooltip").value != '') {

	        this.toolTipPopup.innerHTML = this.overElement.attributes.getNamedItem("tooltip").value;
	        this.toolTipPopup.style.visibility = "visible";
	                
	        this.visible = true;
	        this.currentElement = this.overElement;
        }
    }
}

wxToolTipCtrl.prototype.pop = function() {
    if (this.visible) {
        this.toolTipPopup.style.visibility = "hidden";
                    
        this.visible = false;                                                            
        this.currentElement = null;
    }
}

wxToolTipCtrl.prototype.setEnabled = function(value) {
    this.enabled = value;
    
    if (!this.enabled) {
        if (this.visible) {
            this.toolTipPopup.style.visibility = "hidden";
            
            this.visible = false;
            this.currentElement = null;
        }
    } else {
        if (null != this.overElement) {
            if (this.tracking) {
                this.popup();
            } else {
                this.timerId = setTimeout(this.idleHandler, this.timeout);
            }
        }
    }
}

wxToolTipCtrl.prototype.setTracking = function(value) {
    this.tracking = value;
    if (!value) {
        this.pop();
    } else {
        this.popup();
    }
}

