//*************************************************
// imagePreloader.js
//
//Summary:     Javascript class which preloads
//             a list of images and makes calls
//             to other functions to update the dom
//
//Author:      Jeff Buhr
//
//Based on Tutorial from:
// http://www.trap17.com/index.php/image-preloader-progress-bar-status_t21316.html
//
//Created:     28 Apr 08
//Summary of Modifications:
//       none
//*************************************************


// Javascript Class which preloads the array of images given in "imageNamesArray"
function ImagePreload(imageNamesArray, curPercentFunc, curFinishedFunc, curImageDoneFunc) {

   // "constants"
   this.MAX_PRELOAD = 3;
   this.BASE_DIR = "photogallery/";

   // Call-back functions
   this.curPercent   = curPercentFunc;
   this.curFinished  = curFinishedFunc;
   this.curImageDone = curImageDoneFunc;

   // const variables for call-back functions
   this.PROGRESS_BAR     = "progressBar";
   this.FINISHED_PERCENT = "Done.";
   this.IMG_LST_PREFIX   = "pictureElm";
   this.LOADED_COLOR     = "#00d900";

   // Class Member Vars
   this.nbrLoaded       = 0;
   this.nbrPreloaded    = 0;
   this.imagesArray     = new Array;
   this.imageNamesArray = imageNamesArray;
   this.nbrImages       = imageNamesArray.length;
   
   // preloads a single image (imageName) with id (i)
   this.Preload = function(imageName,i) {  
      var myImage = new Image;
      this.imagesArray[i] = myImage; // add the image to the list of images

      // set the onload function pointer
      myImage.onload = this.OnLoad;

      // keep information about this image
      myImage.myImagePreload = this;     // Give the Image Object a Reference to the ImagePreload Class
      myImage.isLoaded = false;          // Custom value added to track state
      myImage.source = imageName;        // Path to image (for later use)
      myImage.src = this.BASE_DIR + imageName; // Image Object Source Path
      myImage.id = i;
   }

   // loads image and preloads MAX_PRELOAD images after it
   this.load = function(i) {
      this.Preload(this.imageNamesArray[i],i);
   }
   
   this.preloadFrom = function(i) {
      
      // preload images
      var nbrPreloaded = 0;
      for (var x = i; nbrPreloaded < this.MAX_PRELOAD; x++) {
      
         // check bounds too
         if (x >= this.imageNamesArray.length) {
            x = 0;
         }
         
         if (!this.isLoaded(x)) {
            this.Preload(this.imageNamesArray[x],x);
         }
         nbrPreloaded++;
      }
   }
   
   this.isLoaded = function(i) {
      return isset(this.imagesArray[i]);
   }

   // called when an images' preloading is complete
   this.OnComplete = function() {  
      this.nbrPreloaded++;

      // call finished instead of all images are loaded
      if ( this.nbrPreloaded == this.nbrImages ) {
          this.curFinished();
      }
      else {
          this.curPercent( Math.round( (this.nbrPreloaded / this.nbrImages) * 100 ));
      }
   }

   // called when an images preloading has successfully finished
   this.OnLoad = function() {  
      // 'this' pointer points to myImage Object because this function was previously assigned
      // as an event-handler for the myImage Object.
      this.isLoaded = true;
      this.myImagePreload.nbrLoaded++; // Access our Class with the Reference assigned previously..
      this.myImagePreload.OnComplete();
      this.myImagePreload.curImageDone(this.id,true);
   }

   // called when image unable to load correctly
   this.OnError = function() { 
      // 'this' pointer points to myImage Object
      this.myImagePreload.OnComplete();
      this.myImagePreload.curImageDone(this.id,false);
   }

   // accessor function
   this.getImageArray = function() {
      return this.imagesArray;
   }
   
   // accessor function
   this.getImageNamesArray = function() {
      return this.imageNamesArray;
   }
}
