/*	
 *	jQuery fitImage 1.0b
 *	Demo's and docuimgntation:
 *	www.dosne.net (Soon)
 *
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 */

		var nbImg = 0;
		var nbLoadedImg = 0;
		
(function ($){
	
	$.fn.fitImage = function ( aWidth, aHeight, aType, aCompleteFunction, aClasses )
	{
		var me = $(this);
		nbImg = me.length

		me.one("load",function()
		{
			var img = $(this);
			var additionalClasses = aClasses != undefined ? aClasses : '';
			var wrap = img.wrap('<span class="img-fit-wrapper '+additionalClasses+'"></span>').parent();
			if( additionalClasses != '' )
				img.removeClass(additionalClasses);
				
			wrap.css({
				'display': 'block',
				'width': aWidth,
				'height': aHeight
			});
			placeImage(img, aWidth, aHeight, aType, aCompleteFunction)
		})
		.each(function()
		{
			if( this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) )
				$(this).trigger("load");
		});
	}
})(jQuery);
function placeImage( img, aWidth, aHeight, aType, aCompleteFunction )
{
	nbLoadedImg++;
	img.isset = true;
	var w = img.width();
	var h = img.height();
	var ratio = w / h;
	switch( aType )
	{
		case 'adjust' :
			if( w < h )
			{
				img.css('height', aHeight);
				img.css('width', aHeight*ratio);
				img.css('margin-left', (img.width() - aWidth) / 2 * -1);
				img.css('margin-top', 0);
			}
			else
			{
				img.css('width', aWidth);
				img.css('height', aWidth/ratio);
				img.css('margin-left', 0);
				img.css('margin-top', (img.height() - aHeight) / 2 * -1);
			}
			break;
			
		case 'scale' :
			img.css('height', aHeight);
			img.css('width', aWidth);
			break;
			
		case 'fill' :
		default :
			if( w > h )
			{
				if( w < aWidth )
				{
					img.css('width', aWidth);
					img.css('margin-left', 0);
					img.css('margin-top', (img.height() - aHeight) / 2 * -1);
				}
				else
				{
					img.css('height', aHeight);
					img.css('margin-left', (img.width() - aWidth) / 2 * -1);
					img.css('margin-top', 0);
				}
			}
			else
			{
				if( h < aHeight )
				{
					img.css('height', aHeight);
					img.css('margin-left', (img.width() - aWidth) / 2 * -1);
					img.css('margin-top', 0);
				}
				else
				{
					img.css('width', aWidth);
					img.css('margin-left', 0);
					img.css('margin-top', (img.height() - aHeight) / 2 * -1);
				}
			}
			break;
	}
	if( nbLoadedImg == nbImg && aCompleteFunction != undefined)
	{
		if( typeof(aCompleteFunction) == 'string' )
			eval(aCompleteFunction);
		else
			aCompleteFunction();
	}
};
