
onload=
function() {
 var imgs = document.getElementsByTagName('img');
 for(var i=0;i<imgs.length;i++) {
	if(imgs[i].border != '1') {	
		imgs[i].border = '0';
	}
   imgs[i].onerror = function(){this.style.display='none'}
 }
}

//===============================================================
//Build Image
//===============================================================

function imgResize(imgsrc, max_w, max_h,aspect){
  var img, w, h, rslt, new_size;

  img = new Image();
  img.src = document.images[imgsrc].src;

  w = img.width;
  h = img.height;

  rslt = needResize(max_w,max_h,w,h);

  if(aspect){
    if(rslt){
      new_size = resize(img, max_w, max_h);	//causing error!
      //alert(new_size);
      if(diff(max_w, w) > diff(max_h, h)){
        document.images[imgsrc].width = img.width * new_size;
        document.images[imgsrc].height = img.height * new_size;
      }
      else{
        document.images[imgsrc].width = img.width * new_size;
        document.images[imgsrc].height = img.height * new_size;
      }
    }
  }
  else{
    document.images[imgsrc].width = max_w;
    document.images[imgsrc].height = max_h;
  } 
}

//===============================================================
//Need Resize?
//===============================================================

function needResize(max_w, max_h, act_w, act_h){
  var dif_w, dif_h;

  dif_w = diff(max_w, act_w);
  dif_h = diff(max_h, act_h);

  if(dif_w > 0){
    return true;
  }
  else if(dif_h > 0){
    return true;
  }
  else{
    return false;
  }
}


//===============================================================
//Difference
//===============================================================

function diff(max, act){
  if(act > max){
    return (act - max);
  }
  else{
    return 0;
  }
}



//===============================================================
//Resize Image
//===============================================================

function resize(img, max_w, max_h){
  var dif, per, r;
  if(diff(max_w,img.width) > diff(max_h,img.height)){
    r = max_w/img.width;
  }
  else{
    r = max_h/img.height;
  }
  return r;
}

