// **************************************************
// configuration variables
// **************************************************

/** The flag if images should be preloaded */
var enablePreloadImages = true;

/** The interval for rechecking completeness of image-preloading */
var preloadCheckInterval = 250;

/** The maximum interval to wait for completeness of image-preloading before skipping */
var preloadMaxInterval = 10000;

// **************************************************
// private variables
// **************************************************

/** The url's of the images which should be preloaded */
var _preloadImageUrls = new Array();

/** The image which should be preloaded */
var _preloadCurrentImage = null;

/** The interval of the currently preloaded image */
var _preloadCurrentInterval = 0;

// **************************************************
// public functions
// **************************************************

/**
 * Preloads an image.
 * The images are preloaded in the order they are added via this function.
 *
 * @param imgUrl  The url of the image
 */
function preloadImage(imgUrl) {
	if (imgUrl == '' || typeof imgUrl == 'undefined') return false;
	if (enablePreloadImages == false) return false;
	
	_preloadImagePushBack(imgUrl);
	return true;
}

/**
 * Preloads an image directly.
 * The given image is preloaded directly after the current running preload.
 *
 * @param imgUrl  The url of the image
 */
function preloadImageNow(imgUrl) {
	if (imgUrl == '' || typeof imgUrl == 'undefined') return false;
	if (enablePreloadImages == false) return false;
	
	_preloadImagePushFront(imgUrl);
	return true;
}

// **************************************************
//  private functions
// **************************************************

/**
 * Preloads an image directly.
 * The given image is preloaded directly after the current running preload.
 *
 * @param imgUrl  The url of the image
 */
function _preloadImagePushFront(imgUrl) {
	_preloadImageUrls.unshift(imgUrl);
	_preloadImage();
}

/**
 * Preloads an image.
 * The images are preloaded in the order they are added via this function.
 *
 * @param imgUrl  The url of the image
 */
function _preloadImagePushBack(imgUrl) {
	_preloadImageUrls.push(imgUrl);
	_preloadImage();
}

/**
 * Preloads the images.
 * The images are preloaded one after another int the order they are stored in an array.
 */
function _preloadImage() {
	// do nothing if preload is in progress
	if (_preloadCurrentImage != null) return;
	
	// do nothing if no more images to preload
	if (_preloadImageUrls.length == 0) return;
	
	// preload next image
	var imgUrl = _preloadImageUrls.shift();
	_preloadCurrentImage = new Image();
	if (imgUrl == '' || typeof imgUrl == 'undefined') alert('_preloadImage with empty or undefined imgUr');
	_preloadCurrentImage.src = imgUrl;
	_preloadCurrentInterval = 0;
	// set timeout for checking if preload is complete
	window.setTimeout('_checkPreloadingImage()', preloadCheckInterval);
}

/**
 * Checks if the preloading of the current image is complete.
 * Starts the next image-preload when last is complete.
 */
function _checkPreloadingImage() {
	var finished = _preloadCurrentImage.complete;
	
	// skip image if preloading takes too long
	_preloadCurrentInterval += preloadCheckInterval;
	if (_preloadCurrentInterval > preloadMaxInterval) {
		finished = true;
	}
	
	if (finished) {
		// finish completly preloaded or skipped image
		_preloadCurrentImage = null;
		_preloadImage();
	} else {
		// check again later
		window.setTimeout('_checkPreloadingImage()', preloadCheckInterval);
	}
}

