﻿Type.registerNamespace('RateCalc.UI.Shared.CustomServerControls.AjaxExtenders');

RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.DisableControlBehavior = function(element) {
    RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.DisableControlBehavior.initializeBase(this, [element]);

    // Properties
    this._disableCssClass = null;

    // Variables
    this._backgroundElement = null;
    this._resizeHandler = null;
    this._isEnabled = null;
}

RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.DisableControlBehavior.prototype = {

    initialize : function() {
        RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.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;
    
        RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.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;
    }
}

RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.DisableControlBehavior.registerClass('RateCalc.UI.Shared.CustomServerControls.AjaxExtenders.DisableControlBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();