var timer;
var clickCount;
var timerSeconds = 10000;

function startTimer()
{
 timer=setTimeout("loopTimer()",timerSeconds);
}
function loopTimer()
{
 next_slide();
 timer=setTimeout("loopTimer()",timerSeconds);
}
function stopTimer()
{
 clearTimeout(timer);
}


$(document).ready( function() {

 clickCount = 0;

 // Activer un seul slide
 $('#slide .tab.active').removeClass('active');
 var active_slide = $('#slide .tab:last');
 $(active_slide).addClass('active')
 
 // Initialisation
 next_slide();
 
 $("#slide .buttons a").not('.active').click( function() {
  if( !$(this).hasClass( "active" ) && !$(this).hasClass( "disabled" ) )
  {
   $("#content .client").hide();
   var name_client = $(".clientImage .active");
   var title_client = $(".clientTitle .active");
   name_client.removeClass('active');
   title_client.removeClass('active');
   var cible = $(this).attr("ref");
   $(".clientTitle ."+cible+"").addClass('active');
   $(".clientImage ."+cible+"").addClass('active');
 
   change_slide( $( $(this).attr("href") ) );
   $("#content .client").fadeIn("slow");
  }
  return false;
 });
 
});

next_slide = function() {
 var active_slide = $('#slide .tab.active');
 
 if( active_slide.length == 0 )
 {
  $('#slide .tab').removeClass('active');
  var active_slide = $('#slide .tab:last');
  $(active_slide).addClass('active')
 }
 var next = $(active_slide).next(".tab");
 if( next.length == 0 )
  next = $('#slide .tab:first');
 change_slide( next );
 
 
 $("#content .client").hide();
 
 var name_client = $(".clientImage .active");
 var title_client = $(".clientTitle .active");
 name_client.removeClass('active');
 title_client.removeClass('active');
 var next_client  = name_client.next("a");
 var next_title  = title_client.next("a");
 
 if ( next_client.length != 0 )
 {
 next_client.addClass('active');
 next_title.addClass('active');
 }
 else
 {
 $('.clientImage a:first').addClass('active');
 $('.clientTitle a:first').addClass('active');
 }
 
 $(".clientTitle .viewRefs").attr("href", $(".clientImage .active").attr("href"));
 $("#content .client").fadeIn("slow");
 
}

show_slide = function( background, slide, name_slide ) {
 
 // Hide Loader
 $('#slide .loading').hide();
 
 // Hide other backgrounds
 $('#backgrounds div').fadeOut(2000);
 
 // Create and show background
 $("#backgrounds").stop(true, true);
 $("#backgrounds").append( background );
 $('#slide .buttons a').removeClass("disabled");
 $(background).fadeIn(2000, function() {
 });
 
 // Show current slide
 $(slide).fadeIn(2000, function() {
  clickCount--;
  timer=setTimeout("next_slide()",timerSeconds);
 });
 $('#slide .buttons a').removeClass('active');
 $('#slide .tab').removeClass('active');
 $('#slide .buttons a.'+name_slide).addClass('active');
 $('#slide #'+name_slide).addClass('active');
}

change_slide = function( slide ) {
 if( clickCount < 2 )
 {
  clickCount++;
  $('#slide .buttons a').addClass( "disabled" );
  $(".clientTitle .viewRefs").attr("href", $(".clientImage .active").attr("href"));
  
  // Hide tabs and show loader
  $('#slide .tab').fadeOut(200);
  $('#slide .loading').fadeIn();
  clearTimeout(timer);
  
  var name_slide = $(slide).find('input[name=name]').val();
  var url_image = $(slide).find('input[name=background]').val();
  
  // Background already exists ?
  if( $("#backgrounds ."+name_slide).length == 1 )
  {
   var background = $("#backgrounds ."+name_slide);
   show_slide( background, slide, name_slide );
  }
  else
  {
   var myImg = new Image;
   myImg.src = url_image;
   myImg.onload = function(){
    // When image is loaded
    $(myImg).css("display","none");
    var background = $('<div class="'+name_slide+'" style="background-image:url('+url_image+');"></div>');
    show_slide( background, slide, name_slide );
    
    // Load all the pictures
    $.each( $("#slide .tab input[name='background']"), function( i, e ) {
     var img = new Image;
     img.src = $(e).val();
     img.onload = function() { };
    });
    
   };
  }
 }
 else
  return false;
}
