var theFullScreen = new Class({
    Implements: Options,
    options: {
        'image':'../images/mybackground.jpg',
		'imageClass':'imgFullScreen',
		'container':null,
		'injectOption':'bottom',
		'wrapClass':'fullScreenWrap',
		'fadeIn':true,
		'center':true,
		'vCenter':true
    },
    initialize: function(options) {
        this.setOptions(options);
		
		if(!this.options.container) {
			this.options.container = $(document.body);
		} else {
			this.options.container = $(this.options.container);
		}
		
		this.img = new Asset.image(this.options.image, {
			'onLoad':function() {
				this.injectImage();
				this.toSize();
			}.bind(this)
		});
		
		window.addEvent('resize', function(event) {
			this.toSize();
		}.bind(this));
	},
	injectImage: function() {
		if(this.options.wrapClass) {
			var wrap = new Element('div',{'class':this.options.wrapClass});
			wrap.inject(this.options.container,this.options.injectOption);
			this.img.inject(wrap);
		} else {
			this.img.inject(this.options.container,this.options.injectOption);
		}
		if(this.options.fadeIn) {
			this.img.fade('hide');
			this.img.fade('in');
		}
		this.img.addClass(this.options.imageClass);
	},
	toSize: function() {
		var winSize = $(window).getSize();
		var winWidth = winSize.x;
		var winHeight = winSize.y;
		
		var imgSize = this.img.getSize();
		var imgWidth = imgSize.x;
		var imgHeight = imgSize.y;
		
		var imgRatio = imgWidth/imgHeight;
		var winRatio = winWidth/winHeight;
		
		var newWidth = 0;
		var newHeight = 0;
		
		if(imgRatio < winRatio) {
			// On s'adapte à la largeur
			newWidth = winWidth;
			newHeight = imgHeight*(winWidth/imgWidth);
		} else {
			// On s'adapte à la hauteur
			newWidth = imgWidth*(winHeight/imgHeight);
			newHeight = winHeight;
		}
		
		// Center
		var newTop = 0;
		var newLeft = 0;
		if(this.options.center) {
			newLeft = 0 - ((newWidth-winWidth)/2);
		}
		if(this.options.vCenter) {
			newTop = 0 - ((newHeight-winHeight)/2);
		}
		
		this.img.setStyles({
			'width':newWidth+'px',
			'height':newHeight+'px',
			'top':newTop+'px',
			'left':newLeft+'px'
		});
	}
});

