/* ********************************************************************
 * Create script hooks for the standard evertz.com popup window
 *
 */
var Lightbox;
var ROOTPATH = window.location.pathname.replace(/\/(products|tools|frame|contact|careers|downloads|resources)\/.*$/, "/");


function Popup(id, closer, lightbox) {
  var self = this;

  this.id = id;
  this.closer = closer;
  this.lightbox = lightbox;

  this.isIE6 = /*@cc_on@if(@_jscript_version == 5.6)!@end@*/false;

  this.element = document.getElementById(id);
  this.container = this.element.parentNode;
  this.closerImg = ROOTPATH + "img/icon.close.black.png";
  this.header = this.element.getElementsByTagName('h2')[0];

  // Create a shared lightbox
  if (!Lightbox) {
    Lightbox = document.createElement('div');
    Lightbox.id = "lightbox";
    document.body.appendChild(Lightbox);
  }
  this.lightboxElement = Lightbox;


  // ***** Add the close button ***********************************
  if (this.closer) {
    var img = document.createElement('img');
        img.src = this.closerImg;
        img.alt = "Close";
        img.title = "Close window";
        img.onclick = function() { self.hide(); };
      this.header.insertBefore(img, this.header.firstChild);
  }

  // ***** Make the window draggable ******************************
  if (this.header.className.match(/\bpopupDragon\b/)) {
    this.header.moving = false;
    this.header.onmousedown = function(e) {
      e = e || event;
      var _self = this;
      if (!this.moving) {
        this.x = Number(this.parentNode.offsetLeft) - e.clientX - scrollDist()[0];
        this.y = Number(this.parentNode.offsetTop) - e.clientY - scrollDist()[1];;
        this.limit = [
          self.container.offsetLeft + self.container.offsetWidth - self.element.offsetWidth - 2,
          self.container.offsetTop + self.container.offsetHeight - self.element.offsetHeight - 2
        ];
        document.documentElement.onmousemove = function(e) {
          e = e || event;
          this.moving = true;
          var sd = scrollDist();
          self.element.style.left = Math.max(0, Math.min(_self.limit[0], _self.x + e.clientX) + sd[0]) + "px";
          self.element.style.top = Math.max(0, Math.min(_self.limit[1], _self.y + e.clientY) + sd[1]) + "px";
          e.cancelBubble = true;
          return false;
        };
        document.documentElement.onmouseup = function(e) {
          self.header.moving = false;
          document.documentElement.onmousemove = null;
          document.documentElement.onmouseup = null;
        };
      }
      return false;
    };
  }

  if (this.lightbox) {
    document.body.appendChild(this.element.parentNode.removeChild(this.element));
    this.container = document.body;
  } else if (this.isIE6)
    this.element.insertBefore(document.createElement('iframe'), this.element.firstChild);

  this.show = function() {
    this.element.style.display = "block";
    if (this.lightbox) {
      this.lightboxElement.style.display = "block";
      this.lightboxElement.style.height = this.lightboxElement.parentNode.offsetHeight + "px";
      if (this.isIE6) document.body.className += " lightbox";
      this.element.style.top = (50 + scrollDist()[1]) + "px";
      this.element.style.left = ((this.element.parentNode.offsetWidth - this.element.offsetWidth) / 2) + "px";
    }
    this.position();
  };
  this.hide = function() {
    if (this.lightbox) {
      this.lightboxElement.style.display = "none";
      if (this.isIE6) document.body.className = document.body.className.replace(/ ?lightbox ?/g, "");
    }
    this.element.style.display = "none";
    this.onhide();
  };
  this.onhide = function() {};
  this.position = function() {};
}



/* ********************************************************************
 * Return the document scroll value for all browsers
 *
 */
function scrollDist() {
  var html = document.getElementsByTagName('html')[0];
  if ((html.scrollTop && document.documentElement.scrollTop) || (html.scrollLeft && document.documentElement.scrollLeft)) {
    return [html.scrollLeft, html.scrollTop];
  } else if (html.scrollTop || document.documentElement.scrollTop || html.scrollLeft || document.documentElement.scrollLeft) {
    return [html.scrollLeft + document.documentElement.scrollLeft, html.scrollTop + document.documentElement.scrollTop];
  } else if (document.body.scrollTop || document.body.scrollLeft)
    return [document.body.scrollLeft, document.body.scrollTop];
  return [0, 0];
}
