/// PanicGoods eList / popbox Functions
// (C) 2005 Panic, Inc. / Cabel Sasser
//
// All Rights Reserved. Must not be reproduced without express written permission of Panic, INc.
//
// For questions or licensing contact Panic: goods @ panic dot com

// Precache Spinner (again -- this one is gray)

spinner2_on  = new Image(16,16); spinner2_on.src  = "images/elist-spinner.gif";
spinner2_off = new Image(16,16); spinner2_off.src = "/images-global/spacer.gif";

// ELIST SIGNUP - Simply submit the elist info to a page.

function elistSignup() {
  document.images["elist-spinner"].src = spinner2_on.src;
  tempName = document.elist.elistName.value;
  document.getElementById('elistSubmit').src = '/bin/elist.cgi?liv=true&app=Goods&add='+tempName;
}

// Elist Done -- Function called by the HTML loaded into the iFrame, once complete.

function elistDone(respMsg) {

  // Turn off the spinner, we're done processing

  document.images["elist-spinner"].src = spinner2_off.src;

  // Handle the response

  if (respMsg != "OK") {
    // Display an error, if returned
    alert(respMsg);
  } else {
    // Get rid of the pop-up, done OK!
    popbox = document.getElementById("PopBox");
    if (popbox.style.visibility == "visible") {
      togglePopBox();
    }
  }

  // Clear out the iframe (in case of page reloads, etc.)

  document.getElementById('elistSubmit').src = 'blank.html';
}

// POPBOX - Functions for the dead simple pop-up box for elist signup.
// (C) 2005 Panic, Inc. / Cabel Sasser

function togglePopBox() {

  pophost = document.getElementById("PopHost");
  popbox = document.getElementById("PopBox");

  if (popbox.style.visibility == "hidden" || popbox.style.visibility == "") {

    // Find the location of where we should position the pop-up box
    // "PopHost" is the document element near where the pop-up box should appear

    var pophostY = 0;
    var pophostX = 0;
    var count = 0;
    var pophostFind = pophost;

    // Iterate through the target item's many potential parents to calculate position X and Y values

    do {
      pophostY += pophostFind.offsetTop;
      pophostX += pophostFind.offsetLeft;
    } while ( pophostFind = pophostFind.offsetParent )

    // Now position the pop-up box. I swear, lord, that I tried to do this dynamically, but it 
    // wasn't as easy as it seems. NOTE: If PopBox gets resized, this must change as well.

    popbox.style.left = pophostX - 143;
    popbox.style.top = pophostY + 38;

    // popbox.style.left = pophostX - (popbox.offsetWidth / 2.5);
    // popbox.style.top = pophostY + (pophost.offsetHeight);

    // DON'T ASK. Workaround for Firefox Flicker Pain. First set of 100 = brief dissapearing.

    if (navigator.userAgent.indexOf("Firefox") != -1) {
      setOpacity(99.999, "PopBox");
    }
 
    // Now make the box visible (fade in?)

    popbox.style.visibility = "visible";

  } else {
    popbox = document.getElementById("PopBox");
    // Fade out the box quickly
    fadeElementSetup("PopBox", 100, 0, 10);
  }
}

// FADE (OUT) ITEM function
//
// NOTE: Slightly different than the more flexible "mininfo" code; this will hide the layer
// when finished and set the opacity back up to 100, so it's really intended
// only for fading an item out.
//
// Pass opacity values from 1 - 100.

function fadeElementSetup(theID, fdStart, fdEnd, fdSteps) {
  fadeSteps = fdSteps;
  fadeCurrent = 0;
  fadeAmount = (fdStart - fdEnd) / fadeSteps;
  fadeTimer = setInterval("fadeElement('"+theID+"')", 50);
}

function fadeElement(theID) {
  fadeCurrent++;
  // Set the opacity depending on if we're adding or subtracting (pos or neg)
  if (fadeAmount < 0) {
    setOpacity(Math.abs(fadeCurrent * fadeAmount), theID);
  } else {
    setOpacity(100 - (fadeCurrent * fadeAmount), theID);
  }
  if (fadeCurrent == fadeSteps) {
    // We're done, so clear
    clearInterval(fadeTimer);
    document.getElementById(theID).style.visibility = "hidden";
    setOpacity(100, theID);
  }
}

function setOpacity(opacity, theID) {

  // Multi-browser opacity setting
  var object = document.getElementById(theID).style;
  object.filter = "alpha(opacity=" + opacity + ")"; // IE/Win
  object.KhtmlOpacity = (opacity / 100);            // Safari 1.1 or lower, Konqueror
  object.MozOpacity = (opacity / 100);              // Older Mozilla+Firefox
  object.opacity = (opacity / 100);                 // Safari 1.2, Firefox+Mozilla
}