// Banner_Rotate.js
// This file randomly changes the site's banner at regular intervals
// with a cross-fade effect.  The image rotation script is original
// but the actual cross-fade effect was graciously provided by a 
// programmer by the name of Brothercake - http://www.brothercake.com/
// The rest of the javascript/HTML/PHP code is done by Adrian Marceau,
// AKA "Ageman20XX" of http://plutolighthouse.net/

// Define the array to hold all banners
var _allbanners = new Array();
// Define all banners to use in rotation here if they will be consistent
// throughout the site.  Otherwise, if banners need to be defined on a
// page-by-page basis, define these in the <head> section of the document
// instead.
_allbanners[0] = "CMX-banner1.jpg";
_allbanners[1] = "CMX-banner2.jpg";
_allbanners[2] = "CMX-banner9.jpg";
_allbanners[3] = "CMX-banner3.jpg";
_allbanners[4] = "CMX-banner8.jpg";
_allbanners[5] = "CMX-banner10.jpg";
_allbanners[6] = "CMX-banner5.jpg";
_allbanners[7] = "CMX-banner6.jpg";
_allbanners[8] = "CMX-banner11.jpg";
_allbanners[9] = "CMX-banner7.jpg";

//_allbanners[8] = "CMX-banner_group15.jpg";
//_allbanners[9] = "CMX-banner_group3.jpg";

// Configurable settings
var _rotate_delay = 8; // Number of milliseconds between banners
var _fade_duration = 2; // Number of seconds for the actual cross-fade
var _image_path = 'banners/'; // The path (relative to the the php/html file) of the banner folder
var _banner_id = 'bannerimage'; // The ID for the banner image <img id="?" />

// Global variables
var _t;
var _position = 0;
var _firstload = true;
var _image;
var _newimagesource;
var _image_exists;

// This function starts the actual banner timer
function start_banner_rotation()
{
	_t = setTimeout("start_banner_rotation()", (_rotate_delay * 1000));
	if (_firstload == true) { _firstload = false; preload_banners(); }
  else { rotate_banner(); }
}

function stop_banner_rotation()
{
	clearTimeout(_t);
}

// This function does the actual image transition
function rotate_banner()
{
  // Determin the new _max value based on the size of the array
  _max = _allbanners.length;
  // If there are no banners, return false
  if (_max < 1) { return false; }
  // Define the objects we'll be using
  _image = document.getElementById(_banner_id); 
  // Pull the new image source based on the position
  _newimagesource = _allbanners[_position];
  // If the current image and the next image in rotation are the same,
  // increment _position
  _image_exists = _image.src.search(_image_path+_newimagesource);
  if (_image_exists == -1)
  {
	  //alert("Image Sorce : "+_image.src+"\r\n"+"New Source : "+_image_path+_newimagesource+"\r\n"+"Image Exists : "+_image_exists);
	  // Start the cross-fade
	  crossfade(_image, _image_path+_newimagesource, _fade_duration);
	  // Increment/loop _position
	  if (_position < (_allbanners.length - 1)) { _position = _position + 1; }
	  else { _position = 0; }	  
  }
  else
  {
	  // Increment/loop _position
	  if (_position < (_allbanners.length - 1)) { _position = _position + 1; }
	  else { _position = 0; }
	  // Recursively call this function to try again
	  rotate_banner();
	  
  }
  // Return true
  return true;
}

//This function shuffles the banner array around so that the order is random
function shuffle_banner_order()
{
	_allbanners = shuffle(_allbanners);
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

// This function preloads all the banners so that the transitions are smoother
function preload_banners()
{
  // Define the number of banners to loop through
  var _numbanners = _allbanners.length;
  // Create the image object
  preload_banner_object = new Image();
  // Loop through all the banners, preloading each
  for (i = 0; i < _numbanners; i++)
  {
	  // Preload the banner
	  preload_banner_object.src = _allbanners[i];
	  // Throw an error
	  //document.write("Banner "+_allbanners[i]+" has been loaded.");
  }
}

