var SQCover = function(){
  this.NbCover = $(".myCover").length;
  this.NbByPage = 4;
  this.CurrentPage = 1;
  this.TimeAnim = 4000; // Temps entre chaque anim
  this.TimeTopAnim = 150; // Temps entre chaque cover avant de monter
  this.TimeNextPage = 300; // Temps avant la fin de l'anim1 et l'anim
  this.TimeFlopAnim = 250; // Temps entre chaque cover pour apparaitre
  this.TimeGoto = 120; // Temps pour suivant, precedent et page
  this.isStop=0;
  this.isAnim=0;
  this.timer=null;
  this.init();
};
SQCover.prototype.init = function(){
  var i=0; this.setHeight();
  $(".myCover").each(function(){ i++; $(this).attr("id","myCover"+i); });
  this.Width=$("#SQCover").width();
  this.TotalPage = this.NbCover/this.NbByPage;
  if( this.TotalPage != Math.floor(this.TotalPage) ) this.TotalPage = Math.floor(this.TotalPage)+1;
  var t=this;
  if(this.TotalPage>1){
    this.timer=setTimeout(function(){ t.Start(); },t.TimeAnim);
    $("#CoverButton").click(function(){ t.ToggleAnim(); }).html('<img src="images/cover_pause.jpg" alt="Pause" />');
    $("#CoverNext").click(function(){ t.nextPage(); }).html('<img src="images/cover_next.jpg" alt="Suivante" />');
    $("#CoverPrev").click(function(){ t.prevPage(); }).html('<img src="images/cover_prev.jpg" alt="Précédente" />');
    $("#CoverNavSel").show(); this.updateNavi();
  }
  setTimeout(function(){ t.setHeight(); }, 200);
  this.Width2=0; $("#SQCoverInner > .myCover").each(function(){ t.Width2 += $(this).width(); }); $("#SQCoverInner").width(this.Width2+'px');
};

SQCover.prototype.setHeight = function(){
  var h=$("#SQCoverInner").height(); var h=Number(h); this.Height=h; var top=0;
  if(h<170){ top=(170-h)/2; } var bottom=top;
  $("#SQCover").animate({'height':this.Height+'px',marginTop:top+"px",marginBottom:bottom+'px'},100);
};

SQCover.prototype.Start = function(){
  // Anim des actuel (parte vers le haut)
  this.setHeight();
  var debut = ((this.CurrentPage-1)*this.NbByPage)+1;
  var fin = (this.CurrentPage*this.NbByPage); if(fin>this.NbCover) fin=this.NbCover;
  if(this.isStop==0) this.AnimTop(debut,fin);
  this.preloadAjax();
};
SQCover.prototype.AnimTop = function(deb,fin){
  this.setHeight();
  var t=this; $("#myCover"+deb).animate({top: '-'+this.Height+'px'},500); t.isAnim=1;
  if(deb<fin) setTimeout(function(){ t.AnimTop(deb+1,fin); }, this.TimeTopAnim);
  else setTimeout(function(){ t.Start2(); }, this.TimeNextPage);
};

SQCover.prototype.Start2 = function(){
  this.setHeight();
  // Passe à la suivante, ou retourne au debut
  this.CurrentPage = Number(this.CurrentPage)+1;
  if(this.CurrentPage>this.TotalPage) this.CurrentPage=1;
  this.updateNavi();

  // Anim les suivantes (1,3,2,4..)
  var debut = ((this.CurrentPage-1)*this.NbByPage)+1;
  var fin = (this.CurrentPage*this.NbByPage);
  this.AnimFlop(debut,fin,true);
};
SQCover.prototype.AnimFlop = function(deb,fin,init,flag){
  if(init){
    for(var i=deb;i<=fin;i++) $("#myCover"+i).css({'visibility':'hidden','top':'0px'}); var flag=0;
    $("#SQCoverInner").css({left: -((this.CurrentPage-1)*this.Width)+'px'});
  }
  this.setHeight();
  var t=this; $("#myCover"+deb).fadeIn(350).css('visibility','visible');
  if(flag==0){ var next=deb+2; flag=1; }else{ var next=deb-1; flag=0; }
  if(deb<fin) setTimeout(function(){ t.AnimFlop(next,fin,false,flag); }, this.TimeFlopAnim);
  else{ t.isAnim=0; this.timer=setTimeout(function(){ t.Start(); }, this.TimeAnim); }
};

