var preloaded_images = new Array();
var preloaded = false;

function newImage(url) {
	var img = new Image();
	img.src = url;
	return img;
}

function preload_images() {
	var images = document.getElementsByTagName("img");
	var i;
	var j = 0;

	for(i = 0; i < images.length; i++) {
		var image = images[i];
		if(image.className != 'preload' && image.className != 'preload_current') {
			continue;
		}
		// preloading
		var imgUrl = image.src;
		imgUrl = imgUrl.replace(/-n\.([a-z]+)$/, '-o.$1');
		preloaded_images[j++] = newImage(imgUrl);
		
		if (image.className == 'preload_current') {
			image.src = imgUrl;
		} else {

			// urls opslaan
			image.Normal = image.src;
			image.MouseOver = imgUrl;

			// event handlers koppelen
			image.onmouseover = function() { doMouseOver(this); };
			image.onmouseout = function() { doMouseOut(this); };
			image.onclick = function() { doClick(this); };
		}
	}

	preloaded = true;
}

function doMouseOver(image) {
	if(preloaded) image.src = image.MouseOver;
}

function doMouseOut(image) {
	if(preloaded) image.src = image.Normal;
}

function doClick(image) {
	image.onmouseover = null;
	image.onmouseout = null;
	doMouseOver(image);
}

window.onload = function() { preload_images(); };
