

/**
 * @class CoreModalPopUp creates a modal pop up "bubble"
 */
Core.ModalPopUp= function () {
    /**
     *  The layer of the whole modal window (object)
     */
    var _backgroundElement=null;

    /**
     * The pop up that will show up (object)
     */
    this._foregroundElement=null;

    /**
     * the Id of the foregroundElement (string)
     */
    this._PopupControlID=null;
    
    /**
     * the name of the function to be called before the popup opens
     */
    this.beforeOpen=null; //null;
    
    /**
     * the name of the function to be called before the popup closes
     */
    this.beforeClose=null;
    
    this._IsforegroundElementLarge=false;
    
   

    /**
     * _Initialize
     */
    this._Initialize=function() {
        if (_backgroundElement==null) {
            _backgroundElement = document.createElement('div');
            _backgroundElement.style.display = 'none'; //none
            _backgroundElement.style.position = 'absolute';
            _backgroundElement.className = 'modalBackground';
            document.body.appendChild(_backgroundElement);
        }
    }

    /**
     *  _InitializeForeground
     */
    this._InitializeForeground=function() {
        this._foregroundElement = $get(this._PopupControlID);
        this._foregroundElement.style.display = 'none';
        this._foregroundElement.style.position = 'absolute';
        document.body.appendChild(this._foregroundElement);
    }

    /**
      * Show Modal
      */
    this.ShowModal=function () {
        if ((_backgroundElement==null) || (typeof(_backgroundElement)=='undefined') ) {
            this._Initialize()
        }

        if (this._PopupControlID !=null) {
            this._InitializeForeground();
        }
        
        // [AS] - Set the background here.
        var clientWidth = 0;
        var clientHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            } 
        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
        } 
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            clientWidth = document.body.clientWidth;
            clientHeight = document.body.clientHeight;
        }

	    if (document.body.scrollHeight)
	    {
		    if (document.body.scrollHeight >= clientHeight)
		    {
			    clientHeight = document.body.scrollHeight;
		    }
		}
		
		_backgroundElement.style.left = '0px';
		_backgroundElement.style.top = '0px';
		_backgroundElement.style.width = document.body.clientWidth + 'px';
		_backgroundElement.style.height = clientHeight + 'px';
		_backgroundElement.style.display = 'block';

        // Reset for sizing the foreground element
        clientWidth =630;
        clientHeight=460;
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

        if (window.innerWidth) {
            clientWidth = (window.__safari ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
        }
        else {
            clientWidth = document.documentElement.clientWidth;
        }

        if (window.innerHeight) {
            clientHeight = (window.__safari ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
        }
        else {
            clientHeight = document.documentElement.clientHeight;
        }

        //show foreground element
        this._foregroundElement.style.display = 'block'; 
        //position it
        if (this._foregroundElement!=null) {
           this._foregroundElement.style.left = scrollLeft+((clientWidth-this._foregroundElement.offsetWidth)/2)+'px';
            //this._foregroundElement.style.top = scrollTop+((clientHeight-this._foregroundElement.offsetHeight-60)/2)+'px';
           //alert(this._IsforegroundElementLarge);
           if (this._IsforegroundElementLarge==true) {
           
              this._foregroundElement.style.top = scrollTop+((clientHeight-this._foregroundElement.offsetHeight)/4)+'px';
           
           }
           else {
             
             this._foregroundElement.style.top = scrollTop+((clientHeight-this._foregroundElement.offsetHeight)/2)+'px';
        
           }
             
                      
        }
        IS_MODELPOPUP_VISIBLE=true;
        if (this.beforeOpen!=null) {
             eval(this.beforeOpen + "();");
        
        }
     }

    /**
     * Hide with event
     */
    this.Hide=function() {
        this.HideModal();
        if (this.beforeClose!=null) {
             eval(this.beforeClose + "();");
        
        }
    }
    /**
     * Hide without event
     */
    this.HideModal=function() {
        if (_backgroundElement!=null) {
            _backgroundElement.style.display = 'none';
        }
        if (this._foregroundElement!=null) {
            this._foregroundElement.style.display = 'none';
        }
        IS_MODELPOPUP_VISIBLE=false;
        this._IsforegroundElementLarge=false;
    }
    

    /**
     * ResizeHandler 
     */
    this.ResizeHandler=function() {
//        [AS] - Disable the resize
//        if (_backgroundElement!=null && _backgroundElement.style.display != 'none' ){
//            this.ShowModal();
//        }
    }

    /** 
     * ScrollHandler    
     */
    this.ScrollHandler=function() {
//        [AS] - Disable the scroll
//        if (_backgroundElement!=null && _backgroundElement.style.display != 'none' ){
//            this.ShowModal();
//        }
    }
}
/*
 * It registers the WebServiceAPI_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ModalPopUp.registerClass('Core.ModalPopUp'); }

