// (C) 2007 by Jean-Christophe Koerber. All rights reserved.

function addBackgroundItem (background, image, key)
{
 background[background.length]=new BackgroundItem(image, key);
}

function BackgroundItem (image, key)
{
 this.image=image;
 this.key=key;
}

function buildBackground (background, identity, position, repeat, key)
{
 var element;
 var content;
 var index;
 var maxIndex;
 var backgroundItem;

 if (background.length>0)
 {
  element=document.getElementById(identity);

  if (element)
  {
   content="";

   if (key!="")
   {
    for (index=0, maxIndex=background.length; (index<maxIndex)&&(background[index].key!=key); index++);

    if (index<maxIndex)
     backgroundItem=background[index];
    else
     backgroundItem=background[0];
   }
   else
   {
    backgroundItem=background[Math.floor(Math.random()*background.length)];
   }

   content+="url('"+backgroundItem.image+"')";

   element.style.backgroundImage=content;
   element.style.backgroundPosition=position;
   element.style.backgroundRepeat=repeat;
  }
 }
}