/**
 * jQuery.fn.rewImgSizer
 * version 0.9
 * COPYRIGHT 2010
 *
 * Usage: $('img').rewImgSizer({});
 *
 * Options:
 * 		method : 'scale' or 'zoomcrop' (default: scale)
 * 		scale : (ratio -- E.G. 1.25, 1.5, 2, default: 1 (100%))
 * 		center: BOOLEAN (true or false, defaults to true)
 *
 */
jQuery.fn.rewImgSizer = function(options) {

	var $self = this;

	var defaults = {
		method: 'scale',
		scale: 1,
		center: true,
		effect: 'fadeIn'
	};

	var options = $.extend(defaults, options);

	$self.resize = function ($img) {

		$container = $img.parent();
		$container.css('display','block');

		$img.css({height: 'auto', width: 'auto', position: 'absolute', left: '-999em'});
		$img.attr('height',''); $img.attr('width','');

		var containerH		 = 	$container.height();
		var containerW		 = 	$container.width();

		var imgH			 = 	$img.height();
		var imgW			 = 	$img.width();
		var imgHNew			 = 	0;
		var imgWNew			 = 	0;
		var offset			 =  0;

		if (imgH < imgW) {
			imgAspectR 		 = 'landscape';
			imgRatio		 = Math.round((imgH / imgW) * 100) / 100;
			imgRatioX		 = Math.round((imgW / imgH) * 100) / 100;
		} else {
			imgAspectR		 = 'portrait';
			imgRatio		 = Math.round((imgW / imgH) * 100) / 100;
			imgRatioX		 = Math.round((imgH / imgW) * 100) / 100;
		}

		$img.attr('class', imgAspectR);

    	if (options.sizeMethod == 'zoomcrop') {

			$img.addClass('zoomcrop');

    		/* sizing: zoomcrop */
    		if (imgAspectR == 'landscape') {
    			imgWNew = containerW;
    			imgHNew = imgWNew * imgRatio;
    		} else if (imgAspectR == 'portrait') {
    			imgWNew = containerW;
    			imgHNew = imgWNew * imgRatioX;
    		}

    	} else {

    		/* sizing: scale */
    		if (imgAspectR == 'landscape') {
    			imgWNew = containerW;
    			imgHNew = imgWNew * imgRatio;

    		} else if (imgAspectR == 'portrait') {
    			imgHNew = containerH;
    			imgWNew = imgHNew * imgRatio;
    		}
    	}

		/* SCALE OPTION */
		if(options.scale) {
			imgWNew  = Math.round(imgWNew * options.scale);
			imgHNew  = Math.round(imgHNew * options.scale);
		}

    	$img.width(imgWNew);
    	$img.height(imgHNew);

    	/* now we center with margins, if specified */
    	if(options.center == true) {
    		$img.css('margin-top', -(imgHNew - containerH) / 2 + 'px');    		
    		$img.css('margin-left', -(imgWNew - containerW) / 2 + 'px');
		} else {
			$img.css('margin',0);
		}

		if($img.width() < 1 || $img.height() < 0) $img.attr('style','');

		/* real hide, so we can animate */
		$img.hide();
		/* ok, we're ready. position and show */
		$img.css({position: 'relative', left: '0'});

		$img.fadeIn();
	};

    jQuery(this).each(function () {
 
		$img = $(this);
		$img.css({position: 'absolute', left: '-999em'});

		$img.attr('src', $img.attr('src')).load(function(){
			$self.resize($(this));
			$container.show();
		});
	});

};

$(document).ready(function(){
	$('.thumbset img').rewImgSizer({scale:1.85});
});