/* ********************************************************************
 * Create script hooks for the standard evertz.com popup window
 *
 */
function makeStdwin(container) {
  container = container || document.documentElement;

  for (var x = 0, wins = document.getElementsByTagName('div'), stdwin; stdwin = wins[x++];) {
    if (stdwin.className == "stdwin") {

      // ***** Add the close button ***********************************
      var img = document.createElement('img');
          img.src = "/img/icon.close.yellow.png";
          img.alt = "";
          img.className = "stdwincloser";
          img.title = "Close window";
          img.onclick = function() { this.parentNode.style.display = "none"; };
      stdwin.insertBefore(img, stdwin.getElementsByTagName('h2')[0].nextSibling);

      // ***** Make the window draggable ******************************
      for (var y = 0, claws = stdwin.getElementsByTagName('h2'), clw; clw = claws[y++];) {
        if (clw.className == "dragon") {
          clw.onmousedown = function(e) {
            e = e || event;
            var self = this;
            if (this.className == "dragon") {
              this.className = "dragon moving";
              this.x = e.clientX + scrollDist()[0];
              this.y = e.clientY + scrollDist()[1];
              this.px = Number(this.parentNode.offsetLeft);
              this.py = Number(this.parentNode.offsetTop);
              this.actualHeight = (container == document.documentElement) ? innerDimensions()[1] : container.offsetHeight;
              document.documentElement.onmousemove = function(e) {
                e = e || event;
                if (self.className == "dragon moving") {
                  var sd = scrollDist();
                  self.parentNode.style.left = Math.max(-container.offsetLeft, Math.min(container.offsetLeft + container.offsetWidth - self.parentNode.offsetWidth, self.px + e.clientX - self.x) + sd[0]) + "px";
                  self.parentNode.style.top = Math.max(-container.offsetTop, Math.min(container.offsetTop + self.actualHeight - self.parentNode.offsetHeight, self.py + e.clientY - self.y) + sd[1]) + "px";
                  e.cancelBubble = true;
                } return false;
              };
              document.documentElement.onmouseup = function(e) {
                self.className = "dragon";
                document.documentElement.onmousemove = null;
                document.documentElement.onmouseup = null;
              };
            }
            return false;
          };
        }
      }
    }
  }
}


/* ********************************************************************
 * Return the dimensions of the viewport, also from Quirksmode.org
 *
 */
function innerDimensions() {
  if (self.innerHeight) {
    return [self.innerWidth, self.innerHeight];
  } else if (document.documentElement && document.documentElement.clientHeight) {
    return [document.documentElement.clientWidth, document.documentElement.clientHeight];
  } else if (document.body)
    return [document.body.clientWidth, document.body.clientHeight];
  return [0, 0];
}

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