/****
	* auto resize the image which detact is larger than width or height
	*
	* autofit_hw( 190, this, 'w' )
	* autofit_hw( 190, this)	//default check the width
	* autofit_hw( 190, this, 'wh' ) // either width or height larger than the check px
	*/

function autofit_hw( fit_size, imageObj, /* "w" || "h" || "wh" || "hw" */ fit_type ){
	
	if (typeof(fit_size) == "undefined" || fit_size == "" || fit_size == 0)
		fit_size = 190;
	
	if (typeof(fit_type) == "undefined" || fit_type == "")
		fit_type = "w";
	
	// Grab the image's dimensions
	var imgH = imageObj.clientHeight;
	var imgW = imageObj.clientWidth;
	
	// Default = 0 = No need to do resize process
	var resize_is_needed = 0;
	
	// Check whether one of the larger length of the image dimension is greater than 'fit_size'
	// If this is the case, we are going to resize the source image size.
	if( fit_type=="wh" || fit_type=="hw" ){
		if (imgH > fit_size || imgW > fit_size)
			resize_is_needed = 1;
	}else if (fit_type=="h"){
		if (imgH > fit_size)
			resize_is_needed = 1;
	}else{
	//}else if (fit_type=="w"){
		if (imgW > fit_size)
			resize_is_needed = 1;
	}

	// Resize module
	if (resize_is_needed == 1){	
		// Find which dimension is scaled the most
		var scaleH =  fit_size / imageObj.clientHeight;
		var scaleW = fit_size / imageObj.clientWidth;
	
		// Scale the image
		if (scaleH < scaleW) {
			imageObj.style.height = fit_size;
			imageObj.style.width = Math.round(imgW * scaleH) + "px";
		} else {
			imageObj.style.width = fit_size;
			imageObj.style.height = Math.round(imgH * scaleW) + "px";
		}

	} else {
		// No need to resize, set as original dimensions
		imageObj.style.height = imgH; 
		imageObj.style.width = imgW;
	}
}
