<!-- 
/*
File: image_rotation_script_v02.js
Author: Sean O'Neill (sean@marsworks.com)
Company: MARSWorks Inc.
Date: December 14, 2003
Purpose: code to swap the images in the Ottawa.com homepage
Needs: swap_script.js to be included in the file this is used
Notes: this is the second version that loads all of the images for the swaps after the initial page load
this is not the best way of doing this...the v01 file used better logic, but crapped out in IE for some unknown reason
Usage:
	simply place "loadOtherImages();" in the "onLoad" of the body tag	
*/

// script to switch the images on the home page on a timer...
// set the image pointers...actually just var them here, set them after loading...
var imgMainPhoto = null;
var imgMainText = null;
var imgMainBubble = null;

// set the time interval for switching...
var timeInterval = 10000; // in milleseconds

// the current image sequence being shown
var currentImage = 0;

var imagesToLoad = 0; // global variable to keep track of the images to download each switch

// arrays for the images to rotate
var arrayImageNames = new Array('worldClass', 'cultural', 'entrepreneurial', 'smart', 'sophisticated', 'vibrant');
imagesToLoad = arrayImageNames.length * 3; // 3 images per type, image, text and bubble

// the first set of images will load before the sequence begins...preload the others
function preloadImages(imgObj, imgSrc) {
	eval(imgObj + ' = new Image()');
	eval(imgObj + '.src = "' + imgSrc + '"');
	eval(imgObj + '.onload = loadCountdown');
}

function loadOtherImages() {
	// get a timestamp for the time the other images started to load
	startTimeStamp = new Date();
	startTimeStamp = startTimeStamp.getTime();

	// now that the page is loaded, set these variables to the proper image objects
	imgMainPhoto = document.images['imgMainPhoto'];
	imgMainText = document.images['imgMainText'];
	imgMainBubble = document.images['imgMainBubble'];

	for (var i = 0; i < arrayImageNames.length; i++) {
		// "lflag" is set in "swap_script.js" and carries the language of the page
		// load the main photo
		preloadImages('img_' + arrayImageNames[i] + '_images', '/images/homepage_images/' + arrayImageNames[i] + '_' + lflag + '.jpg');
			
		// load the text image
		preloadImages('img_' + arrayImageNames[i] + '_text', '/images/homepage_text/' + arrayImageNames[i] + '_' + lflag + '.gif');
			
		// load the bubble image
		preloadImages('img_' + arrayImageNames[i] + '_bubbles', '/images/homepage_bubbles/' + arrayImageNames[i] + '.gif');
	}
	
	setTimeout('checkDownload()', 3000); // give it three seconds to load the images, then start checking every 1/4 of a second to see if we're done
}

// countdown 1 for every image that loads up, then when we hit 0, start the sequence
function loadCountdown() {
	imagesToLoad--;
}

// function to change the main images to the next sequence of images
function swapMainImages() {
	// augment the currentImage pointer...if we're at the last image, start over
	if (currentImage == (arrayImageNames.length - 1)) {
		currentImage = 0;
	}
	else {
		currentImage++;
	}

	imgMainPhoto.src = eval('img_' + arrayImageNames[currentImage] + '_images.src');
	imgMainText.src = eval('img_' + arrayImageNames[currentImage] + '_text.src');
	imgMainBubble.src = eval('img_' + arrayImageNames[currentImage] + '_bubbles.src');
	
	// keep the swapping going!
	setTimeout('swapMainImages()', timeInterval);
}

function checkDownload() {
	if (imagesToLoad == 0) {
		// swap the images...may have to wait a bit here
		// get a timestamp for the second that the images are finished loading
		endTimeStamp = new Date();
		endTimeStamp = endTimeStamp.getTime();
		
		// since all images are loaded, start the next sequence if the elapsed time has been
		// at least the time interval specified between switches
		if (endTimeStamp - startTimeStamp >= timeInterval) {
			swapMainImages();		
		}
		else {
			setTimeout('swapMainImages()', timeInterval - (endTimeStamp - startTimeStamp))
		}
	}
	else {
		// check for the images being loaded every 1/4 of a second
		setTimeout('checkDownload()', 250);
	}
}

// -->
