﻿Ipopup = {
            TargetControlId:null,
            PopupControlId:null,
            CancelControlId:null,
            windowWidth:1000,
            windowHeight:500,
            popupBackgroundClass:'',
            $:function() {
                var elements = new Array();

                for (var i = 0; i < arguments.length; i++) {
                var element = arguments[i];
                if (typeof element == 'string')
                    element = document.getElementById(element);

                if (arguments.length == 1)
                    return element;

                elements.push(element);
                }

                return elements;
            },
            setMargin:function(){
               
                if( typeof( window.innerWidth ) == 'number' ) {
                    //Non-IE
                    this.windowWidth = window.innerWidth;
                    this.windowHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                    //IE 6+ in 'standards compliant mode'
                    this.windowWidth  = document.documentElement.clientWidth;
                    this.windowHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                    //IE 4 compatible
                    this.windowWidth  = document.body.clientWidth;
                    this.windowHeight = document.body.clientHeight;
                }
               
                var pW = this.Style.getWidth(this.$(this.PopupControlId));
                var pH = this.Style.getHeight(this.$(this.PopupControlId));

                var left = (this.windowWidth  - pW)/2;
                var top = (this.windowHeight - pH)/2;
                
                this.Style.setLeft(this.$(this.PopupControlId),3);
                this.Style.setTop(this.$(this.PopupControlId),top);
            },
            overlay:function(){
                var ovr = document.createElement('div');
                ovr.setAttribute('id','Ipopup_overlay');
                this.Style.setLeft(ovr,'0');
                this.Style.setTop(ovr,'0');
                this.Style.setTop(ovr,'0');
                this.Style.setZindex(ovr,100000);
                this.Style.setWidth(ovr,this.windowWidth);
                this.Style.setHeight(ovr,this.windowHeight);
                this.Style.setPosition(ovr,'fixed');
                if(this.popupBackgroundClass!=''){
                    this.Style.setClass(ovr,this.popupBackgroundClass);
                }
                this.$(this.PopupControlId).parentNode.appendChild(ovr);
                Ipopup.$('Ipopup_overlay').style.visibility = 'hidden';
            },
            show:function(){
                Ipopup.$(Ipopup.PopupControlId).style.visibility = 'visible';
                Ipopup.$('Ipopup_overlay').style.visibility = 'visible';
            },
            hide:function(){
                Ipopup.$(Ipopup.PopupControlId).style.visibility = 'hidden';
                Ipopup.$('Ipopup_overlay').style.visibility = 'hidden';
            },
            Style:{
               setWidth:function(ele,width){
                   ele.style.width = width + 'px'; 
               },
               setHeight:function(ele,height){
                    ele.style.height = height + 'px';
               },
               getWidth:function(ele){
                    if(ele.style&&ele.style.width){
                        return ele.style.width.replace('px');
                    }else
                        return ele.offsetWidth;
               },
               getHeight:function(ele){
                    if(ele.style&&ele.style.height){
                        return ele.style.height.replace('px');
                    }
                    else
                        return ele.offsetHeight;
               },
               setLeft:function(ele,left){
                    ele.style.left = left + 'px';
               },
               setTop:function(ele,top){
                    ele.style.top = top + 'px';
               },
               setZindex:function(ele,zIndex){
                    ele.style.zIndex = zIndex;
               },
               setClass:function(ele,className){
                    ele.className = className;
               },
               setPosition:function(ele,position){
                    ele.style.position = position;
               }
            },
            initialize:function(){
                if(Ipopup.TargetControlId==null){
                    alert('Target control id could not be empty.');
                    return false;
                }
                else if(Ipopup.PopupControlId==null){
                    alert('Popup control id could not be empty.');
                    return false;
                }
                else{
                    Ipopup.$(Ipopup.TargetControlId).onclick = Ipopup.show;
                    Ipopup.Style.setPosition(Ipopup.$(Ipopup.PopupControlId),'absolute');
                    Ipopup.setMargin();
                    Ipopup.overlay();
                    Ipopup.Style.setZindex(Ipopup.$(Ipopup.PopupControlId),100001);
                    if(Ipopup.CancelControlId!=null){
                        Ipopup.$(Ipopup.CancelControlId).onclick = Ipopup.hide;
                    }
                }
            },
            addLoadEvent: function (func) {
                var oldonload = window.onload;
                if (typeof window.onload != 'function') {
                    window.onload = func;
                }
                else {
                    window.onload = function () {
                        oldonload();
                        func();
                    }
                }
            } 
        }
        
        Ipopup.TargetControlId = 'anchrControl';
        Ipopup.PopupControlId = 'IpopupWindow';
        Ipopup.popupBackgroundClass = 'Ipopup-background';
        Ipopup.CancelControlId = 'closeWindow';
        Ipopup.addLoadEvent(Ipopup.initialize);