﻿Type.registerNamespace('Qbgroup.WebUI');

Qbgroup.WebUI.CalloutCorner = function() { throw Error.invalidOperation(); }
Qbgroup.WebUI.CalloutCorner.prototype =
{
    TopLeft: 0,
    TopRight: 1,
    BottomLeft: 2,
    BottomRight: 3
}

Qbgroup.WebUI.CalloutCorner.registerEnum("Qbgroup.WebUI.CalloutCorner", false);

Qbgroup.WebUI.Callout = function(element)
{
    Qbgroup.WebUI.Callout.initializeBase(this, [element]);
    
    // Inizializzazione
    
    this._target = null;
    this._ie6Shim = null;
    this._targetCorner = Qbgroup.WebUI.CalloutCorner.TopRight;
    this._boundCorner = Qbgroup.WebUI.CalloutCorner.TopLeft;
    this._cssClass = null;
    this._closeOnClick = true;
    this._closingTime = 250;
    
    this._closeTimeout = null;
    this._hasFocus = false;
    this._isOver = false;
    
    this._focusHandler = null;
    this._blurHandler = null;
    this._clickHandler = null;
    this._targetIsFocusBased = null;
    
    this._fadeFxStartedAt = null;
    this._fadeFxDuration = 250;
};

Qbgroup.WebUI.Callout.prototype =
{
    initialize : function()
    {
        Qbgroup.WebUI.Callout.callBaseMethod(this, 'initialize');
        
        // DOM di supporto
        
        this.get_element().className = this.get_cssClass();
        
        if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7))
        {
            try
            {
                this._ie6Shim = document.createElement('IFRAME');
                this._ie6Shim.style.position = 'absolute';
                this._ie6Shim.style.display = 'block';
                this._ie6Shim.style.zIndex = '-1';
                this._ie6Shim.style.top = '0';
                this._ie6Shim.style.left = '0';
                this._ie6Shim.style.filter = 'mask()';
                this._ie6Shim.frameBorder = 'no';
                
                this.get_element().appendChild(this._ie6Shim);
            }
            catch(ex)
            {
                this._ie6Shim = null;
            }
        };        
        
        // Wireup eventi

        if (this.get_target() != null)
        {
            var tagName = this.get_target().tagName.toLowerCase();
            this._targetIsFocusBased = (tagName == 'input') || (tagName == 'textarea') || (tagName == 'select');
            
            this._focusHandler = Function.createDelegate(this, this._onFocus);
            this._blurHandler = Function.createDelegate(this, this._onBlur);
            this._mouseOverHandler = Function.createDelegate(this, this._onMouseOver);
            this._mouseOutHandler = Function.createDelegate(this, this._onMouseOut);
            this._clickHandler = Function.createDelegate(this, this._onClick);

            $addHandler(this.get_target(), this._targetIsFocusBased ? 'focus' : 'mouseover', this._focusHandler);
            $addHandler(this.get_target(), this._targetIsFocusBased ? 'blur' : 'mouseout', this._blurHandler);
            $addHandler(this.get_element(), 'mouseover', this._mouseOverHandler);
            $addHandler(this.get_element(), 'mouseout', this._mouseOutHandler);
            $addHandler(this.get_element(), 'click', this._clickHandler);
        }
        
        if (this.get_element())
        {
            this.get_element().style.visibility = 'hidden';
            this.get_element().style.display = 'block';
        }
    },
    
    dispose : function()
    {
        if (this.get_target() != null)
        {
            $removeHandler(this.get_element(), 'click', this._clickHandler);
            $removeHandler(this.get_element(), 'mouseout', this._mouseOutHandler);
            $removeHandler(this.get_element(), 'mouseover', this._mouseOverHandler);
            $removeHandler(this.get_target(), this._targetIsFocusBased ? 'blur' : 'mouseout', this._blurHandler);
            $removeHandler(this.get_target(), this._targetIsFocusBased ? 'focus' : 'mouseover', this._focusHandler);

            this._mouseOverHandler = null;
            this._mouseOutHandler = null;
            this._blurHandler = null;
            this._focusHandler = null;
        }
        
        Qbgroup.WebUI.Callout.callBaseMethod(this, 'dispose');
    },
    
    _onClick: function()
    {
        if (!this._closeOnClick)
            return;
            
        this._hasFocus = false;
        this._isOver = false;
        this._close();
    },
    
    _onFocus: function()
    {
        Sys.Debug.trace('_onFocus');
        
        this._keepAlive();

        if (this._hasFocus)
            return;

        if (this._isOver)
        {
            this._hasFocus = true;
            return;
        }
        
        var boundsTarget = Sys.UI.DomElement.getBounds(this.get_target());
        var boundsCallout = Sys.UI.DomElement.getBounds(this.get_element());
        
        // Recupero della nuova posizione del callout
        
        var x = boundsTarget.x;

        if ((this._targetCorner != Qbgroup.WebUI.CalloutCorner.TopLeft) && (this._targetCorner != Qbgroup.WebUI.CalloutCorner.BottomLeft))
            x += boundsTarget.width;
        if ((this._boundCorner != Qbgroup.WebUI.CalloutCorner.TopLeft) && (this._boundCorner != Qbgroup.WebUI.CalloutCorner.BottomLeft))
            x -= boundsCallout.width;

        var y = boundsTarget.y;

        if ((this._targetCorner != Qbgroup.WebUI.CalloutCorner.TopLeft) && (this._targetCorner != Qbgroup.WebUI.CalloutCorner.TopRight))
            y += boundsTarget.height;
        if ((this._boundCorner != Qbgroup.WebUI.CalloutCorner.TopLeft) && (this._boundCorner != Qbgroup.WebUI.CalloutCorner.TopRight))
            y -= boundsCallout.height;
        
        Sys.UI.DomElement.setLocation(this.get_element(),
            x + 1,
            y + 1);
        this.get_element().style.zIndex = '10000';
        
        if (this._ie6Shim)
        {
            this._ie6Shim.style.width = boundsCallout.width;
            this._ie6Shim.style.height = boundsCallout.height;
        }

        this.get_element().style.visibility = 'visible';
        this._hasFocus = true;
    },

    _onBlur: function()
    {
        Sys.Debug.trace('_onBlur');

        this._hasFocus = false;
        this._tryClose();
    },
    
    _onMouseOver: function()
    {
        Sys.Debug.trace('_onMouseOver');

        this._keepAlive();
        this._isOver = true;
    },

    _onMouseOut: function()
    {
        Sys.Debug.trace('_onMouseOut');

        this._isOver = false;
        this._tryClose();
    },
    
    _tryClose: function()
    {
        Sys.Debug.trace('TryClose');

        if (this._hasFocus || this._isOver)
            return;

        this._closeTimeout = window.setTimeout(Function.createDelegate(this, this._close), this._closingTime);
    },
    
    _close: function()
    {
        Sys.Debug.trace('Close');

        if (this.get_element())
            this.get_element().style.visibility = 'hidden';
    },
    
    _keepAlive: function()
    {
        window.clearTimeout(this._closeTimeout);
        this._closeTimeout = null;
    },
    
    // Proprietà del controllo

    get_target: function()
    {
        return this._target;
    },
    
    set_target: function(value)
    {
        this._target = value;
        this.raisePropertyChanged('target');
    },

    get_targetCorner: function()
    {
        return this._targetCorner;
    },
    
    set_targetCorner: function(value)
    {
        this._targetCorner = value;
        this.raisePropertyChanged('targetCorner');
    },

    get_boundCorner: function()
    {
        return this._boundCorner;
    },
    
    set_boundCorner: function(value)
    {
        this._boundCorner = value;
        this.raisePropertyChanged('boundCorner');
    },
    
    get_cssClass: function()
    {
        return this._cssClass;
    },
    
    set_cssClass: function(value)
    {
        this._cssClass = value;
        this.raisePropertyChanged('cssClass');
    },
    
    get_closingTime: function()
    {
        return this._closingTime;    
    },
    
    set_closingTime: function(value)
    {
        this._closingTime = value;
        this.raisePropertyChanged('closingTime');    
    },    
    
    get_closeOnClick: function()
    {
        return this._closeOnClick;
    },
    
    set_closeOnClick: function(value)
    {
        this._closeOnClick = value;
        this.raisePropertyChanged('closeOnClick');
    }
};

Qbgroup.WebUI.Callout.closeContainer = function Qbgroup$WebUI$Callout$closeContainer(element)
{
    var elCurrent = element;
    
    while (elCurrent)
    {
        if (elCurrent.control && Qbgroup.WebUI.Callout.isInstanceOfType(elCurrent.control))
        {
            elCurrent.control._close();
            break;
        }
        elCurrent = elCurrent.parentElement;
    }
};

Qbgroup.WebUI.Callout.registerClass('Qbgroup.WebUI.Callout', Sys.UI.Control);