SQCover.prototype.preloadAjax = function(){
  var next = Number(this.CurrentPage)+2;
  if(next>this.TotalPage) next=1;
  if(next>2) this.load(next);
};

SQCover.prototype.load = function(next){
  var debut = ((next-1)*this.NbByPage)+1;
  var fin = (next*this.NbByPage);
  for(var i=debut;i<=fin;i++){
    var c=$("#myCover"+i); var rel=c.attr('rel'); c.attr('rel','');
    if(rel!='') c.find('img').attr('src',rel);
  }
};

SQCover.prototype.ToggleAnim = function(){
  clearTimeout(this.timer);
  if(this.isStop==1){
    this.isStop=0; var t=this; $("#CoverButton").html('<img src="images/cover_pause.jpg" alt="Pause" />');
    this.timer=setTimeout(function(){ t.Start(); }, this.TimeAnim);
  }else{
    this.isStop=1; $("#CoverButton").html('<img src="images/cover_play.jpg" alt="Lecture" />');
  }
};

SQCover.prototype.nextPage = function(){
  this.goToPage(Number(this.CurrentPage)+1,'next');
  this.preloadAjax();
};

SQCover.prototype.prevPage = function(){
  this.goToPage(Number(this.CurrentPage)-1,'prev');
  this.load(Number(this.CurrentPage)-1);
};

SQCover.prototype.goToPage = function(num,act){
  if(this.isAnim==0){
    if(this.isStop!=1) this.ToggleAnim();
    if(act=='next'&&num>this.TotalPage){ this.updateNavi(); return; }
    else if(act=='prev'&&num<1){ this.updateNavi(); return; }

    this.CurrentPage = Number(num);
    if(this.CurrentPage>this.TotalPage) this.CurrentPage=1;
    if(this.CurrentPage<1) this.CurrentPage=this.TotalPage;

    this.load(this.CurrentPage);
    this.updateNavi();

    var debut = ((this.CurrentPage-1)*this.NbByPage)+1;
    var fin = (this.CurrentPage*this.NbByPage); if(fin>this.NbCover) fin=this.NbCover;
    for(var i=debut;i<=fin;i++) $("#myCover"+i).show().css({'visibility':'visible','top':'0px'});
    $("#SQCoverInner").animate({left: -((this.CurrentPage-1)*this.Width)+'px'},this.TimeGoto);

    this.setHeight();
  }
};

SQCover.prototype.updateNavi = function(){
  if(this.CurrentPage>=this.TotalPage) $("#CoverNext").css('visibility','hidden');
  else $("#CoverNext").css('visibility','visible');
  if(this.CurrentPage<=1) $("#CoverPrev").css('visibility','hidden');
  else $("#CoverPrev").css('visibility','visible');
  var navi=''; for(var i=1;i<=this.TotalPage;i++){
    if(this.CurrentPage==i) sel=' select'; else sel='';
    navi+='<div class="coverNav'+sel+'" id="coverNav'+i+'" rel="'+i+'" alt="Page '+i+'" title="Page '+i+'">'+i+'</div>';
  } $("#CoverNavContent").html(navi); var t=this;
  $(".coverNav").click(function(){ var id=$(this).attr('rel'); t.goToPage(id,false); }).hover(this.setHover,function(){ t.setOut(t); });
  this.setCurrentNav();
};

SQCover.prototype.setHover = function(){
  var el=$(this); var h=el.offset(); var h2=el.parent().offset(); var left = h.left-h2.left;
  $("#CoverNavSel").stop().animate({left: left+'px',width: (el.width()+10)+'px', height: (el.height()+4)+'px'},150);
};

SQCover.prototype.setOut = function(t){
  var el=$("#coverNav"+t.CurrentPage);
  var h=el.offset(); var h2=el.parent().offset();
  var left = h.left-h2.left;
  $("#CoverNavSel").stop().animate({left: left+'px',width: (el.width()+10)+'px', height: (el.height()+4)+'px'},150);
};

SQCover.prototype.setCurrentNav = function(){
  var el=$("#coverNav"+this.CurrentPage);
  var h=el.offset(); var h2=el.parent().offset();
  var left = h.left-h2.left;
  $("#CoverNavSel").animate({left: left+'px',width: (el.width()+10)+'px', height: (el.height()+4)+'px'},150);
};

var myCover;
$(document).ready(function(){ myCover = new SQCover(); });
addOnLoad('myCover.setHeight');
