
// SWAP IMAGE FUNCTION for MinInfo
// (C) 2005 Panic, Inc. / Cabel Sasser
//
// Cross-fade between the two images, and swap out the thumbnail with the highlighted thumbnail

function swapImage(divID, imageID, imageToSwap) {

  globalDivID = divID;
  globalImageID = imageID;

  if (document.getElementById(imageID).src.indexOf(eval(imageToSwap+".src")) == -1) {

  	// Set the background image to the currently displaying image
        // This is now done on HTML render and when fade is complete
  	// document.getElementById(divID).style.backgroundImage = "url(" + document.getElementById(imageID).src + ")";
  
  	// Set the top image to invisible
  	setOpacity(0, imageID);

  	// Set the top image to the target image
  	document.getElementById(imageID).src = eval(imageToSwap+".src");
  	
  	// Slowly fade in the top image back to visible
  	fadeElementSetup(imageID, 0, 100, 7);

  } else {

	 // alert("Already Set");

  }
}

// HIGHLIGHT image function
//
// Replaces image source with [imagename]-on.[gif]
// Undoes last image (if any).
//
// imageID = the ID of the image to replace
// modeFlag = 0 for don't highlight, 1 for highlight

function highlightImage(imageID) {
  var imageSrc = document.getElementById(imageID).style.backgroundImage;
  if (imageSrc.indexOf("-on") != -1) {
     // alert("Already On");
  } else {
    if (typeof(lastChangedID) != "undefined") {
      // We've changed something previously.
      // Remove the "-on" from the filename and set the image back.
      var lastSrc = document.getElementById(lastChangedID).style.backgroundImage;
      var lastType = lastSrc.substring(lastSrc.lastIndexOf('.'), lastSrc.length);
      var lastName = lastSrc.substring(0, lastSrc.lastIndexOf('-on'));
      document.getElementById(lastChangedID).style.backgroundImage = lastName+lastType;
    }
    // Now add "-on" to the changed one and set the image.
    var fileType = imageSrc.substring(imageSrc.lastIndexOf('.'), imageSrc.length);
    var newSrc = imageSrc.replace(fileType, "-on"+fileType);
    document.getElementById(imageID).style.backgroundImage = newSrc;
    lastChangedID = imageID;
  }
}

// FADE ITEM function
// (C) 2005 Panic, Inc.
//
// Ex: href="javascript:fadeElementSetup('testimg',100,0,10)"
// Because we can't accruately get the opacity of an item (it's always zero),
// we must force a start and an end (it can't be computed on the fly).
// So, call this as a normal function.
//
// 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);

    // Here's "mininfo" specific code, that sets the background to be prepared for the next fade
    // Set the background image to the currently displaying image
    document.getElementById(globalDivID).style.backgroundImage = "url(" + document.getElementById(globalImageID).src + ")";

  }
}

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
}
