/**
 * Resize an image to given maxh and maxw remain the proper ratio 
 */
function resize(img, maxh, maxw, iwidth, iheight) {
  var image = document.getElementById(img);
  var ratio = maxh / maxw;
  var width = iwidth;
  var height = iheight;
  if (height/width > ratio){
     // height is the problem
    if (height > maxh){
      image.setAttribute('width', Math.round(width*(maxh/height)));
      image.setAttribute('height', maxh);
    }
  } else {
    // width is the problem
    if (width > maxh){
    	image.setAttribute('height', Math.round(height*(maxw/width)));
    	image.setAttribute('width', maxw);
    }
  } 
}