// Entry point.
$(document).ready(function() {
	// Set background image.
	var image = $("#background img").clone();
	var filename = image.attr("alt")
		.toLowerCase()
		.replace(/[ äöüß]/g,function(c) {
			switch(c) {
				case ' ':
					return '';
				case 'ä':
					return 'ae';
				case 'ö':
					return 'oe';
				case 'ü':
					return 'ue';
				case 'ß':
					return 'ss';
				default:
					return c;
			}
		}) + "big.jpg";
	// Prevent caching in IE and Opera because "load" event may not be fired.
	if($.browser.msie || $.browser.opera)
		filename += "?" + (new Date()).getTime();
	var imgurl = image.attr("src").replace(/blank\.gif/,filename);
	image.attr("src",imgurl).load(function() {
		layoutBackgroundImage(image);
		// Make image visible.
		$("#background img").replaceWith(image);
	});
	
	// React to window size changes.
	setTimeout(function() {
		$(window).resize(function() {
			layoutBackgroundImage($("#background img"));
		});
	},100);
});

/**
 * Lays out the background image so its width is 100%
 * of the screen. It is assumed that the image is already
 * loaded.
 *
 * @param image jQuery object for the image.
 */
function layoutBackgroundImage(image) {
	var width = image.width();
	var height = image.height();
	if(width == 1 && height == 1)
		return;	// We have the blank image (due to a reload).
	
	// We need to remember the original image size.
	if(!image.data("originalWidth")) {
		image.data("originalWidth",width);
		image.data("originalHeight",height);
	} else {
		width = image.data("originalWidth");
		height = image.data("originalHeight");
	}
	
	image.width($(window).width()).height(height * $(window).width() / width);
		
}
