/// <reference name="MicrosoftAjax.js" />
/// <reference name="Telerik.Web.UI.Common.Core.js" assembly="Telerik.Web.UI"/>

Type.registerNamespace("Telerik.Web.UI");

Telerik.Web.UI.GridSliderTooltipExtender = function (element)
{
    Telerik.Web.UI.GridSliderTooltipExtender.initializeBase(this, [element]);

    this._grid = null;
    this._toolTipManager = null;

    this._toolTipShowDelegate = null;

    this._contentCache = new Telerik.Web.UI.GridSliderToolTipCache();
    this._timeOutObj = null;
};

Telerik.Web.UI.GridSliderTooltipExtender.prototype = {
    initialize: function ()
    {
        Telerik.Web.UI.GridSliderTooltipExtender.callBaseMethod(this, "initialize");

        this._grid = $find(this.get_element().id);

        this._toolTipShowDelegate = Function.createDelegate(this, this._toolTipShown);

        var toolTips = this.get_toolTipManager().get_toolTips();
        for (var i = 0, length = toolTips.length; i < length; i++)
        {
            toolTips[i].add_show(this._toolTipShowDelegate);
        }
    },

    dispose: function ()
    {
        var toolTips = this.get_toolTipManager().get_toolTips();
        if (toolTips)
        {
            for (var i = 0; i < toolTips.length; i++)
            {
                toolTips[i].remove_show(this._toolTipShowDelegate);
            }
        }

        this._toolTipShowDelegate = null;
        Telerik.Web.UI.GridSliderTooltipExtender.callBaseMethod(this, "dispose");
    },

    _buildEventArgs: function (eventArgs, eventData)
    {
        for (var property in eventData)
        {
            var name = property;
            var value = eventData[property];

            if (name.indexOf("et_") > 0)
            {
                eventArgs[name] = value;
                continue;
            }

            eventArgs["_" + name] = value;

            eventArgs["get_" + name] = function (propertyName)
            {
                return function ()
                {
                    return this["_" + propertyName];
                }
            } (name);

            eventArgs["set_" + name] = function (propertyName)
            {
                return function (value)
                {
                    this["_" + propertyName] = value;
                }
            } (name);
        }

        return eventArgs;
    },

    _getToolTipFromSliderId: function (sliderId)
    {
        return this.get_toolTipManager().getToolTipByElement($get(sliderId));
    },

    _slide: function (sliderId, isSlideStart)
    {
        var slider = $find(sliderId);
        var toolTip = this._getToolTipFromSliderId(sliderId);

        if (this._timeOutObj)
        {
            clearTimeout(this._timeOutObj);
        }

        if (slider && toolTip)
        {
            if (isSlideStart)
            {
                this._showToolTip(slider, toolTip);
            }
            else
            {
                var thisObj = this;
                this._timeOutObj = setTimeout(function ()
                {
                    thisObj._showToolTip(slider, toolTip);

                }, this.get_toolTipManager().get_showDelay());
            }
        }
    },

    _slideEnd: function (sliderId)
    {
        var slider = $find(sliderId);
        var toolTip = this._getToolTipFromSliderId(sliderId);

        if (toolTip)
        {
            toolTip.hide();
        }
    },

    _showToolTip: function (slider, toolTip)
    {
        var tableView = this._getOwnerTableView(slider);

        if (!tableView)
        {
            return;
        }

        var eventData = {
            gridPageIndex: slider.get_value(),
            gridPageSize: tableView.get_pageSize(),
            value: ""
        };

        var args = this._buildEventArgs(new Sys.CancelEventArgs(), eventData);

        this.raise_beforeShow(args);

        if (args.get_cancel())
        {
            return;
        }

        var eventArgStr = args.get_gridPageIndex() + ":" + args.get_gridPageSize() + ":" + args.get_value();

        var cachedToolTipContent = this._contentCache.getContent(toolTip, eventArgStr);
        if (cachedToolTipContent)
        {
            toolTip._hideNoAnimation();
            toolTip.set_content(cachedToolTipContent);
            toolTip._show();
        }
        else
        {
            toolTip._requestEventArgStr = eventArgStr;
            toolTip.set_serverValue(eventArgStr);
            toolTip.show();
        }
    },

    _toolTipShown: function (sender, args)
    {
        var _extender = this;

        setTimeout(function ()
        {
            _extender._contentCache.cacheContent(sender, sender._requestEventArgStr);
        }, 100);
    },

    _getOwnerTableView: function (control)
    {
        var el = control.get_element ? control.get_element() : control;

        var tableEl = Telerik.Web.UI.Grid.GetFirstParentByTagName(el, "table");
        while (tableEl && !Sys.UI.DomElement.containsCssClass(tableEl, "rgMasterTable") && !Sys.UI.DomElement.containsCssClass(tableEl, "rgDetailTable"))
        {
            tableEl = Telerik.Web.UI.Grid.GetFirstParentByTagName(tableEl.parentNode, "table")
        }

        return $find(tableEl.id);
    },

    add_beforeShow: function (handler)
    {
        this.get_events().addHandler("beforeShow", handler);
    },

    remove_beforeShow: function (handler)
    {
        this.get_events().removeHandler("beforeShow", handler);
    },

    raise_beforeShow: function (eventArgs)
    {
        var handler = this.get_events().getHandler("beforeShow");
        if (handler)
        {
            if (!eventArgs)
            {
                eventArgs = new Sys.CancelEventArgs();
            }
            handler(this, eventArgs);
        }
    },

    get_toolTipManager: function ()
    {
        return this._toolTipManager;
    },
    set_toolTipManager: function (value)
    {
        this._toolTipManager = value;
    }
}

Telerik.Web.UI.GridSliderTooltipExtender.registerClass("Telerik.Web.UI.GridSliderTooltipExtender", Sys.UI.Behavior);

Telerik.Web.UI.GridSliderToolTipCache = function ()
{
    this._data = {};
}

Telerik.Web.UI.GridSliderToolTipCache.prototype = {
    getContent: function (toolTip, parameter)
    {
        var toolTipData = this._data[toolTip.get_targetControlID()];
        if (toolTipData)
        {
            return toolTipData[parameter];
        }

        return "";
    },

    cacheContent: function (toolTip, parameter)
    {
        var toolTipData = this._data[toolTip.get_targetControlID()];
        if (toolTipData)
        {
            toolTipData[parameter] = toolTip.get_content();
        }
        else
        {
            var parameterData = {};
            parameterData[parameter] = toolTip.get_content();
            this._data[toolTip.get_targetControlID()] = parameterData;
        }
    }
}