var currentNews = 0;
var newsCount = 0;

$(document).ready(function($)
{
   if( $('#newsObject') && $('#newsBar') )
      makeAutoNews( 'newsObject', 'newsBar' );
      
   $("a[href^='\#']").click(function(e)
    {
        e.preventDefault();
        document.location.hash=this.href.substr(this.href.indexOf('#')+1);
    });
          
   
   //$('#date_label').html(months[3]);
   //$('#aa').animate({left: '+=193'}, 0);}
   
   if(id<=5 && id>=0)
   {
      $('#date_label').html(months[1]);
      $('#aa').animate({left: '-=193'}, 0);
   }
   if(id<=5 && id>=0)
   {
      $('#date_label').html(months[2]);
      $('#aa').animate({left: '-=193'}, 0);
   }
   if(id<=5 && id>=0)
   {
      $('#date_label').html(months[3]);
      $('#aa').animate({left: '-=193'}, 0);
   }
   
   

   			$("a[rel=example_group]").fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">'+ (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
			      
			
   			/*
            $("#content a img").parent().fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '';
				}
			});				*/
			
			
   			$("#gal a img").parent().fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '';
				}
			});			

});
var months=new Array('');
    months[0]='Lipiec 2010';
    months[1]='Sierpień 2010';
    months[2]='Wrzesień 2010';
    months[3]='Październik 2010';    
    months[4]='Listopad 2010';
    months[5]='Grudzień 2010';
var id=0;          

function prev_calendar()
{    
    id=(((parseInt($('#aa').css('left'))+193)/193-1)*(-1))-1;
if(id<=5 && id>=0){
$('#date_label').html(months[id]);
$('#aa').animate({left: '+=193'}, 500);}
}

function next_calendar(){   
id=(((parseInt($('#aa').css('left'))-193)/193-1)*(-1))-1;
if(id<=5 && id>=0){
$('#date_label').html(months[id]);
$('#aa').animate({left: '-=193'}, 500);}
}


function makeAutoNews( containerID, barID )
{
    var element_id = 0;
    var is_first = true;
    
    //var size = 0;
    
    $('#' + containerID + ' img').each( function() 
    { 
        $(this).attr( 'id', 'ano_' + element_id ); 
        
        if( $(this).attr('url') != '' )
        {
            $(this).css( 'cursor', 'pointer' );
            $(this).click( function() { document.location.href = $(this).attr('url');  } );
        }
        
        //size = size + $(this).width();
        
        if( is_first == true )
        {
            insertNewsButton( containerID, barID, 'ano_' + element_id, 1, element_id );
            is_first = false;
            $('.opis').html($('#ano_' + element_id).attr('desc'));
            $('#polecamy_title').html($('#ano_' + element_id).attr('title')); 
            $('#polecamy_title2').html($('#ano_' + element_id).attr('title2')); 
            $('#ano_' + element_id).addClass('act'); 
        }
        else
        {
            insertNewsButton( containerID, barID, 'ano_' + element_id, 0, element_id );
            $('.opis').html($('#ano_' + element_id).attr('desc'));
            $('#polecamy_title').html($('#ano_' + element_id).attr('title')); 
            $('#polecamy_title2').html($('#ano_' + element_id).attr('title2')); 
              $('#ano_' + element_id).addClass('act'); 
        }
        element_id++;
        newsCount++;
    } );
    
    $('#' + containerID).scrollTo( $('#ano_0'), 0 );
    $('.opis').html($('#ano_0').attr('desc'));
    $('#polecamy_title').html($('#ano_0').attr('title')); 
    $('#polecamy_title2').html($('#ano_0').attr('title2'));
    $('#ano_0').addClass('act'); 
    
    
    setInterval( function() {
        autoSetNews( containerID, barID );
      }, 15000 );
}

function autoSetNews( containerID, barID )
{
    if( newsCount > 1 )
    {
        if( (currentNews+1) == newsCount )
        {
            currentNews = 0;
            clearSel( barID );
            $('#sel_ano_0').addClass('act'); 
            $('.opis').html($('#ano_0').attr('desc'));
            $('#polecamy_title').html($('#ano_0').attr('title')); 
            $('#polecamy_title2').html($('#ano_0').attr('title2')); 
            $('#' + containerID).scrollTo( $('#ano_0'), 500 );
        }
        else
        {
            currentNews++;
            clearSel( barID );    
            $('#sel_ano_' + currentNews).addClass('act'); 
            $('.opis').html($('#ano_' + currentNews).attr('desc'));
            $('#polecamy_title').html($('#ano_' + currentNews).attr('title'));
            $('#polecamy_title2').html($('#ano_' + currentNews).attr('title2')); 
            $('#' + containerID).scrollTo( $('#ano_' + currentNews), 500 );
        }
    }
}

function insertNewsButton( containerID, barID, imageID, isSelected, objID )
{
    if( isSelected )
    {          
        $('#' + barID).append('<a id="sel_' + imageID + '" class="act" onfocus="blur();" onclick="return false;" href="javasrcipt:;" >' + (objID+1) + '</a>');
        $('#sel_' + imageID).click( function() {
            clearSel( barID );
            currentNews = objID;
            $(this).addClass('act'); 
            $('.opis').html($('#'+imageID).attr('desc'));
            $('#polecamy_title').html($('#'+imageID).attr('title'));
            $('#polecamy_title2').html($('#'+imageID).attr('title2')); 
            $('#' + containerID).scrollTo( $('#' + imageID), 500 );
        });
    }
    else
    {
        $('#' + barID).append('<a id="sel_' + imageID + '" onfocus="blur();" onclick="return false;" href="javasrcipt:;" >' + (objID+1) + '</a>');
        $('#sel_' + imageID).click( function() {
            currentNews = objID;
            clearSel( barID );
            $('.opis').html($('#'+imageID).attr('desc'));
            $('#polecamy_title2').html($('#'+imageID).attr('title2'));
            $('#polecamy_title').html($('#'+imageID).attr('title')); 
            $(this).addClass('act'); 
            $('#' + containerID).scrollTo( $('#' + imageID), 500 );
        });
    }
}

function clearSel( barID )
{
    $('#' + barID + ' a').each( function() 
    { 
        $(this).removeClass('act'); 
    });
}
