// JavaScript Document

/**
 * jQuery.fn.rewImgSizer
 * version 1.3
 * 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) {

	    $self.show();

		/* photo
		----------------------------------------------------------- */

		var pho_vsize = 1;
		var pho_hsize = 1;
		var pho_width = $img.width();
		var pho_heigh = $img.height();

		// Generate both to prevent square images
		pho_vsize = Math.round((pho_width / pho_heigh) * 100) / 100;
		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.width();
		var can_heigh = $can.height();

		// Generate both to prevent square images
		can_vsize = Math.round((can_width / can_heigh) * 100) / 100;
		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.
		----------------------------------------------------------- */
		//$img.attr('title', 'photo height=' + pho_hsize + '; photo width=' + pho_vsize + '; canvas height=' + can_hsize + '; canvas width=' + can_vsize);

		if (options.method == 'crop') {

			if (pho_hsize > can_hsize) {
				$img.width(can_width);
				$img.height($img.width() * pho_hsize);
			} else if (pho_vsize > can_vsize) {
				$img.height(can_heigh);
				$img.width($img.height() * pho_vsize);
			} else {
				// square 1:1
				$img.width(can_width);
				$img.height($img.width() * pho_hsize);
			}

		} 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();

		options.onBeforeShow.call(this);
		$img.fadeIn('slow', options.onAfterShow.call(this));

		options.method = oldmethod;

	};

    jQuery(this).each(function (i) {

		$img = $(this);
		$img.css({position: 'absolute', left: '-999em'});
		
		/* fixes issue when image is resized multiple times or already has sizing applied */
		$img.css({height: 'auto', width: 'auto'});

		var orgSrc = this.src;
		if (!$.browser.msie) {
			this.src = ""; //ie will do funky things if this is here (show the image as an X, only show half of the image, etc)
		}
		$(this).bind('load', function(){
		    $(this).unbind('load');
			$self.resize($(this));
		});
		if (!$.browser.msie || $.browser.version >=9.0) {
			this.src = orgSrc; //needed for potential cached images
		} else if (this.complete || this.complete === undefined) {
			this.src = orgSrc;
			$(this).trigger('load');
		}

	});

	// allow jQuery chaining
	return this;

};
