var _images		  					= null;
var min_index	   				= null;
var _current_index 				= null;
var max_index	   				= null;
var init_interval 				= null;
var image_div	   				= null;
var next_image						= null;
var micro							= null;
var a_second						= null;
var micro_image_switch_time 	= null;

function preload_images() {
	for(var i = 0; i < _image_files.length; i++) {
		_images[i] 		= new Image();
		_images[i].src = _image_files[i];
	}					
}

function slide_show() {
	clearTimeout(init_interval);
	if(image_loading_completed()) {
		/* Figure out the next image to show */ 
		if(_current_index < max_index) { 
			_current_index  = _current_index + 1;
			next_image 		 = _images[ _current_index ];
		} else if (_current_index >= max_index) {
			_current_index  = min_index;
			next_image 		 = _images[_current_index];
		}
		$('img#slide-show').fadeOut('slow', function() {
			$('img#slide-show').attr('src', next_image.src);
			$('img#slide-show').fadeIn('slow');
		});
		init_interval 	= setTimeout('slide_show()', micro_image_switch_time);
	} else {
		init_interval = setTimeout('slide_show()', a_second); /* Wait one more second for the images to load */ 		
	}
}

function image_loading_completed() {
	var all_loaded = true;
	for(var i = 0; i < _image_files.length; i++) {
		if( !image_loaded(_images[i]) ) {
			all_loaded = false;
		}
	}
	return all_loaded;
}

function image_loaded(the_image) {
	return the_image.complete ? true : false;
}

function init_slide_show() {
	_images		  				= new Array();
	min_index	   			= 0;
	current_index 				= min_index;
	max_index	   			= null;
	init_interval 				= null;
	image_div	   			= null;
	next_image					= new Image();
	micro							= 1000;
	a_second						= micro * 1;
	micro_image_switch_time = null;
	micro_image_switch_time = image_switch_time * micro;
	max_index 					= _image_files.length - 1;
	preload_images();
	init_interval = setTimeout('slide_show()', micro_image_switch_time);
}