/**
 * jQuery.fn.rewImgSizer
 * version 1.2.1
 * COPYRIGHT 2010
 *
 * Usage: $('img').rewImgSizer({});
 *
 * Options: 
 *
 * scale : Ratio (e.g 1.25)
 * align: 'top left', 'top center', 'top right', 'center', 'center right', 'center left', 'bottom right', 'bottom center' or 'bottom left'
 * scale: 'scale', 'crop', 'fit' or 'none'
 * noUpscale: bollean (true or false)
 * onBeforeShow:
 * onAfterShow:
 *
**/
jQuery.fn.rewImgSizer = function(options) {

	var $self = this;

	var defaults = {
		method: 'crop',
		scale: 1,
		align: 'center',
		effect: 'fadeIn',
		noUpScale: false,
		onBeforeShow: function(){},
		onAfterShow: function(){}
	};

	var options = $.extend(defaults, options);

	$self.resize = function ($img) {


		/* lets kill styles */
		$img.css({'width':'auto','height':'auto'});

		/* photo
		----------------------------------------------------------- */

		var pho_vsize = 1;
		var pho_hsize = 1;
		var pho_width = $img.width();
		var pho_heigh = $img.height();

		if(pho_width > pho_heigh) {
			pho_vsize = Math.round((pho_width / pho_heigh) * 100) / 100;
		} else{
			pho_hsize = Math.round((pho_heigh / pho_width) * 100) / 100;
		}


		/* canvas
		----------------------------------------------------------- */

		$can = $img.parent();

		var can_vsize = 1;
		var can_hsize = 1;
		var can_width = $can.innerWidth();
		var can_heigh = $can.height();

		if(can_width > can_heigh) {
			can_vsize = Math.round((can_width / can_heigh) * 100) / 100;
		} else{
			can_hsize = Math.round((can_heigh / can_width) * 100) / 100;
		}


		/* store in case we want to revert */
		oldmethod = options.method;

		if(options.noUpScale) {
			if(pho_width < can_width && pho_heigh < can_heigh) {
				options.method = 'none';
			}

		}


		/* ect.
		----------------------------------------------------------- */

		//console.log(pho_hsize + ':' + pho_vsize + ' in ' + can_hsize + ':' + can_vsize);

		if (options.method == 'crop') {

			if(pho_hsize > can_hsize) {
				$img.width(can_width);
			} else if(pho_vsize > can_vsize) {
				$img.height(can_heigh);
			} else {
				// square 1:1
				if(can_hsize > can_vsize) {
					$img.height(can_heigh);
				} else {
					$img.width(can_width);
				}
			}

		} else if (options.method == 'fit') {
		
			$img.height(can_heigh);
			$img.width(can_width);

		} else if (options.method == 'scale') {
		
			if(pho_hsize > can_hsize) {
				$img.height(can_heigh);
			} else if(pho_vsize > can_vsize) {
				$img.width(can_width);
			} else {
				// square 1:1
				if(can_hsize > can_vsize) {
					$img.width(can_width);
				} else {
					$img.height(can_heigh);
				}
			}

		} else {

			/* no resize */

		}

		/* SCALE OPTION */
		if(options.scale) {
			sImgH = $img.height() * options.scale;
			sImgW = $img.width() * options.scale;
			$img.width(sImgW); $img.height(sImgH);
		}

		$img.css({position:'absolute', left: ''});

		switch(options.align) {
			case 'center':
    			$img.css('top', -($img.height() - can_heigh) / 2 + 'px');
    			$img.css('left', -($img.width() - can_width) / 2 + 'px');
			break;
			case 'top left':
    			$img.css({'top': 0, 'left': 0});
			break;
			case 'top center':
    			$img.css('top', 0);
    			$img.css('left', -($img.width() - can_width) / 2 + 'px');
			break;
			case 'top right':
    			$img.css('top', 0);
    			$img.css('right', 0);
			break;
			case 'center right':
    			$img.css('top', -($img.height() - can_heigh) / 2 + 'px');
    			$img.css('right', 0);
			break;
			case 'center left':
    			$img.css('top', -($img.height() - can_heigh) / 2 + 'px');
    			$img.css('left', 0);
			break;
			case 'bottom right':
    			$img.css('bottom', 0);
    			$img.css('right', 0);
			break;
			case 'bottom center':
    			$img.css('bottom', 0);
    			$img.css('left', -($img.width() - can_width) / 2 + 'px');
			break;
			case 'bottom left':
    			$img.css('bottom', 0);
    			$img.css('left', 0);
			break;
		}

		$img.hide();
	
		showImage();

		function showImage() {
			options.onBeforeShow.call(this);
			$img.fadeIn('slow', options.onAfterShow.call(this));
		}
		
		options.method = oldmethod;

	};



    jQuery(this).each(function () {
 
		$img = $(this);
		$img.css({position: 'absolute', left: '-999em'});

		var imgsrc = $img.attr('src');

		$img.attr('src', '').load(function(){
			$self.resize($(this));
		});

		$img.attr('src', imgsrc);

	});

	// allow jQuery chaining 
	return this;

};
