// -----------------------------------------------------------------------------
// Code by Leigh Harrison   http://www.else.co.nz   January 2009
// -----------------------------------------------------------------------------



// Change the following variables to alter the behaviour
var gWelcomeSlideStep = 10; // milliseconds between changes in opacity 
var gWelcomeSlideDuration = 7000; // milliseconds to show a slide once visible

var gWelcomeSlideShowing;
var gWelcomeSlideHiding;
var gWelcomeSlideTimer;



function showSlide(bStart) {
  // Are we stopping? 
  if (bStart == false) {
    // Only clear the timer if it's set
    if (gWelcomeSlideTimer) {
      clearTimeout(gWelcomeSlideTimer);
    } 
    gWelcomeSlideShowing = null;
    gWelcomeSlideHiding = null; 
    return false;
  };
  
  // If there's no slide to show, load the next one
  if (!gWelcomeSlideShowing) {
    // If there's a slide ready to hide, load the one following
    if (gWelcomeSlideHiding && obj('slide_' + (gWelcomeSlideHiding + 1)) != null) {
      gWelcomeSlideShowing = (gWelcomeSlideHiding + 1);
    } 
    else {
      gWelcomeSlideShowing = 1000;
    }
    setSlide(gWelcomeSlideShowing, 0);
    // Preload the image and give it time to get on board
    var imgNew = new Image();
    imgNew.src = 'images/slide_' + gWelcomeSlideShowing + '.jpg;';
    gWelcomeSlideTimer = setTimeout('showSlide(true)', 400);
    return false;
  }
  
  // Have we fully shown the slide?
  var iShowing = getOpacity(gWelcomeSlideShowing);
  var iHiding = getOpacity(gWelcomeSlideHiding);
  if (iShowing > 99 && iHiding < 1) {
    // Move the Showing slide to Hiding and sleep until it's time to fade again
    gWelcomeSlideHiding = gWelcomeSlideShowing;
    gWelcomeSlideShowing = null;
    gWelcomeSlideTimer = setTimeout('showSlide(true)', gWelcomeSlideDuration);
  }
  else {
    // Increase and decrease opacity, then come back and do it again soon
    fadeSlide(gWelcomeSlideShowing, 1);
    fadeSlide(gWelcomeSlideHiding, -1);
    gWelcomeSlideTimer = setTimeout('showSlide(true)', (gWelcomeSlideStep));
  }
}

function fadeSlide(iID, iChange) {
  if (iID) {
    var oSlide = obj('slide_' + iID).style;
    var iValue;
    if (oSlide) {
      if (oSlide.opacity) {
        iValue = Math.round((oSlide.opacity * 100) + iChange);
      }
      else {
        iValue = (iChange == -1) ? 99 : 1;
      }
      if (iValue >= 0 && iValue <= 100) {
        setSlide(iID, iValue);
      }
    }
  }  
}

function setSlide(iID, iValue) {
  var oSlide = obj('slide_' + iID).style;
  if (oSlide) {
    oSlide.opacity = (iValue / 100);
    oSlide.MozOpacity = (iValue / 100);
    oSlide.KhtmlOpacity = (iValue / 100);
    oSlide.filter = "alpha(opacity=" + iValue + ")";
  }  
}

function getOpacity(iID) {
  if (!iID) {
    return -1;
  }
  var iValue = -1;
  var oSlide = obj('slide_' + iID);
  if (oSlide) {
    if (oSlide.style.opacity) {
      iValue = oSlide.style.opacity * 100;
    }
    else if (oSlide.filters.alpha.opacity) {
      iValue = oSlide.filters.alpha.opacity;
    }  
  } 
  return Math.round(iValue);
}
