/*
 * Copyright 2006, Jeffrey Palm.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
// --------------------------------------------------
// Image preloading
// --------------------------------------------------
 
/**
 * Taken from:
 * http://www.webreference.com/programming/javascript/gr/column3/ and
 * adapted to prototype.
 */
var ImagePreloader = Class.create();
ImagePreloader.prototype = {
  initialize: function(images,callBack) {
     
    // store the callBack
    this.callBack = callBack;
     
    // 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]);
  },
   
  preload: function(image) {

     
    // create new Image object and add to array
      var oImage = new Image;
      oImage.width  = 1;
      oImage.height = 1;
      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
      if (PRELOAD_IMAGES) {
        oImage.src = image;
      } else {
        this.onload();
      }

  },
   
  onComplete: function() {
    this.nProcessed++;
    this.callBack(this.aImages, this.nLoaded);
  },
   
  onload: function() {
    this.bLoaded = true;
    this.oImagePreloader.nLoaded++;
    this.oImagePreloader.onComplete();	
  },
   
  onerror: function() {
    this.bError = true;
    this.oImagePreloader.onComplete();
  },
   
  onabort: function() {
    this.bAbort = true;
    this.oImagePreloader.onComplete();
  }
};
 
 
//
// Before we start up we want to show a loading screen. This screen
// has id 'startupScreen' and we will hide it as soon as the main
// screen is loaded in 'onload()'
// 
 
var Startup = Class.create();
Startup.prototype = {
   
  initialize: function() {
  },
   
  startUp: function() {
    document.write('<div id="startupScreen" style="display:none">');
    document.write('<table width=100% height=80% border=0 cellpadding=0 cellspacing=0>');
    document.write('<tr><td>');
    document.write('<div style="text-align:center" id="startupCenter">');
    document.write('<img src="/images/logo.jpg"/></br>');
    document.write('Loading... please be patient.</br>');
    document.write('<span id="percentDone">0</span>% done.');
    document.write('</br><span id="refererMsg"</span>');
    document.write('</div>');
    document.write('</td></tr>');
    document.write('</table>');
    document.write('</div>');
     
    doAppear('startupScreen');

    new Ajax.Request('/recordLoad.php?load=0', {
	    method: 'get',
		onSuccess: function(transport){
	    }
    });
    
    //
    // preload all the images
    //
    var ids = PRODUCT_ID>0 ? new Array() : getAllProductIds();
    var imgs = new Array();
    document.write('<div id="preload">');
    for (var i=0, N=ids.length; i<N; i++) {
      imgs.push(getImageThumb(ids[i]));
      //imgs.push(getImageTiny(ids[i]));
    }
    document.write('</div>');
    this.preLoader = new ImagePreloader(imgs, function(imgs,n) {
                                          var percDone = Math.round(100*n/imgs.length);
                                          if (percDone > 100) percDone = 100;
                                          $('percentDone').innerHTML = ""+percDone;
                                          var i = n-1; 
                                          if (i<0) i = 0;
                                          var img = imgs[i];
 //$('preloadImg').src = img;                                          
                                          });
  },
   
  shutDown: function() {
    doFade('startupScreen');
	new Ajax.Request('/recordLoad.php?load=1', {
		method: 'get',
		    onSuccess: function(transport){
		}
	});
    }
}
 
/*
 * Only start up if we haven't already done so.
 */
    var theStartup = new Startup();
theStartup.startUp();

 

