// PanicGoods eList / Fading Div / 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  = "/goods/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(openHere) {

  pophost = document.getElementById(openHere);
  popbox = document.getElementById("PopBox");

  // If it's hidden, show it

  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.
    // This is passed in as a variable (openHere) from the caller.

    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;
      // alert(pophostY + " " + pophostX + " / " + pophostFind.offsetTop + " " + pophostFind.offsetLeft + " " + pophostFind.offsetHeight);
    } while ( pophostFind = pophostFind.offsetParent )

    // Now position the pop-up box, based on the parent element's location and height.
    
    popbox.style.left = pophostX - (popbox.offsetWidth / 1.7);
    popbox.style.top = pophostY + (pophost.offsetHeight / 2);

    // Make sure the layer (which is theoretically invisible) has the right opacity
    // FIREFOX? DON'T ASK. Workaround for Firefox Flicker Pain. First set of 100 = brief dissapearing.

    if (navigator.userAgent.indexOf("Firefox") != -1) {
      setOpacity(99.999, "PopBox");
    } else {
      setOpacity(100, "PopBox");
    }
 
    // Now make the box visible (fade in?)

    popbox.style.visibility = "visible";

  } else {
    // Otherwise, fade out the box quickly
    popbox = document.getElementById("PopBox");
    fadeElementSetup("PopBox", 100, 0, 10);
  }
}

// FADE DIV functions (1.1)
// (C) 2005 Panic, Inc.
//
// Made more flexible so that each fading <div> can operate on its own
// No more global variables that can get clobbered

var fadeActive = new Array();
var fadeQueue  = new Array();
var fadeTimer  = new Array();

// Initialize the fade function

function fadeElementSetup(theID, fdStart, fdEnd, fdSteps) {

  if (fadeActive[theID] == true) {
    // Already animating, queue up this command
    fadeQueue[theID] = new Array(theID, fdStart, fdEnd, fdSteps);
  } else {
    fadeSteps = fdSteps;
    fadeCurrent = 0;
    fadeAmount = (fdStart - fdEnd) / fadeSteps;
    fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 40);
    fadeActive[theID] = true;
  }
}

// Do the fade. This function will call itself, modifying the parameters, so
// many instances can run concurrently.

function fadeElement(theID, fadeCurrent, fadeAmount, fadeSteps) {
  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.
    // If we faded out (amount was positive), then hide the layer

    if (fadeAmount > 0) {
      document.getElementById(theID).style.visibility = "hidden";
    }

    // Tear Down

    clearInterval(fadeTimer[theID]);
    fadeActive[theID] = false;
    
    // Hang on.. did a command queue while we were working? If so, make it happen now
    
    if (fadeQueue[theID] && fadeQueue[theID] != false) {
      fadeElementSetup(fadeQueue[theID][0], fadeQueue[theID][1], fadeQueue[theID][2], fadeQueue[theID][3]);
      fadeQueue[theID] = false;
    }
    
  } else {
    // Keep going, and send myself the updated variables
    clearInterval(fadeTimer[theID]);
    fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 40);
  }
}

// Set the opacity, compatible with a number of browsers

function setOpacity(opacity, theID) {

  var object = document.getElementById(theID).style;

  // If it's 100, set it to 99 for Firefox.

  if (navigator.userAgent.indexOf("Firefox") != -1) {
    if (opacity == 100) { opacity = 99.999; } // This is majorly retarded
  }

  // Multi-browser opacity setting

  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
}
