var SQCover = function(){
  this.NbCover = $(".myCover").length;
  this.CurrentPage = 1;
  this.TimeWait = 6500; // Temps entre chaque anim
  this.TimeAnim = 150; // Temps d'anime fadeOut
  this.TimeAnim2 = 150; // Temps d'anime fadeIn
  this.timer=null;
  this.timer2=null;
  this.timer3=null;
  this.init();
};
SQCover.prototype.init = function(){
  var i=1; $(".myCover").each(function(){
    $(this).attr("id","myCover"+i); i++;
  });
  if( this.NbCover > 1 ) this.Start();
};

SQCover.prototype.Start = function(){
  // Anim des actuel (parte vers le haut)
  clearTimeout(this.timer); clearTimeout(this.timer2); clearTimeout(this.timer3);
  var t = this;
  this.timer = setTimeout(function(){ t.HideCov(); },this.TimeWait);
};

SQCover.prototype.HideCov = function(){
  $("#myCover"+this.CurrentPage).fadeOut(this.TimeAnim);
  var t = this;
  clearTimeout(this.timer); clearTimeout(this.timer2); clearTimeout(this.timer3);
  this.timer2 = setTimeout(function(){ t.ShowCov(); },this.TimeAnim+2);
};

SQCover.prototype.ShowCov = function(){
  this.CurrentPage+=1; if(this.CurrentPage>this.NbCover) this.CurrentPage=1;
  $("#myCover"+this.CurrentPage).fadeIn(this.TimeAnim2);
  var t = this;
  clearTimeout(this.timer); clearTimeout(this.timer2); clearTimeout(this.timer3);
  this.timer3 = setTimeout(function(){ t.Start(); },this.TimeAnim2+2);
  this.preloadAjax();
};

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

SQCover.prototype.load = function(i){
  var c=$("#myCover"+i); var rel=c.attr('rel'); c.attr('rel','');
  if(rel!='') c.find('img').attr('src',rel);
};

var myCover;
$(document).ready(function(){ myCover = new SQCover(); });
/*var exists = function(v){ try{ eval(v); }catch(e){ return false; } if(window[v]===null||window[v]===undefined) return false; else return true; };
if( !exists('ListOnLoad') ){ var ListOnLoad=new Array(); };
var addOnLoad = function(fn){ ListOnLoad[ListOnLoad.length] = fn; };
window.onload = function(){ for(i=0;i<ListOnLoad.length;i++){ eval(ListOnLoad[i]+"()"); } };*/
