/*
ScaleBG (Scale Background) version 1.5,
Last edit: 02.04.2010
By Morten Sørdahl Nielsen, http://www.sordahl.dk
*/

var scaleBackground = function(){
	var bgImageSize = {
		width : 1933,
		height : 900
	};
	var imgAR;
	var resizeAction = function() {
		var win = {
			height : $(window).height(),
			width : $(window).width()
		};

		// The current aspect ratio of the window
		var winAR = win.width / win.height;
		window.status = 'ok';
		// Determine if we need to show the image and whether it needs to stretch tall or wide
		if (winAR < imgAR) {

			// Streching image
			$("#wallpaper").width("auto");
			$("#wallpaper").height("100%");

			// Center image vertically
			if (navigator.userAgent.toLowerCase().indexOf('msie 6') == -1){
				$("#wallpaper").css("position", "absolute");
				var newLeft = Math.round($("#wallpaper").width()/2);
				newLeft = newLeft*-1;
				newLeft = newLeft + Math.round(win.width/2);
				$("#wallpaper").css("left", newLeft+"px");
			} else {
				window.status = 'IE6';
				$("#wallpaper").width("100%");
				$("#wallpaper").height("auto");
			}
		} else {
			window.status = 'width'+winAR+' VS '+imgAR;
			// Streching image
			$("#wallpaper").css("left", '0px');

			$("#wallpaper").width("100%");
			$("#wallpaper").height("auto");
		}
		
		// Show background (prevents the loading of large images to show while loading)
		$("#wallpaper").css("visibility", "visible");
	};
	
	return {
		
		// Sets up the basic functionality
		initialize : function() {

			// Define CSS
			$("#wallbody").css({'position' : 'fixed', 'width' : '100%', 'height' : '100%', 'z-index' : '10', 'overflow' : 'hidden'});
		
			// Get the aspect ratio of the image
			imgAR = bgImageSize.width / bgImageSize.height;


			// Set up a resize listener to add/remove classes from the body 
			$(window).bind('resize', resizeAction);

			// Set it up by triggering a resize
			$(window).trigger('resize');
			
			$("#wallbody").fadeIn();
		}
	};
}();

$(window).load(scaleBackground.initialize);
