$(document).ready( function() {
  
  $.getJSON( '/modules/tools/header.php', {present: $('#header-imgs img:first').attr( 'src' )}, function(data) {
    for( i = 0; i < data.length; i++ ) {
      var img = new Image();
      $(img).load(function() {
        $(this).attr( 'alt', 'Header foto ' + ( i + 2 ) );
        $('#header-imgs').append( this );
      }).attr( 'src', data[i] );  
    }
    setInterval( 'slideSwitch()', 8000 );
  });  

});

function slideSwitch() {
  var $active = $( '#header-imgs img.active' );
  
  if ( $active.length == 0 ) $active = $( '#header-imgs img:last' );
  var $next =  $active.next().length ? $active.next() : $( '#header-imgs img:first' );
  $active.addClass('last-active');
  $next.css( { opacity: 0 } )
       .addClass( 'active' )
       .animate( { opacity: 1 }, 1500, function() {
         $active.removeClass( 'active last-active' );
       });
}	                     		   
