function RandomContentChooser()
{
  this.setSecondsBetween(30);
  this.contentDivs = new Array();
  for (var i = 0; i < arguments.length; i++)
  {
    this.add(arguments[i]);
  }
}

RandomContentChooser.prototype.add = function (aContentDiv)
{
  this.contentDivs.push(aContentDiv);
}

RandomContentChooser.prototype.setSecondsBetween = function (aSecondsBetween)
{
  this.secondsBetween = aSecondsBetween;
}


RandomContentChooser.prototype.populatePeriodically = function ()
{
  this.populate.apply(this,arguments);
  var me = this;
  var args = arguments;
  var callMe = function()
    {
      me.populatePeriodically.apply(me,args);
    };
  setTimeout(callMe, this.secondsBetween * 1000);
}

RandomContentChooser.prototype.populate = function ()
{
  var used = new Array();
  for (var i = 0; i < arguments.length; i++)
  {
    var divId = arguments[i];
    var targetDivElem = document.getElementById(divId);
    
    if (!targetDivElem)
    {
      throw "Unable to find target element for id '"+divId+"'.";
    }
    
    var limit;
    var maxSize = this.contentDivs.length;
    var theChoice;
    
    for (limit = 0; limit < maxSize; limit++)
    {
      var r = Math.floor(Math.random() * maxSize);
      var theChoice = this.contentDivs[r];
      if (!used[theChoice])
      {
        break;
      }
    }
    if (limit == maxSize)
    {
      throw "Unable to find unused content. Content pool has count '"+limit+"', for '"+arguments.length+"' content placements.";
    }
    
    used[theChoice] = true;
  
    var sourceDivElem = document.getElementById(theChoice);
    if (!sourceDivElem)
    {
      throw "Invalid source content div '"+theChoice+"', type '"+typeof(theChoice)+"'.";
    }

    // Unset current source id as used.
    if (used[targetDivElem.sourceDivElem])
    {
      used[targetDivElem.sourceDivElem] = false;
    }

    targetDivElem.sourceDivElem = theChoice;   
    targetDivElem.innerHTML = sourceDivElem.innerHTML;
  }
}