/** FUNCIONES RELACIONADAS CON EL TRATAMIENTO DE IMAGENES **/

function ImagePreloader(images, callback, maxWidth, maxHeight, idImages)
{
   // store data
   this.callback = callback;
   this.maxWidth = maxWidth;
   this.maxHeight = maxHeight;
   this.idImages = idImages;

   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array(); 

   // record the number of images.
   this.nImages = images.length;

   // for each image, call preload()
   for ( var i = 0; i < images.length; i++ ) 
      this.preload(images[i]);
}


ImagePreloader.prototype.preload = function(image)
{
   // create new Image object and add to array

   var oImage = new Image;
   this.aImages.push(oImage);

   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;

   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;
   // assign the .src property of the Image object
   oImage.src = image;
}


ImagePreloader.prototype.onComplete = function()
{
   this.nProcessed++;
   if ( this.nProcessed == this.nImages )
   {
      this.callback(this.aImages, this.nLoaded, this.maxWidth, this.maxHeight, this.idImages);
   }
}

ImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}

function setSize(){
  var img = arguments[0];
  var imgId = arguments[4];
  
  var maxWidth = arguments[2];
  var maxHeight = arguments[3];
  
  for ( var i = 0; i < img.length; i++ ) {
	  var newWidth = img[i].width;
	  var newHeight = img[i].height;
	  
	  if(img[i].width>maxWidth && maxWidth>0){
	    newHeight = parseInt(img[i].height + (maxWidth - img[i].width)/img[i].width*img[i].height);
	    newWidth = maxWidth;
	    if(newHeight>maxHeight && maxHeight>0){
	      newWidth = parseInt(newWidth + (maxHeight - newHeight)/newHeight*newWidth);
   		  newHeight = maxHeight;
	    } 
	  }
	  else if(img[i].height>maxHeight && maxHeight>0){
	    newWidth = parseInt(img[i].width + (maxHeight - img[i].height)/img[i].height*img[i].width);
	    newHeight = maxHeight;
	    if(newWidth>maxWidth && maxWidth>0){
	      newHeight = parseInt(newHeight + (maxWidth - newWidth)/newWidth*newHeight);
	      newWidth = maxWidth;
	    } 
	  }
	
	  document.getElementById(imgId[i]).style.width = newWidth + "px";
	  document.getElementById(imgId[i]).style.height = newHeight + "px"
	  document.getElementById(imgId[i]).src = img[i].src;
  }
}