﻿/*
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 : 1440,
		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;
		// 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 {

				$(".wallpaper").width("100%");
				$(".wallpaper").height("auto");
			}
		} else {
			// 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");
		
		//Fade in background
		$("#wallbody").fadeIn();

	};
	
	return {
		
		// Sets up the basic functionality
		initialize : function() {
			//BUG FIX
			if ($(".wallpaper").width() == 0){
				setTimeout(resizeAction,1);
			}
				// Define CSS
				$("#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 
				$(window).bind('resize', resizeAction);

				// Set it up by triggering a resize
				$(window).trigger('resize');
		}
	};
}();

$(window).load(scaleBackground.initialize);
