/*
Front-page Image Rotation
-------------------------
Rotate the front-page image at a regular interval.
Ensure we preload the next image in advance.


Version 0.1, 2009-10-02, Kendall Anderson
- initial design and coding
*/

var path_images   = '/sites/all/themes/oin_guide/css/i/';
//var images        = [ 'clinicaltrials.jpg', 'coping.jpg', 'diagrams.jpg', 'home.jpg', 'login.jpg', 'printable.png', 'psforms.jpg', 'resources.jpg', 'sideeffects.jpg', 'treatments.jpg', 'yourhospital.jpg' ];

var images        = [ 'coping.jpg', 'home.jpg', 'diagrams.jpg', 'login.jpg', 'psforms.jpg', 'yourhospital.jpg', 'resources.jpg', 'sideeffects.jpg', 'printable.png', 'clinicaltrials.jpg', 'treatments.jpg' ];


var current       = 0;//Math.floor(Math.random() * (images.length + 1));
var preloaded_all = 0;
var timeout_id    = 0;

function rotator()
{
	preload_next_image();
	timeout_id = setInterval(rotate_image, 5000);
}


function get_next_image_id()
{
	return ((current + 1) > (images.length - 1)) ? 0 : current + 1;
};


function preload_next_image()
{
	var next_id = get_next_image_id();
	if (preloaded_all === 1 || next_id === 0) {
		preloaded_all = 1;
		$('#preloaded').remove();
		return;
	}

	// To preload an image, we create a new '#preloaded' id at the page bottom, with 0 width and height.
	// We set it to be the url of the next image, which causes it to be fetched.
	$('body').append('<img id="preloaded" src="' + path_images + 'cap.' + images[next_id] + '" width="0" height="0" />');
};


function rotate_image()
{
	current = get_next_image_id();
	$('#image-rotate').attr('src', path_images + 'cap.' + images[current]);
	preload_next_image();
};

$(document).ready(function() {
	rotator();
});
;

