/* Resize main image */
function resize_image() {
	var max_width = 300;
	var max_height = 300;
	var image = document.getElementById("detalle_image");

	/* Check the width */
	if (image.width > max_width) {
		image.height *= max_width / image.width;
		image.width = max_width;
	}
	/* Check the height */
	if (image.height > max_height) {
		image.width *= max_height / image.height;
		image.height = max_height;
	}
}

function copy_image(image) {
	var main_image = document.getElementById("detalle_image");

	main_image.src = image.src;
	main_image.width = image.width;
	main_image.height = image.height;
	resize_image();
}

function show_image(url) {
	var new_image = new Image();

	document.getElementById("detalle_image").onload = null; // Disable the old handler, because it does not know the new size
	new_image.onload = function() { copy_image(new_image); };
	new_image.src = url;
}

function next_image(current) {
	if (null != current){
		/* Add 1 to current image number and get corresponding thumb */
		var newNum = parseInt(current.replace(/\D/g,'')) + 1;
		var thumb = document.getElementById('thumbImg' + newNum);

		/* If no more images, go back to first image */
		if (null == thumb) {
			newNum = 0;
			thumb = document.getElementById('thumbImg0');
		}
		if (null != thumb){
			show_image(thumb.src.replace('thumbs','images'));
			return 'thumb' + newNum;
		}
	}	
}
