﻿//Register the namespace for control class.
Type.registerNamespace("OeS");

//ModalDialog Control

/*
    ModalDialog control
    
    Define control's constructor function to receive a
    DOM element arugment.
*/
OeS.ModalDialog = function(element) {

    //Call base class initializer method and pass DOM element.
    OeS.ModalDialog.initializeBase(this, [element]);
    
    //Private properites used by control
    this._textValue = "";
    this._captionValue = "";
    this._cssClassValue = "";
    this._backgroundElement = null;
    this._foregroundElement = null;
    this._xCoordinate = -1;
    this._yCoordinate = -1;
    this._BackgroundCssClass = "";
    this._resizeHandler = null;
    this._scrollHander = null;
    this._defaultHandler = null;
    this._windowHandlersAttached = false;
    this._callback = null;
    this._closeElementId = "";
    this._OKElementId = "";
    this._cancelElementId = "";
}

//Define a prototype for control
OeS.ModalDialog.prototype = {
    
    //text property accessors
    get_text: function() {
        return this._textValue;
    },
    set_text: function(value) {
        this._textValue = value;
    },
    
    //cssClass property accessors
    get_cssClass: function() {
        return this._cssClassValue;
    },
    set_cssClass: function(value) {
        this._cssClassValue = value;
    },
    
    //background css class property accessors
    get_backgroundCssClass: function() {
        return this._BackgroundCssClass;
    },
    set_backgroundCssClass: function(value) {
        this._BackgroundCssClass = value;
    },
    get_closeElementId : function()
    {
        return this._closeElementId;
    },
    set_closeElementId : function(value)
    {
        this._closeElementId = value;
    },
    //OK element
    get_OKElementId : function()
    {
        return this._OKElementId;
    },
    set_OKElementId : function(value)
    {
        this._OKElementId = value;
    },
    //Cancel element
    get_cancelElementId : function()
    {
        return this._cancelElementId;
    },
    set_cancelElementId : function(value)
    {
        this._cancelElementId = value;
    },    
    /*
    Overide the base class Sys.UI.Control.initialize method.
    
    This is where properties and event listners are initialized.
    
    */
    initialize: function() {
        var element = this.get_element();
              
        //Initialize the modal background elements used by the popup.
        this._foregroundElement = element;
        this._backgroundElement = document.createElement('div');
        this._backgroundElement.style.display = 'none';
        this._backgroundElement.style.position = 'absolute';
        this._backgroundElement.style.left = '0px';
        this._backgroundElement.style.top = '0px';
        // Want zIndex to big enough that the background sits above everything else
        // CSS 2.1 defines no bounds for the <integer> type, so pick arbitrarily
        this._backgroundElement.style.zIndex = 100000;
        if (this._BackgroundCssClass) {
            this._backgroundElement.className = this._BackgroundCssClass;
        }
        this._foregroundElement.parentNode.appendChild(this._backgroundElement);

        this._foregroundElement.style.display = 'none';
        this._foregroundElement.style.position = 'absolute';
        this._foregroundElement.style.zIndex = 100001;
        
        
        //Setup event delegates for Window events scroll and resize.
        this._scrollHandler = Function.createDelegate(this, this._onLayout);
        this._resizeHandler = Function.createDelegate(this, this._onLayout);
        
        //The default function to run after any popup buttons are clicked.
        this._defaultHandler = Function.createDelegate(this,this._onHide);
        
        
        var closeElement = $get(this._closeElementId);
        if (closeElement != null)
        {
            $addHandler(closeElement,"click",this._defaultHandler);
        }
        
        var okElement = $get(this._OKElementId);
        if (okElement != null)
        {
            $addHandler(okElement,"click",this._defaultHandler);
        }
        
        var cancelElement = $get(this._cancelElementId);
        if (cancelElement != null)
        {
            $addHandler(cancelElement,"click",this._defaultHandler);
        }
        
    
    },

    _attachPopup : function() {
        /// <summary>
        /// Attach the event handlers for the popup
        /// </summary>


        $addHandler(window, 'resize', this._resizeHandler);
        $addHandler(window, 'scroll', this._scrollHandler);
        this._windowHandlersAttached = true;
    },
    _onLayout: function() {
        this._layout();
    },
    _onHide: function(e) {
        this._hide();
    },
    _detachPopup : function() {
        /// <summary>
        /// Detach the event handlers for the popup
        /// </summary>

        if (this._windowHandlersAttached) {
            if (this._scrollHandler) {
                $removeHandler(window, 'scroll', this._scrollHandler);
            }

            if (this._resizeHandler) {
                $removeHandler(window, 'resize', this._resizeHandler);
            }
            this._windowHandlersAttached = false;
        }
        


    },

    show: function(caption) {        
            var element = this._foregroundElement;        
            
            this._attachPopup();
            this._backgroundElement.style.display = '';
            this._foregroundElement.style.display = '';
            this._layout();
            
            var closeElement = $get(this._closeElementId);
            if (closeElement != null)
            {
                closeElement.innerHTML = caption;
            }
            this._layout();
            
        
    },
    close: function() {
        this._hide();
    },
    _createButton: function(parent,text) {
        var button = document.createElement("button");
        button.style.width="75px";
        button.id = text+"Button";
        
        /*
            You must set the type to "button" for Mozilla or it 
            will default to type "submit" and cause an unwanted 
            postback
        */
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer)
        {
            button.type = "button";
        }      
        
        button.appendChild(document.createTextNode(text));
        parent.appendChild(button);

    },    
    _layout : function() {
        /// <summary>
        /// Position the modal dialog in the center of the screen
        /// </summary>

        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var clientBounds = this._getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        var elementWidth = Math.max(parseInt(this._foregroundElement.style.width),this._foregroundElement.offsetWidth);
        var elementHeight = Math.max(parseInt(this._foregroundElement.style.height),this._foregroundElement.offsetHeight);
        var b = Sys.UI.DomElement.getBounds(this._foregroundElement);
        
        
        
        this._backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
        this._backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';
        
        var isIE6 = (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7);
        if(this._xCoordinate < 0)
        {
            this._foregroundElement.style.left = scrollLeft+((clientWidth-this._foregroundElement.offsetWidth)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                this._foregroundElement.style.position = 'absolute';
                this._foregroundElement.style.left = (this._xCoordinate + scrollLeft) + 'px';
            }
            else
            {
                this._foregroundElement.style.position = 'fixed';
                this._foregroundElement.style.left = this._xCoordinate + 'px';
            }
        }
        if(this._yCoordinate < 0)
        {
            this._foregroundElement.style.top = scrollTop+((clientHeight-this._foregroundElement.offsetHeight)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                this._foregroundElement.style.position = 'absolute';
                this._foregroundElement.style.top = (this._yCoordinate + scrollTop) + 'px';
            }
            else
            {
                this._foregroundElement.style.position = 'fixed';
                this._foregroundElement.style.top = this._yCoordinate + 'px';
            }
        }
        
        
    },    
    _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);
    },
    _hide: function() {
        this._detachPopup();
        this._backgroundElement.style.display = 'none';
        this._foregroundElement.style.display = 'none';
    }
}

OeS.ModalDialog.registerClass('OeS.ModalDialog', Sys.UI.Control);
// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();