﻿function PopupClass(id,h)
{
    this.id = id;
    this.properties = null;
    this.context = window;
    this.container = null;
    this.frame = null;
    this.loading = null;
    this.visible = false;
    this.sc = null;
    this.handler = h;
    this.urlNotSetException = new Error ("URL argument not set!");
    this.propertiesNotSetException = new Error ("Properties argument not set!");
    this.tidNotSetException = new Error ("Template control ID argument not set or invalid!");
    
    this._g = function(p,d){return this.properties[p];}
    this._s = function(p,v,d){this.properties[p]=(v==null || v=="undefined" || v=='')?d:v;}
    this._is = function(p,o){if (!o) o=this.properties;if (o==null || o[p]=="undefined" || o[p]==null || o[p]=='') return false;return true;}
    this._r = function(p,d){if (this._is(p)) return;if (this._is(p,this.handler.properties)){this._s(p,this.handler.properties[p]);return;}this._s(p,d);}
    
    this.Show = function(){this.visible = true;if (this.container) this.container.style.display = "block";}
    this.Hide = function(){this.visible=false;if (this.container) this.container.style.display = "none";}
    
    this.Open = function(properties)
    {
        if (properties == null) throw this.propertiesNotSetException;
        this.properties = properties;
        if (!this._is("url")) throw this.urlNotSetException;
        
        this._r("url");this._r("w","auto");this._r("h","auto");this._r("class");
        this._r("sc");this._r("t");this._r("tid");this._r("loadingText","");this._r("onOpening");
        this._r("onOpen");this._r("onPosition");this._r("onClose");this._r("loadingClass");
        if (this._is("t")){this._s("tid",this._g("tid") + "_" + id);this._s("t",this._g("t").replace("[CONTAINER]",this._g("tid")));}
              
        this.container = document.createElement("div");
        document.body.appendChild(this.container);
        if (this._is("class")) this.container.className = this._g("class");
        
        this.frame = document.createElement("iframe");  
        this.frame.allowTransparency=true;
        
        this.frame.frameBorder=0;
		this.frame.style.border=0;
        this.frame.style.padding=0;
        this.frame.style.margin=0;
        this.frame.name="Zebra.Web.UI.Webcontrols.Popup";
        this.container.style.top="-9999px";
        this.container.style.left="-9999px";
        
        this.loading = document.createElement("div");
        if (this._is("loadingClass")) this.loading.className = this._g("loadingClass");
        this.loading.innerHTML = this.properties["loadingText"];
       
        var lH = this.container;         
        if (this._is("t"))
        {this.container.innerHTML=this._g("t");lH = document.getElementById(this._g("tid"));if (!lH) throw this.tidNotSetException;}
               
        lH.appendChild(this.frame);
        document.body.appendChild(this.loading);
        this.frame.src = this._g("url");
        AddEvent(document,"keydown",Popup.cesc);
        this.visible = true;
        if (this._is("onOpening")) eval(this._g("onOpening"));        
    }
    
    this.Close = function(e,cn,ca)
    {
          if (!e) e=false;
          if (!cn) cn='';
          if (!ca) ca='';
          if (this.container)
          {
            this.container.style.display="none";
            try{if (this.loading) document.body.removeChild(this.loading);}catch (e){}
            document.body.removeChild(this.container);
            
            if (this._is("onClose")) eval(this._g("onClose"));
            
            this.container = null;
            this.loading = null;
            this.frame=null;
            RemoveEvent(document,"keydown",Popup.cesc);
       
             if (e)
            {
                if (this._is("sc"))
                {
                    this.context.setTimeout("__doPostBack(\"" + this._g("sc") + "\",\"" + cn + "|" + ca + "\");",0);
                }
            }
          }
          var p=this.handler.glwr("h");
          if (p) p.Show();
          
           
          
    }
    
    this.OpenDone = function()
    {
        try {if (this.loading) document.body.removeChild(this.loading);} catch(e){};
        
        this.frame.style.display="block";
        this.Position();
        this.loading = null;
        if (this._is("onOpen")) eval(this._g("onOpen"));
    }
    
    this.Position = function(p)
    {
        if (!this.container) return;
        var fullH = this._g("h")=="max"?true:false;
        var fullW = this._g("w")=="max"?true:false;
        var autoH = this._g("h")=="auto"?true:false;
        var autoW = this._g("w")=="auto"?true:false;
        var isPostback = (p)?p:false;
        var clientSize = GetClientSize();
	    var newSize;
	    var newPosition;
	    var scrollPos = GetScrollXY();
	    var ibody = this.frame.contentWindow.document.getElementsByTagName("body")[0];
	    var lNSW = this._g("w");
	    var lNSH = this._g("h");
	    
	    if (lNSW=="auto") lNSW=ibody.offsetWidth;
	    if (lNSW=="max") lNSW=clientSize[0];
	    if (lNSH=="auto") lNSH=ibody.scrollHeight;
	    if (lNSH=="max") lNSH=clientSize[1];

	    newSize = [lNSW,lNSH];
	    newPosition = [(clientSize[0] - newSize[0])/2,(clientSize[1] - newSize[1])/2];
	    
	    if (newPosition[0] < 0) newPosition[0] = 0;
	    if (newPosition[1] < 0) newPosition[1] = 0;

        if (newSize[1] > (clientSize[1])){
		    newSize[1] = clientSize[1] ;	
		    
		    newPosition[1] = 0;
		    this.frame.scrolling = "auto";
	    } else {
		    this.frame.scrolling = "no";
	    }

        this.container.style.left = (parseInt(newPosition[0] + scrollPos[0])) + 'px';
        this.container.style.top = (parseInt(newPosition[1] + scrollPos[1])) + 'px';
	    this.frame.style.left = "0px";
	    this.frame.style.top = "0px";
        this.container.style.width = (parseInt(newSize[0])) + 'px';
	    this.container.style.height = (parseInt(newSize[1])) + 'px';
	    this.frame.style.width = (parseInt(newSize[0])) + 'px';
	    this.frame.style.height =  (parseInt(newSize[1])) + 'px';
	    if (this._is("onPosition")) eval(this._g("onPosition"));
    }
}

function PopupHandlerClass()
{
    this.id = null;
    this.window = null;
    this.properties = new Object();
    this.items = new Object();
    if (InitializePopup) InitializePopup(this);
    this.c = function(id){return new PopupClass(id,this);}
    this.cesc = function(e){CaptureEsc(e,"Popup.Close()");}
    this.num = function(){var l = 0;for (var i in this.items){if (this.items[i] && this.items[i].container) l++;} return l;}
    this.glwr = function(p)
    {
        var o=null;
        for (var i in this.items)
        {
            if (this.items[i] && this.items[i].Close)
            {
                switch(p)
                {
                    case "h":{if (this.items[i].visible==false && this.items[i].container) o =this.items[i];break;}
                    case "v":{if (this.items[i].visible==true && this.items[i].container) o =this.items[i];break;}
                    default: {o =this.items[i];break;}
                }
            }
         }
         return o;
    }
    
    this.Open = function(id,properties)
    {
         this.CloseAll();
         var l = this.items[id]?this.items[id]:this.c(id);
         l.Open(properties);
         this.items[id] = l;
    }
    
    this.Close = function(id){var o = null;if (!id){o=this.glwr("v");}else{o=this.items[id];}if (o){o.Close();}}
    this.CloseAll = function(){for (var i in this.items){if (this.items[i] && this.items[i].Close){this.items[i].Close();}}}
}

var Popup = new PopupHandlerClass();




