//   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.
/*
var assets = {

   //   List images here.
   //   Each image is an associative array with:
   //      id: an identifier
   //      url: a url to load the image from (can be relative)
   //      required: (optional) if '1', calls failure callback
   //         if there is an error loading this image
   //         (Thus, all images default to being optional.)
   /* { id: 'k2_baum', url: '/images/k2_baum.jpg' },
   *    { id: 'k2_blindtext', url: '/images/k2_blindtext.png' },
       { id: 'k2_kaffee', url: '/images/k2_kaffee.jpg' },
   *    { id: 'k2_persoenlich', url: '/images/k2_persoenlich.png' },
       { id: 'k2_projekte', url: '/images/k2_projekte.png' },
   *    { id: 'k2_spiegelung', url: '/images/k2_spiegelung.png' },
       { id: 'k2_text', url: '/images/k2_text.png' },
       { id: 'k2_wasserturm', url: '/images/k2_wasserturm.jpg' } * /

   images: [
      { id: 'k2_jubi_1', url: '/images/k2_jubi_1.png' },
      { id: 'k2_blindtext', url: '/images/k2_blindtext.png' },
      { id: 'k2_chameleon', url: '/images/k2_chameleon.jpg' },
      { id: 'k2_persoenlich', url: '/images/k2_persoenlich.png' },
      { id: 'k2_erbsen', url: '/images/k2_erbsen.jpg' },
      { id: 'k2_spiegelung', url: '/images/k2_spiegelung.png' },
      { id: 'k2_huette', url: '/images/k2_huette.jpg' },
      { id: 'k2_service', url: '/images/k2_service.jpg' },
      { id: 'k2_vorsicht_bissig', url: '/images/k2_vorsicht_bissig.jpg' }//,
  
      
    //  { id: 'another_image', url: 'images/another_image.png' },
    //  { id: 'required_image', url: 'images/important_image.png', required: 1 }
   ],

   //   List scripts here.
   //   Each script is an associative array with:
   //      id: an identifier
   //      url: a url to load the script from (can be relative)
   //      optional: (optional) if '1', does not call failure callback
   //         if there is an error loading this script
   //         (Thus, all scripts default to being required.)

   scripts: [
      //{ id: 'required_javascript_thing', url: 'javascripts/important.js' },
      //{ id: 'optional_thing', url: 'javascripts/optional.js', optional: 1 }
   ]

};*/

//   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( completeCallback, errorCallback, statusCallback ) {
      this.success = completeCallback;
      this.failure = errorCallback;
      this.status = statusCallback;
      this.loadNextImage();
   },
   loadNextImage: function() {
      if (this.progress >= assets.images.length) {
         this.progress = 0;
         this.loadNextScript();
         return;
      }
      imageObject = new Image();
      imageObject.onload = function() { preLoader.imageLoaded(); }
      imageObject.onerror = function() { preLoader.imageError(); }
      imageObject.src = assets.images[ this.progress ].url;
      assets.images[ this.progress ].image = imageObject;
   },
   imageLoaded: function() {
      var perc = Math.round((this.progress + 1) * 100 / assets.images.length);
      assets.images[ this.progress ].success = 1;
      this.preloaderMessage( perc, 'Loading Images', assets.images[ this.progress ].url );
      this.progress++;
      this.loadNextImage();
   },
   imageError: function() {
      var perc = Math.round((this.progress + 1) * 100 / assets.images.length);
      assets.images[ this.progress ].success = 0;
      this.errors.images++;
      this.errortext += '<div>Error loading ' + assets.images[ this.progress ].url + '</div>';
      this.preloaderMessage( perc, 'Loading Images', assets.images[ this.progress ].url );
      if (assets.images[ this.progress ].required == 1) return this.failure();
      this.progress++;
      this.loadNextImage();
   },
   loadNextScript: function() {
      if (this.progress >= assets.scripts.length) {
         this.progress = 0;
         this.success();
         return;
      }
      this.ajax = new Ajax.Request( 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 / assets.scripts.length);
      assets.scripts[ this.progress ].success = 1;
      this.preloaderMessage( perc, 'Loading Scripts', assets.scripts[ this.progress ].url );
      this.progress++;
      this.loadNextScript();
   },
   scriptError: function() {
      var perc = Math.round((this.progress + 1) * 100 / assets.scripts.length);
      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 (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 );
      
   }

};    
