var RandomItems = {
  items: [],
  retrievingItem: false,
  init: function(){
    
  },
  nextItem: function(){
    if(RandomItems.retrievingItem)
      return;
    if(RandomItems.items.length == 0){
      RandomItems.retrievingItem = true;
      new Ajax.Request('/random_items', {
        method: 'get',
        onSuccess: function(transport){
          RandomItems.items = eval('('+transport.responseText+')');
          RandomItems.showItem(RandomItems.items.pop());
          RandomItems.retrievingItem = false;
        }
      });
    }
    else
      RandomItems.showItem(RandomItems.items.pop());
      
  },
  showItem: function(item){

	new Effect.Opacity('homeRandomContent', { from: 0, to: 1, duration: 1}); 

    $$('.homeRandomOwner a').each(function(a){a.href = item.user.url;});
    $$('.homeRandomOwner img').each(function(img){img.src = item.user.image;});
    $$('.homeRandomOwner span a').each(function(a){a.innerHTML = item.user.identity;});
    $$('.homeRandomItem a').each(function(a){a.href = item.url;});
    $$('.homeRandomItem img').each(function(img){img.src = item.image;});
    $$('.homeRandomItem span a').each(function(a){a.innerHTML = item.name;});
    //request the following item images so that the following transition would be smooth
    if(RandomItems.items.length > 0){
      $('tmp_image_1').src = RandomItems.items[0].image;
      $('tmp_image_2').src=RandomItems.items[0].user.image;
    }
  }
}
