/*
**  Utilisation de la librairie jQuery
**  http://docs.jquery.com/Downloading_jQuery#Download_jQuery
*/

jQuery.noConflict();

    function pageselectCallback1(page_index, jq){
      var items_per_page = 6;
      var max_elem = Math.min((page_index+1) * items_per_page, jQuery('#hiddenresult1').find('.result1').length);
      var newcontent = '';

      // Iterate through a selection of the content and build an HTML string
      for(var i=page_index*items_per_page;i<max_elem;i++)
      {
        newcontent += jQuery('#hiddenresult1').find('.result1:eq('+i+')').html();
      }

      // Replace old content with new content
      jQuery('#Searchresult1').html(newcontent);

      // Prevent click eventpropagation
      return false;
    }

jQuery(function($) {

    //Gestion du clearInput (vide les champs texte)
    function clearInput(_item) {
        $(_item).focus(function() {
            if(this.value == this.defaultValue) {
                this.value = "";
            }
        }).blur(function() {
            if(!this.value.length) {
                this.value = this.defaultValue;
            }
        });
    }
    clearInput('.clearInput');
    
    //Gestion de la navigation principale (mainNav)
    $('#mainNav > li > a').bind('click', function(e) {
       if ($(this).parent().hasClass('directLink') == false)
       {
            if($(this).is('.open') == false) {
            $('#mainNav > li > a.open').removeClass('open actif').next().slideUp();
            $('#mainNav > li > a.actif').removeClass('actif');
            $(this).addClass('open actif').next().slideDown();
        } 
        else if($(this).is('.open')) {
            $(this).removeClass('open actif').next().slideUp();
        }
        
        if($(this).is('actif')) {
                $(this).addClass('open').next().slideDown();            
        }
        
        
            e.preventDefault();
       }
        
    });
    
    //Gestion des onglets (page thematique foire)
    $('#navOnglets a').bind('click', function(e) {
        var idOnglet = $(this).attr('href');
        if($(this).is('.actif') == false) {
            $('#navOnglets > a.actif').removeClass('actif');
            $('#content .article').find('.ongletActif').removeClass('ongletActif').hide();
            $(this).addClass('actif');
            $('#content .article '+ idOnglet).addClass('ongletActif').show();
        }
        e.preventDefault();
    });
    
    //GESTION DES CAROUSELS
    
    //Gestion du carousel animation (page d'accueil)
    $('#carouselAnim').carouselInfini({ 
        manual: true,
        auto: true,
        speed: 500,
        frequency: 3000,
        itemVisibles: 1
    });
    
    //Gestion du carousel Pub (#aside)
    $('#carouselPub').carouselInfini({ 
        manual: false,
        auto: true,
        speed: 500,
        frequency: 8000,
        itemVisibles: 1
    });
    
    /**
     * Callback function that displays the content.
     *
     * Gets called every time the user clicks on a pagination link.
     *
     * @param {int} page_index New Page index
     * @param {jQuery} jq the container with the pagination links as a jQuery object
     */
    function pageselectCallback(page_index, jq){

      var items_per_page = 6;
      var max_elem = Math.min((page_index+1) * items_per_page, $('#hiddenresult').find('.result').length);
      var newcontent = '';

      // Iterate through a selection of the content and build an HTML string
      for(var i=page_index*items_per_page;i<max_elem;i++)
      {
        newcontent += $('#hiddenresult').find('.result:eq('+i+')').html();
      }

      // Replace old content with new content
      $('#Searchresult').html(newcontent);


      // Prevent click eventpropagation
      return false;
    }

    function pageselectCallback2(page_index, jq){

      var items_per_page = 6;
      var max_elem = Math.min((page_index+1) * items_per_page, $('#hiddenresult2').find('.result2').length);
      var newcontent = '';

      // Iterate through a selection of the content and build an HTML string
      for(var i=page_index*items_per_page;i<max_elem;i++)
      {
        newcontent += $('#hiddenresult2').find('.result2:eq('+i+')').html();
      }

      // Replace old content with new content
      $('#Searchresult2').html(newcontent);


      // Prevent click eventpropagation
      return false;
    }

    /**
     * Initialisation function for pagination
     */
    function initPagination(request, json)
    { 
      if($('#Pagination').length)
      {
        // count entries inside the hidden content
        var num_entries = jQuery('#hiddenresult div.result').length;
        // Create content inside pagination element
        $("#Pagination").pagination(num_entries, {
            callback: pageselectCallback,
            items_per_page:6 // Show only one item per page
        });
      }


      if($('#Pagination2').length)
      {
        var num_entries2 = jQuery('#hiddenresult2 div.resul2t').length;
        // Create content inside pagination element
        $("#Pagination2").pagination(num_entries2, {
            callback: pageselectCallback2,
            items_per_page:6 // Show only one item per page
        });
      }
    }

    $('#inscription_atelier_jour').change(function(){
      if($('#inscription_atelier_jour option:selected').text() == 'Conseils et relooking maquillage, coiffure et beauté')
      {
        $('#choixNbPers label:gt(1)').hide();
        $('#choixNbPers input:gt(1)').hide();
      }
      else
      {
        $('#choixNbPers label:gt(1)').show();
        $('#choixNbPers input:gt(1)').show();
      }
    });

    $('#atelierForm').submit(function(){
      var erreur = 0;
      var horaire = '';
      var nbPers = '';

      var date = $('#inscription_atelier_jour').val();
      if($('#inscription_atelier_nom').val() == '')
      {
        erreur = 1;
      }

      if($('#inscription_atelier_prenom').val() == '')
      {
        erreur = 1;
      }

      if($('#inscription_atelier_tel').val() == '')
      {
        erreur = 1;
      }

      if($('#inscription_atelier_email').val() == '')
      {
        erreur = 1;
      }

      if($('#t1').not(':checked').length && $('#t2').not(':checked').length && $('#t3').not(':checked').length)
      {
        erreur = 1;
      }
      else
      {
        if($('#t1:checked').length)
        {
          horaire = '10_13';
        }
        else if($('#t2:checked').length)
        {
          horaire = '15_18';
        }
        else if($('#t3:checked').length)
        {
          horaire = '19_22';
        }
      }

      if($('#n1').not(':checked').length && $('#n2').not(':checked').length && $('#n3').not(':checked').length && $('#n4').not(':checked').length && $('#n5').not(':checked').length && $('#n6').not(':checked').length && $('#n7').not(':checked').length && $('#n8').not(':checked').length)
      {
        erreur = 1;
      }
      else
      {
        if($('#n1:checked').length)
        {
          nbPers = 1;
        }
        else if($('#n2:checked').length)
        {
          nbPers = 2;
        }
        else if($('#n3:checked').length)
        {
          nbPers = 3;
        }
        else if($('#n4:checked').length)
        {
          nbPers = 4;
        }
        else if($('#n5:checked').length)
        {
          nbPers = 5;
        }
        else if($('#n6:checked').length)
        {
          nbPers = 6;
        }
        else if($('#n7:checked').length)
        {
          nbPers = 7;
        }
        else if($('#n8:checked').length)
        {
          nbPers = 8;
        }
      }

      if(erreur == 0)
      {
        $.ajax({
          type: "post",
          url: "/content/returnErreurAtelier",
          data: {date: date, horaire: horaire, nbPers: nbPers},
          success: function(content){
            if(content)
            {
              $('#erreur').html("<p style='color: red; text-align: center'>Il n'y a plus de place disponible pour cette session.</p>");
              return false;
            }
            else
            {
              return true;
            }
          }
        });
      }
      else
      {
        $('#erreur').html('<p style="color: red; text-align: center">Merci de renseigner les champs obligatoires signalés par *</p>');
        return false;
      }
    });

    initPagination();
    
    if($('.overlayVideoAccueil').length)
    {
      $('#overlay').show();
      $('#gallery').show();
    }

    $('#overlay').click(function(){
      $('#overlay').hide();
      $('#gallery').hide();
    });

    $('#btCloseLightBox').click(function(){
      $('#overlay').hide();
      $('#gallery').hide();
    });

    /*$('#logoFdf').mouseover(function(){
      $('#logoFdf').attr('src', '/images/img/logo-fdf-gris.jpg');
    });

    $('#logoFdf').mouseout(function(){
      $('#logoFdf').attr('src', '/images/img/logo-fdf-couleur.jpg');
    });*/

    $('#logoFs').mouseover(function(){
      $('#logoFs').attr('src', '/images/img/logo-fs-gris.jpg');
    });

    $('#logoFs').mouseout(function(){
      $('#logoFs').attr('src', '/images/img/logo-fs-couleur.jpg');
    });


    if ( $('#prehome').length )
    {
        $('#prehome,#overlayPrehome').show();
        //$('select').hide();
        $('#overlayPrehome, #closePrehome').click(function(){
            $('#prehome,#overlayPrehome').hide();
            //$('select').show();
            return false;
        });
    }

}); /* jQuery end !*/
