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

var $j = jQuery.noConflict();

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

		// The current aspect ratio of the window
		var winAR = win.width / win.height;

		// Determine if we need to show the image and whether it needs to stretch tall or wide
		if (winAR < imgAR) {

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

			// Center image vertically
			$j("#wallpaper").css("position", "absolute");
			$j("#wallpaper").css("left", (($j("#wallpaper").width()/2)*-1)+(win.width/2));
		} else {
			// Streching image
			$j("#wallpaper").css("left", '0px');
			$j("#wallpaper").width("100%");
			$j("#wallpaper").height("auto");
		}
		
		// Show background (prevents the loading of large images to show while loading)
		$j("#wallpaper").css("visibility", "visible");
	};
	
	return {
		
		// Sets up the basic functionality
		initialize : function() {

			// Define CSS
			$j("#wallbody").css({'position' : 'absolute', '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 
			$j(window).bind('resize', resizeAction);

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

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