// File: image_display_mgr.js
// Contains: Code for handling the special effects display of images 
// Includes: booklist, sectionImages


var numImages_bl = 0;           // number of images that should be loaded
var numCompleted_bl = 0;        // total number of booklist images loaded or error.

var numImages_sect = 0;           // number of section images that should be loaded
var numCompleted_sect = 0;        // total number of section images loaded or error.


/* --------------------------------------------------------

	booklistManager 

------------------------------------------------------------*/

function booklistManager()
{
  
  this.init = function (imageNames, path, mode)
  {
    this.imageArray = new Array();
    this.nameArray  = new Array();  // we may not want to keep this in memory. TBC
  
  
	  if(imageNames == null || path == null )
		  return;
      
    numCompleted_bl = 0;    
    numImages_bl = 0; 
    
    this.nameArray = imageNames.split(',');
    numImages_bl = this.nameArray.length;
    
    for(i=0; i < numImages_bl; i++)
    {

      this.imageArray[i] = new imgPreloader(path + this.nameArray[i], registerBooklistImage, i);
//      this.imageArray[i] = new Image;
      this.imageArray[i].onload = registerBooklistImage;
      this.imageArray[i].onerror = onerrorBooklistImage;
      this.imageArray[i].src = path + this.nameArray[i]; 
    }  
  }
}
//   END of : booklistManager Definition

function registerBooklistImage() 
{
//debugAlert("registerBooklistImage");
  // check to see if all images are accounted for
  numCompleted_bl++;
  if (numCompleted_bl >= numImages_bl)
  {
//    debugAlert("numCompleted_bl is full");    
    // Show the images as soon as all are loaded (if any are unloadable we show what we can)
    doBookListAppearSequence(0);
  }
}


function onerrorBooklistImage() 
{
//  debugAlert("onerrorBooklistImage");
  registerBooklistImage()
}

function doBookListAppearSequence(id)
{
  var elemID = "BOOK_LIST_1_ITEM_"+id;
  debugAlert("showing elemID" + elemID);
  doDivItemAppear(elemID);
  id++;
  if (id < 5)
  {
    var cmd = "doBookListAppearSequence(" + id + ")";
    setTimeout(cmd, 200);  
  }
}

/* --------------------------------------------------------

  sectionImageManager 

------------------------------------------------------------*/

// Instance of imageDisplayManager used for section header image management
function sectionImageManager()
{
  this.doAppearSequence = function (){
    showSectionImages();
  }
}
//sectionImageManager.prototype = new imageDisplayManager();

function sectionImageManager()
{
  
  this.init = function (imageNames, path, mode)
  {
    this.imageArray = new Array();
    this.nameArray  = new Array();  // we may not want to keep this in memory. TBC
  
    if(imageNames == null || path == null )
      return;
      
    this.nameArray = imageNames.split(',');
    numImages_sect = this.nameArray.length;

    for(i=0; i < numImages_sect; i++)
    {
//      this.imageArray[i] = new imgPreloader(path + this.nameArray[i], registerSectionImage, i);
      this.imageArray[i] = new Image;
      this.imageArray[i].onload = registerSectionImage;
      this.imageArray[i].onerror = registerSectionImage;
      this.imageArray[i].src = path + this.nameArray[i]; 
    }  
  }
  
}
//   END of : booklistManager Definition

function registerSectionImage() 
{
//debugAlert("registerSectionImage");
  // check to see if all images are accounted for
  numCompleted_sect++;
  if (numCompleted_sect >= numImages_sect)
  {
    // Show the images as soon as all are loaded (if any are unloadable we show what we can)
    showSectionImages(0);
  }
}


function showSectionImages()
{
  doDivItemAppear('sectionImageDiv');
}


function hideSectionImages()
{
  doDivItemFade('sectionImageDiv');
}



/* ------------------ Utility Functions  ------------------------------------*/
/*
  Class Definition : imgPreloader 
  Note: creation of the object will initiate image preloading automatically
    
  Constructor requires 2 args:
    imageURL - URL of the image to be loaded
    callback - callback to be inovked when the image loading is complete.
*/

function imgPreloader(imageURL, callback, itemID)
{
  if (imageURL == null || callback == null || imageURL == "" )
    return null;
  
  this.status = "empty";
  notify = callback;
  this.id = itemID;
  this.url = imageURL;
  
//  debugAlert("imgPreloader :" + this.id + " url is " + imageURL);
  img = new Image;

  img.onload = function() {  
//    debugAlert("imgPreloader.onload ");  
    this.status = "loaded";
    notify();
  }

  
  img.onerror = function() {
    this.status = "error";
    notify();    
  }
  

  img.src = this.url; 
}


function doDivItemAppear(elemID)
{  
  elem = $(elemID);
  if (elem != null)
  {
//    var eff = new Effect.Appear(elem, arguments[1] || {});
    var eff = new Effect.Opacity(elem, {duration:1.0, from:0.0, to:0.9999});
    
  }
}

function doDivItemFade(elemID)
{  
  elem = $(elemID);
  if (elem != null)
  {
    var eff = new Effect.Fade(elem, arguments[1] || {});
  }
}

function doDivItemBlindUp(elemID)
{  
  elem = $(elemID);
  if (elem != null)
  {
    var eff = new Effect.BlindUp(elem, arguments[1] || {});
  }
}




