﻿//   asset_loader.js//   Script by Nephilim//   http://nephilim.blogspot.com/2007/09/preloading-both-images-and-scripts-in.html//   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.)   images: [      { id: 'filmLoad', url: 'film/filmLoad1.jpg', required: 1 },      { id: 'filmLoad', url: 'film/filmLoad2.jpg', required: 1 },      { id: 'filmLoad', url: 'film/filmLoad3.jpg', required: 1 },      { id: 'filmLoad', url: 'film/filmLoad4.jpg', required: 1 },                              { id: 'boxa1', url: 'film/menuimages/blue5_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow6_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green3.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red1.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red5_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue1.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue2.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow7_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue3_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green2_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow6.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue4.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red2_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green4.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red7_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green4_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red6.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red4.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red2.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow4_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow1_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow7.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow5.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green1.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue2_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green2.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue3.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green3_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue1_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red1_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow3_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow5_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red3_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow2_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow2.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue4_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow1.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red7.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red5.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/blue5.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red3.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red4_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow3.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/yellow4.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/green1_on.jpg', required: 1 },      { id: 'boxa1', url: 'film/menuimages/red6_on.jpg', 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: 'buildroll', url: 'js/buildroll.js' },   ]};//   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();   },   //   images      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();   },//   scripts      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 );         }};
