var currentNews = 0;
var newsCount = 0;

$(document).ready(function($)
{
   if( $('#newsObject') && $('#newsBar') )
      makeAutoNews( 'newsObject', 'newsBar' );
     
      init_calendar();
      

 
 
 ddsmoothmenu.init({
 mainmenuid: "menuBg", //menu DIV id
 orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
 classname: 'ddsmoothmenu', //class added to menu's outer DIV
 //customtheme: ["#1c5a80", "#18374a"],
 contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
})
        
   $("a[href^='\#']").click(function(e)
    {
        e.preventDefault();
        document.location.hash=this.href.substr(this.href.indexOf('#')+1);
    });
    
      $("#fortables a img").parent().fancybox({
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">'+ (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
       
   			$("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>';
				}
			});
			

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


$('a').focus(function(){$(this).blur();});  // delete links frames

});         

function prev_calendar()
{    

act_m=parseInt(act_m);
if(act_m==1)
{act_m=12;act_y=parseInt(act_y)-1;}
else
act_m=act_m-1;
$('#calendar_loader').fadeIn('slow');
$.ajax({
  url: '/kalendarium.html?mode=ajax&act_m='+act_m+'&act_y='+act_y+'&reqest=date',
  success: function(data) {
    $('#date_label').html(data);
  }
});
$.ajax({
  url: '/kalendarium.html?mode=ajax&act_m='+act_m+'&act_y='+act_y+'&reqest=calendar',
  success: function(data) {
    $('#cal').html(data);
    $('#calendar_loader').fadeOut('slow');
  }
});
}

function init_calendar()
{    
$('#calendar_loader').fadeIn('slow');
$.ajax({
  url: '/kalendarium.html?mode=ajax&reqest=date',
  success: function(data) {
    $('#date_label').html(data);
  }
});
$.ajax({
  url: '/kalendarium.html?mode=ajax&reqest=calendar',
  success: function(data) {
    $('#cal').html(data);
    $('#calendar_loader').fadeOut('slow');
  }
}); 
}

function next_calendar(){ 
act_m=parseInt(act_m);  
if(act_m==12)
{act_m=1;act_y=parseInt(act_y)+1;}
else
act_m=act_m+1;
$('#calendar_loader').fadeIn('slow');
$.ajax({
  url: '/kalendarium.html?mode=ajax&act_m='+act_m+'&act_y='+act_y+'&reqest=date',
  success: function(data) {
    $('#date_label').html(data);
  }
});
$.ajax({
  url: '/kalendarium.html?mode=ajax&act_m='+act_m+'&act_y='+act_y+'&reqest=calendar',
  success: function(data) {
    $('#cal').html(data);
  $('#calendar_loader').fadeOut('slow');
  }
});
}


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'); 
    });
}
