var dynBg = {
	'block': new Element('img').inject(document.body, 'top'),
	'aspect': 0,
	'init': function() {
		if(this.block) {
			this.block.setStyles({
				'position': 'fixed',
				'display': 'block',
				'width': '100%',
				'height': '100%'
			});
			
			this.block.set('src', BackgroundImage.src);
			var imgsize = {
				'x': BackgroundImage.width,
				'y': BackgroundImage.height
			};
			this.aspect = imgsize.x / imgsize.y;
			
			dynBg.resize();
			return true;
		} else {
			return false;
		}
	},
	'resize': function() {
		var winsize = window.getSize();
		var winaspect = winsize.x / winsize.y;
		
		var scaledsize = {
			'x': 0,
			'y': 0
		};
		
		scaledsize.x = winsize.y*this.aspect;
		scaledsize.y = winsize.y;
		
		// we've matched the height, but if the width is too small we need to match the width
		if(scaledsize.x < winsize.x) {
			scaledsize.x = winsize.x;
			scaledsize.y = winsize.x/this.aspect;
		}
		
		this.block.setStyles({
			'width': scaledsize.x,
			'height': scaledsize.y,
			'margin-left': 0.5*(winsize.x-scaledsize.x),
			'margin-top': 0.5*(winsize.y-scaledsize.y)
		});
	}

};
window.addEvent('load', function () {
	if(dynBg.init()) {
		window.addEvent('resize', function() {
			dynBg.resize();
		});
	}
});

