// -------------------------------------------------------------------------
// --- Logo slice animation mechanism --------------------------------------
// -------------------------------------------------------------------------

// jquery must be loaded before this script will run - for example:
//   <script src="http://code.jquery.com/jquery-latest.js"></script>

function LogoSliceAnimator (sliceBaseName, sliceCount, sliceSuffix, displayTime, animateTime)
{
  this.sliceBaseName = sliceBaseName;
  this.sliceCount = sliceCount;
  this.sliceSuffix = sliceSuffix;
  this.logoDisplayTime = displayTime;   // Number of milliseconds to display logo
  this.logoAnimateTime = animateTime;   // Number of milliseconds for logo fade in or fade out

  this.logoSlice = $("#sponsorLogoSlice");

  this.currentLogoSliceIndex = -1;
  this.currentLogoSlice = null;
  /*keep record of current size*/
  this.currentSize = null;
  
  /*array to store indexes*/
  this.myIndexArray = null;
}

/**
 * Choose an initial logo, then start the logo slice animation.
 */
LogoSliceAnimator.prototype.start = function ()
{
  // --- If no logo slice is found, don't start 
  //alert (this.logoSlice.length); 
  if (this.logoSlice.length == 0) return;
  
  // --- Choose and immediately activate the first logo slice
  this.chooseNextLogoSlice ();
  this.activateNextLogoSlice ();

  // --- Start the preloading of the next image slice
  this.chooseNextLogoSlice ();

  // --- Start animation loop
  this.logoSliceAnimation ();
}


/**
 * Choose next logo slice and start preloading it.
 */

LogoSliceAnimator.prototype.chooseNextLogoSlice = function ()
{
  
  this.nextLogoSliceIndex = this.chooseLogoSliceIndex(this.currentLogoSliceIndex);
  this.nextLogoSliceImage = new Image ();
  this.nextLogoSliceImage.src = this.generateImageUrl (this.nextLogoSliceIndex);
}

/**
 * Make the next logo slice be the current slice
 */
LogoSliceAnimator.prototype.activateNextLogoSlice = function ()
{
  //console.log ("src was "+this.logoSlice[0].src);
  this.logoSlice[0].src = this.nextLogoSliceImage.src;
  //console.log ("set src to "+this.logoSlice[0].src+" idx="+this.nextLogoSliceIndex);
  this.currentLogoSliceIndex = this.nextLogoSliceIndex;
}

/**
 * Logo animation.
 */
LogoSliceAnimator.prototype.logoSliceAnimation = function ()
{
  var logoSliceAnimator = this;  

  // --- Leave logo frame up for configured logoDisplayTime
  this.logoSlice.animate({opacity: 1.0}, this.logoDisplayTime)

  // --- Fade out logo frame
  this.logoSlice.fadeOut (this.logoAnimateTime,
			  function () { logoSliceAnimator.switchLogoSlice (); });

  // --- Fade in the new logo, calling this animation when we are done
  this.logoSlice.fadeIn (this.logoAnimateTime, 
			 function () { logoSliceAnimator.logoSliceAnimation (); });
}

LogoSliceAnimator.prototype.switchLogoSlice = function () 
{
  // --- Activate the already preloaded next logo slice
  this.activateNextLogoSlice ();
  
  // --- Choose the next logo slice and start preloading.
  this.chooseNextLogoSlice ();
}

/**
 * Generate an image url given a logo slice index.
 */
LogoSliceAnimator.prototype.generateImageUrl = function (logoSliceIndex)
{ return this.sliceBaseName+(logoSliceIndex+1)+this.sliceSuffix; }
/*
  Generates a random logo slice index while never returning "bannedSliceIndex".
  If you don't want to ban a slice, pass -1 into bannedSliceIndex.  This banning
  mechanism exists so that we won't show the same slice twice in a row.
LogoSliceAnimator.prototype.chooseLogoSliceIndex = function (bannedSliceIndex) 
{  
  if (bannedSliceIndex == -1)
     return Math.floor(Math.random() * this.sliceCount);
  else
    {
       var r = Math.floor(Math.random() * (this.sliceCount-1));
       if (r >= bannedSliceIndex) r++;
       return r;
    }
}*/
LogoSliceAnimator.prototype.chooseLogoSliceIndex = function (bannedSliceIndex) 
{  
  if (bannedSliceIndex == -1 || this.currentSize == 0)
  {
    this.initializeArray ();
    var r = Math.floor(Math.random() * this.sliceCount);
    var retVal = this.myIndexArray[r];
    this.myIndexArray.splice(r,1);
    this.currentSize = this.sliceCount - 1;
    return retVal;
  }
  else
  {
    var r = Math.floor(Math.random() * this.currentSize);
    var retVal = this.myIndexArray[r];
    this.myIndexArray.splice(r,1);
    this.currentSize--;
    return retVal;
  }
}
LogoSliceAnimator.prototype.initializeArray = function ()
{
  this.myIndexArray = null;
  this.myIndexArray = new Array(this.sliceCount);
  
  var i = 0;
  for( i = 0; i<this.sliceCount; i++)
  {
    this.myIndexArray[i] = i;
  }
}
