var background = function() {
	// Get the Container and Image
	var container = $(document.body).getElement('div.img-container'), image = container.getElement('img');
	// Get aspect ratio
	var ratio = image.getSize().x/image.getSize().y;
	// Get page content size
	var min = contentheight = 0;
	$each($(document.body).getChildren().erase(container), function(item) {
		min = item.getSize().x>min ? item.getSize().x : min;
		contentheight = contentheight + item.getSize().y;
	});
	// Calculate image size
	var width = container.getSize().x.limit(min, min+container.getSize().x), height = Math.floor(width/ratio);
	// Apply sizes to container and image
	container.setStyles({'min-width': min, 'height': height.limit(0, (window.getSize().y>contentheight ? window.getSize().y : contentheight))});
	image.set({'width': width, 'height': height});
	// Create a div over the image
	if ($chk(container.getElement('div'))) container.getElement('div').setStyles({'width': width, 'height': height})
	else new Element('div', {'styles': {'position': 'absolute', 'z-index': '10', 'width': width, 'height': height}}).inject(container);
	// Footer
	var footer = $(document.body).getElement('div.footer');
	var footersize = (container.getStyle('height').toInt() - (contentheight - footer.getSize().y)).limit(5, 10000);
	footer.setStyle('min-height', footersize);
};

window.addEvent('domready', function() {
	// Run script only if browser doesn't support background-size style
	if ($(document.body).getStyle('background-size')==undefined || $(document.body).getStyle('background-size')=="") {
		// Keep image loaded and take it off the background of body
		var bgimage = $(document.body).getStyle('background-image').substr(4).replace(/[\"|)]/g, '');
		$(document.body).erase('style');
		// Create div with image for background
		var div = new Element('div', {'class': 'img-container'}).inject(document.body, 'top');
		new Element('img', {'src': bgimage, 'width': 2560, 'height': 1974, 'alt': ''}).inject(div);
		// Event on resize
		background.run();
		window.addEvent('resize', function() { background.run(); });
	}
});
