﻿Type.registerNamespace('BIT.WebControls');


BIT.WebControls.PopUpPanel = function(element) 
{
    BIT.WebControls.PopUpPanel.initializeBase(this,[element]);
    
    this.addProperty('clientVisible', false);
    this.addProperty('blockMode', false);
    this.addProperty('collapseTimeout', 200);
    
    this.addProperty('clientStateField', null);
    
    this._pageRequestManager = null;
    this._handleEndRequestDelegate = null;
    
	this._animator = null;
	
	this.addProperty('animationType', BIT.WebControls.AnimationType.exp);
    this.addProperty('animationLength', 35);
    
    
    this.addProperty('useIframe', (Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7));
}

BIT.WebControls.PopUpPanel.prototype =
{
    set_useIframe: function(value) {
        if (this.get_isInitialized())
            throw Error.invalidOperation();

        this.useIframe = value;
    },

    set_clientVisible: function(value) {
        if (this.clientVisible == value)
            return;

        if (this.clientVisible = value)
            this.show();
        else
            this.hide();
    },

    setPosition: function(x, y) {
        if ((!x && x !== 0) && (!y && y !== 0)) {
            with (this.get_element().style) {
                position = null;
                top = null;
                left = null;
            }
        }
        else {
            with (this.get_element().style) {
                position = 'absolute';
                left = (x || x === 0) ? x + 'px' : null;
                top = (y || y === 0) ? y + 'px' : null
            }
        }
    },

    show: function() {
        var elm = this.get_element();

        if (this.useIframe && !this.blockMode && !elm.iframe)
            elm.iframe = $addIframe(elm);

        this._updClientVisible(true);

        $clearTimeout(this, '_timerId');
    },

    hide: function() {
        if (this.clientVisible)
            $setTimeout(this, '_timerId', function() { this._updClientVisible(false); }, this.collapseTimeout);
    },


    toggle: function() {
        if (this.clientVisible)
            this.hide();
        else
            this.show();
    },

    _updClientVisible: function(vis) {
        this.clientVisible = !!vis;
        this.saveClientState();

        var elm = this.get_element();

        with (elm.style) {
            overflow = 'hidden';
            position = (this.blockMode) ? '' : 'absolute';
        }

        if (this._animator) {
            Sys.UI.DomElement.setOpacity(elm, vis ? 0 : 1);

            with (elm.style) {
                height = vis ? '1px' : ((this.blockMode) ? '' : Math.min(elm.clientHeight, elm.scrollHeight) + 'px');
                display = 'block';
            }

            this._animator.set_reverse(!vis);
            this._animator.set_steps(this.animationLength / this._animator.get_delay());
            this._animator.set_type(this.animationType);

            this._animator.start();
        }
        else {
            this._animStateChanged();
        }
    },

    saveClientState: function() {
        if (this.get_clientStateField())
            this.get_clientStateField().value = "{clientVisible:" + (!!this.clientVisible).toString() + "}";
    },

    _animStepChanged: function(obj, args) {
        var val = obj.calc(0, 1);

        var elm = this.get_element();

        elm.style.height = Math.min(elm.clientHeight, elm.scrollHeight) * val + 'px';

        Sys.UI.DomElement.setOpacity(elm, val);
    },

    _animStateChanged: function(obj, args) {
        if (!args || args.get_state() == BIT.WebControls.AnimationState.stopped) {
            var elm = this.get_element();

            with (elm.style) {
                height = (this.clientVisible) ? ((this.blockMode) ? '' : Math.min(elm.clientHeight, elm.scrollHeight) + 'px') : '1px';
                display = (this.clientVisible) ? 'block' : 'none';
            }


            Sys.UI.DomElement.setOpacity(elm, (this.clientVisible) ? 1 : 0);

            if (document.recalc)
                document.recalc();
        }
    },


    initialize: function() {
        BIT.WebControls.PopUpPanel.callBaseMethod(this, "initialize");

        this.get_element()._object = window[this.get_id()] = this;

        if (!BIT.WebControls.Animator)
            throw Error.invalidOperation();

        this._handleEndRequestDelegate = Function.createDelegate(this, this._handleEndRequest);


        this._animStepDelegate = Function.createDelegate(this, this._animStepChanged);
        this._animStateDelegate = Function.createDelegate(this, this._animStateChanged);

        this._animator = $create(BIT.WebControls.Animator);
        this._animator.set_steps(0);

        this._animator.add_stepChanged(this._animStepDelegate);
        this._animator.add_stateChanged(this._animStateDelegate);

        if (Sys.WebForms && Sys.WebForms.PageRequestManager)
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        if (this._pageRequestManager != null)
            this._pageRequestManager.add_endRequest(this._handleEndRequestDelegate);
    },

    _handleEndRequest: function(sender, arg) {
        var dataItem = arg.get_dataItems()[this.get_id()];

        if (dataItem)
            this.set_clientVisible(dataItem);
    },


    dispose: function() {
        if (this._pageRequestManager != null)
            this._pageRequestManager.remove_endRequest(this._handleEndRequestDelegate);

        var elm = this.get_element();

        if (this._animator) {
            this._animator.remove_stateChanged(this._animStateDelegate);
            this._animator.remove_stepChanged(this._animStepDelegate);

            this._animator.dispose();
            this._animator = null;
        }

        if (elm.iframe) {
            elm.iframe.parentNode.removeChild(elm.iframe);
            elm.iframe = null;
        }

        this._animStepDelegate = null;
        this._animStateDelegate = null;

        elm._object = window[this.get_id()] = null;

        BIT.WebControls.PopUpPanel.callBaseMethod(this, "dispose");
    }
}

BIT.WebControls.PopUpPanel.registerClass('BIT.WebControls.PopUpPanel', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();