var picture_changer_interval=6000;	// how long (ms) between image changes
var picture_changer_fade_time=3.0;	// how long to spend fading the images (s)

// var curr_img = 0; defined before this script is loaded so that we can function properly if we aren't starting at image 0
var next_img = get_next_picture_changer_img();
var top_img = 0;
var bottom_img = 1;
var timer_id;	// the last activated timer
var cancel_timer = false;
var next_img_title = '';

function auto_swap_image()
{
//alert( top_img + "\n" + bottom_img );
//alert( curr_img + "\n" + next_img );
	if( cancel_timer )
		return;
	/* The Fade effect is non-blocking */
	Effect.Appear('image'+top_img, {duration:0, from: 1, to: 1});
	Effect.Appear('image'+bottom_img, {duration:0, from: 1, to: 1});
	// top image will be transparent at present
	var next_img_container = document.getElementById('image'+top_img);
	var curr_img_container = document.getElementById('image'+bottom_img);

	// update for next time
	top_img = top_img ? 0 : 1;
	bottom_img = bottom_img ? 0 : 1;

	next_img_container.style.zIndex = 100;
	curr_img_container.style.zIndex = 101;
	
	// next_img is now hidden from view, so switch to the next one
	next_img_title = image_list[next_img][2].toLowerCase();
	if(next_img_title.toLowerCase().indexOf('pinnacle')>=0){
		next_img_container.innerHTML = '<a href="/pinnacle-garden-office.php"><img src="' + image_list[next_img][1] + '" title="' + unescape( image_list[next_img][2] ) + '" alt="' + unescape( image_list[next_img][2] ) + '"/></a>';
	}else{
		next_img_container.innerHTML = '<img src="' + image_list[next_img][1] + '" title="' + unescape( image_list[next_img][2] ) + '" alt="' + unescape( image_list[next_img][2] ) + '"/>';
	}
	//alert( next_img_container.innerHTML );
	Effect.Fade('image'+top_img, {duration:picture_changer_fade_time, from: 1, to: 0});
	 /* keep the highlighted thumbnail in sync.
	 	*750 = 1000 * 0.75 (change active thumbnail at 75% through the fade */
	setTimeout( 'update_thumbnails(' + image_list[next_img][0] + ', true)', (picture_changer_fade_time*750) );
	
	/* Setup everything ready for the next call to this function */
	curr_img = next_img;
	next_img = get_next_picture_changer_img();

	//alert( 'curr_img == ' + curr_img + "\nnext_img == " + next_img );
	timer_id = setTimeout( 'auto_swap_image()', picture_changer_interval );
}

function update_thumbnails( id, delayed )
{
	if( cancel_timer && delayed )
	{	// timer has been cancelled, but this function call is a residual setTimer() based one
		return;
	}
	document.getElementById('thumbnails').setAttribute( 'class', 'thumbnail_' + id );
	document.getElementById('thumbnails').setAttribute( 'className', 'thumbnail_' + id );	
}

function set_main_photo( id, photo_path, photo_caption )
{
	cancel_timer = true;	// bodge because cancelTimer is coming up with a "not defined" error
//	cancelTimer( timer_id );	// the user has started clicking piccies, so kill off the auto changer
	var curr_img_container = document.getElementById('image'+bottom_img);
	
	if(photo_caption.toLowerCase().indexOf('pinnacle') >= 0){
		curr_img_container.innerHTML = '<a href="/pinnacle-garden-office.php"><img height="300" width="400" src="' + photo_path + '" title="' + unescape( photo_caption ) + '" alt="' + unescape( photo_caption ) + '"/></a>';
	}else{
		curr_img_container.innerHTML = '<img height="300" width="400" src="' + photo_path + '" title="' + unescape( photo_caption ) + '" alt="' + unescape( photo_caption ) + '"/>';
	}
	
// set class on #thumbs to the active thumbnail, and let the main stylesheet deal with it - no need to clear the class on all other thumbs
	update_thumbnails( id, false );
}

function get_next_picture_changer_img()
{
	if( (curr_img + 1) >= image_list.length )
		next_img = 0;
	else
		next_img = curr_img + 1;
	return( next_img );
}

function preload_images()
{
	for( var i=0; i<image_list.length; i++ )
	{
		image_list[i][3].src = image_list[i][1];
//		alert( 'src=' + image_list[i][3].src );
	}
}

function init_auto_changer()
{
	// timeout set to the 100% opacity time for the images
	timer_id = setTimeout( 'auto_swap_image()', (picture_changer_interval-(picture_changer_fade_time*1000)) );
	preload_images();
}

