var canImproveResolution = false;
thumbResize = function(thumb, min_width_orig, min_height_orig, orientation, reverse) {

	reverse = (reverse == null) ? "no" : reverse;
	
	if (reverse == "no"){
		twidth = jQuery(thumb).width();
		theight = jQuery(thumb).height();
		min_width = min_width_orig;
		min_height = min_height_orig;
	}else{
		twidth = min_width_orig;
		theight = min_height_orig;
		min_width = jQuery(thumb).width();
		min_height = jQuery(thumb).height();
	}	
	
	new_height = theight;
	new_width = twidth;
	
	//calculate the good size
	if(twidth>min_width && theight>min_height){
		if (orientation=='landscape') {
			ratio = min_width/min_height;
			tratio = twidth/theight;
			if (tratio>ratio) {
				new_height = min_height;
				new_width = parseInt(new_height*tratio);
			}
			else {
				new_width = min_width;
				new_height = parseInt(new_width/tratio);
			}
		}
		else {
			ratio = min_height/min_width;
			tratio = theight/twidth;
			if (tratio>ratio) {
				new_width = min_width;
				new_height = parseInt(new_width*tratio);
			}
			else {
				new_height = min_height;
				new_width = parseInt(new_height/tratio);
			}
		}
	}

	// resize thumb
	if (theight!=new_height || twidth!=new_width) {
		//alert('Width:' + new_width);
		//alert('Height:' + new_height);
		jQuery(thumb).height(new_height);
		jQuery(thumb).width(new_width);
	}
	// adjust position (vertical and horizontal centering for css cropping)
	if (new_width >= min_width) {
		moveleft = parseInt((new_width-min_width)/2);
		jQuery(thumb).css('left', '-'+moveleft+'px');
	}else{
		jQuery(thumb).css('left', '0px');
	}
	if (new_height >= min_height) {
		movetop = parseInt((new_height-min_height)/2);
		jQuery(thumb).css('top', '-'+movetop+'px');
	}else{
		movetop = parseInt((min_height/2)-(new_height/2));
		jQuery(thumb).css('top', movetop+'px');
	}
}

resizeThumbs = function(thumbs, min_width, min_height) {
	orientation = (min_height > min_width) ? 'portrait' : 'landscape';
	// hide images before resizing to avoid bad visual effect
	thumbs.each( function() {
		jQuery(this).css('visibility', 'hidden');
		})
	// use window.load because on document.ready images are not always loaded
	jQuery(window).load(function() {
		thumbs.each( function() {
			thumbResize(this, min_width, min_height, orientation );
			jQuery(this).css('visibility', 'visible');
		})
	});
}

