﻿// register namespace
Type.registerNamespace('AenP.Cms.UI.Shared');

AenP.Cms.UI.Shared.DynamicPanelHeightBehavior = function(element) { 
    AenP.Cms.UI.Shared.DynamicPanelHeightBehavior.initializeBase(this, [element]);

    this._minusHeight = null;
    this._minHeight = null;
}

//
// Create the prototype for the behavior.
//
AenP.Cms.UI.Shared.DynamicPanelHeightBehavior.prototype = {

    initialize : function() {
      AenP.Cms.UI.Shared.DynamicPanelHeightBehavior.callBaseMethod(this, 'initialize');

      // Attach the onresize and onscroll handler
      $addHandlers(window, {  scroll: this._resizePanelHeightHandler,
                              resize : this._resizePanelHeightHandler }, this);

      // resize on load
      this._resizePanelHeightHandler(null);
    },

    dispose : function() {
      $clearHandlers(this.get_element());
      AenP.Cms.UI.Shared.DynamicPanelHeightBehavior.callBaseMethod(this, 'dispose');
    },
    
    //
    // Event delegates
    //
    _resizePanelHeightHandler : function(e) {
      
      var panel = this.get_element();
      
      if (panel) {
          
          // get the client bounds
          var clientBounds = this._getClientBounds();
          // we only need the height of the client window
          var height = clientBounds.height;
          
          // get the current (actual) panelBounds
          var currentPanelHeight = Sys.UI.DomElement.getBounds(panel).height;
          
          // set the new height for the panel element
          var minusHeight = this._minusHeight;
          var minHeight = this._minHeight;
          
          // calc the desired panelHeight
          var disiredPanelHeight = Math.max(height - minusHeight, minHeight);

//          Sys.Debug.trace(" currentPanelHeight: " + currentPanelHeight +  
//                          "\n height: " + height + 
//                          "\n minusHeight: "  + minusHeight +
//                          "\n minHeight: "  + minHeight +
//                          "\n disiredPanelHeight: " + disiredPanelHeight +
//                          "\n ------------------------");
//        
          // only set new height when:
          // - difference between window height and minusHeight is greater than 0 
          // Ad 2007-10-16 no more // - difference between window height and minusHeight is greater than minHeight
          // Ad 2007-10-16 no more // - the current height of the panel is smaller than the window height and minusHeight
          //if ( (disiredPanelHeight > 0) 
            //&& (disiredPanelHeight > minHeight)
            //&& (currentPanelHeight < disiredPanelHeight)  ) {
          if (disiredPanelHeight > 0) {
                panel.style.minHeight = disiredPanelHeight + "px";
                // add an expression for ie 6 browser
                if (panel.style.setExpression) {
                  if (document.documentMode == undefined) { 
                    panel.style.setExpression("height", "this.scrollHeight < " + disiredPanelHeight + " ? '" + disiredPanelHeight + "px' : 'auto'");

                    // force panel update
                    panel.style.display = "none";
                    var dummy = panel.offsetHeight;
                    panel.style.display = "block";
                  } 
                }
                
//          Sys.Debug.trace(" panel.style.minHeight: " + panel.style.minHeight +  
//                          "\n ========================");
          } 
      }
    },

    //
    // Helper method (taken from AjaxControlToolkit)
    //
    
    _getClientBounds : function() {
        /// <summary>
        /// Gets the width and height of the browser client window (excluding scrollbars)
        /// </summary>
        /// <returns type="Sys.UI.Bounds">
        /// Browser's client width and height
        /// </returns>

        var clientWidth;
        var clientHeight;
        switch(Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
                break;
            case Sys.Browser.Safari:
                clientWidth = window.innerWidth;
                clientHeight = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.
                clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
                break;
        }
        return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
    },

    //
    // Behavior properties
    //
    get_minusHeight : function() {
        return this._minusHeight;
    },

    set_minusHeight : function(value) {
        if (this._minusHeight !== value) {
            this._minusHeight = value;
            this.raisePropertyChanged('minusHeight');
        }
    },
    
    get_minHeight : function() {
        return this._minHeight;
    },

    set_minHeight : function(value) {
        if (this._minHeight !== value) {
            this._minHeight = value;
            this.raisePropertyChanged('minHeight');
        }
    }


}
    
// Register the class as a type that inherits from Sys.UI.Control.
AenP.Cms.UI.Shared.DynamicPanelHeightBehavior.registerClass('AenP.Cms.UI.Shared.DynamicPanelHeightBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();