//   asset_loader.js
//   This object handles all the assets for the page.

//   First, the master list of application assets to preload.
//   All images are considered to be optional unless required is set to 1.
//   All scripts are considered to be required unless optional is set to 1.



/*
function initialize() {
   preLoader.startLoading( preloadSuccess, preloadFailure, preloadStatus );
}

function preloadStatus( perc, header, msg, errors ) {
      $('preloader-message').innerHTML = '\
         <div style="text-align: center; font-weight: bold; color: #999; margin-top: 80px;">' + header + '</div> \
         <div style="margin: 0px 20px; border: 2px solid #00f; position: relative; height: 16px;"> \
            <div style="position: absolute; left: 0px; top: 0px; width: ' + perc + '%; height: 16px; \
               background-color: #008;"></div> \
            <div style="position: relative; text-align: center; color: #999;">' + msg + '</div> \
         </div>';
      $('preloader-error').innerHTML = errors;
}

function preloadFailure() {
   $('preloader').innerHTML = '<div style="text-align: center;"><h1>Failed to Load!</h1></div>';
}

function preloadSuccess() {
   $('preloader').innerHTML = '<div style="text-align: center;"><h1>Success!</h1></div>';
}
*/

//   The preloader object.
//   This object handles the job of preloading everything.
//   Usage:
//
//      preLoader.startLoading( preloadSuccess, preloadFailure, preloadStatus );
//
//   Where:
//      preloadSuccess: the function to call when preloading was successful
//      preloadFailure: the function to call when preloading failed
//      preloadStatus:  the function to call to report status of preloading in progress
//
//   Scripts grabbed in this manner are eval()'d by Prototype.

var preLoader = {
   errors: { images: 0, scripts: 0 },
   errortext: '',
   progress: 0,
   startLoading: function( assetsObject, completeCallback, errorCallback, statusCallback ) {
	  this.assets = assetsObject
      this.success = (completeCallback != undefined) ? completeCallback : function() {} ;
      this.failure = (errorCallback != undefined) ? errorCallback : function() {} ;
      this.status = (statusCallback != undefined) ? statusCallback : function() {} ;
      this.loadNextImage();
   },
   loadNextImage: function() {
      if (this.progress >= this.assets.images.length) {
         this.progress = 0;
         this.loadNextScript();
         return;
      }
      imageObject = new Image();
      imageObject.onload = function() { preLoader.imageLoaded(); }
      imageObject.onerror = function() { preLoader.imageError(); }
      imageObject.src = this.assets.images[ this.progress ].url;
      this.assets.images[ this.progress ].image = imageObject;
   },
   imageLoaded: function() {
      var perc = Math.round((this.progress + 1) * 100 / this.assets.images.length);
      this.assets.images[ this.progress ].success = 1;
      this.preloaderMessage( perc, 'Loading Images', this.assets.images[ this.progress ].url );
      this.progress++;
      this.loadNextImage();
   },
   imageError: function() {
      var perc = Math.round((this.progress + 1) * 100 / this.assets.images.length);
      this.assets.images[ this.progress ].success = 0;
      this.errors.images++;
      this.errortext += '<div>Error loading ' + this.assets.images[ this.progress ].url + '</div>';
      this.preloaderMessage( perc, 'Loading Images', this.assets.images[ this.progress ].url );
      if (this.assets.images[ this.progress ].required == 1) return this.failure();
      this.progress++;
      this.loadNextImage();
   },
   loadNextScript: function() {
      if ((this.assets.scripts == undefined) || this.progress >= this.assets.scripts.length) {
         this.progress = 0;
         this.success();
         return;
      }
      this.ajax = new Ajax.Request( this.assets.scripts[ this.progress ].url, {
         method: 'get',
         onSuccess: function(transport) {
            preLoader.scriptLoaded();
         },
         onFailure: function(transport) {
            preLoader.scriptError();
         }
      });
   },
   scriptLoaded: function() {
      var perc = Math.round((this.progress + 1) * 100 / this.assets.scripts.length);
      this.assets.scripts[ this.progress ].success = 1;
      this.preloaderMessage( perc, 'Loading Scripts', this.assets.scripts[ this.progress ].url );
      this.progress++;
      this.loadNextScript();
   },
   scriptError: function() {
      var perc = Math.round((this.progress + 1) * 100 / this.assets.scripts.length);
      this.assets.scripts[ this.progress ].success = 0;
      this.errors.scripts++;
      this.errortext += '<div>Error loading ' + assets.scripts[ this.progress ].url + '</div>';
      this.preloaderMessage( perc, 'Loading Scripts', assets.scripts[ this.progress ].url );
      if (this.assets.scripts[ this.progress ].optional != 1) return this.failure();
      this.progress++;
      this.loadNextScript();
   },
   preloaderMessage: function( perc, header, msg ) {
   
      this.status( perc, header, msg, this.errortext );
      
   }

};