/**
 * Boutik Circus
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 *
 * @copyright   Copyright (c) 2010 Boutik Circus (http://www.boutik-circus.fr)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

Boutikcircus_Slideshow = Class.create({
  
  // Initialisation du Slideshow
  initialize: function(selector) {
    
    // On initialise les Slides
    $$(selector+' a').each(this.initTab.bind(this));
    
  },
  
  // Initialisation des Onglets
  initTab: function(el,i) {
    
      i++;
      var li = el.parentNode;
      
      // On désactive les liens des onglets
      el.href = 'javascript:void(0)';
      
      // On met les id sur les onglets
      li.id = 'slide_'+i;
      
      // On met les id sur les slides
      $$('.'+li.parentNode.className+'Content div').each(function(content,slide){
        slide++;
        content.id = 'slide_'+slide+'_contents';
      });
      
      if (i == '1') {
        
        // On active le premier onglet
        Element.addClassName(li.id, 'active');
        
        // Puis on lance le déroulement automatique
        this.initSlide(li);
      
      } else {
        
        // On désactive les autres onglets
        $(li.id+'_contents').hide();
        
        // On en profite pour ajouter la classe 'last' sur le dernier onglet
        if (i == li.parentNode.childElementCount) {
        Element.addClassName(li.id, 'last');
        }
        // Patch pour IE
        if (i == li.parentNode.childNodes.length) {
        Element.addClassName(li.id, 'last');
        }
      }
      
      // On met un observer sur les onglets
      el.observe('click', this.showContent.bind(this, el.parentNode));
  },

  // On affiche le slide correspondant à l'onglet
  showContent: function(tab) {
    
    // On arrête le déroulement automatique
    autoSlide.stop();
    
    // On traite chaque Slide et Onglet
    var ul = $(tab.parentNode);
    ul.getElementsBySelector('li', 'ol').each(function(el){
      var contents = $(el.id+'_contents');
      if (el==tab) {
        el.addClassName('active');
        Effect.Appear(contents, { duration:1, from:0.0, to:1.0 });
      } else {
        el.removeClassName('active');
        Effect.Fade(contents, { duration:1, from:1.0, to:0.0 });
      }
    });
    
    // On relance le déroulement automatique à partir du slide actif 
    this.initSlide(tab);
  },
  
  
  initSlide: function(el) {
    
    // On récupère le nombre de slides
    var length = el.parentNode.getElementsByTagName('li').length;
    
    // On récupère l'élement actif
    var i = el.id.substr(6);
    
    // On lance l'execution automatique
    autoSlide = new PeriodicalExecuter(function(pe) {
      if ( i >= length) {
        i = 0;
      }
      i++;
      var tab = $('slide_'+i);
      var ul = $(tab.parentNode);
      ul.getElementsBySelector('li', 'ol').each(function(el){
        var contents = $(el.id+'_contents');
        if (el==tab) {
          Effect.Appear(contents, { duration:1, from:0.0, to:1.0 });
          el.addClassName('active');
        } else {
          Effect.Fade(contents, { duration:1, from:1.0, to:0.0 });
          el.removeClassName('active');
        }
      });
    }, 5);
  }
  
});


