﻿

if (typeof(window.ToolTip) == "undefined" || window.ToolTip == null) {
    window.ToolTip = function (elementID, options) {
        this.initialize(elementID, options);
    }

    window.ToolTip.prototype = {
        initialize: function (elementID, options) {
            this.elementID = elementID || "toolTip";

            options = options || {};
            this.targetElementID = options.targetElementID || null;
            this.text = options.text || "ToolTip text";
            this.cssClass = options.cssClass || "toolTip";

            this.element = null;
            this.textElement = null;
        },

        isVisible: function () {
            return this.element != null && this.element.style.display != "none";
        },

        show: function (showAbove) {
            if (this.element == null)
                this.createElements();

            this.textElement.innerHTML = this.text.escapeLineBreaks();

            var targetElement = document.getElementById(this.targetElementID);
            
            var offset = $(targetElement).offset();
            var pos = "absolute";

            if (Utils.hasFixedParentNode(targetElement)) {
                offset.top -= Utils.getScrollY();
                pos = "fixed";
            }

            var h = $(this.element).height();
            var t = (offset.top - h - 34);
            var l = offset.left - 10;

            if (showAbove || (showAbove == null && t > 10)) {
                $("div[id=" + this.elementID + "] div[id*=arrow]")[0].className = "down";
            }
            else {
                $("div[id=" + this.elementID + "] div[id*=arrow]")[0].className = "up";
                t = offset.top + $(targetElement).height() + 30;
            }

            with (this.element.style) {
                top = t + "px";
                left = l + "px";
                display = "block";
                position = pos;
            }
        },

        hide: function () {
            if (this.element != null) {
                this.element.style.display = "none";
                this.disposeElements();
            }
        },

        createElements: function () {
            if (this.element == null) {
                var div = document.createElement("div");
                div.id = this.elementID;
                div.className = this.cssClass;
                document.body.appendChild(div);

                var span = document.createElement("span");
                span.id = this.elementID + "_txt";
                div.appendChild(span);

                var classNames = ["tl", "tr", "bl", "br", "top", "bottom", "left", "right"];
                for (var i = 0; i < classNames.length; i++) {
                    var item = document.createElement("div");
                    item.className = classNames[i];
                    div.appendChild(item);
                }

                var arrow = document.createElement("div");
                arrow.id = this.elementID + "_arrow";
                arrow.className = "up";
                //arrow.setAttribute("tt", "arrow");
                div.appendChild(arrow);

                this.element = div;
                this.textElement = span;
            }
        },

        disposeElements: function () {
            if (this.element != null) {
                while (this.element.childNodes.length > 0)
                    this.element.removeChild(this.element.childNodes[this.element.childNodes.length - 1]);

                try {
                    document.body.removeChild(this.element);
                }
                catch (ex) {
                }

                this.element = null;
                this.textElement = null;
            }
        },

        setText: function (text) {
            this.text = text;

            if (this.textElement != null)
                this.textElement.innerHTML = this.text.escapeLineBreaks();
        },

        setTargetElement: function (elementID) {
            this.targetElementID = elementID;

            //if (this.element && this.element.style.display != "none")
            //    this.show();
        }
    }

}
