var OPEN_INATUR_POPUPS = new Array();
// o2EnableDebug();

function togglePopup(id, top, left, dontMove) {
  var elm = document.getElementById(id);
  if (!elm) {
    alert("Couldn't toggle popup " + id + ". No such element.");
    return;
  }
  if (elm.style.display === "block") {
    hidePopup(id);
  }
  else {
    showPopup(id, top, left, dontMove);
  }
}

function showPopup(id, top, left, dontMove) { // Top, left are optional
  var elm = document.getElementById(id);
  if (!elm) {
    alert("Couldn't show popup " + id + ". No such element.");
    return;
  }

/* This is dangerous if for example the contents of the popup should belong to a form.
  if (elm.parentNode.nodeName.toLowerCase() != "body") {
    elm = elm.parentNode.removeChild(elm);
    document.body.appendChild(elm);
  }
*/

  // IE6 hack to hide <select>s beneath the popup
  if (navigator.appVersion.match(/MSIE 6/)) {
    var iframes = elm.getElementsByTagName("iframe");
    if (iframes.length == 0) {
      iframe = document.createElement("iframe");
      iframe.setAttribute("src", "/o2/Blank?color=%23f5c13a");
      iframe.setAttribute("class", "ie6Hack");
      iframe.setAttribute("frameborder", "0");
      iframe.setAttribute("vspace", "0");
      elm.style.overflow = "hidden";
      elm.appendChild(iframe);
    }
  }

  if (!dontMove) {
    center(elm);
  }
  if (!navigator.appVersion.match(/MSIE 6/)) {
      changeOpac(0, elm.id);
  }
  elm.style.display = 'block';
  if (top && !navigator.appVersion.match(/MSIE 6/)) { // There's a position:fixed hack in popup.ie6.css
    elm.style.top = top + "px";
  }
  if (left) {
    elm.style.left = left + "px";
  }
  if (navigator.appVersion.match(/MSIE 6/)) {
    return;
  }
  shiftOpacity(id,500);
  OPEN_INATUR_POPUPS[id] = 1;
  removeEvent(document, "click", hidePopups); // Make sure we don't have more than one of these eventlisteners at a time.
  setTimeout("addEvent(document, 'click', hidePopups);", 10);
}

function hidePopups(e) {
  // Only if clicked element isn't a popup
  var elm = e.getTarget();
  while (elm.parentNode) {
    if (elm.nodeName.toLowerCase() === "div" && elm.className === "popup") {
      return; // The click was on a popup, so cancel hiding popups
    }
    elm = elm.parentNode;
  }

  for (var id in OPEN_INATUR_POPUPS) {
    if (OPEN_INATUR_POPUPS[id]) {
      hidePopup(id);
    }
  }
}

function hidePopup(id) {
  var elm = document.getElementById(id);
  if (!elm) {
    alert("Couldn't hide popup " + id + ". No such element.");
    return;
  }
  /* shiftOpacity(id,500); */
  /* must find a better way to do this, set display: none after timeOut */
  elm.style.display = 'none';
  OPEN_INATUR_POPUPS[id] = 0;
  removeEvent(document, "click", hidePopups);
}

/* A little object oriented, so we don't need to use global variables */
var popupDrag = {
  initialStyleLeft : null,
  initialStyleTop  : null,
  initialMouseX    : null,
  initialMouseY    : null,
  draggedElement : null,

  setupDragDrop : function() {
    o2EventHandler.addEventListener({
      "eventType"      : "mousedown",
      "className"      : "dragarea",
      "tagName"        : "div",
      "functionObject" : popupDrag.startDrag
    });
  },

  startDrag : function(e) {
    popupDrag.draggedElement = e.getTarget().parentNode.parentNode;

    addEvent(document, "mousemove", popupDrag.handleDrag);
    addEvent(document, "mouseup",   popupDrag.endDrag);

    popupDrag.initialMouseX = parseInt( e.getX() );
    popupDrag.initialMouseY = parseInt( e.getY() );
    popupDrag.initialStyleLeft = parseInt( popupDrag.draggedElement.offsetLeft );
    popupDrag.initialStyleTop  = parseInt( popupDrag.draggedElement.offsetTop );
    return false;
  },

  handleDrag : function(e) {
    var deltaX = parseInt( e.getX() )  -  popupDrag.initialMouseX;
    var deltaY = parseInt( e.getY() )  -  popupDrag.initialMouseY;
    popupDrag.draggedElement.style.left = (popupDrag.initialStyleLeft + deltaX) + "px";
    popupDrag.draggedElement.style.top  = (popupDrag.initialStyleTop  + deltaY) + "px";
    return false; // Don't select any text
  },

  endDrag : function(e) {
    removeEvent(document, "mousemove", popupDrag.handleDrag);
    removeEvent(document, "mouseup",   popupDrag.endDrag);
  }
};

addLoadEvent(popupDrag.setupDragDrop);

function opacity(id, opacStart, opacEnd, millisec) {
  //speed for each frame
  var speed = Math.round(millisec / 100);
  var timer = 0;

  //determine the direction for the blending, if start and end are the same nothing happens
  if (opacStart > opacEnd) {
    for (var i = opacStart; i >= opacEnd; i--) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
  else if (opacStart < opacEnd) {
    for (var i = opacStart; i <= opacEnd; i++) {
      setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
      timer++;
    }
  }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
  var object = document.getElementById(id).style;
  object.opacity = (opacity / 100);
  object.MozOpacity = (opacity / 100);
  object.KhtmlOpacity = (opacity / 100);
  object.filter = "alpha(opacity=" + opacity + ")";
} 

// Changes opacity from 1 to 0 or 0 to 1.
function shiftOpacity(id, millisec) {
  //if an element is invisible, make it visible, else make it ivisible
  if(document.getElementById(id).style.opacity == 0) {
    opacity(id, 0, 100, millisec);
    return;
  }
  opacity(id, 100, 0, millisec);
}

function center(elm) {
  var elmWidth  = elm.scrollWidth;
  var elmHeight = elm.scrollHeight;
  var windowWidth  = screen.width;
  var windowHeight = screen.height;
  if (elmHeight && !navigator.appVersion.match(/MSIE 6/)) { // There's a position:fixed hack in popup.ie6.css
    elm.style.top = windowHeight / 2 - elmHeight / 2 + 'px';
  }
  else if (!navigator.appVersion.match(/MSIE 6/)) { // There's a position:fixed hack in popup.ie6.css
    elm.style.top = windowHeight / 4 + 'px';
  }
  if (elmWidth) {
    elm.style.left = windowWidth / 2 - elmWidth / 2 + 'px';
  }
  else {
    elm.style.left = windowWidth / 4 + 'px';
  }
}
