﻿Type.registerNamespace('Framework.Controls');

Framework.Controls.Tooltip = function(element) {
    Framework.Controls.Tooltip.initializeBase(this, [element]);
    this._isBuilt = false;
    this._xoffset = 0;
    this._yoffset = 0;
    this._message = "";
    this._align = "auto";
    this._width = "250px";
    this._popupBehavior;
    this._popupTable = null;
    this._arrowDiv;
    this._isOpen = false;
    this._el = element;
    this._cssClass = "ToolTip";
    this._showHandler = Function.createDelegate(this, this.show_event);
    this._showDelayHandler = Function.createDelegate(this, this.show);
    this._hideHandler = Function.createDelegate(this, this.hide);
    this._hidePreHandler = Function.createDelegate(this, this.hide_pre);
    this._hideCallbackHandler = Function.createDelegate(this, this.hide_callback);
    this._targetControl = null;
    this._valign = "auto";
    this._fadeTime = 0;
    this._delay = 0;
    this._persist = false;
    this._PopupMode = 0;
    this._Enabled = true;
    this._ContentControl = null;
    this._ContentControlDisplay = false;
    this._timeout = null;
    this._position = 1;
}

Framework.Controls.Tooltip.prototype = {
    get_isOpen: function() { return this._isOpen; },
    set_isOpen: function(value) { this._isOpen = value; },
    get_width: function() { return this._width; },
    set_width: function(value) { this._width = value; },
    get_message: function() { return this._message; },
    set_message: function(value) { this._message = value; },
    get_targetControlID: function() { return this._targetControl; },
    set_targetControlID: function(value) {
        if (value) this._targetControl = $get(value);
    },
    get_targetControlCssClass: function() { return this._targetControl; },
    set_targetControlCssClass: function(value) {
        if (value) this._targetControl = $("." + value)[0];
    },
    get_delay: function() { return this._delay; },
    set_delay: function(value) { this._delay = value; },
    get_persist: function() { return this._persist; },
    set_persist: function(value) { this._persist = value; },
    get_align: function() { return this._align; },
    set_align: function(value) { this._align = value; },
    get_xoffset: function() { return this._xoffset; },
    set_xoffset: function(value) { this._xoffset = value; },
    get_yoffset: function() { return this._yoffset; },
    set_yoffset: function(value) { this._yoffset = value; },
    get_PopupMode: function() { return this._PopupMode; },
    set_PopupMode: function(value) { this._PopupMode = value; },
    get_FadeTime: function() { return this._fadeTime; },
    set_FadeTime: function(value) { this._fadeTime = value; },
    get_Enabled: function() { return this._Enabled; },
    set_Enabled: function(value) { this._Enabled = value; },
    get_ContentControl: function() { return this._ContentControl; },
    set_ContentControl: function(value) { this._ContentControl = $get(value); },
    get_ContentControlDisplay: function() { return this._ContentControlDisplay; },
    set_ContentControlDisplay: function(value) { this._ContentControlDisplay = value; },
    get_cssClass: function() { return this._cssClass; },
    set_cssClass: function(value) { this._cssClass = value; },
    get_Position: function() { return this._position; },
    set_Position: function(value) { this._position = value; },

    initialize: function() {
        Framework.Controls.Tooltip.callBaseMethod(this, 'initialize');

        if (!this._Enabled) return;
        this._el = this.get_element();
        if (!this._el || !this._targetControl) return;

        if (!this._persist) {
            if (this._PopupMode == 1) {
                this.show();
                this.hide();
                $addHandler(this._popupTable, "mouseover", this._showHandler);
            } else {
                $addHandler(this._targetControl, "mouseover", this._showHandler);
                $addHandler(this._targetControl, "mouseout", this._hidePreHandler);
            }
            $addHandler(document.body, "click", this._hidePreHandler);
        } else {

            if (this._isOpen) {
                this.show();
                setTimeout(this._hideHandler, this._delay);
            } else {
                this.show();
            }
        }
    },
    dispose: function() {
        try {
            Framework.Controls.Tooltip.callBaseMethod(this, 'dispose');
            if (!this._Enabled) return;
            if (!this._persist) {
                if (this._PopupMode == 1) {
                    if (this._popupTable) {
                        $removeHandler(this._popupTable, "mouseover", this._showHandler);
                    }
                } else {
                    if (this._targetControl) {
                        $removeHandler(this._targetControl, "mouseover", this._showHandler);
                        $removeHandler(this._targetControl, "mouseout", this._hidePreHandler);
                    }
                }
                $removeHandler(document.body, "click", this._hidePreHandler);
                if (this._isBuilt) {
                    $removeHandler(this._popupTable, "mouseout", this._hidePreHandler);
                }
            }
        } catch (ex) { }
    },
    _ensureCallout: function() {
        var _width = parseInt(this._width.replace(/px/, "").replace(/%/, ""));

        // Calculate auto align
        if (this._align == "auto") {
            var _bounds = Sys.UI.DomElement.getBounds(this._targetControl);
            if ((_bounds.x - _width) < 0) this._align = "left";
            else this._align = "right";
        }

        if (!this._isBuilt) {
            var popupTableBody = document.createElement("tbody");
            var popupTableRow = document.createElement("tr");
            var calloutCell = document.createElement("td");
            var calloutTable = document.createElement("table");
            var calloutTableBody = document.createElement("tbody");
            var calloutTableRow = document.createElement("tr");
            var iconCell = document.createElement("td");
            var closeCell = document.createElement("td");
            var closeCellInnerDiv = this._closeCellInnerDiv = document.createElement("div");
            var popupTable = this._popupTable = document.createElement("table");
            var calloutArrowCell = this._calloutArrowCell = document.createElement("td");
            var warningIconImage = this._warningIconImage = document.createElement("img");
            var closeImage = this._closeImage = document.createElement("img");
            var errorMessageCell = this._errorMessageCell = document.createElement("td");
            popupTable.id = this._el.id + "_popupTable";
            popupTable.cellPadding = 0;
            popupTable.cellSpacing = 0;
            popupTable.border = 0;
            popupTable.width = this.get_width();
            popupTable.className = this._cssClass + " " + this._cssClass + "_popup_table_" + this._align;
            //popupTableRow.className = "ToolTip_popup_table_row_" + this._align;
            calloutCell.className = this._cssClass + "_callout_cell_" + this._align;
            calloutTable.cellPadding = 0;
            calloutTable.cellSpacing = 0;
            calloutTable.border = 0;
            calloutTable.className = this._cssClass + "_callout_table_" + this._align;
            calloutTableRow.className = this._cssClass + "_callout_table_row_" + this._align;
            calloutArrowCell.className = this._cssClass + "_callout_arrow_cell_" + this._align;
            iconCell.className = this._cssClass + "_icon_cell_" + this._align;
            warningIconImage.border = 0;
            errorMessageCell.className = this._cssClass + "_error_message_cell_" + this._align;

            if (this._ContentControl != null) {
                // Copy control content
                errorMessageCell.appendChild(this._ContentControl);
                if (this._ContentControlDisplay)
                    this._ContentControl.style.display = "";
            } else {
                // Set tooltip message
                errorMessageCell.innerHTML = this._message;
            }

            closeCell.className = this._cssClass + "_close_button_cell_" + this._align;
            closeCellInnerDiv.className = this._cssClass + "_innerdiv_" + this._align;
            this._el.parentNode.appendChild(popupTable)
            popupTable.appendChild(popupTableBody);
            popupTableBody.appendChild(popupTableRow);

            if (this._align == "left") {
                popupTableRow.appendChild(calloutCell); // Left arrow cell
                popupTableRow.appendChild(iconCell);
            }
            else {
                popupTableRow.appendChild(closeCell);
                closeCell.appendChild(closeCellInnerDiv); // Right close cell
            }

            calloutCell.appendChild(calloutTable);
            calloutTable.appendChild(calloutTableBody);
            calloutTableBody.appendChild(calloutTableRow);
            calloutTableRow.appendChild(calloutArrowCell);
            //iconCell.appendChild(warningIconImage);
            iconCell.innerHTML = "&nbsp;";
            popupTableRow.appendChild(errorMessageCell); // Middle error message
            //closeCellInnerDiv.appendChild(closeImage);
            this._popupTable.style.position = "absolute";

            if (this._align == "left") {
                popupTableRow.appendChild(closeCell);
                closeCell.appendChild(closeCellInnerDiv); // Right close cell
            }
            else {
                popupTableRow.appendChild(iconCell);
                popupTableRow.appendChild(calloutCell); // Left arrow cell
            }

            var div = document.createElement("div");
            div.className = this._cssClass + "_innerdiv_" + this._align;
            calloutArrowCell.appendChild(div);

            // valign
            var _viewPort = { width: $(window).width(), height: $(window).height() };
            var _bounds = Sys.UI.DomElement.getBounds(this._targetControl);
            var _boundsEl = Sys.UI.DomElement.getBounds(this._popupTable);
            var _yposfix = (_boundsEl.y + _boundsEl.height) - _viewPort.height;
            if (_yposfix > 0) {
                this._valign = "bottom";
                div.style.borderTop = "none";

                for (var i = 0; i < 15; i++) {
                    var line = document.createElement("div");
                    line.style.width = i.toString() + "px";
                    div.appendChild(line);
                }
            } else {
                this._valign = "top";
                div.style.borderBottom = "none";

                for (var i = 14; i > 0; i--) {
                    var line = document.createElement("div");
                    line.style.width = i.toString() + "px";
                    div.appendChild(line);
                }
            }

            this._arrowDiv = div;
            this._targetControl.parentNode.appendChild(this._popupTable);
            this._isBuilt = true;

            // Set tooltip min height
            //if (_boundsEl.height < 25) this._popupTable.style.height = "25px";
            this._popupTable.style.zIndex = 31000;

            // Add mouse out handler
            if (!this._persist) {
                $addHandler(this._popupTable, "mouseout", this._hidePreHandler);
            }
        }
    },
    show_event: function(e) {
        if (this._timeout) clearTimeout(this._timeout);
        if (this._delay == 0) {
            this.show();
            return;
        }
        this._timeout = setTimeout(this._showDelayHandler, this._delay);
    },
    show: function(e) {
        this._ensureCallout();
        var _location = Sys.UI.DomElement.getLocation(this._targetControl);

        // Get width
        var _width = parseInt(this._width.replace(/px/, "").replace(/%/, ""));

        // Get target element bounds
        var _parent = this._targetControl.parentNode;
        var _targetBounds = Sys.UI.DomElement.getBounds(this._targetControl);
        while (_parent) {
            if (_parent.style && _parent.style.position == "absolute") {
                var _parentBounds = Sys.UI.DomElement.getBounds(_parent);
                var _popupBounds = Sys.UI.DomElement.getBounds(this._popupTable);
                _targetBounds.x -= _parentBounds.x;
                _targetBounds.y -= _parentBounds.y + _popupBounds.height;
                break;
            }
            _parent = _parent.parentNode;
        }

        // Adjust position
        if (this._align == "left") {
            Sys.UI.DomElement.setLocation(this._popupTable, _targetBounds.x + _targetBounds.width + this._xoffset, _targetBounds.y + this._yoffset);
        } else {
            Sys.UI.DomElement.setLocation(this._popupTable, _targetBounds.x - _width + this._xoffset, _targetBounds.y + this._yoffset);
        }

        if (this._position == 0) this._popupTable.style.position = "static";
        if (!this.get_isOpen()) {
            $(this._popupTable).fadeTo(0, 0);
            this._popupTable.style.display = "";
            $(this._popupTable).fadeTo(this._fadeTime, 100);
            this._isOpen = true;
        }
    },
    hide: function(e) {
        if (this._timeout) clearTimeout(this._timeout);
        if (!this._isOpen) return;

        if (e) {
            var popupBounds = Sys.UI.DomElement.getBounds(this._popupTable);
            var mouseBounds = { x: e.clientX, y: e.clientY, width: 5, height: 5 };
            if (Utils.overlaps(mouseBounds, popupBounds)) return;
        }

        if (this._delay > 0) {
            this._timeout = setTimeout(this._hidePreHandler, this._delay);
        }
        else {
            this.hide_pre();
        }
    },
    hide_pre: function(e) {
        if (this._timeout) clearTimeout(this._timeout);
        if (!this._isOpen) return;

        if (e) {
            var popupBounds = Sys.UI.DomElement.getBounds(this._popupTable);
            var mouseBounds = { x: e.clientX, y: e.clientY, width: 5, height: 5 };
            if (Utils.overlaps(mouseBounds, popupBounds)) return;
        }

        if (this._fadeTime > 0) {
            $(this._popupTable).fadeTo(this._fadeTime, 0, this._hideCallbackHandler);
        } else {
            this.hide_callback();
        }
        this._isOpen = false;
    },
    hide_callback: function(e) {
        this._popupTable.style.display = "none";
    }
}
Framework.Controls.Tooltip.registerClass("Framework.Controls.Tooltip", Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();