var interval = null;
var delay = null;

$(document).ready(function(){

  startInterval();

  var panove_kolecka = $('#aktuality_scroll_holder a');
  
  panove_kolecka.click(function(){
      switch_kolecko(this,1);
      clearInterval(interval);
      delay = setTimeout(function(){
        clearInterval(interval);      
        startInterval();
      },20000);
      return false;  
  });
  
});

function startInterval()
{
  clearTimeout(delay);
  interval = setInterval('rotateAktuality()',10000); 
}

function rotateAktuality()
{
  var panove_kolecka = $('#aktuality_scroll_holder a');
  var pocet = panove_kolecka.length - 1;
  var curr = $('#aktuality_scroll_holder .selected').attr('id'); 
  curr = curr.substr(12,curr.length);
  if (curr <= 0) {
    next = pocet;
  } else {
    next = curr - 1;
  }
  var nextKolecko = $('#pan_kolecko_'+next);
  switch_kolecko(nextKolecko,0);

}

function switch_kolecko(elem,type)
{
  var panove_kolecka = $('#aktuality_scroll_holder a');
  var id = $(elem).attr('id');
  id = id.substr(12,id.length);
  panove_kolecka.removeClass('selected');
  $(elem).addClass('selected');
  show_akt(id,type);
}

function show_akt(id,type)
{
  var aktuality = $('.aktualita_holder');
  if (type==1) {
    rychlost = 150;
  } else {
    rychlost = 500;
  }
  
  $('.aktualita_holder.selected').animate({
    opacity: 0
  },rychlost,function(){
    $('.aktualita_holder.selected').hide();
    aktuality.removeClass('selected');
  
    $('#aktualita_'+id).css('opacity',0);
    $('#aktualita_'+id).show();
    $('#aktualita_'+id).animate({
      opacity: 1
    },rychlost,function(){
      $('#aktualita_'+id).addClass('selected');
    });
  });
  
}

