
window.addEvent('domready', function() {
	
	var setUpBackgroundImage  = function(source){
		var backgroundContainer = null;
		var backgroundImageUrl = source.getStyle('background-image');
		var backgroundImage = null; 
			
		source.setStyle('background-image', 'none');
		
		var resizeBackground = function(){
			if(backgroundContainer){
				var windowHeight = window.getHeight();
				var windowWidth = window.getWidth();
				
				
				if(backgroundImage.height / backgroundImage.width < windowHeight / windowWidth) {
					
					backgroundImage.setStyles({
						'height': windowHeight,
						'width': 'auto'
					});
				} else {
					backgroundImage.setStyles({
						'height': 'auto',
						'width': windowWidth
					});
				}
			}
		};
		
		// css property 
		if(backgroundImageUrl.match(/^url\((?:\"|\')?([^\)]*)(?:\"|\')?\)$/i)){
			
			backgroundImageUrl = backgroundImageUrl.replace(/^url\((?:\"|\')?([^\)]*)(?:\"|\')?\)$/i, '$1');
			backgroundImageUrl = backgroundImageUrl.replace('%22', '');
			backgroundImageUrl = backgroundImageUrl.replace('"', '');
			
			backgroundContainer = new Element('div', {id: 'background'});
			backgroundImage = new Element('img', {src: backgroundImageUrl, 'class': 'background-image', alt:''});
			backgroundImage.inject(backgroundContainer, 'bottom');

			backgroundContainer.inject(source, 'bottom');
			
			backgroundContainer.setStyles({
				'position' : 'fixed', 
				'top' : '0px', 
				'z-index' : -10 , 
				'display' : 'block',
				'overflow': 'hidden',
				'text-align': 'left',
				'width': '100%',
				'height': '100%'
			});

			resizeBackground();
		}

		$(window).addEvent('resize', resizeBackground);
	};

	// ie 6 is excluded
	if (!window.ie6) {
		setUpBackgroundImage($$('body')[0]);
	}
});
