// TUNABLE GLOBALS

var g_HoldDuration = 4000; // Number of milliseconds to pause between fades.
var g_FadeDuration = 750; // Duration of fade (in milliseconds).

// INTERNAL GLOBALS

// The splash images to cycle through
var g_SplashImageArray = null;

// Index of image currently at 100%
var g_CurrentImageIndex = 0;

// Index of image currently being faded
var g_FadeImageIndex = 1;

// Opacity of image above it
var g_FadeOpacity = 0.0;

// Last time a fade was started (as returned by the Date getTime() function).
// If <0 then fadeTick() should set it to the current time.
var g_LastSwapTime = -1;

function setOpacity( element, alpha ) {
	var style = element.style;
	if( style.MozOpacity != undefined ) { //Moz and older
		style.MozOpacity = alpha;
	}
	else if( style.filter != undefined ) { //IE
		style.filter = "alpha(opacity=0)";
		element.filters.alpha.opacity = ( alpha * 100 );
	}
	else if( style.opacity != undefined ) { //Opera
		style.opacity = alpha;
	}
}

/* The world's most useful JavaScript function! */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function fadeTick()
{
  var d = new Date();
  var now = d.getTime();
   
  var nextTick = g_FadeDuration / 30;

  if(g_LastSwapTime < 0)
  {
    g_LastSwapTime = now;
  }

  g_FadeOpacity = (now - g_LastSwapTime) / 1000.0;

  if(g_FadeOpacity < 1.0) {
    if(g_FadeImageIndex > g_CurrentImageIndex) {
      setOpacity(g_SplashImageArray[g_FadeImageIndex], g_FadeOpacity);
    } else {
      setOpacity(g_SplashImageArray[g_FadeImageIndex], 1.0);
      setOpacity(g_SplashImageArray[g_CurrentImageIndex], 1.0 - g_FadeOpacity);
    }
  } else {
    var nextImageIndex = g_FadeImageIndex + 1;
    
    if(nextImageIndex >= g_SplashImageArray.length)
    {
      nextImageIndex = 0;
    }

    setOpacity(g_SplashImageArray[g_FadeImageIndex], 1.0);
    g_SplashImageArray[g_CurrentImageIndex].style.display = 'none';
    setOpacity(g_SplashImageArray[nextImageIndex], 0.0);
    g_SplashImageArray[nextImageIndex].style.display = 'block';
    
    g_CurrentImageIndex = g_FadeImageIndex;
    g_FadeImageIndex = nextImageIndex;
    g_FadeOpacity = 0.0;
    g_LastSwapTime = -1;

    nextTick = g_HoldDuration;
  }

  setTimeout("fadeTick()", nextTick);
}

function startSplashScreenCycle()
{
  /* Firstly, get a list of the splash images */
  g_SplashImageArray = $( 'homeSplashWrapper' ).getElementsByTagName('img');

  /* Set all elements but the bottom-most to have opacity 0% and be invisible. */
  for(var i = 1; i < g_SplashImageArray.length; i++)
  {
    setOpacity(g_SplashImageArray[i], 0);
    g_SplashImageArray[i].style.display = 'none';
  }

  g_SplashImageArray[1].style.display = 'block';
  g_CurrentImageIndex = 0;
  g_FadeImageIndex = 1;
  g_FadeOpacity = 0.0;
  g_LastSwapTime = -1;

  setTimeout("fadeTick()", g_HoldDuration);
}

function homeSplashStart()
{
  /* This does the requisite CSS magic to remove the original background
   * on the hom-page splace and replace it with our funky version. */
  var homeSplashObj = $( 'homeSplashWrapper' );
  homeSplashObj.style.display = 'block';
  var homeHighlightsObj = $( 'home_highlights' );
  homeHighlightsObj.style.position = 'absolute';
  homeHighlightsObj.style.zIndex = '2';
  homeHighlightsObj.style.background = 'none';

  setOpacity( $( 'home_news_wrapper' ), 0.65 );

  startSplashScreenCycle();
}
