var PradaBackground = new Class({
	Implements : Options,
		options: {
			mode : 'fill',
			verticalAnchor : 'center',
			horizontalAnchor : 'center'
		},
	
	initialize : function(img,options){
		this.setOptions(options);
		this.img = $(img);
		this.container = this.img.getParent();
		this.container.setStyle('overflow','hidden');
		if(this.container.getStyle('position') != 'absolute' && this.container != document.body) this.container.setStyle('position','relative');
		this.setDimensions();
		window.addEvent('resize',function(){
			this.setDimensions();
		}.bind(this));
		if(Browser.Engine.webkit){
			window.addEvent('scroll',function(){
				this.img.setStyles({
					'top':  (typeof( window.pageYOffset ) == 'number') ? window.pageYOffset : document.body.scrollTop,
					'left': (typeof( window.pageYOffset ) == 'number') ? window.pageXOffset : document.body.scrollLeft
				});
			}.bind(this));
		}
	},

	// methods
	getDimensions : function(){
		imgWidth = this.img.getSize().x;
		imgHeight = this.img.getSize().y;
		imgRatio = imgWidth/imgHeight;
		containerWidth = this.container.getSize().x;
		containerHeight = this.container.getSize().y;
		windowRatio = containerWidth/containerHeight;
	},
	
	setDimensions : function(){
		this.getDimensions();
		switch (this.options.mode){
			case 'fill': this.fill(); break;
			case 'fit': this.fit(); break;
			case 'stretch': this.stretch();
		}
	},

	fill : function(){
		if(imgRatio >= windowRatio){ 
			this.img.setStyles({
				'height': containerHeight,
				'width' : '',
				'left' : '',
				'top' : '',
				'position' : Browser.Engine.webkit ? 'absolute' : 'fixed'
			});
			this.getDimensions();
			switch(this.options.horizontalAnchor){
				case 'center': this.img.setStyle('left',-(imgWidth-containerWidth)/2); break;
				case 'right': this.img.setStyle('left',-(imgWidth-containerWidth));
			}			
		} else { 
			this.img.setStyles({
				'height': '',
				'width' : containerWidth,
				'left' : '',
				'top' : ''
			});
			this.getDimensions();
			switch(this.options.verticalAnchor){
				case 'center': this.img.setStyle('top',-(imgHeight-containerHeight)/2); break;
				case 'bottom': this.img.setStyle('top',-(imgHeight-containerHeight));
			}
		}
	},
	
	stretch : function(){
		this.img.setStyles({
			'height' : containerHeight,
			'width' : containerWidth
		});
	},
	
	fit : function(){
		if(imgRatio >= windowRatio){ 
			this.img.setStyles({
				'height': '',
				'width' : containerWidth,
				'left' : '',
				'top' : ''
			});
			this.getDimensions();
			switch(this.options.verticalAnchor){
				case 'center': this.img.setStyle('top',(containerHeight-imgHeight)/2); break;
				case 'bottom': this.img.setStyle('top',containerHeight-imgHeight);
			}
		} else { 
			this.img.setStyles({
				'height': containerHeight,
				'width' : '',
				'left' : '',
				'top' : ''
			});
			this.getDimensions();
			switch(this.options.horizontalAnchor){
				case 'center': this.img.setStyle('left',(containerWidth-imgWidth)/2); break;
				case 'right': this.img.setStyle('left',containerWidth-imgWidth);
			}
		}
	}
});
