$(document).ready(function() {

//DÃ©finition
  //Contenu
  var content          =  new Array();
  
  //-------------------------------------------
  //-------------Contenu Ã  afficher
  //-------------------------------------------
  for(i=1;i<=52;i++)
  {
     content.push('public/html/read_case.php?case='+i+'&cle=0&racine=0');
  }
  
  var contentNbre    = content.length - 1;//-1 car contentId commence Ã  0
  var contentId      = 0;
  
  var climb          = true;//climb indique si le texte monte
  
  var load           = false;//Indique si le player a dÃ©jÃ  Ã©tÃ© chargÃ©

//---------
var html_background_color = $('html').css('background-color');
var body_background_color = $('body').css('background-color');

//-------------------------------------------
//-------------Ouverture du player
//-------------------------------------------
//Lancement du player page 0
$(".playerStart").click(function(){
  $().openPlayer();     
  $().startPlayer(0);
});
//Lancement du player page x
$.fn.playerPage = function(y){
   y-=1;
   $().openPlayer();
   $().startPlayer(y);
}

//Ouverture du player
$.fn.openPlayer = function(){
  $(window).scrollTop(0);
  $("body").css({"background-color" : "#000", "overflow" : "hidden"});
  $("html").css({"background-color" : "#000", "overflow" : "hidden"});
  $("#main").css({"opacity": "0.1"});
  $("#player").show();
  
  
  //Affichage des pages dans le player
  if(!load){
    for(i=0;i<=contentNbre;i++){
	  k=i+1;
	  $('#playerPage').append('<span class="fakelink" onclick="$().goToContent(\''+k+'\')" >'+k+'</span>');
	  if(k<=contentNbre) $('#playerPage').append(' - ');
	}
  }
  
  load = true;
}

//Affichage d'une page
$.fn.startPlayer = function(page){   
   $("#playerPlay").hide();
   $("#playerStop").show(); 
   $("#playerReplay").hide();   
   contentId = page;
   climb = true;//Monte
   $().loadContent(); 
}

//-------------------------------------------
//-------------Fermeture du player (####Button####)
//-------------------------------------------
$("#playerClose").click(function(){
  $("html").css({"background-color" : html_background_color});
  $("body").css({"background-color" : body_background_color, "overflow" : "scroll"});
  $("#main").css({"opacity": "1"});
  $("#player").hide();
  
  $('#playerContent'+contentId).hide();
  if (typeof(contentOldId)!='undefined')$('#playerContent'+contentOldId).hide();
  //Stop action
  $().stopContent();
});

//-------------------------------------------
//-------------Play (####Button####)
//-------------------------------------------
$("#playerPlay").click(function(){
   $("#playerPlay").hide();
   $("#playerStop").show();
   if (!climb)//Content est Ã  l'arrÃªt
   {
     $().changeOldId();
   }
   else //Content est en train de monter
   {
     $().climb();
     $().climbAndDisappear();
   }
});

//-------------------------------------------
//-------------Stop (####Button####)
//-------------------------------------------
$("#playerStop").click(function(){
   $("#playerStop").hide();
   $("#playerPlay").show();
   $().stopContent();
});

//-------------------------------------------
//-------------Replay (####Button####)
//-------------------------------------------
$("#playerReplay").click(function(){
   $('#playerContent'+contentId).hide();   
   $().startPlayer(0); 
});

//-------------------------------------------
//-------------Previous (####Button####)
//-------------------------------------------
$("#playerPrevious").click(function(){
   if(contentId>0)
   {
     $().changeManuelContent(); 
     contentId--;
     $().loadContent();
   }
});

//-------------------------------------------
//-------------Next (####Button####)
//-------------------------------------------
$("#playerNext").click(function(){
   if(contentId<contentNbre)
   {
     $().changeManuelContent(); 
     contentId++;
     $().loadContent();
   }
});

//-------------------------------------------
//-------------Aller Ã  une page (####Button####)
//-------------------------------------------
$.fn.goToContent = function(page){  
  $().changeManuelContent(); 
  contentId = page - 1;
  $().loadContent();
} 

//Changement de page manuel
$.fn.changeManuelContent = function()
{
  $('#playerContent'+contentId).hide();
  if (typeof(contentOldId)!='undefined')$('#playerContent'+contentOldId).hide();
  $().stopContent();    
  
  $("#playerStop").show();
  $("#playerPlay").hide();
  $("#playerReplay").hide();
  delete(contentOldId);  
  climb = true;//Monte
}

//-------------------------------------------
//-------------Climb, first step
//-------------------------------------------
$.fn.climb = function(){     
   
   $('#playerContent'+contentId).show();
   position = parseInt($('#playerContent'+contentId).css('bottom')) + $('#playerContent'+contentId).height();
   if(position<0)position=0;    
   animate = $(window).height() - 10 - position;
   $('#playerContent'+contentId).animate({"bottom": "+="+animate+"px"}, 5000, function(){

     //Vérification de la position et éventuellement correction (cas de vidéo dailymotion/youtube etc...)
     if(parseInt($('#playerContent'+contentId).css('top')) < 0)
     {
	newbottom = $(window).height() - $('#playerContent'+contentId).height() -10;
	$('#playerContent'+contentId).css({"bottom": newbottom+"px"})
     }

     climb = false;//ArrÃªt lecture
     //Enclenchement de l'action suivante si le max n'est pas atteind
     pause = parseInt(($('#playerContent'+contentId).html().length * 1000) / 30);
     if(contentId<contentNbre) executeNextAction = setTimeout(function(){$().changeOldId();}, pause);
     else{//Fin du content=>Affhichage du bouton Replay
	    $("#playerStop").hide();
            $("#playerPlay").hide();
            $("#playerReplay").show();
     }
   });   
}

//-------------------------------------------
//-------------Changement de old_id
//-------------------------------------------
$.fn.changeOldId = function(){
   climb = true;//Monte
   contentOldId = contentId;
   $().climbAndDisappear();
}

//-------------------------------------------
//-------------Climb, second step
//-------------------------------------------
$.fn.climbAndDisappear = function(){

  oldposition = parseInt($('#playerContent'+contentOldId).css("bottom"));
  if(oldposition<0)oldposition=0; 
  animate = $(window).height() + $('#playerContent'+contentOldId).height() - oldposition;
  $('#playerContent'+contentOldId).animate({"bottom": "+="+animate+"px"}, 4000, function(){$('#playerContent'+contentOldId).hide();});
  
  //Lancement du content suivant
  if(contentId<contentNbre && contentOldId==contentId){
    contentId++;
	$().loadContent();
  }
  
}

//-------------------------------------------
//-------------Chargement d'une page
//-------------------------------------------
$.fn.loadContent = function(){
   $("#playerCurrentPage").html(contentId + 1);//Page en cours
   
   //Affichage des boutons previous et next
   if(contentId<=0)$("#playerPrevious").hide(); 
   else $("#playerPrevious").show(); 
   if(contentId>=contentNbre)$("#playerNext").hide();
   else $("#playerNext").show(); 
   
   if($('#playerContent'+contentId).length==0)
   {
     $.ajax({
        url: content[contentId],
        success: function(data) {
		   $('#playerContent').append('<div id="playerContent'+contentId+'" class="hide content"></div>');
		   $('#playerContent'+contentId).css({"position": "absolute", "width": "90%"});
	           $('#playerContent'+contentId).html(data);
		   
		   $('#playerContent'+contentId).append('<div id="infoContent'+contentId+'"></div>');
		   p = contentId+1;
		   $('#infoContent'+contentId).html('Page '+p)		   
		   
		   $('#playerContent'+contentId).css({"bottom": "-"+$('#playerContent'+contentId).height()+"px"});
		   $().climb();
        }  
     });
   }
   else {
     p = contentId+1;
     $('#infoContent'+contentId).html('Page '+p)
		   
     $('#playerContent'+contentId).css({"position": "absolute", "bottom": "-"+$('#playerContent'+contentId).height()+"px"});
     $().climb();
   }
}

//-------------------------------------------
//-------------Stop (arrÃªt de l'action en cours)
//-------------------------------------------
$.fn.stopContent = function(){
  if (typeof(executeNextAction)!='undefined') 
  {  
     clearTimeout(executeNextAction);
	 delete(executeNextAction); 
  }
  $('#playerContent'+contentId).stop();
  if (typeof(contentOldId)!='undefined')$('#playerContent'+contentOldId).stop();
}

});
