﻿Type.registerNamespace('MattBerseth.WebControls.AJAX.DisableControl');

MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior = function(element) {
    MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior.initializeBase(this, [element]);

    // Properties
    this._disableCssClass = null;
    
    // Variables
    this._backgroundElement = null;
    this._resizeHandler = null;
    this._isEnabled = null;
}

MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior.prototype = {

    initialize : function() {
        MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior.callBaseMethod(this, 'initialize');

        if(!this._backgroundElement){
            //  create the div that we will position over the disabled element
            this._backgroundElement = document.createElement('div');
            this._backgroundElement.className = this.get_DisableCssClass();    
            //  make the zIndex very large so we can be sure this div
            //  will cover the element
            this._backgroundElement.style.zIndex = 10000;  
            this._backgroundElement.style.display = 'none';  
            
            //  let us know when the window is resized so we can reposition the div
            this._resizeHandler = Function.createDelegate(this, this._onResize);
            $addHandler(window, 'resize', this._resizeHandler);
            
            //  add the element to the DOM
            this.get_element().parentNode.appendChild(this._backgroundElement);   
            
            //  default to true
            this._isEnabled = true;                   
        }
    },

    dispose : function() {
        //  detach the resize event handler
        $removeHandler(window, 'resize', this._resizeHandler);
        this._resizeHandler = null;
    
        MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior.callBaseMethod(this, 'dispose');
    },
    
    disable : function() {
        if(this._isEnabled){
            //  position the div
            this._onLayout();  
            //  update the enabled state
            this._isEnabled = false;
        } 
    },

    enable : function() {
        if(!this._isEnabled){
            //  hide the element
            this._backgroundElement.style.display = 'none';
            //  update the enabled state
            this._isEnabled = true;
        }
    },

    _onResize : function() {
        if(!this._isEnabled){
            //  position the div
            this._onLayout();             
        }
    }, 

    _onLayout : function() {
        //  determine the boundary of the element
        var elementBounds = Sys.UI.DomElement.getBounds(this.get_element());                
        
        //  set the div's height/width to that of the element
        this._backgroundElement.style.width = elementBounds.width + 'px';
        this._backgroundElement.style.height = elementBounds.height + 'px';     
        
        //  place the div over the element
        Sys.UI.DomElement.setLocation(this._backgroundElement, elementBounds.x, elementBounds.y); 
                
        //  show the div
        this._backgroundElement.style.display = '';      
    },    

    get_DisableCssClass : function() {
        return this._disableCssClass;
    },

    set_DisableCssClass : function(value) {
        this._disableCssClass = value;
    }
}

MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior.registerClass('MattBerseth.WebControls.AJAX.DisableControl.DisableControlBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();