$('document').ready(function()
{
  // Get images that will take part in the hovering
  var imgs  = $("#caroussel img");
  // defines unique id for each
  var names = jQuery.map(imgs, function(item)
  {
    var p  = item.src.split('/');
    var n  = new String(p.slice(p.length - 1)).match(/\w+/);
    $(item).attr('id', n);

    return n;
  })

  $("#caroussel img").each(function(i,item){
    $('#caroussel').data(names[i], {from: $(item).attr('src'), to: $(item).attr('src').replace('nb/', '')});
  })

  $("#caroussel img").hover(
    function(event){
      var name = names[$('#caroussel img').index($(event.target))];
      $(event.target).attr('src', $('#caroussel').data(name).to);
    },
    function(event){
      var name = names[$('#caroussel img').index($(event.target))];
      $(event.target).attr('src', $('#caroussel').data(name).from);
    }
  );

  var animations = $('#header-middle .animated');
  var i = 0;
  function animate(e)
  {
    e.fadeIn("fast");
    // a setInterval like method
    e.animate({left:'+=0'}, 2500);
    e.fadeOut(300, nextAnimation);
  }

  function nextAnimation ()
  {
    if(i >= animations.length)
    {
      i = 0;
      // due to an excess consumption of CPU in MSIE, don't loop
      if($.browser.msie)
      {
        return;
      }
    }

    animate($(animations.get(i)));
    i++;
  }
  nextAnimation();
})
