// Namespace function
function namespace(ns) {
    ns = ns.split('.');
    var cur = window, i;
    while ( i = ns.shift() ) {
        if ( !cur[i] ) cur[i] = {};
        cur = cur[i];
    }
}

namespace("website");

$(document).ready(function(){
	website.background_setup();	
});

website = {
	
	background_setup: function() {
		$bg = $("#bgimg");
		$bg.one('load',function(){
			var ratio = $bg.width() / $bg.height();
			website.background_resize($('#bgimg'), ratio);
			$bg.show();
			$(window).resize(function(){
				website.background_resize($('#bgimg'), ratio);
			});
		}).each(function(){
			if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)){
				$(this).trigger("load");
			}
		});
	},
	
	background_resize: function($bg, ratio) {
		var currentRatio = $(window).width() / $(window).height();
		if(currentRatio > ratio){
			$bg.css('width', '100%');
			$bg.css('height', 'auto');
		}else{
			$bg.css('width', 'auto');
			$bg.css('height', '100%');
		}
		var xDisplace = $bg.width() - $(window).width();
		var yDisplace = $bg.height() - $(window).height();
	
		$bg.css('left', (-xDisplace/2) + "px");
		$bg.css('top', (-yDisplace/2) + "px");
	}
};
