/***************************************
 preload images before changing the url
Written by Mark Wilton-Jones, 10/12/2001
            Version 1.0.1
  Updated 5/01/2007 to work in Safari
****************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

syntax used to call this function:
	preLdImg(desired_Href,functionName,image1Name[,image2Name[,image3Name[,etc.]]])
no two image names should be the same or the new URL will not be loaded.
If any image name is not a string, the new URL will be loaded immediately.
FunctionName must be written as a string, or null if none. The function will
be passed the portion of how many images have loaded every time an image loads,
eg. yourFunction(0.714)
You could use this along with my progress bar script using
'yourProgressBar.setBar'

*/

function preLdImg(oHref) {
	if( typeof( oHref ) != 'string' ) { return; }
	if( window.imgLdAr ) { for( var x = 2; x < window.imgLdAr.length; x++ ){ imgLdAr[x].onerror = null; imgLdAr[x].onload = null; } }
	window.imgLdAr = new Array(); window.imgCount = 0; window.hasLoadedImg = new Array();
	for( var x = 2; x < arguments.length; x++ ) {
		/* for each image name given as an argument,
		create a new image and as images load, count
		how many have loaded. When all have, change
		the URL. */
		if( typeof( arguments[x] ) != 'string' || !document.images ) { self.location.href = oHref; }
		imgLdAr[x] = new Image();
		imgLdAr[x].onload = new Function( //write the function as a string. Allows me to put in the number of images
			'if( hasLoadedImg[imgLdAr['+x+'].src] ){ return; } hasLoadedImg[imgLdAr['+x+'].src] = true; window.imgCount++;'+
			( arguments[1] ? ( arguments[1] + '( window.imgCount / ' + ( arguments.length - 2 ) + ' );' ) : '' )+
			"if( imgCount == " + ( arguments.length - 2 ) + " ) { self.location.href = '"+oHref+"'; }" );
		imgLdAr[x].onerror = new Function( "for(var x = 2;x<window.imgLdAr.length;x++){imgLdAr[x].onerror=null;imgLdAr[x].onload=null;}if( window.confirm( 'Pre-load error:\\n\\nThe following image failed to load:\\n'+this.src+'\\n\\nDo you want to jump to the next page without pre-loading images?' ) ) { self.location.href = '"+oHref+"'; }" );
		imgLdAr[x].src = arguments[x];
		/* Officially, if the image is already in cache, onload will not fire. It does in Internet
		Explorer. If this is Internet Explorer or due to some fast connection, the image has
		managed to load since the last line of code, then the onload function will be executed
		twice, doubling the image count. This will cause the new URL to be loaded before all images
		are cached. The onload function includes a check to see if this image has already triggered
		the onload function and if it has, it does not increase the count. */
		if( imgLdAr[x].complete ) { imgLdAr[x].onload(); }
	}
}