﻿Type.registerNamespace('Framework.Controls');

Framework.Controls.CollapsePanelExpandDirection = function() {
    throw Error.invalidOperation();
}
Framework.Controls.CollapsePanelExpandDirection.prototype = {
    Horizontal: 0,
    Vertical: 1
}

Framework.Controls.CollapsePanelExpandDirection.registerEnum("Framework.Controls.CollapsePanelExpandDirection", false);

Framework.Controls.CollapsePanelBehavior = function(element) {
    Framework.Controls.CollapsePanelBehavior.initializeBase(this, [element]);
    this._el = element;
    this._InitCollapsed = false;
    this._collapsed = false;
    this._expandControlID = null;
    this._collapseControlID = null;
    this._expandControlClientID = null;
    this._collapseControlClientID = null;
    this._callback = "";
    this._expandDirection = Framework.Controls.CollapsePanelExpandDirection.Vertical;
    this._suppressPostBack = null;
    this._speed = 1000;
    this._EnableClick = true;
    this._EnableMouseOver = false;
    this._AutoHide = false;
    this._EnableMouseOut = false;
    this._isClick = false;
    this._isPinned = 0; // 0 = state not defined, 1 = not pinned, 2 = pinned
    this._EnablePin = false;
    this._expandClickHandler = null;
    this._collapseClickHandler = null;
    this._expandOverHandler = null;
    this._collapseOverHandler = null;
    this._expandOutHandler = null;
    this._collapseOutHandler = null;
    this._toggleClickHandler = null;
    this._collapseCompleteHandler = null;
    this._expandCompleteHandler = null;
    this._OnClientExpand = null;
    this._OnClientCollapse = null;
    this._EnableTargetMouseOut = null;
    this._TargetMouseOutExtendedControlClientID = null;
    this._isAnimating = false;
}
Framework.Controls.CollapsePanelBehavior.prototype = {
    initialize: function() {
        Framework.Controls.CollapsePanelBehavior.callBaseMethod(this, 'initialize');
        if (!this._el) return;

        if (this._suppressPostBack == null) {
            if (this._el.tagName == "INPUT" && this._el.type == "checkbox") this._suppressPostBack = false;
            else if (this._el.tagName == "A") this._suppressPostBack = true;
        }

        if (this._EnableClick) {
            if (this._expandControlClientID == this._collapseControlClientID) {
                this._toggleClickHandler = Function.createDelegate(this, this.togglePanel);
                $addHandler($get(this._expandControlClientID), "click", this._toggleClickHandler);
            } else {
                this._expandClickHandler = Function.createDelegate(this, this._expandClick);
                this._collapseClickHandler = Function.createDelegate(this, this._collapseClick);
                $addHandler($get(this._collapseControlClientID), "click", this._collapseClickHandler);
                $addHandler($get(this._expandControlClientID), "click", this._expandClickHandler);
            }
        }

        if (this._EnableMouseOver) {
            this._expandOverHandler = Function.createDelegate(this, this._expandOver);
            this._collapseOverHandler = Function.createDelegate(this, this._collapseOver);
            $addHandler($get(this._expandControlClientID), "mouseover", this._expandOverHandler);
            $addHandler($get(this._collapseControlClientID), "mouseover", this._collapseOverHandler);
        }

        if (this._EnableMouseOut) {
            this._expandOutHandler = Function.createDelegate(this, this._expandOut);
            this._collapseOutHandler = Function.createDelegate(this, this._collapseOut);
            $addHandler($get(this._expandControlClientID), "mouseout", this._expandOutHandler);
            $addHandler($get(this._collapseControlClientID), "mouseout", this._collapseOutHandler);
            $addHandler(document, "mousedown", this._collapseOutHandler);
        }

        if (this._EnableTargetMouseOut) {
            $addHandler(this._el, "mouseout", this._collapseOutHandler);
        }

        this._expandCompleteHandler = Function.createDelegate(this, this._expandComplete);
        this._collapseCompleteHandler = Function.createDelegate(this, this._collapseComplete);
    },
    dispose: function() {
        if (this._EnableClick) {
            if (this._expandControlClientID == this._collapseControlClientID) {
                $removeHandler($get(this._expandControlClientID), "click", this._toggleClickHandler);
            } else {
                $removeHandler($get(this._collapseControlClientID), "click", this._collapseClickHandler);
                $removeHandler($get(this._expandControlClientID), "click", this._expandClickHandler);
            }
        }

        if (this._EnableMouseOver) {
            $removeHandler($get(this._expandControlClientID), "mouseover", this._expandOverHandler);
            $removeHandler($get(this._collapseControlClientID), "mouseover", this._collapseOverHandler);
        }

        if (this._EnableMouseOut) {
            $removeHandler($get(this._expandControlClientID), "mouseout", this._expandOutHandler);
            $removeHandler($get(this._collapseControlClientID), "mouseout", this._collapseOutHandler);
            $removeHandler(document, "mousedown", this._collapseOutHandler);
        }

        if (this._EnableTargetMouseOut) {
            $removeHandler(this._el, "mouseout", this._collapseOutHandler);
        }

        this._expandCompleteHandler = null;
        this._collapseCompleteHandler = null;

        Framework.Controls.CollapsePanelBehavior.callBaseMethod(this, 'dispose');
    },

    _expandClick: function(e) {
        this._isClick = true;

        if (this._EnablePin) {
            // First time pin, do not change collapsed state
            if (this._isPinned == 0) {
                this._isPinned = 2;
                this._callbackState(this._collapsed);
                return;
            }

            // Control was pinned before so this time we change state
            if (this._isPinned > 0) {
                // Check if state is equal to the state we started with
                if (this._InitCollapsed == false) {
                    // Reset pin
                    this._isPinned = 0;
                    this._collapsed = false;
                    this._callbackState(this._collapsed);
                    return;
                }
            }
        } else {
            this._collapsed = false;
        }

        this.expandPanel(true);
    },
    _collapseClick: function(e) {
        this._isClick = true;

        if (this._EnablePin) {
            // First time pin, do not change collapsed state
            if (this._isPinned == 0) {
                this._isPinned = 2;
                this._callbackState(this._collapsed);
                return;
            }

            // Control was pinned before so this time we change state
            if (this._isPinned > 0) {
                // Check if state is equal to the state we started with
                if (this._InitCollapsed == true) {
                    // Reset pin
                    this._isPinned = 0;
                    this._collapsed = true;
                    this._callbackState(this._collapsed);
                    return;
                }
            }
        } else {
            this._collapsed = true;
            if (!this._EnableMouseOver && !this._EnableMouseOut) {
                this._callbackState(this._collapsed);
            }
        }

        this.collapsePanel(true);
    },
    _expandOver: function(e) {
        this._isClick = false;
        if (this._isPinned > 0) return;
        this.expandPanel(true);
    },
    _collapseOver: function(e) {
        this._isClick = false;
    },
    _expandOut: function(e) {
        this._isClick = false;
    },
    _collapseOut: function(e) {
        this._isClick = false;
        if (this._isPinned > 0) return;
        if (this._TargetMouseOutExtendedControlClientID) {
            if (e && (Utils.isHit(e, this._el) ||
                Utils.isHit(e, $get(this._expandControlClientID)) ||
                Utils.isHit(e, $get(this._collapseControlClientID)) ||
                Utils.isHit(e, $get(this._TargetMouseOutExtendedControlClientID)))) return;
        } else {
            if (e && (Utils.isHit(e, this._el) ||
                Utils.isHit(e, $get(this._expandControlClientID)) ||
                Utils.isHit(e, $get(this._collapseControlClientID)))) return;
        }

        setTimeout(Function.createDelegate(this, this.collapsePanel), 200);
    },
    togglePanel: function(e) {
        if (this._collapsed) this.expandPanel(true);
        else this.collapsePanel(true);
    },
    expandPanel: function(persists) {
        if (this._isAnimating) return;
        this._isAnimating = true;

        // Toggle collapse / expand controls
        if (this._AutoHide) {
            if ($get(this._collapseControlClientID)) $get(this._collapseControlClientID).style.display = "";
            if ($get(this._expandControlClientID)) $get(this._expandControlClientID).style.display = "none";
        }

        if (!this._EnableMouseOver && !this._EnableMouseOut) {
            this._isClick = true;
            this._collapsed = false;
        }

        // Collapse and store state
        $(this._el).slideDown(this._speed, this._expandCompleteHandler);
        if (!persists) this._isPinned = 0;
        this._callbackState(this._collapsed);

        // Call client event
        if (this._OnClientExpand) eval(this._OnClientExpand);
        this._collapsed = false;
    },
    _expandComplete: function() {
        this._collapsed = false;
        this._isAnimating = false;
    },
    collapsePanel: function(persists) {
        if (this._isAnimating) return;
        this._isAnimating = true;

        // Toggle collapse / expand controls
        if (this._AutoHide) {
            if ($get(this._collapseControlClientID)) $get(this._collapseControlClientID).style.display = "none";
            if ($get(this._expandControlClientID)) $get(this._expandControlClientID).style.display = "";
        }

        if (!this._EnableMouseOver && !this._EnableMouseOut) {
            this._isClick = true;
            this._collapsed = true;
        }

        // Collapse and store state
        $(this._el).slideUp(this._speed, this._collapseCompleteHandler);
        if (!persists) this._isPinned = 0;
        this._callbackState(this._collapsed);

        // Call client event
        if (this._OnClientCollapse) eval(this._OnClientCollapse);
        this._collapsed = true;
    },
    _collapseComplete: function() {
        this._collapsed = true;
        this._isAnimating = false;
    },
    _callbackState: function(state) {
        if (!this._isClick) return;
        if (!this._callback || this._callback == "") return;
        var clbck = this._callback.replace(/cpeState/, state);
        var clbck = clbck.replace(/cpePinned/, this._isPinned);
        eval(clbck);
    },

    get_Collapsed: function() {
        return this._collapsed;
    },
    set_Collapsed: function(value) {
        this._collapsed = value;
    },
    get_CollapseControlID: function() {
        return this._collapseControlID;
    },
    set_CollapseControlID: function(value) {
        this._collapseControlID = value;
    },
    get_ExpandControlID: function() {
        return this._expandControlID;
    },
    set_ExpandControlID: function(value) {
        this._expandControlID = value;
    },
    get_CollapseControlClientID: function() {
        return this._collapseControlClientID;
    },
    set_CollapseControlClientID: function(value) {
        this._collapseControlClientID = value;
    },
    get_ExpandControlClientID: function() {
        return this._expandControlClientID;
    },
    set_ExpandControlClientID: function(value) {
        this._expandControlClientID = value;
    },
    get_Callback: function() {
        return this._callback;
    },
    set_Callback: function(value) {
        this._callback = value;
    },
    get_ExpandDirection: function() {
        return this._expandDirection == Framework.Controls.CollapsePanelExpandDirection.Vertical;
    },
    set_ExpandDirection: function(value) {
        this._expandDirection = value;
    },
    get_SuppressPostBack: function() {
        return this._suppressPostBack;
    },
    set_SuppressPostBack: function(value) {
        this._suppressPostBack = value;
    },
    get_Speed: function() {
        return this._speed;
    },
    set_Speed: function(value) {
        this._speed = value;
    },
    get_EnableClick: function() {
        return this._EnableClick;
    },
    set_EnableClick: function(value) {
        this._EnableClick = value;
    },
    get_EnableMouseOver: function() {
        return this._EnableMouseOver;
    },
    set_EnableMouseOver: function(value) {
        this._EnableMouseOver = value;
    },
    get_AutoHide: function() {
        return this._AutoHide;
    },
    set_AutoHide: function(value) {
        this._AutoHide = value;
    },
    get_EnableMouseOut: function() {
        return this._EnableMouseOut;
    },
    set_EnableMouseOut: function(value) {
        this._EnableMouseOut = value;
    },
    get_EnablePin: function() {
        return this._EnablePin;
    },
    set_EnablePin: function(value) {
        this._EnablePin = value;
    },
    get_InitCollapsed: function() {
        return this._InitCollapsed;
    },
    set_InitCollapsed: function(value) {
        this._InitCollapsed = value;
    },
    get_IsPinned: function() {
        return this._isPinned;
    },
    set_IsPinned: function(value) {
        this._isPinned = value;
    },
    get_OnClientExpand: function() {
        return this._OnClientExpand;
    },
    set_OnClientExpand: function(value) {
        this._OnClientExpand = value;
    },
    get_OnClientCollapse: function() {
        return this._OnClientCollapse;
    },
    set_OnClientCollapse: function(value) {
        this._OnClientCollapse = value;
    },
    get_EnableTargetMouseOut: function() {
        return this._EnableTargetMouseOut;
    },
    set_EnableTargetMouseOut: function(value) {
        this._EnableTargetMouseOut = value;
    },
    get_TargetMouseOutExtendedControlClientID: function() {
        return this._TargetMouseOutExtendedControlClientID;
    },
    set_TargetMouseOutExtendedControlClientID: function(value) {
        this._TargetMouseOutExtendedControlClientID = value;
    }
}
Framework.Controls.CollapsePanelBehavior.registerClass('Framework.Controls.CollapsePanelBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();