/**
  
  Dynamic billboard
  @author   FrantaToman
  @www      www.frantatoman.cz
  @version  1.0
  
*/

jQuery.fn.dynamicbillboard = function(options){

  var itemsCount = $(this).children("div#references-wrap").children("#references-list img").size();
  var active = 0;
  
  var width = 864;
  var leftPx = width / 2;

  $(this).children("div#references-wrap").children("#references-list img").css("opacity", "0.0");

  var next = function(){  
            
    if(itemsCount > 0){

      //active++;

      if(active >= itemsCount ){
        active = 0;
        //alert("test");
      }

      var prevItemId = 0;
      if(active > 0){ 
        prevItemId = active;
      }
      
     // alert(prevItemId);
      
      var activeItem = $("#references-list img").get(prevItemId); 
      
      //alert( $(activeItem).attr('src'));
      
      var itemWidthPx = $(activeItem).css("width");    // active item width
      
      //alert(itemWidthPx)
      
      var itemWidth = itemWidthPx.substr(0, itemWidthPx.length - 2);
      
      var beginItemLeft = leftPx + "px";
      var itemLeft = (leftPx - (itemWidth / 2)) + "px";
      
      itemWidth = (itemWidth/100) * 80;
      itemLeftStep1 = (leftPx - (itemWidth / 2)) + "px";
      itemWidthPxStep1 = itemWidth + "px";
      
      $(activeItem).css({width : "1px", left : beginItemLeft });
      $(activeItem).animate({left: itemLeftStep1, opacity: '1.0', width: itemWidthPxStep1}, 800);
      
      $(activeItem).animate({opacity: '.0', width : itemWidthPx, left: itemLeft}, 400);
      
      active++;
      
      setTimeout(function(){ 
        next();
       }, 1200);
      
    }

  };
  
  next();

};
