function Popup(elementname, xpos, ypos, level, relativeelement, base) {

   this.elementname = elementname;
   this.element = document.all[elementname];
   this.xpos = xpos || 0;
   this.ypos = ypos || 0;
   this.level = level || 1;
   this.relative = relativeelement;
   this.base = base || "L";
   if (this.base.length == 1) {
      this.base = this.base + "T";
   }

   this.visible = false;

   this.width = 0;
   if (is_nav4) {
      this.width = this.element.clip.width;
   }
   if (is_nav5up) {
      this.width = this.element.offsetWidth;
   }
   if (is_ie4up) {
      this.width = this.element.scrollWidth;
   }

   this.height = 0;
   if (is_nav4) {
      this.height = this.element.clip.height;
   }
   if (is_nav5up) {
      this.height = this.element.offsetHeight;
   }
   if (is_ie4up) {
      this.height = this.element.scrollHeight;
   }


//alert("Making " + elementname);

   // Keep an array of all popup elements
   this.all[this.all.length++] = this;
}
Popup.prototype.show = Popup_show;
Popup.prototype.hide = Popup_hide;
Popup.prototype.hideGE = Popup_hideGE;
Popup.prototype.hideGEnoblur = Popup_hideGEnoblur;
Popup.prototype.allOff = Popup_allOff;
Popup.prototype.toString = Popup_toString;
Popup.prototype.all = [];
PopupTimerID = null;


function Popup_show() {
//alert("Showing " + this.elementname);
   var htmlobj   = this.element;
   if (is_nav4) {
      var i = this.xpos;
      if (this.relative) {
         i = i + this.relative.x;
      }
      if (this.base.substr(0,1)=='C') {
         i = i - Math.round(this.width / 2);
      }
      if (this.base.substr(0,1)=='R') {
         i = i - this.width;
      }
      htmlobj.left = i;

      var i = this.ypos;
      if (this.relative) {
         i = i + this.relative.y;
      }
      if (this.base.substr(1,1)=='M') {
         i = i - Math.round(this.height / 2);
      }
      if (this.base.substr(1,1)=='B') {
         i = i - this.height;
      }
      htmlobj.top = i;

   } else {
      var i = this.xpos;
      if (this.relative) {
         i = i + ie4realLeft(this.relative);
      }
      if (this.base.substr(0,1)=='C') {
         i = i - Math.round(this.width / 2);
      }
      if (this.base.substr(0,1)=='R') {
         i = i - this.width;
      }
      if (is_nav5up) {
         i = i + "px";
      }
      htmlobj.style.left = i;

      var i = this.ypos;
      if (this.relative) {
         i = i + ie4realTop(this.relative);
      }
      if (this.base.substr(1,1)=='M') {
         i = i - Math.round(this.height / 2);
      }
      if (this.base.substr(1,1)=='B') {
         i = i - this.height;
      }
      if (is_nav5up) {
         i = i + "px";
      }
      htmlobj.style.top = i;

      htmlobj.style.width = this.width;
   }

   if (is_nav4) {
      htmlobj.popup = this;       // This is used for the Dragable code the change the popup coordinates
      if (window.location.search.indexOf("drag=yes") > 0  ||
          (window.location.search.indexOf("drag="+this.elementname) > 0)) {
         var d = new Dragable(htmlobj);
      }
   }

   if (is_nav4) {
      htmlobj.visibility = "show";
      htmlobj.zIndex = 99 + this.level;
   } else {
      htmlobj.style.visibility = "visible";
      htmlobj.style.zIndex = 99 + this.level;
   }

   this.visible = true;
   if (PopupTimerID != null) {
      clearTimeout(PopupTimerID);
      PopupTimerID = null;
   }
}


function ie4realLeft(obj) {
   var x = obj.offsetLeft;
   var e = obj.offsetParent;
   while (e != null) {
      x += e.offsetLeft;
      e = e.offsetParent;
   }

   return x;
}


function ie4realTop(obj) {
   var y = obj.offsetTop;
   var e = obj.offsetParent;
   while (e != null) {
      y += e.offsetTop;
      e = e.offsetParent;
   }

   return y;
}


function Popup_hide() {
   //alert("Hiding " + this.elementname);
   if (!this.visible) { return; }

   var htmlobj = this.element;
   if (is_nav4) {
      htmlobj.visibility = "hide"
   } else {
      htmlobj.style.visibility = "hidden";
   }
   //this.element.zIndex = 1;
   //if (is_nav4) {
   //   htmlobj.left = 0;
   //   htmlobj.top = 0;
   //} else {
   //   htmlobj.style.left = 0;
   //   htmlobj.style.top = 0;
   //}
   //if (is_nav5) {
   //   htmlobj.style.left = 9999;
   //   htmlobj.style.top = 9999;
   //}
   this.visible = false;
}


function Popup_hideGE() {
   for (var i = 0; i < this.all.length; i++) {
      if (this.all[i].level >= this.level) {
         this.all[i].hide();
      }
   }

   /* Blur the search box, if any */
   //if (document.layers  &&  document.layers['KSsrc']) {
   //   document.layers['KSsrc'].document.forms[0].qt.blur();
   //}
}


function Popup_hideGEnoblur() {
   for (var i = 0; i < this.all.length; i++) {
      if (this.all[i].level >= this.level) {
         this.all[i].hide();
      }
   }
}


function Popup_allOff() {
   for (var i = 0; i < this.all.length; i++) {
      this.all[i].hide();
   }
}


function Popup_toString() {

   return "Popup.elementname = " + this.elementname
          + "\n .xpos = " + this.xpos
          + "\n .ypos = " + this.ypos
   ;

}
