// -*-java-*-
// ***************************************************************************************
// Functions to preload and play with Images
// ***************************************************************************************
// Version 0.3
//
// (C) 1999 Pinuts Media + Science GmbH
// Author: Raimund Jacob (jacob@pinuts.de)
//
// Note: Include this file into the html before you need it:
// <script language="JavaScript" src="pinuts-images.js" type="text/javascript"></script>
//
// Preloading tries not to preload the same Image twice

var preload_seen = new Array();


// preloads a range of images. The individual names are constructed as following:
// low is counted to high (for loop) and the number is right-padded with 0 to be
// len characters long. This is then put between basename and .extension.
// The function returns an array of image objects.
// Example:
// preload_range("bla", 0, 2, 3, "gif");
// loads bla000.gif and bla001.gif
function preload_range(basename, low, high, len, extension) {
    var picarray = new Array();

    for (g = 0, i = low; i < high; i++, g++) {
	var num = "" + i;
	while( num.length < len ) {
	    num = "0" + num;
	}
	var filename = basename + num + "." + extension;
	var seen = preload_seen[filename];
	if (seen) {
	    picarray[g] = seen;
	} else {
	    picarray[g] = new Image();
	    picarray[g].src = filename;
	    preload_seen[filename] = picarray[g];   // Remember this Image in the hash
	}
    }

    return picarray;
}


// Preloads another Image and appends it to the given picture array
function preload_picture(picarray, filename) {
    if (!picarray) {
	picarray = new Array();
    }

    var seen = preload_seen[filename];

    if (seen) {
	picarray[picarray.length] = seen;
    } else {
	var image = new Image();
	image.src = filename;
	picarray[picarray.length] = image;
    }

    return picarray;
}


// counts the number of images in a given array of images that have been loaded
// completely
// Note: Netscape reports not-existing images as complete while IE reports them
//       as !complete
function count_loaded(picarray) {
    var r = 0;
    for ( i=0; i<picarray.length; i++ ) {
	if (picarray[i].complete) r++;
    }
    return r;
}

// Constructs a string loaded/all from a given array of images.
// Note: Result differs in NS/IE if images don't exist
function progress(picarray) {
    return count_loaded(picarray) + "/" + picarray.length;
}

function img_tag(src, name, width, height) {
    var n = (name?'name="'+name+'" ':'');
    var w = (width?'width="'+width+'" ':'');
    var h = (height?'height="'+height+'" ':'');

    return '<img src="'+src+'" '+n+w+h+'border="0">';
}
