
function switchImages_goBackAndPrepareForNext( idSelector ){
  
  var elestr = $( idSelector ).attr( 'switchImages' );
  var images = elestr.split( ';' );
  if( images.length <= 1 )
    return;
  
  // fade-out del div passato come argomento
  $( idSelector ).animate( { opacity: 0 } , 600 , function() {
    // estrapoliamo l id del div dal this
    switchImages_switchImageAndGoFront( '#' + $( this ).attr( 'id' ) );
  } );
}

function switchImages_switchImageAndGoFront( selector ){
    
  // Prende l array delle immagini dall attr html switchImages ( separati 
  // da ; )
  var elestr = $( selector ).attr( 'switchImages' );
  
  // legge il valore dell attr html curImage, nel caso non
  // esiste lo inizializza a 0
  var act = parseInt( $( selector ).attr( 'curImage' ) );
  if( isNaN( act ) )
    act = 0;
     
  var images = elestr.split( ';' );
  
  var next = act + 1;
  if( next >= images.length || next < 0 ){
    next = 0;
  }
  
  var nextImage = images[next];
  
  // se esiste in div con Id #switchImagesTarget cambia il background-image di
  // quest ultimo altrimenti del div indicato in selector
  var target = $( selector + ' > #switchImagesTarget' ).length ? selector + ' > #switchImagesTarget' : selector;
  
  $( target ).css( 'background-image' , 'url( \'' + nextImage + '\' )' );
  
  // fadeIn
  $( selector ).animate( { opacity: 1 } , 600 , function(){
    setTimeout( 'switchImages_goBackAndPrepareForNext( "' + '#' + $( this ).attr( 'id' ) + '" )' , 5000 );
  });
  
  $( selector ).attr( 'curImage'  , next );
  
}

$( document ).ready( function() {

  $( '.area_immagini' ).fadeIn();
  
  setTimeout( "switchImages_goBackAndPrepareForNext( '#header_bigimage' )" , 5000 );
  
  setTimeout( "switchImages_goBackAndPrepareForNext( '#switchImagesClienti' )" , 5000 );
  setTimeout( "switchImages_goBackAndPrepareForNext( '#switchImagesInstallazioni' )" , 5000 );
} );
