﻿var mImgHeight = 0; //Max Height
var mImgWidth = 600; //Max Width
addLoadEvent(IRO);
// Image Resize Onload
function IRO() {
      var myClass = document.getElementsByClassName('msgBody');
      for (i = 0; i < myClass.length; i++) {
         var myTag = myClass[i].getElementsByTagName('img');
         for (i1 = 0; i1 < myTag.length; i1++) {
            if ((mImgHeight != 0 || mImgWidth != 0) && (myTag[i1].width > mImgWidth || myTag[i1].Height > mImgHeight)) {
               resize(myTag[i1]);
            }
         }
      }
}
// Image Resize
function resize(oImg){
var oImgHeight = oImg.height;
var oImgWidth = oImg.width;
var strtableid = Math.random();
oImg.id = "img_" + strtableid;


if (mImgHeight != 0 && oImgHeight > mImgHeight) {
   oImg.width = (mImgHeight / oImgHeight ) * oImgWidth;
   oImg.height = mImgHeight;
   new Insertion.Before(oImg, "<table onclick=\"\" style=\"cursor: pointer;\" id=\"" + strtableid + "\" width=\"" + oImg.width + "\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\"><tr><td class=\"errorTable\"><span id=\"t_" + strtableid + "\">Ảnh đã được thu nhỏ .Click vào thanh này để thấy ảnh size nguyên gốc. (" + oImgWidth + " x " + oImgHeight + ")</span></td></tr></table>");
   }
if(mImgWidth != 0 && oImgWidth > mImgWidth) {
   oImg.height = (mImgWidth / oImgWidth ) * oImgHeight;
   oImg.width = mImgWidth;
   new Insertion.Before(oImg, "<table onclick=\"imgview(\'" + strtableid + "\','" + oImgHeight + "\',\'" + oImgWidth + "\',\'" + oImg.height + "\',\'" + oImg.width + "\');\" style=\"cursor: pointer;\" id=\"" + strtableid + "\" width=\"" + mImgWidth + "\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\"><tr><td class=\"errorTable\"><span id=\"t_" + strtableid + "\">Ảnh đã được thu nhỏ .Click vào thanh này để thấy ảnh size nguyên gốc. (" + oImgWidth + " x " + oImgHeight + ")</span></td></tr></table>");
   }

}

function imgview(oImg,oImgHeight,oImgWidth,nImgHeight,nImgWidth){
var TabImg = $(oImg);
var IdImg = $("img_" + oImg);
var TitImg = $("t_" + oImg);
   if (IdImg.width != oImgWidth) {
          IdImg.height = oImgHeight;
          IdImg.width = oImgWidth;
          TabImg.width = oImgWidth;
          TitImg.innerHTML = "Ảnh quá lớn ? Click vào thanh này để thấy ảnh nhỏ. (" + nImgWidth + " x " + nImgHeight + ")";
      }
   else {
          IdImg.height = nImgHeight;
          IdImg.width = nImgWidth;
          TabImg.width = nImgWidth;
          TitImg.innerHTML = "Ảnh đã được thu nhỏ .Click vào thanh này để thấy ảnh size nguyên gốc. (" + oImgWidth + " x " + oImgHeight + ")";
      }
  
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}