
SQCover.prototype.setWidth = function(){
  var t=this;
  $("#SQCover > .myCover").each(function(){ t.myWidth += $(this).width(); t.Height2=Math.max($(this).height(),t.Height2); });
  if(this.myWidth>5) $("#SQCover").width(this.myWidth+'px');
};

SQCover.prototype.preloadAjax = function(){
  var next = Number(this.CurrentPage)+1;
  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.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(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();

  if(this.isMaximize){
    if(act=='max')
      $("#SQCover").css({top: -((this.CurrentPage-1)*((this.Height2+5)*this.NbLigne))+'px',left: '0px'});
    else{
      $("#SQCover").fadeOut(500); var t=this; clearTimeout(t.TimerAnim);
      t.TimerAnim=setTimeout(function(){ $("#SQCover").css({top: -((t.CurrentPage-1)*((t.Height2+5)*t.NbLigne))+'px'}).fadeIn(500); },550);
    }
  }else{
    if(act=='max'){ $("#SQCover").css({top: '0px',left: -((this.CurrentPage-1)*this.Width)+'px'}); }
    else $("#SQCover").animate({left: -((this.CurrentPage-1)*this.Width)+'px'},this.TimeGoto);
  }

};

SQCover.prototype.updateNavi = function(){
  if(this.CurrentPage>=this.TotalPage) $("#CarouRight").animate({opacity: 0.2});
  else $("#CarouRight").animate({opacity: 1});
  if(this.CurrentPage<=1) $("#CarouLeft").animate({opacity: 0.2});
  else $("#CarouLeft").animate({opacity: 1});
  var t = this;
  var navi='';
  if(this.TotalPage>1){
    for(var i=1;i<=this.TotalPage;i++){
      navi+='<span class="coverNav" id="coverNav'+i+'" rel="'+i+'"><a href="boutique/'+this.CatID+'/'+this.ProdID+'-'+this.Rewrite+'/'+i+'/'+this.Maximize+'/" alt="Page '+i+'" title="Page '+i+'">'+i+'</a></span>';
    }
  }
  if(this.TotalPage>1 || this.isMaximize){
    if(!this.isMaximize) navi+='<a id="SQMaxi" href="boutique/'+this.CatID+'/'+this.ProdID+'-'+this.Rewrite+'/1/1/" title="Voir plus de produit">Voir plus de produit</a> ';
    else navi+='<a id="SQMaxi" href="boutique/'+this.CatID+'/'+this.ProdID+'-'+this.Rewrite+'/1/0/" title="Voir moins de produit">Voir moins de produit</a> ';
  }
  $("#SQNavigContent").html(navi); var t=this;

  $(".coverNav").click(function(){ var id=$(this).attr('rel'); t.goToPage(id,false); }).hover(function(){ t.setHover(t,$(this)); },function(){ t.setOut(t); });
  $(".coverNav > a").click(function(){ var id=$(this).parent().attr('rel'); t.goToPage(id,false); return false; });
  $("#SQMaxi").click(function(){ t.ChangeMaxi(t); return false; });
  this.setCurrentNav();
};

SQCover.prototype.ChangeMaxi = function(t){
  if(t.Maximize==1) t.Maximize=0; else t.Maximize=1;
  t.isMaximize = (t.Maximize==1)?true:false;
  //t.CurrentPage=1;
  if(t.isMaximize){
    t.SavePage = t.CurrentPage;
    t.CurrentPage=t.SavePageMax;
    //alert("On agrandi");
    SavePage=t.CurrentPage;
    t.NbByPage = t.NbByLigne*t.NbLigne;
    $("#CarouLeft,#CarouRight,#CarouCenter").addClass('Maximize');
    $("#SQTitre").before('<div id="CarouBack"></div>');
    t.load(t.CurrentPage+1);
  }else{
    t.SavePageMax = t.CurrentPage;
    t.CurrentPage=t.SavePage;
    //alert("on reduit");
    t.NbByPage = t.NbByLigne;
    $("#CarouLeft,#CarouRight,#CarouCenter").removeClass('Maximize');
    $("#CarouBack").remove();
  }
  t.TotalPage = t.NbCover/t.NbByPage;
  if( t.TotalPage != Math.floor(t.TotalPage) ) t.TotalPage = Math.floor(t.TotalPage)+1;
  t.goToPage(t.CurrentPage,'max');
  t.updateNavi();
};

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

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

SQCover.prototype.setCurrentNav = function(){
  if(this.TotalPage>1){
    var el=$("#coverNav"+this.CurrentPage+" > a");
    var h=el.offset(); var h2=el.parent().parent().offset();
    var left = h.left-h2.left;
    $("#SQNavigSel").show().animate({left: left+'px',width: el.width()+'px', height: el.height()+'px'},150);
  }else $("#SQNavigSel").hide();
};



SQCover.prototype.CoverHover = function(t,t2){
  t.isHover=true;
  clearTimeout(t.TimerOut);
  var width = t2.width(); // largeur image
  var alt = t2.attr('alt'); $("#SQTitre").html(alt);
  //var height = t2.height(); // hauteur image
  var height = t2.parent().parent().parent().parent().parent().parent().height(); // hauteur parent table
  //if(height>height2) height=height2; // Recupere la bonne hauteur
  var pos = t2.parent().parent().parent().parent().parent().offset(); // Position du table
  var pos2 = $("#SQCover").offset(); // Position de SQCover
  var left = Number(pos.left - pos2.left);
  var left2 = left+width-2;
  var top = Number(pos.top - pos2.top);
  var top2 = top+height-2;
  $("#SQCovSel1").stop().animate({width: width, top: top+"px", left: left+"px"},t.SelectTime);
  $("#SQCovSel3").stop().animate({width: width, top: top2+"px", left: left+"px"},t.SelectTime);
  $("#SQCovSel2").stop().animate({height: height, top: top+"px", left: left+"px"},t.SelectTime);
  $("#SQCovSel4").stop().animate({height: height, top: top+"px", left: left2+"px"},t.SelectTime);
};

SQCover.prototype.CoverOut = function(t){
  clearTimeout(t.TimerOut); var t=t; t.isHover=false;
  t.TimerOut = setTimeout(function(){
    if(t.HoverBorder==true || t.isHover==true) return;
    var width = $("#SQCover > .select img").width();
    //var height = $("#SQCover > .select img").height();
    var height = $("#SQCover > .select").height();
    //if(height>height2) height=height2;
    var pos = $("#SQCover > .select table").offset();
    var pos2 = $("#SQCover").offset();
    var left = Number(pos.left - pos2.left);
    var left2 = left+width-2;
    var top = Number(pos.top - pos2.top);
    var top2 = top+height-2;
    //var alt=$("#SQCover > .select img").attr('alt');
    alt='';
    $("#SQTitre").html(alt);
    $("#SQCovSel1").stop().animate({width: width, top: top+"px", left: left+"px"},t.SelectTime2);
    $("#SQCovSel3").stop().animate({width: width, top: top2+"px", left: left+"px"},t.SelectTime2);
    $("#SQCovSel2").stop().animate({height: height, top: top+"px", left: left+"px"},t.SelectTime2);
    $("#SQCovSel4").stop().animate({height: height, top: top+"px", left: left2+"px"},t.SelectTime2);
  },60);
};


SQCover.prototype.HoverBord = function(t){
  t.HoverBorder=true;
  t.isHover=false;
};

SQCover.prototype.OutBord = function(t){
  clearTimeout(t.TimerBord); var t=t;
  t.TimerBord = setTimeout(function(){
    t.HoverBorder=false;
    if(t.isHover==false) t.CoverOut(t);
  },35);
};



SQCover.prototype.init = function(){
  var i=0; this.setWidth(); this.Height2=0;
  $(".myCover").each(function(){ i++; $(this).attr("id","myCover"+i); });
  this.Width=$("#CarouCenter").width();
  this.TotalPage = this.NbCover/this.NbByPage;
  if( this.TotalPage != Math.floor(this.TotalPage) ) this.TotalPage = Math.floor(this.TotalPage)+1;

  var t=this;
  $("#SQCover > .myCover img").hover(function(){ t.CoverHover(t,$(this)); },function(){ t.CoverOut(t); });
  $("#SQCover").append('<div id="SQCovSel1"></div><div id="SQCovSel2"></div><div id="SQCovSel3"></div><div id="SQCovSel4"></div>');
  $("#SQCovSel1, #SQCovSel2, #SQCovSel3, #SQCovSel4").hover(function(){ t.HoverBord(t); },function(){ t.OutBord(t); });

  $("#CarouRight, #CarouRight a").click(function(){ t.nextPage(); return false; });
  $("#CarouLeft, #CarouLeft a").click(function(){ t.prevPage(); return false; });
  if(this.TotalPage>1) $("#SQNavig").prepend('<div id="SQNavigSel"></div>');
  this.updateNavi();
};

SQCover.prototype.init2 = function(){
  this.CoverOut(this);
  this.setWidth();
  this.setCurrentNav();
};

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]+"()"); } };
var myCover; $(document).ready(function(){ myCover = new SQCover(); });
addOnLoad('myCover.init2');

