$(document).ready( function(){
	window.Background = new Background();
});

Background = function() {
	var self = this;
	this.body = $("body"); 
	this.container = $("#background");
	this.image = $("img", this.container);
	this.image.css({ position: "absolute", left: "-10000px" });
	this.alternate = false;
	this.check = function() {	
		if(this.alternate && this.image.width() < this.container.width()) {
			this.image.removeClass("alternate");
			this.alternate = false;
		}
		if(!this.alternate && this.image.height() < this.container.height()) {
			this.image.addClass("alternate");
			this.alternate = true;
		}
		var margin = -(this.image.height() - this.container.height())/2 + "px 0 0 " + -(this.image.width() - this.container.width())/2 + "px";
		this.image.css({ margin: margin });
	}
	$(window).resize(function() { self.check(); });
	this.image.load(function() {
		self.check(); 
		$(this).css({ position: "static" });
	});
}