// RESIZE TO FILL

$(document).ready(function() {
	
	/////////////////////////////////
	var fillContainer = ".mGalleryThumbnail";
	/////////////////////////////////
	
	$(fillContainer + ' img').hide();
	
	$(window).load(function() {
		
		$(fillContainer + ' img').each(function() {
				
			var imgW = $(this).width();
			var imgH = $(this).height();
			
			var containerW = $(fillContainer).width();
			var containerH = $(fillContainer).height();
						
			var imgA = (imgW/imgH);
			var containerA = (containerW/containerH);
					
			$(this).css('position', 'relative');
			
			// If the image is larger than the container, resize it. 
			/*if ((imgW*imgH)>(containerW*containerH)) {*/
			
				if( imgA < containerA ) {
					$(this).css('min-width', containerW + 'px');
					$(this).css('max-width', containerW + 'px');
					var NewImgH = $(this).height();
					var CtrH = Math.round(((NewImgH-containerH)/2)*(-1));
					$(this).css('top', CtrH + 'px');
					$(this).show();
					} //if(imgW < imgH)
				
				if( imgA > containerA ) {
					$(this).css('min-height', containerH + 'px');
					$(this).css('max-height', containerH + 'px');
					var NewImgW = $(this).width();
					var CtrW = Math.round(((NewImgW-containerW)/2)*(-1));
					$(this).css('left', CtrW + 'px');
					$(this).show();
					}//if(imgW > imgH)
					
				if( imgA == containerA ) {
					$(this).css('min-width', containerW + 'px');
					$(this).css('max-width', containerW + 'px');
					var NewImgH = $(this).height();
					var CtrH = Math.round(((NewImgH-containerH)/2)*(-1));
					$(this).css('top', CtrH + 'px');
					$(this).show();
					} //if(imgW == imgH)
				
				// If the image is smaller than the container, just center it. 
				/*} else { 
					var CtrW = ((containerW-imgW)/2);
					var CtrH = ((containerH-imgH)/2);
					$(this).css('left', CtrW + 'px');
					$(this).css('top', CtrH + 'px');
					$(this).show();
					}*/
			}); 
			
	}); //(window).load
	
}); //(document).ready
