﻿/// <reference name="MicrosoftAjaxTimer.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../Compat/Timer.js" />
/// <reference path="../Common/Common.js" />

Type.registerNamespace('Viva.Web.UI.AjaxControls');

Viva.Web.UI.AjaxControls.TooltipTextControlMode = function()
{
    return null;
}

Viva.Web.UI.AjaxControls.TooltipTextControlMode.prototype = {
    ServerControl: 0,
    ClientControl: 1
}

Viva.Web.UI.AjaxControls.TooltipTextControlMode.registerEnum("Viva.Web.UI.AjaxControls.TooltipTextControlMode", false);

Viva.Web.UI.AjaxControls.TooltipBehavior = function(element)
{
    Viva.Web.UI.AjaxControls.TooltipBehavior.initializeBase(this, [element]);
    
    this._tooltipStyle = null;
    this._textControlId = null;
    this._textControlMode = Viva.Web.UI.AjaxControls.TooltipTextControlMode.ServerControl;
    this._textClientId = null;
    
    this._id = 'tt';
    this._top = 5;
    this._left = -23;
    this._maxw = 300;
    this._speed = 10;
    this._timer = null;
    this._timerSpeed = 20;
    this._endalpha = 100;
    this._alpha = 0;
    this._tt = null;
    this._t = null;
    this._c = null;
    this._b = null;
    this._h = null;
    this._r = null;
    this._direction = null;
    this._clear = null;
    this._ie = (Sys.Browser.agent == Sys.Browser.InternetExplorer);
}

Viva.Web.UI.AjaxControls.TooltipBehavior.prototype = {
    initialize: function()
    {
        Viva.Web.UI.AjaxControls.TooltipBehavior.callBaseMethod(this, 'initialize');

        var element = this.get_element();
        $addHandlers(element,
            { 'mouseover': this._show, 'mouseout': this._hide }
            , this);
    },

    dispose: function()
    {
        var element = this.get_element();
        if (element.onmouseover != null)
            $removeHandler(element, 'mouseover', this._show);

        if (element.onmouseout != null)
            $removeHandler(element, 'mouseout', this._hide);

        if (element.onmousemove != null)
            $removeHandler(element, 'mousemove', this._position);

        Viva.Web.UI.AjaxControls.TooltipBehavior.callBaseMethod(this, 'dispose');
    },

    get_TooltipStyle: function()
    {
        return this._tooltipStyle;
    },

    set_TooltipStyle: function(value)
    {
        this._tooltipStyle = value;
    },

    get_TextControlId: function()
    {
        return this._textControlId;
    },

    set_TextControlId: function(value)
    {
        this._textControlId = value;
    },

    get_TextClientId: function()
    {
        return this._textClientId;
    },

    set_TextClientId: function(value)
    {
        this._textClientId = value;
    },

    get_TextControlMode: function()
    {
        /// <value type="Viva.Web.UI.AjaxControls.TooltipTextControlMode">
        /// Direction the panel will expand and collapse (can be either "Vertical" or "Horizontal")
        /// </value>
        return this._textControlMode;
    },

    set_TextControlMode: function(value)
    {
        if (this._textControlMode != value)
        {
            this._textControlMode = value;
            this.raisePropertyChanged('TextControlMode');
        }
    },

    _show: function()
    {

        if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
            this._hideDropDowns(true);

        if (this._tt == null)
        {
            this._tt = document.createElement('div');
            this._tt.className = 'tooltip_container';

            this._c = document.createElement('div');
            this._b = document.createElement('div');

            this._tt.appendChild(this._c);
            this._tt.appendChild(this._b);

            document.body.appendChild(this._tt);

            this._tt.style.opacity = 0;
            this._tt.style.filter = 'alpha(opacity=0)';
        }

        this._c.className = 'tooltip_' + this.get_TooltipStyle() + '_content';
        this._b.className = 'tooltip_' + this.get_TooltipStyle() + '_bottom';

        var mode = this.get_TextControlMode();
        var control = (mode == 1 ? this.get_TextClientId() : this.get_TextControlId());

        var control = $get(control);
        if (control == null)
        {
            throw Error.argument('textControlId', String.format(AjaxControlToolkit.Resources.CollapsiblePanel_NoControlID, this.get_TextControlId()));
        }
        else
        {
            this._tt.style.display = 'block';
            this._c.innerHTML = $get(this.get_TextControlId()).innerHTML;
            this._tt.style.width = 'auto';

            var w = null;

            if (!w && this._ie)
            {
                this._b.style.display = 'none';
                this._tt.style.width = this._tt.offsetWidth;
                this._b.style.display = 'block';
            }

            if (this._tt.offsetWidth > this._maxw)
            {
                this._tt.style.width = this._maxw + 'px';
            }

            this._h = parseInt(this._tt.offsetHeight) + this._top;

            if (this._timer == null)
                this._timer = new Sys.Timer();

            var handler = Function.createDelegate(this, function(timer) { this._fade(1); });
            this._timer.add_tick(handler);
            this._timer.set_interval(this._timerSpeed);
            this._timer.set_enabled(true);

            var moveHandler = Function.createDelegate(this, this._position);
            $addHandler(document, 'mousemove', moveHandler);
        }
    },

    _position: function(e)
    {
        var u = e.clientY + document.documentElement.scrollTop;
        var l = e.clientX + document.documentElement.scrollLeft;

        this._tt.style.top = (u - this._h) + 'px';
        this._tt.style.left = (l + this._left) + 'px';
    },

    _hide: function()
    {
        if(this._timer != null)
            this._timer.dispose();

        this._timer = new Sys.Timer();
        this._timer.set_interval(this._timerSpeed);

        var handler = Function.createDelegate(this, function(timer) { this._fade(-1); });

        this._timer.add_tick(handler);

        this._timer.set_enabled(true);
    },

    _createTimer: function(direction)
    {
        this._timer.dispose();

        this._timer = new Sys.Timer();
        this._timer.set_interval(this._timerSpeed);
        var handler = Function.createDelegate(this, function(timer) { this._fade(-1); });
        this._timer.add_tick(handler);

        this._timer.set_enabled(true);
    },

    _fade: function(direction)
    {
        var d = direction;
        var a = this._alpha;

        if ((a != this._endalpha && d == 1) || (a != 0 && d == -1))
        {
            var i = this._speed;

            if (this._endalpha - a < this._speed && d == 1)
                i = this._endalpha - a;
            else if (this._alpha < this._speed && d == -1)
                i = a;

            this._alpha = a + (i * d);
            this._tt.style.opacity = this._alpha * .01;

            this._tt.style.filter = 'alpha(opacity=' + this._alpha + ')';
        }
        else
        {
            if (d == -1)
            {
                this._tt.style.display = 'none';

                var e = this.get_element();
                if (e.onmousemove != null)
                    $removeHandler(e, 'mousemove', e.onmousemove);

                if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7)
                    this._hideDropDowns(true);

            }

            this._timer.dispose();
        }
    },

    _hideDropDowns: function(flag)
    {
        var tags = document.getElementsByTagName('select');
        for (var i = 0; i < tags.length; i++)
            tags[i].style.display = (flag ? 'none' : '');
    }

}

Viva.Web.UI.AjaxControls.TooltipBehavior.registerClass('Viva.Web.UI.AjaxControls.TooltipBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();