/*
 * JavaScript file to handle cycling of promotional images on homepage.
 *
 */

//
// Global vars...
//
// Note that a variable called imgArray is defined in the page and this stores all the images.

var frontImg1Id = 'promotion-img1';
var backImg2Id = 'promotion-img2';

var frontImgNo = 0;
var backImgNo = 1;
var frontImgOpac = 99;   // 100%

var FADE_STEP = 2;
var FADE_INTERVAL = 50;
var PAUSE_INTERVAL = 5000;

//
// Init function that starts the whole thing off...
//
function InitCycleImages(){
  if (imgArray.length > 1){
    var frontImgElement = document.getElementById(frontImg1Id);
    var promoImgLink = document.getElementById('promotionalImageLink');
    
    frontImgElement.src = imgArray[frontImgNo][0];
    frontImgElement.alt = imgArray[frontImgNo][1];
    promoImgLink.href = imgArray[frontImgNo][2];
    
    var backImgElement = document.getElementById(backImg2Id);
    backImgElement.src = imgArray[backImgNo][0];
    backImgElement.alt = imgArray[backImgNo][1];
    
    setTimeout('PerformFadeOut()', PAUSE_INTERVAL);
  }
  
  return true;
}

//
// Perform fade out...
//
function PerformFadeOut(){
  var frontImgElement = document.getElementById(frontImg1Id);
  var backImgElement = document.getElementById(backImg2Id);
  
  frontImgOpac = frontImgOpac - FADE_STEP;
  
  if (frontImgOpac <= 0){
    var promoImgLink = document.getElementById('promotionalImageLink');
    
    SetOpacity(frontImgElement, 1);
    //
    // Front image has faded out so cycle to next set of images
    //
    frontImgNo = backImgNo;
    backImgNo++;
    if (backImgNo >= imgArray.length){
      backImgNo = 0;
    }
    frontImgElement.src = imgArray[frontImgNo][0];
    frontImgElement.alt = imgArray[frontImgNo][1];
    promoImgLink.href = imgArray[frontImgNo][2];
    
    frontImgOpac = 99;
    //SetOpacity(frontImgElement, frontImgOpac);
    setTimeout('SetOpacity(document.getElementById(\'' + frontImg1Id + '\'), ' + frontImgOpac + ');\r\n', 1000);
    setTimeout(
    /*'SetOpacity(document.getElementById(\'' + frontImg1Id + '\'), ' + frontImgOpac + ');\r\n' +*/
    'document.getElementById(\'' + backImg2Id + '\').src = imgArray[' + backImgNo + '][0];\r\n' +
    'document.getElementById(\'' + backImg2Id + '\').alt = imgArray[' + backImgNo + '][1];\r\n', 2000);
  }

	if (frontImgOpac == 99){
	  //
	  // Have finished fade so pause for a period..
	  //
	  setTimeout('PerformFadeOut()', PAUSE_INTERVAL);
	} else {
	  //
    // Set opacity
    //  
    SetOpacity(frontImgElement, frontImgOpac);

	  //
	  // In the middle of fade to continue at normal rate..
	  //
	  setTimeout('PerformFadeOut()', FADE_INTERVAL);
	}
}

//
// Set opacity...
//
function SetOpacity(e, opac){
  if (e.style.filter != null) {
		// MS Internet Explorer
		e.style.filter = "alpha(opacity=" + opac + ")";
	}	else if (e.style.MozOpacity != null) {
		// Mozilla Firefox
		e.style.MozOpacity = opac / 100;
	}	else {
		// CSS3 Standard
		e.style.opacity = opac / 100;
	}
}