function MapPopup(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;
}
MapPopup.prototype.show = MapPopup_show;
MapPopup.prototype.hide = MapPopup_hide;
MapPopup.prototype.hideGE = MapPopup_hideGE;
MapPopup.prototype.hideGEnoblur = MapPopup_hideGEnoblur;
MapPopup.prototype.allOff = MapPopup_allOff;
MapPopup.prototype.toString = MapPopup_toString;
MapPopup.prototype.all = [];
MapPopupTimerID = null;


function MapPopup_show() {
//alert("Showing " + this.elementname);
   var htmlobj   = this.element;
   if (document.getElementById) {
      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;
      }

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

   } else if (document.layers) {
      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 if (document.all) {
      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;
      }
      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;
      }
      htmlobj.style.top = i;

      htmlobj.style.width = this.width;
   }

   if (document.layers) {
      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 (document.layers) {
      htmlobj.visibility = "show";
      htmlobj.zIndex = 99 + this.level;
   } else {
      htmlobj.style.visibility = "visible";
      htmlobj.style.zIndex = 99 + this.level;
   }

   this.visible = true;
}


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) {
   //alert("entering ie4realTop: obj=" + obj + ", OffsetTop="+obj.offsetTop + ", nodeName="+ obj.nodeName + ", id=" + obj.id);
   var y = obj.offsetTop;
   var e = obj.offsetParent;
   while (e != null) {
   //alert("top: y=" + y + ", parent=" + e + ", OffsetTop="+e.offsetTop + ", nodeName="+ e.nodeName + ", id=" + e.id);
   //dump(e);
      y += e.offsetTop;
      e = e.offsetParent;
   }

   return y;
}

function dump(e) {
   for (var i in e) {
      alert(i + "='" + e[i] + "'");
   }
}

function MapPopup_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 MapPopup_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 MapPopup_hideGEnoblur() {
   for (var i = 0; i < this.all.length; i++) {
      if (this.all[i].level >= this.level) {
         this.all[i].hide();
      }
   }
}


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


function MapPopup_toString() {

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

}

