/* Fonctions pour le carousel de promos sur la page d'accueil */

function lancerCarousel(noPromo) {
  btnCtrl.className = classeBtnPause;
  btnCtrl.title = msgBtnPause;
  btnCtrl.innerHTML = msgBtnPause;
  if (noPromo > nbHooks)
    noPromo = 1;
  afficherPromo(noPromo);
  t = window.setTimeout("lancerCarousel("+(noPromo+1)+")",4000);
}

function afficherPromo(noPromo) {
  for (i=1; i<=nbHooks; i++) {
    idPromo = "promo-" + i;
    if (i==noPromo) document.getElementById(idPromo).className = "";
    else document.getElementById(idPromo).className = "hidden";
    btnPromo = "promo-btn" + i;
    if (i==noPromo) document.getElementById(btnPromo).className = "select";
    else document.getElementById(btnPromo).className = "";
  }
  debut = noPromo+1;
}

function interrompreCarousel() {
 window.clearTimeout(t);
  btnCtrl.className = classeBtnPlay;
  btnCtrl.title = msgBtnPlay;
  btnCtrl.innerHTML = msgBtnPlay;
}

function choisirPromo(noPromo) {
  afficherPromo(noPromo);
  interrompreCarousel();
}

function controlerCarousel() {
  if (btnCtrl.className == classeBtnPause) {
    interrompreCarousel();
  }
  else {
    lancerCarousel(debut);
  }
}


// carousel Zone Interactive
function Carousel(params) {

 this.itemWidth = params.width;
 if(this.itemWidth % 10 != 0)
 this.itemWidth -= this.itemWidth % 10;
 
 var carousel = document.getElementById(params.carouselDiv);
 carousel.className = "";
 //carousel.style.width = (this.itemWidth + 46) + "px";
 
 //Cadre du carousel
 this.area = document.createElement("div");
 this.area.className = "cadre";
 this.area.style.width = (this.itemWidth) + "px";
 this.area.style.height = params.height + "px";
 carousel.appendChild(this.area);
 
//Fleche gauche
 this.precedent = document.createElement("div");
 this.precedent.style.top = (params.height/2) -17 + "px";
 var precedent_a = document.createElement("a");
 var precedent_span = document.createElement("span");
 this.precedent.appendChild(precedent_a);
 precedent_a.appendChild(precedent_span);
 precedent_a.appendChild(document.createTextNode("<"));
 this.precedent.className = "flecheGauche";
 this.precedent.carousel = this;
 this.precedent.onclick = function () { this.carousel.left(); }
 carousel.appendChild(this.precedent);
 
 //Fleche droite
 this.suivant = document.createElement("div");
 this.suivant.style.left = params.width - 6 + "px";
 this.suivant.style.top = (params.height/2) - 17 + "px";
 var suivant_a = document.createElement("a");
 var suivant_span = document.createElement("span");
 this.suivant.appendChild(suivant_a);
 suivant_a.appendChild(suivant_span);
 suivant_a.appendChild(document.createTextNode(">"));
 this.suivant.className = "flecheDroite";
 this.suivant.carousel = this;
 this.suivant.onclick = function () { this.carousel.right(); }
 carousel.appendChild(this.suivant);

 //Div horizontale avec tous les items
 var line = document.createElement("div");
 
 //line.className = "ligne";
 this.area.appendChild(line);

 //on parcourt tous les div pour retrouver les items q'on va afficher dans le carousel
 var items = carousel.getElementsByTagName("div");
 var re = new RegExp("item_\\d+");
 this.count = 0;
 var itemsArray = new Array();

 for(var i = 0; i < items.length; i++) {
  if(items[i].id.match(re))
   itemsArray.push(items[i]);
 }

 for(var i = 0; i < itemsArray.length; i++) {
 var item = itemsArray[i];
 item.className = "item";
 item.style.width = (this.itemWidth - 4) + "px";
 line.appendChild(item);
 }
 
 this.count = itemsArray.length;
 
 line.style.width =  (this.count) * this.itemWidth + "px";
 
 if(params.defaultItem)
  this.index = params.defaultItem - 1;
 else
  this.index = (Math.round(Math.random() * 100)) % this.count;

 this.area.scrollLeft = this.index * this.itemWidth;
 
 this.interval;//pour animation

 //variable utiliser pour l'animation
 this.scrollFrom;
 this.scrollTo;
 this.scrolInterval;
 
 this.checkFlashes();
}

Carousel.prototype.checkFlashes = function () {
 if(this.index == 0) {
  this.precedent.className = "flecheGaucheInactive";
  this.precedent.onclick = function () {}
 } else {
  this.precedent.className = "flecheGauche";
  this.precedent.onclick = function () { this.carousel.left(); }
 }
  
 if(this.index == this.count - 1) {
  this.suivant.className = "flecheDroiteInactive";
  this.suivant.onclick = function () {}
 } else {
  this.suivant.className = "flecheDroite";
  this.suivant.onclick = function () { this.carousel.right(); }
 }
}

Carousel.prototype.stop = function () {
 clearTimeout(this.interval);
}

Carousel.prototype.left = function () {
 this.stop();
 this.scrollLeft();
}

Carousel.prototype.right = function () {
 this.stop();
 this.scrollRight();
}

Carousel.prototype.scrollLeft = function () {
 if(this.index > 0) {
  this.scrollFrom = this.index * this.itemWidth;
  this.scrollTo = (this.index - 1) * this.itemWidth;
  this.scrolInterval = -10;
  this.animation();
  this.index--;
 }
 this.checkFlashes();
}

Carousel.prototype.scrollRight = function () {
 if(this.index < this.count - 1) {
  this.scrollFrom = this.index * this.itemWidth;
  this.scrollTo = (this.index + 1) * this.itemWidth;
  this.scrolInterval = 10;
  this.animation();
  this.index++;
 }
 this.checkFlashes();
}

Carousel.prototype.animation = function () {
 if(this.scrollFrom == this.scrollTo) {
  clearTimeout(this.interval);
 } else {
  this.scrollFrom += this.scrolInterval;
  this.area.scrollLeft = this.scrollFrom;
  this.interval = setTimeout(createContextFunction(this, "animation"), 10);
 }
}

function createContextFunction(context, method) {
 return (function(){
  eval("context." + method + "()");
  return false;
    });
}

