var backgroundDiv, backgroundImage, backgroundImageRatio;

window.addEvents({
	'load': function(){
		backgroundDiv = document.id('bg');
		backgroundImage = document.id('bg').getElement('img');
		backgroundImageRatio = backgroundImage.getSize().x / backgroundImage.getSize().y;
		window.fireEvent('resize');
	},
	'resize': function(){
		var newSize = this.getSize();
		if(newSize.x / newSize.y > backgroundImageRatio) backgroundImage.setProperties({'width': newSize.x, 'height': newSize.x / backgroundImageRatio});
		else backgroundImage.setProperties({'width': newSize.y * backgroundImageRatio,'height': newSize.y});
		
	},
	'scroll': function(){
		var newScroll = this.getScroll();
		if(backgroundDiv) backgroundDiv.setStyles({'top': newScroll.y, 'left': newScroll.x});
	}
});
