var GayImage = new Class({
	
	Implements: Options,
	
	options: {
		'imageSource': false,
		'width': 80,
		'height': 80,
		'valign': 'middle',
		'container': false,
		'onComplete': false,
		'inject': 'top',
		'applyMargins': true,
		'nudgeLeft': 0,
		'nudgeTop': 0
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		this.moImage = false;
		this.moSize = false;
	},
	
	load: function(){
		if (this.options.imageSource === false || this.options.container === false){
			return;
		}
		
		if (this.moImage !== false){
			this.setImage(this.moImage);
			return;
		}
			
		var o_image = new Asset.image(this.options.imageSource, {
			'onload': (this.setImage).bind(this)
		});
	},
	
	setImage: function(oImage, bInject){
		bInject = $type(bInject) ? bInject : true;
		
		if (this.moImage === false){
			this.moImage = oImage;
			if (bInject === false){
				this.moSize = oImage.getSize();
			} else {
				this.moSize = {
					'x': parseInt(oImage.get('width'), 10),
					'y': parseInt(oImage.get('height'), 10)
				};
			}
			
			this.resizeImage();
			if (this.options.applyMargins === true){
				this.applyMargins();
			}
		}
		
		if (bInject === true){
			this.moImage.inject(this.options.container, this.options.inject);
		}
		
		if ($type(this.options.onComplete) === 'function'){
			this.options.onComplete(this.moImage);
		}
	},
	
	resizeImage: function(){
		// determine right dimension for image
		var i_factor,
			i_height,
			i_width;
			
		// heavily oversized picture
		if (this.moSize.x > this.options.width && this.moSize.y > this.options.height){
			i_factor = (this.options.width / this.moSize.x);
			this.moSize.x = this.options.width;
			this.moSize.y = Math.round((this.moSize.y * i_factor));
		}
		
		if (this.options.width > this.moSize.x){
			i_factor = (this.options.width / this.moSize.x);
			i_height = Math.round((this.moSize.y * i_factor));
			if (i_height > this.options.height){
				this.moSize.y = i_height;
				this.moSize.x = this.options.width;
			}
		}
		
		if (this.options.height > this.moSize.y){
			i_factor = (this.options.height / this.moSize.y);
			i_width = Math.round((this.moSize.x * i_factor));
			if (i_width > this.options.width){
				this.moSize.x = i_width;
				this.moSize.y = this.options.height;
			}
		}
		
		this.moImage.set('height', this.moSize.y);
		this.moImage.set('width', this.moSize.x);
	},
	
	applyMargins: function(){
		var i_extra, 
			i_x_margin,
			i_y_margin,
			i_max;
		
		// margin-left
		i_extra = parseInt(this.options.container.getStyle('margin-left').replace(/px$/, ''), 10) + parseInt(this.options.container.getStyle('margin-right').replace(/px$/, ''), 10);
		i_x_margin = ((Math.round(((this.moImage.get('width') - this.options.width - i_extra) / 2)) * -1) + this.options.nudgeLeft);
		i_x_margin = Math.min(i_x_margin, 0);
		this.moImage.setStyle('margin-left', i_x_margin + 'px');
		
		// margin-top
		switch (this.options.valign){
			case 'top':
				i_y_margin = 0;
				break;
			
			case 'face':
				i_max = (-1 * (this.moImage.get('height') - this.options.height));
				i_y_margin = (-1 * Math.round(this.moImage.get('height') / (3 * 2)));
				i_y_margin = Math.max(i_y_margin, i_max);
				break;
				
			default:
				i_extra = parseInt(this.options.container.getStyle('margin-top').replace(/px$/, ''), 10) + parseInt(this.options.container.getStyle('margin-bottom').replace(/px$/, ''), 10);
				i_y_margin = (Math.round(((this.moImage.get('height') - this.options.height - i_extra) / 2)) * -1);
				i_y_margin = Math.min(i_y_margin, 0);
		}
		
		this.moImage.setStyle('margin-top', i_y_margin);
	}
});
