﻿$('.resize img,img.resize,.resize-full img,.resize-icon img,.resize-thumb img').hide();
$(function() {
  $('.resize img,img.resize').each(function() {
    getImgSize($(this).attr('src'), $(this), 130, 102);
  });
  $('.resize-icon img').each(function() {
    getImgSize($(this).attr('src'), $(this), 80, 80);
  });
  $('.resize-thumb img').each(function() {
    getImgSize($(this).attr('src'), $(this), 120, 120);
  });
  $('.resize-full img').each(function() {
    getImgSize($(this).attr('src'), $(this), 500, 500);
  });
});
function getImgSize(imgSrc, imgObj, w, h) {
  var newImg = new Image();
  newImg.name = imgSrc;
  newImg.src = imgSrc;
  if (newImg.complete) {
    resize(newImg, w, h, imgObj);
  }
  else {
    newImg.onload = function() {
      resize(newImg, w, h, imgObj);
    }
  }
}
var resize = function(img, maxh, maxw, imgObj) {
  var ratio = maxh / maxw;
  if (img.height / img.width > ratio) {
    // height is the problem
    if (img.height > maxh) {
      img.width = Math.round(img.width * (maxh / img.height));
      img.height = maxh;
    }
  } else {
    // width is the problem
    if (img.width > maxh) {
      img.height = Math.round(img.height * (maxw / img.width));
      img.width = maxw;
    }
  }
  imgObj.attr('width', img.width);
  imgObj.attr('height', img.height);
  imgObj.show();
};

