var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){

	$("a.galeria").fancybox({
		'titleShow'		: true,
		'titlePosition'		: 'outside',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'

	});


  headline_count = $("div.baner").size();
  $("div.baner:eq("+current_headline+")").css('top', '0px');
  $('#baner_pauza').fadeTo('slow', 0);
  del=10000;
  
  $('#baner_kontener').mouseenter(function() {
    clearInterval(headline_interval);
    $('#baner_pauza').fadeTo('slow', 1);
  });
  
  $('#baner_kontener').mouseleave(function(){
    headline_interval = setInterval(headline_rotate,del);
    $('#baner_pauza').fadeTo('slow', 0);
  }); 
 
  $('#baner_nastepny').click(function() {  
    clearInterval(headline_interval);

    if(current_headline==headline_count-1) next=0;  
    else next=current_headline+2;

    headline_rotate(next,'up');   
    //headline_interval = setInterval(headline_rotate,del);
  });
   
  $('#baner_poprzedni').click(function() {  
    clearInterval(headline_interval);
      
    if(current_headline==0) next=headline_count;  
    else next=current_headline;

    headline_rotate(next,'down');
    //headline_interval = setInterval(headline_rotate,del);
  });
  
  headline_interval = setInterval(headline_rotate,del);
});


function headline_rotate(co,jak) {

  current_headline = (old_headline + 1) % headline_count;
  if(co) current_headline=co-1;
  
  if(jak=='down')
  {
  $("div.baner:eq("+ old_headline     +")").css('top', '0px');
  $("div.baner:eq("+ current_headline +")").css('top', '-231px');
  i=1;
  }else
  {
  $("div.baner:eq("+ old_headline     +")").css('top', '0px');
  $("div.baner:eq("+ current_headline +")").css('top', '231px');
  i=-1;  
  }  
  
  
  $("div.baner:eq("+ old_headline     +")").animate({top: 231*i},"slow");   
  $("div.baner:eq("+ current_headline +")").animate({top: 0},"slow"); 

  if(jak=='down')
  {
  $("div.baner:eq("+ old_headline     +")").css('top', '0px');
  $("div.baner:eq("+ current_headline +")").css('top', '-231px');
  }else
  {
  $("div.baner:eq("+ old_headline     +")").css('top', '0px');
  $("div.baner:eq("+ current_headline +")").css('top', '231px');  
  }  
  

  old_headline = current_headline;
}
