

  function PopupManager(){
    this.lock = false;
    this.activePopups = new Array();
    this.id = "PopupManager";
    this.displayCount = 0;
    this.display = null;
    this.topLayer = 1000;
  }
  
  PopupManager.prototype.getTopPop = function(){
    return this.activePopups[this.activePopups.length-1];
  }
  
  PopupManager.prototype.display = function (popId){
    var thePop = this.activePopups[popId];
    if(thePop != null){
      thePop.display();
    }
  }
  
  PopupManager.prototype.getPopup = function(popId){
    return this.activePopups[popId];
  }
  
  PopupManager.prototype.popup = function (src, handler){
    var ret = new Popup(src, this, handler);
    this.registerPopup(ret);
    ret.display();
    return ret;
  }
  
  PopupManager.prototype.unpopup = function (popId, returnVal){
    var thePop = this.activePopups[popId];
    
    if(thePop != null){
      thePop.close(returnVal);
    }
    thePop = null;
  }
  
  PopupManager.prototype.cancel = function (popId){
    var thePop = this.activePopups[popId];
    
    if(thePop != null){
      thePop.cancel();
    }
    thePop = null;
  }  
  
  PopupManager.prototype.isTop = function (popId){
    var thePop = this.activePopups[popId];
    
    if(thePop != null && thePop.level == this.displayCount)
      return true;
    
    return false;
  }
  
  PopupManager.prototype.registerPopup = function (popup){
    try{
      while(this.lock){}
      this.lock = true;
   
      var place = this.activePopups.length;
      popup.id = place;
      popup.pop.id = place;
      popup.pop.style.zIndex = this.topLayer++;
      this.activePopups[place] = popup;
      this.displayCount++;
      popup.level = this.displayCount;
      popup.pop.level = popup.level;
    }finally{
      this.lock = false;
    }
  }
  
  PopupManager.prototype.unregisterPopup = function (popup){
    try{
      while(this.lock){}
      this.lock = true;
      this.activePopups[popup.id] = null;
      this.displayCount--;
    }finally{
      this.lock = false;
    }
  }
  
  function Popup(src, manager, handler){
    this.pop = document.getElementById("popupFrame").cloneNode(true);
    this.pop.id = "";
    this.pop.style.height=document.documentElement.clientHeight+'px';
    if(document.body.scrollHeight > document.documentElement.clientHeight)
    {
    	this.pop.style.height=document.body.scrollHeight+'px';
    }
    this.pop.style.overflow="hidden";
    this.pop.popup = this;
    this.handler = handler;
    this.pop.src = src;
    document.body.appendChild(this.pop);
    this.manager = manager;
    this.level = 0;
    this.inited = false;
    
   	var scrollX, scrollY;
    if (document.all)
    {
      if (!document.documentElement.scrollLeft)
        scrollX = document.body.scrollLeft;
      else
        scrollX = document.documentElement.scrollLeft;
      if (!document.documentElement.scrollTop)
        scrollY = document.body.scrollTop;
      else
        scrollY = document.documentElement.scrollTop;
    }
    else
    {
      scrollX = window.pageXOffset;
      scrollY = window.pageYOffset;
    }
		this.pop.popup.offsetX=scrollX;
		this.pop.popup.offsetY=scrollY;
  }
  
  Popup.prototype.init = function(appcontroller){
    this.inited = true;
  }
  
  Popup.prototype.display = function (){
    this.pop.style.display = "";
    this.visible = true;
  }
  
  Popup.prototype.hide = function (){
    this.pop.style.display = "none";
    this.visible = false;
  }
  
  Popup.prototype.toggle = function (){
    if(this.visible){
      this.hide();
    }else{
      this.display();
    }    
  }
  
  
  Popup.prototype.cancel = function()
  {
    this.pop.src = "/blank.jsp";
    this.pop.contentWindow.close();
    this.pop.parentNode.removeChild(this.pop);
    var man = this.manager;
    man.unregisterPopup(this);
    this.topLayer--;
  }
  
  
  Popup.prototype.close = function (returnVal){
    if(this.handler != null){
      this.handler.call(this, returnVal);
    }
    
    this.pop.src = "/blank.jsp";
    this.pop.contentWindow.close();
    this.pop.parentNode.removeChild(this.pop);
    var man = this.manager;
    man.unregisterPopup(this);
    this.topLayer--;
  }
  
  Popup.prototype.isTop = function (){
    if(this.level == this.manager.displayCount)
      return true;      
    
    return false;
  }
  


  
  

