﻿var currentVisibleMenu = null;

Type.registerNamespace('Kennedy.Controls');

Kennedy.Controls.ExpandablePanel = function(element) { 
    Kennedy.Controls.ExpandablePanel.initializeBase(this, [element]);

    this._panelDiv = null;
    this._shouldBeClosedByTimeout = false;
    this._shouldBeOpenedByTimeout = false;
    this._interval = 50;
    this._expandValue = 10;
    this._currentDivHeight = 0;
    this._expandHeight = 100;
    this._closeTimer = null;
    this._isAlwaysOpened = false;
}

Kennedy.Controls.ExpandablePanel.prototype = {
    initialize : function() {
        Kennedy.Controls.ExpandablePanel.callBaseMethod(this, 'initialize');
        $addHandlers(this.get_element(), { 'mouseover' : this._onControlOver }, this);
        $addHandlers(this.get_element(), { 'mouseout' : this._onControlLeave }, this);
        $addHandlers($get(this._panelDiv), { 'mouseout' : this._onPanelLeave }, this);
        $addHandlers($get(this._panelDiv), { 'mouseover' : this._onPanelOver }, this);
        $get(this._panelDiv).style.display = "none";
        $get(this._panelDiv).style.height= "0px";
        $get(this._panelDiv).style.overflow = "hidden";
        
        if (this.get_isAlwaysOpened()) {
            this.expandPanelWithoutAnimation();
        }
    },

    dispose : function() {
        $clearHandlers(this.get_element());
        Kennedy.Controls.ExpandablePanel.callBaseMethod(this, 'dispose');
    },

    _onControlOver : function(e) {
        this._shouldBeClosedByTimeout = false;
        if (!this._shouldBeClosedByTimeout)
        {
            window.setTimeout(Function.createDelegate(this, this.showPanel), 100);
            this._shouldBeOpenedByTimeout = true;
        }
    },
    
    _onControlLeave : function(e) {
        if (!this._shouldBeClosedByTimeout || this._shouldBeOpenedByTimeout)
        {
            this._shouldBeClosedByTimeout = true;
            this._shouldBeOpenedByTimeout = false;
            window.clearTimeout(this._closeTimer);
            this._closeTimer = window.setTimeout(Function.createDelegate(this, this.hidePanel), 3000);
        }
    },
    
    
    _onPanelOver : function(e) {
        this._shouldBeClosedByTimeout = false;
        if (!this._shouldBeClosedByTimeout )
        {
           window.setTimeout(Function.createDelegate(this, this.showPanel), 100);
            this._shouldBeOpenedByTimeout = true;
        }
    },
    
    _onPanelLeave : function(e) {
        if (!this._shouldBeClosedByTimeout || this._shouldBeOpenedByTimeout)
        {
            this._shouldBeClosedByTimeout = true;
            this._shouldBeOpenedByTimeout = false;
            window.clearTimeout(this._closeTimer);
           this._closeTimer = window.setTimeout(Function.createDelegate(this, this.hidePanel), 3000);
        }
    },

    hidePanel : function() {
        if (this._shouldBeClosedByTimeout && this._currentDivHeight >=  this._expandHeight )
        {
            this._shouldBeClosedByTimeout = false;
            this.collapsePanel();
        }
    },

    showPanel : function() {
        if (this._shouldBeOpenedByTimeout)
        {
            window.clearTimeout(this._closeTimer);
            var panel = $get(this._panelDiv);
            if(this._currentDivHeight == 0)
            {
                panel.style.display = "block";
                panel.style.height = "0px";
            }
            this._shouldBeOpenedByTimeout = false;
            this.expandPanel()
            
        }
    },
    
    expandPanel : function() {
            var panel = $get(this._panelDiv);
            if(this._currentDivHeight < this._expandHeight)
            {
                panel.style.height = this._currentDivHeight + this._expandValue + "px";
                this._currentDivHeight += this._expandValue;
                window.setTimeout(Function.createDelegate(this, this.expandPanel), this._interval);
            }
    },
    
    expandPanelWithoutAnimation : function() {
        var panel = $get(this._panelDiv);
        this._currentDivHeight = this._expandHeight;
        panel.style.height = this._expandHeight + "px";
        panel.style.display = "block";
    },
    
    collapsePanel : function() {
        if (this.get_isAlwaysOpened()) return;
        
        var panel = $get(this._panelDiv);
        if(this._currentDivHeight > 0)
        {
            panel.style.height = this._currentDivHeight - this._expandValue + "px";
            this._currentDivHeight -= this._expandValue;
            window.clearTimeout(this._closeTimer);
            this._closeTimer = window.setTimeout(Function.createDelegate(this, this.collapsePanel), this._interval);
        }
        else
        {
            panel.style.display = "none";
        }
    },
    
    
    //properties
    get_panelDiv : function() {
        return this._menuDiv;
    },

    set_panelDiv : function(value) {
         if(this._panelDiv !== value) {
            this._panelDiv = value;
            this.raisePropertyChanged('panelDiv');
        }
    },
    
    get_interval : function() {
        return this._interval;
    },

    set_interval : function(value) {
        if (this._interval !== value) {
            this._interval = value;
            this.raisePropertyChanged('interval');
        }
    },
    
    get_expandValue : function() {
        return this._expandValue;
    },

    set_expandValue : function(value) {
        if (this._expandValue !== value) {
            this._expandValue = value;
            this.raisePropertyChanged('expandValue');
        }
    },
    
     get_expandHeight : function() {
        return this._expandHeight;
    },

    set_expandHeight : function(value) {
        if (this._expandHeight !== value) {
            this._expandHeight = value;
            this.raisePropertyChanged('expandHeight');
        }
    },
    
    
    get_shouldBeClosedByTimeout : function() {
        return this._shouldBeClosedByTimeout;
    },

    set_shouldBeClosedByTimeout : function(value) {
        if (this._shouldBeClosedByTimeout !== value) {
            this._shouldBeClosedByTimeout = value;
            this.raisePropertyChanged('shouldBeClosedByTimeout');
        }
    },

    get_shouldBeOpenedByTimeout : function() {
        return this._shouldBeOpenedByTimeout;
    },

    set_shouldBeOpenedByTimeout : function(value) {
        if (this._shouldBeOpenedByTimeout !== value) {
            this._shouldBeOpenedByTimeout = value;
            this.raisePropertyChanged('shouldBeOpenedByTimeout');
        }
    },

    get_isAlwaysOpened : function() {
        return this._isAlwaysOpened;
    },

    set_isAlwaysOpened : function(value) {
        if (this._isAlwaysOpened !== value) {
            this._isAlwaysOpened = value;
            this.raisePropertyChanged('_isAlwaysOpened');
        }
    }
}

Kennedy.Controls.ExpandablePanel.registerClass('Kennedy.Controls.ExpandablePanel', Sys.UI.Control);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
