/**
 * Hi there, you're looking at my source, I'm flattered =)
 * What's here is just a little javascript to make the little movable popups.
 * Is this code copyrighted by me? ... um... sure~
 * but you can use it if you want.
 * Can you just drop me a line if you use it?
 * I just want to know if you like my code enough that you want to use it!
 * Also, if you have any questions, comments, or flames, drop me a line, too!
 *
 * Consistently Good (kekeke),
 * Robert LaThanh
 * hybrid@pearlmagik.com
 *
 * P.S. I bet you hate my coding style. All cluttered and MESSY, you're thinking, huh?
 *      Well I like it, it's compact! (Let me know what you think.)
 */
 
Ns = (document.all) ? 0 : 1; // is this Netscape?
dragged = false;             // has the popup been dragged? if so, the next popup will show up at the same location

if (Ns) {visible = 'show';    hidden = 'hide';}   // Netscape uses 'show' and 'hide' instead of the cSS standard
else    {visible = 'visible'; hidden = 'hidden';}

/** Showing and hiding popups
 *  It's pretty straight-forward. show() to make a popup visible, hideMe() to hide the last popup
 *  show() automatically hides the last popup, so we don't end up with a mess of popups on the page
 *  hideMe() is called when the user clicks on the X on the popup
 */
lastPopup = null;  // keep a handle on the last popup
function show(newPopup, e) {
  if (Ns) {newPopupProp = document.layers[newPopup];    if (lastPopup) lastPopupProp = document.layers[lastPopup];}
  else    {newPopupProp = document.all[newPopup].style; if (lastPopup) lastPopupProp = document.all[lastPopup].style;}
  if (newPopup != lastPopup && newPopupProp.visibility != visible) {
    if (dragged) { // If the previous popup was dragged to a certain location, I want the next popup to show up there
      newPopupProp.left = lastPopupProp.left; newPopupProp.top = lastPopupProp.top
    } else {       // otherwise, I'm just going to put it 20 pixels to the right of where the mouse is
      if (Ns) {newPopupProp.left = e.pageX +16; newPopupProp.top  = e.pageY;}
      else {newPopupProp.left = e.x + document.body.scrollLeft +16; newPopupProp.top = e.y + document.body.scrollTop;}
    }
    if (lastPopup) lastPopupProp.visibility = hidden; // hide the last popup (if there was one)
    newPopupProp.visibility = visible;                // make the new popup visible
    lastPopup = newPopup;
  }
  endDragPopup(null);
}
function hideMe(me) { // If you have trouble understanding this function, it's time to reevaluate your programming skills
  if (Ns) daPopup = document.layers[me].visibility = hidden;
  else    daPopup = document.all[me].style.visibility = hidden;
  dragged = false; endDragPopup(null);  
}

/** dragging popups
 * When the toggleDragPopup() function is activated, it tells Netscape (IE doesn't need to be)
 *  to start watching the mouse.
 * The popup then follows mouse movement until a MOUSEDOWN or MOUSEUP.
 * Note that the MOUSEUP doesn't register if the user merely "clicks".
 * This allows the user to drag the popup normally, i.e. MOUSEDOWN-drag-MOUSEUP
 *  this only works in IE.
 * Otherwise (in both browsers) it goes CLICK-drag-MOUSEDOWN
 *  the user clicks, the window follows the mouse, and then the next MOUSEDOWN drops the popup
 */
var startX, startY;
function toggleDragPopup(e) {
  dragged = true;
  if (Ns) {startX = e.pageX - document.layers[lastPopup].left; startY = e.pageY - document.layers[lastPopup].top;}
  else    {startX = event.x - document.all[lastPopup].style.left.slice(0,-2)*1; startY = event.y - document.all[lastPopup].style.top.slice(0,-2)*1}
  if (Ns) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
  document.onmousemove = dragPopup;
  window.status="Click again to drop window";
}
function dragPopup(e) {
  document.onmouseup = endDragPopup;
  document.onmousedown = endDragPopup;
  if (Ns) document.layers[lastPopup].moveTo(e.pageX-startX, e.pageY-startY);
  else {document.all[lastPopup].style.left = event.x - startX;document.all[lastPopup].style.top  = event.y - startY;}
}
function endDragPopup(e) {
  if (Ns) document.releaseEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
  document.onmousemove = null;
  document.onmousedown = null;
  document.onmouseup   = null;
  window.status="";
}
