/**
 * itm.prospectus.js
 * fichier js pour la partie prospectus de la home
 *
 * @package intermarche
 * @subpackage js
 * @author     2S3i
 * @version    SVN: $Id: itm.prospectus.js 103 2009-01-12 10:01:37Z bozec $
 */

/*
 *  interval
 *  by Steven Wicklund 
 *  A setInterval wrapper for jQuery
 *  So this works for 1 timer interval, but the currentlyExecuting
 *  singleton will likely cause issues
 *  if there is more than one interval per page...
 */
jQuery.extend({
  interval:function(frequency, fn) {
    this.currentlyExecuting = false;
    setInterval(function() {$.onTimerEvent(fn);}, frequency * 1000);
  },
 /*
     * onTimerEvent courtesy Prototype PeriodicalExecuter()
     * (c) 2005 Sam Stephenson 
     * (MIT license see:  http://prototype.conio.net/)
     */
  onTimerEvent: function(callback) {
    if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        callback();
      } finally {
        this.currentlyExecuting = false;
      }
    }
  }
});
 
if (ITM === undefined) { var ITM = {} };

(function($) {
  ITM.slide = {
    // définition du temps d'affichage par défaut
    timer: 5,
    // définition du nombre maximum d'éléments à l'écran
    maxElements: 4,
    // cursor
    cursor: '<span id="pagelet_home_prospectus_pager_cursor"></span>',
    // défintion du selecteur pour les images
    selectorPicture: '#pagelet_home_prospectus_content p.item',
    // définitin du sélecteur pour les éléments de pagniation
    selectorPager: '#pagelet_home_prospectus_pager td',
    selectorPagerLink: '#pagelet_home_prospectus_pager td a',
    // boolean si oui ou non on démarre le slide
    activeSlideShow: null,
    // index de la section courante
    currentSectionIndex: null,
    // index et élément courant
    currentIndex: null,
    // index et élément suivant
    nextIndex: null,
    
    construct: function() {
      // pas d'élément courant préselectionné, on peut initialiser les éléments normalement
      ITM.slide.displayCurrentPicture(ITM.slide.currentIndex);
      ITM.slide.displayCurrentPagerSection(ITM.slide.currentIndex);
      // si plus d'un élément on met à true l'activation du slide
      if ($(ITM.slide.selectorPager).length > 1) {
        ITM.slide.activeSlideShow = true;
      }
    },
    
    displayItem: function(index) {
      // on cache l'element courant
      $(ITM.slide.selectorPicture).eq(ITM.slide.currentIndex).css({
        'display': 'none',
        'opacity': 0
      });
      // on vérifie si on doit changer de lot de pager 
      if (ITM.slide.isNewPager(index)) {
        ITM.slide.displayCurrentPagerSection(index);
      }
      // on affiche le nouvel élément
      $(ITM.slide.selectorPicture).eq(index).css({
        'display': 'block',
        'opacity': 1
      });
      ITM.slide.displayCursor(index);
      ITM.slide.currentIndex = index;
      // on vérifie qu'il n'y ait pas plus d'un élément affiché
      ITM.slide._controlDisplay();
    },
    
    cycle: function(index) {
      ITM.slide.nextIndex = (ITM.slide.currentIndex + 1) % $(ITM.slide.selectorPicture).length;
      if (ITM.slide.activeSlideShow == true) {
        $(ITM.slide.selectorPicture).eq(ITM.slide.currentIndex).hide('fast', ITM.slide._fadingIn);
      }
    },
    
    _fadingIn: function() {
      ITM.slide.displayItem(ITM.slide.nextIndex);
    },
    
    displayCurrentPicture: function(currentIndex) {
      selector = $(ITM.slide.selectorPicture);
      selector.hide();
      selector.eq(currentIndex).show();
    },
    
    isNewPager: function(index) {
      newSectionIndex = Math.floor(index / ITM.slide.maxElements);
      if (newSectionIndex != ITM.slide.currentSectionIndex) {
        return true;
      }
      return false;
    },
    
    displayCurrentPagerSection: function(indexCurrent) {
      selector = $(ITM.slide.selectorPager);
      selector.hide();
      // définition du lot à utiliser
      ITM.slide.currentSectionIndex = Math.floor(indexCurrent / ITM.slide.maxElements);
      indexStart = ITM.slide.currentSectionIndex * ITM.slide.maxElements;
      indexEnd = indexStart + ITM.slide.maxElements;
      // affichage du bon lot de pagination
      selector.slice(indexStart, indexEnd).show();
      ITM.slide.displayCursor(indexCurrent);
    },
    
    getCurrentPagerIndex: function() {
      currentPagerIndex = 0;
      $(ITM.slide.selectorPager).each( function(index, el) {
        if ($(this).hasClass('current_pager')) {
          currentPagerIndex = index;
        }
      });
      return currentPagerIndex;
    },
    
    displayCursor: function(index) {
      // suppression de l'ancien curseur
      $('#pagelet_home_prospectus_pager_cursor').remove();
      // calcul de la place du nouveau
      nbPart = $(ITM.slide.selectorPager + ':visible').length;
      largeurSection = 500 / nbPart;
      $(ITM.slide.selectorPager).css({
        'width': largeurSection + 'px'
      });
      relativeIndex = index % ITM.slide.maxElements;
      leftPosition = (largeurSection * relativeIndex) + (largeurSection / 2) - 4.5;
      // affichage du nouveau
      $(ITM.slide.selectorPager).removeClass('current');
      $(ITM.slide.selectorPager).eq(index).addClass('current');
      $(ITM.slide.selectorPager).eq(index).prepend(ITM.slide.cursor);
      $('#pagelet_home_prospectus_pager_cursor').css({
        'left': leftPosition + 'px'
      });
    },
    
    _controlDisplay: function() {
      if ($(ITM.slide.selectorPicture + ':visible').length > 1) {
        $(ITM.slide.selectorPicture + ':visible').eq(0).css({
          'display': 'none',
          'opacity': 0
        });
      }
    }
  };

  $(document).ready( function() {
    if ($(ITM.slide.selectorPager + '.current_pager').length == 0) {
      ITM.slide.currentIndex = ITM_START_INDEX;
      ITM.slide.construct();
      if (ITM.slide.activeSlideShow == true) {
        $.interval(ITM.slide.timer, ITM.slide.cycle);
      }
      // ajout des events sur les éléments cliquables du pager
      $(ITM.slide.selectorPagerLink).each( function(index, element) {
        $(element).click( function(event) {
          event.preventDefault();
          ITM.slide.activeSlideShow = false;
          ITM.slide.displayItem(index);
        });
      });
      $('#pagelet_home_prospectus_items_prev p a').click( function(event) {
        event.preventDefault();
        ITM.slide.activeSlideShow = false;
        previousIndex = (ITM.slide.currentIndex - 1);
        if (previousIndex >= 0) {
          ITM.slide.displayItem(previousIndex);
        }
      });
      $('#pagelet_home_prospectus_items_next p a').click( function(event) {
        event.preventDefault();
        ITM.slide.activeSlideShow = false;
        nextIndex = (ITM.slide.currentIndex + 1);
        if (nextIndex < $(ITM.slide.selectorPicture).length) {
          ITM.slide.displayItem(nextIndex);
        }
      });
    }
    else {
      // on rechercher l'index du pager courant
      ITM.slide.currentIndex = ITM.slide.getCurrentPagerIndex();
      ITM.slide.displayCurrentPicture(ITM.slide.currentIndex);
      ITM.slide.displayCurrentPagerSection(ITM.slide.currentIndex);
    }
  });
})(jQuery);