var listimgtoresize = new Array();
var maxheighttor;
var maxwidthtor;

function resizeimagetodo(){

  while (listimgtoresize.length>0){
    reimg=listimgtoresize.pop();
    modif=resizeimageaction(reimg,maxheighttor,maxwidthtor);
    if (modif==0){
      var img = window.document.getElementById(reimg);
      img.setAttribute('height',maxheighttor);
      img.setAttribute('width',maxwidthtor);
    }
  }
}


function resizeimageaction(idimage,maxheight,maxwidth){
  var img = window.document.getElementById(idimage);
  maxheighttor=maxheight;
  maxwidthtor=maxwidth;

  var ih=img.height;
  var iw=img.width;
  var nh=ih;
  var nw=iw;

  var modif=0;

  if (ih>maxheight && iw>maxwidth){
    rh=ih/maxheight;
    rw=iw/maxwidth;
    if (rh>rw){
      nh=maxheight;
      nw=iw*maxheight/ih;  
      modif=1;
    } else {
      nw=maxwidth;
      nh=ih*maxwidth/iw;  
      modif=1;
    }
  } else {
    if (ih>maxheight){      
      nh=maxheight;
      nw=iw*maxheight/ih;  
      modif=1;
    } else {
      if (iw>maxwidth){ 
        nw=maxwidth;
        nh=ih*maxwidth/iw;  
        modif=1;
      }
    }
  }

  if (modif==1){
    img.setAttribute('height',nh);
    img.setAttribute('width',nw);
  } 
  if (nh==ih && nw==iw && nh>0  && nw>0){
    modif=1;
  }
  return modif;
}

function resizeimage(idimage,maxheight,maxwidth){
  modif=resizeimageaction(idimage,maxheight,maxwidth);
  if (modif==0) {
    listimgtoresize.push(idimage)
    if (listimgtoresize.length==1){
      window.setTimeout(resizeimagetodo,300);
    }
  }
}


var maxwidthall=0;

function resizeallimagesaction(){
  var images = window.document.getElementsByTagName("img");
  var img_src = new Array();
  for (var i = 0; i < images.length; i++) {
    var img = images[i];
    var iw=img.width;
    if (iw!=0 && iw>maxwidthall){ 
      var nh=img.height*maxwidthall/iw;  
      img.setAttribute('height',nh);
      img.setAttribute('width',maxwidthall);
    }
  }
}

function resizeallimages(maxwidth){
  maxwidthall=maxwidth;
  resizeallimagesaction();
  window.setTimeout(resizeallimagesaction,1000);
}
