function ImageChoice(aImagePath, aUrl)
{
  this.imagePath = aImagePath;
  if (aUrl)
  {
    this.clickUrl = aUrl;
  }
}

ImageChoice.prototype.getImagePath = function ()
{
  return this.imagePath;
}

ImageChoice.prototype.getClickUrl = function ()
{
  return this.clickUrl;
}


function RandomImageChooser(aSeconds)
{
  this.targetIds = new Array();
  this.storageArray = new Array();
  this.waitSeconds = (aSeconds && aSeconds > 0) ? aSeconds : 20;
}

RandomImageChooser.prototype.add = function (aImageChoice)
{
  this.storageArray.push(aImageChoice);
}

RandomImageChooser.prototype.getRandom = function ()
{
  var max = this.storageArray.length;
  var r = Math.floor(Math.random() * max);
  return this.storageArray[r];
}

RandomImageChooser.prototype.applyImagesPeriodically = function ()
{
  this.applyImages.apply(this,arguments);
  var me = this;
  var args = arguments;
  this.targetIds = args;
  var callMe = function()
    {
      me.applyImagesPeriodically.apply(me,args);
    };
  setTimeout(callMe, this.waitSeconds * 1000);
}

RandomImageChooser.prototype.applyImages = function ()
{
  var used = new Array();
  for (var i = 0; i < arguments.length; i++)
  {
    var imageId = arguments[i];

    var RANDOM_TRY_COUNT = 100;    
    var path;
    var url;
    
    var j,k;
    for (j = 0; j < RANDOM_TRY_COUNT; j++)
    {
      var imageChoice = this.getRandom();
      path = imageChoice.getImagePath();
      url = imageChoice.getClickUrl();
      if (!used[path])
      {
        break;
      }
    }
    if (j == RANDOM_TRY_COUNT)
    {
      for (k = 0; k < this.storageArray.length; k++)
      {
        var imageChoice = this.storageArray[k];
        path = imageChoice.getImagePath();
        url = imageChoice.getClickUrl();
        if (!used[path])
        {
          break;
        }
      }
    }
    if (k == this.storageArray.length)
    {
      throw "Unable to select unused image from '"+this.storageArray.length+"' images for '"+arguments.length+"' spots.";
    }
    
    used[path] = true;
  
    var targetElement = document.getElementById(imageId);
    if (!targetElement)
    {
      throw "Invalid image '"+imageId+"', type '"+typeof(imageId)+"'.";
    }

    var theImage = new Image();
    theImage.src = path;
    
    if (targetElement.src) // An IMG
    {
      // Unset current image as used.
      if (used[targetElement.src])
      {
        used[targetElement.src] = false;
      }
      targetElement.src = theImage.src;
    }
    else if (targetElement.style.backgroundImage) // Probably a DIV
    {
      targetElement.style.backgroundImage = "url("+theImage.src+")";      
    }
    else
    {
      throw "Unsupported element type for id '"+imageId+"'.";
    }
    
    targetElement.title = null; 
    targetElement.style.cursor = 'auto'; 
    $(targetElement).unbind("click"); 
    $(targetElement).unbind("mouseover"); 

    if (url)
    {
    
      targetElement.title = url; 
      
      var clicker = function() { document.location = arguments.callee.url };
      clicker.url = url;
      $(targetElement).bind("click", clicker); 
      $(targetElement).bind("mouseover", function() { this.style.cursor = 'pointer'}); 
    }
  } // for
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

