/**
 * Mod by Raymond 2010-02-06
 *
 * Resize thumbnail image with repect to fit into 80x80 div
 *
 */

function resizeThumb(img, w, h) { 

	// IE - solve IE onload handler loading too soon problem
    if (img.width == 0) {

    	// disable onload handler - loading image in load handler would cause infinite recursion
    	img.onload = null;
	    
		// explicitly load new image from javascript, forces it to load properly
    	var new_img = new Image();

	    new_img.src = img.src;		
		makeThumb(img, new_img, w, h);
		return;
	
	// All other browsers
	} else {

		makeThumb(img, null, w, h);
	}
}

function makeThumb(img, new_img, w, h) {

	var tmp_img = (new_img != null) ? new_img : img;
	var img_width = (new_img != null) ? new_img.width : img.width;
	var img_height = (new_img != null) ? new_img.height : img.height;

	// image in scale or image
	if (tmp_img.width == tmp_img.height) {

		img.width = w;
		img.height = h;
		
	// image not in scale	
	} else {
			
		tmp_img.width = Math.ceil(img_width / img_height * w);		
		tmp_img.height = Math.ceil(img_height / img_width * h);

		img.width = tmp_img.width;
		img.height = tmp_img.height;		

		if (tmp_img.width < tmp_img.height) { 

			img.style.height = "auto";
			img.style.width = w + "px";
			
			// center and crop verical image
			img.style.marginTop = -(tmp_img.height - h)/2 + "px";

		} else {

			img.style.width = "auto";
			img.style.height = h + "px";

			// center and crop horizontal image
			img.style.marginLeft = -(tmp_img.width - w)/2 + "px";
		}		
	}

	// IE
	if (new_img != null) {
		
		// load the image back into the original <img>
	  img.src = tmp_img.src;
	}
}
