/**
 * @author CD
**/
CCVU = {

  initialized: false,
  pageActive: null,

  onContentDisplayed: function() {
    CCVU.pageActive = $("#col970 .page:visible");
  },

  onContentLoaded: function() {
    if (!CCVU.initialized) {
      if (window.addthis != undefined && window.addthis.init != undefined)
        window.addthis.init();
      $(".with-tooltip").simpletooltip();
      $(".nyroModal").nyroModal();
      CCVU.initialized = true;
    } else {
      CCVU.onContentDisplayed();
      if (window.addthis != undefined && window.addthis.init != undefined) {
        window.addthis = null;
        $("head").append('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#domready=1"></script>');
      }
      $(".with-tooltip",CCVU.pageActive).simpletooltip();
      $(".nyroModal",CCVU.pageActive).nyroModal();
      // Statistiques du module de référencement
      // Appel de Google Analytics
      var pageAlias = getPageAlias(true);
      _gat._getTracker(site.analytics_tag)._trackPageview('/'+site.name+'/'+language+'/'+pageAlias);
      TZR.referer(site.name+'/'+pageAlias,site.lang.toUpperCase());
    }

  },

  toggleInformations: function(lineID) {
    $(".fiche"+lineID+" .info"+lineID, CCVU.pageActive).toggleClass('hidden');
    if ($(".fiche"+lineID+" .son_contenu", CCVU.pageActive).attr('className') == 'son_contenu')
      $(".fiche"+lineID+" .son_contenu", CCVU.pageActive).attr('className','son_contenu-deroule');
    else
      $(".fiche"+lineID+" .son_contenu-deroule", CCVU.pageActive).attr('className','son_contenu');
    if (!jQuery.mobile)
      $.scrollTo($(".fiche"+lineID,CCVU.pageActive),500, {axis: 'y'});
    return false;
  },

  showCategorie: function(h3) {
    if (jQuery.mobile) return true;
    var show = h3.parentNode.className != 'clicked';
    $(".verticalaccordion li", CCVU.pageActive).removeClass('clicked');
    if (show) {
      h3.parentNode.className = 'clicked';
      $.scrollTo($(".verticalaccordion li.clicked", CCVU.pageActive), 500, {axis: 'y'});
    }
    return false;
  },

  top: function() {
    $.scrollTo("#page",500, {axis: 'y'});
    return false;
  },

  filtrerListeSimple: function(id) {
    var filtres = [];
    $("#form_"+id+" select").each(function(){
      filtres.push("#form_"+id+" select."+$(this).attr('name'));
    });
    $("#form_"+id+" input").each(function(){
      filtres.push("#form_"+id+" input."+$(this).attr('name')+":checked");
    });
    CCVU.filtrerFiches({
      liste: "#liste_"+id,
      total: "#total_"+id,
      filtres: filtres
    });
    return false;
  },


  filtrer: function(id) {
    $("#no-results_"+id).hide();
    // group/accordeon commerce
    $("#liste_"+id+" .verticalaccordion li").removeClass('clicked').hide();
    // les fiches
    var fiches = $("#liste_"+id+" .fiche");
    fiches.hide();
    // filtrer sur des select
    $("#form_"+id+" select").each(function(){
      if (this.value.length)
        fiches = fiches.filter("."+this.value);
    });
    // filtrer sur les checkbox
    var _filtre = [];
    $("#form_"+id+" input:checkbox:checked").each(function(i, e){
      _filtre.push('.'+e.value);
    });
    if (_filtre.length)
      fiches = fiches.filter(_filtre.join(','));
    // filtrer sur datepicker (agenda)
    if ($("#datepicker_"+id).length) {
      var selectedDate = $("#datepicker_"+id).datepicker('getDate');
      $("#date-debut_"+id).html($.datepicker.formatDate($.datepicker.regional[language].dateFormat, selectedDate));
      fiches = fiches.filter(function(index,elem){
        return $.datepicker.parseDate('yy-mm-dd', $("input.data",elem).data('end')) >= selectedDate;
      });
    }
    fiches.show().parents(".verticalaccordion li").show();
    $("#total_"+id).html(fiches.length);
    if (!fiches.length)
      $("#no-results_"+id).show();
    return false;
  },

  // Fonction générique permettant de filtrer un ensemble de fiches
  filtrerFiches: function(param) {

    // On efface les précédents résultats et on affiche le texte d'attente
    var liste = $(param.liste, CCVU.pageActive);
    $(".no-results", liste).hide();

    // On cache toutes les fiches
    var classeFiltre = ".fiche";
    var fiches = $(classeFiltre, liste).hide();

    $.each(param.filtres, function(i, filterSelector){
      $(filterSelector, CCVU.pageActive).each(function(j, filterElem) {
        if (this.value && this.value.length)
          classeFiltre += '.' + this.value;
      });
    });

    // On affiche les éléments correspondants
    var fichesSelectionnees = $(classeFiltre, liste).show();//slideDown();

    // On affiche le nombre d'éléments filtrés
    $(param.total, CCVU.pageActive).html(fichesSelectionnees.length);

    // On supprime le texte d'attente et on affiche un texte si pas de résultats
    if (!fichesSelectionnees.length)
      $(".no-results", liste).show();

    return fichesSelectionnees;
  }
};

