jQuery.noConflict();

jQuery(document).ready(function($){

    $('.externe').click(function(){window.open($(this).attr('href'));return false;});

    //

    var newsletter = 'votre email';
    $("#emailNewsletter").attr('value',newsletter);

    $("#emailNewsletter").attr('value',newsletter).focus(function () {
        if (this.value == newsletter) this.value = '';
    }).blur(function () {
        if (this.value == '') this.value = newsletter;
    });

    $('#menu>ul>li').hover(function() {
        $(this).addClass('jHover');
    }, function() {
        $(this).removeClass('jHover');
    });

    $('#menu>ul>li>a').focus(function() {
        $('#menu>ul>li').removeClass('jHover');
        $(this).parent().addClass('jHover');
    });

    $('#footer .btFooter').click(function() {
        $("#menuCC").slideToggle();
        this.blur();
        return false;
    });

    $('#content .blocContent .offresEmploi>li').addClass('fermer').children(".intitule").click(function() {
        $(this).next().slideToggle("normal", function() {$(this).parent().toggleClass('fermer');});
        this.blur();
        return false;
    });

    /* Lightbox **/
    
    $(".btLightbox").click(function(){
        $("#overlayLightbox").show();
        $("#"+$(this).attr('id')+"Lightbox").show();
        this.blur();
        return false;
    });
    
    $(".lightBox .fermer a").click(function(){
        $("#overlayLightbox").hide();
        $(this).parent().parent().hide();
        this.blur();
        return false;
    });
    
    /* Gestion des Carroussels */

    var animationActive = false;

    $("#content .carroussel").each(function() {

        // initialisation variables
        var item = $(this).find(".slider .item");
        var marge = item.outerWidth(true);
        var vitesse = 700;

        // on determine la taille du slider
        item.parent().css("width",((item.length + 1) * marge)+"px");

        if (item.is("div")) vitesse = 1000;

        $(this).find("a.prec").click(function(){

            if (!animationActive)
            {
                currentSlider = $(this).parent().find(".slider");
                animationActive = true;

                if ($(this).parent().is('.fondu')) {
                    currentSlider.find(".item:first").animate({"opacity": 0}, vitesse, "swing", function() {
                        currentSlider.find(".item:last").clone(true).insertBefore(currentSlider.find(".item:first"));
                        currentSlider.find(".item:first").css("opacity", 0).animate({"opacity": 1}, vitesse, "swing").next().css("opacity", 1);
                        currentSlider.find(".item:last").remove();
                        animationActive = false;
                        if ($(currentSlider).parent().parent().parent().is('.blocActus')){
                          changeTitleBlocActusIfNecessary(currentSlider.find(".item:first"));
                        }
                    });
                }
                else {
                    currentSlider.find(".item:last").clone(true).insertBefore(currentSlider.find(".item:first"));
                    currentSlider.css("left", "-"+marge+"px");
                    currentSlider.animate({"left": "+="+marge+"px"}, vitesse, "swing", function() {
                        currentSlider.find(".item:last").remove();
                        animationActive = false;
                    });
                }
            }
            
            this.blur();
            return false;
        });

        $(this).find("a.suiv").click(function(){

            if (!animationActive)
            {
                currentSlider = $(this).parent().find(".slider");
                animationActive = true;

                if ($(this).parent().is('.fondu')) {
                    currentSlider.find(".item:first").clone(true).insertAfter(currentSlider.find(".item:last"));
                    currentSlider.find(".item:first").animate({"opacity": 0}, vitesse, "swing", function() {
                        $(this).next().css("opacity", 0).animate({"opacity": 1}, vitesse, "swing");
                        $(this).remove();
                        animationActive = false;
                        if ($(currentSlider).parent().parent().parent().is('.blocActus')){
                          changeTitleBlocActusIfNecessary(currentSlider.find(".item:first"));
                        }
                    });
                }
                else {
                    currentSlider.find(".item:first").clone(true).insertAfter(currentSlider.find(".item:last"));
                    currentSlider.animate({"left": "-="+marge+"px"}, vitesse, "swing", function() {
                        currentSlider.find(".item:first").remove();
                        currentSlider.css("left", "0px");
                        animationActive = false;
                    });
                }
            }
            this.blur();
            return false;
        });

        // Autoslide
        if ($(this).is('.autoSlide')){

            var btSuiv = $(this).find("a.suiv");

            if ($(this).is('.fondu')) {
                var interval = setInterval(function() {btSuiv.click()}, 10000);
            }
            else {
                var interval = setInterval(function() {setTimeout(function() {btSuiv.click()}, 2500 )}, 5000);
            }

            $(this).bind('mouseover', function(){clearInterval(interval);});
        }

    });


    // Fonction pour defilement des logos partenaires

    var currentPart = $('#content .blocBienvenu .boutiqueLogo img:first');

    function defilPart() {

        currentPart.fadeOut("normal", function() {

            if ( $(this).next().is('img') ) {
                currentPart = $(this).next();
            }
            else {
                currentPart = $(this).parent().find('img:first');
            }

            currentPart.fadeIn();
        });
    }

    setInterval(function() {defilPart()}, 3000);


    /***********************************************************************
     * AJOUTS
     ***********************************************************************/

    // Ajax sur carroussel des boutiques sur la home

    $('.blocBoutiques .carroussel .slider .item').click(function(){
      $('.blocBoutiques .carroussel .slider .current').removeClass('current');
      $('.blocBoutiques #infoBoutiques').load($(this).attr('rel'));
      $(this).addClass('current');
      return false;
    });

    // Ajax sur carroussel des bons plans sur la page Bons Plans

    $('.carrousselBonsPlans .slider .item').click(function(){
      $('.blocContent').eq(1).find('.content').load($(this).attr('rel'));
      return false;
    });

    // Pagination

    $('.pagination a').click(function(){
      $('.pagination a.on').removeClass('on');
      $(this).addClass('on');
      $('.pages').hide();
      $('#page'+$(this).attr('rel')).show();
      return false;
    });

    // Antispam sur formulaires

    $('#inscriptionNewsletterForm').find('p.text:first').hide();
    $('#contactForm').find('p.text:first').hide();
    $('#inscriptionEvenementForm').find('p.text:first').hide();
    $('#recommanderAmiForm').find('p.text:first').hide();

    // Bouton "ajouter aux favoris"

    $('#ajouterFavoris').jFav();

    // Votes du concours photo

    $('.openLightboxVoteConcoursPhoto').click(function(){

      $('#voteConcoursPhotoLightbox .contentLightbox').load('/concours-photo/lightboxVote-'+$(this).attr("rel")+'.html', function(){
        initLightBox('voteConcoursPhoto');

        $('.doVoteConcoursPhoto').click(function(){
          var id = $(this).attr("rel");
          $(this).hide();
          $.ajax({
            type: 'POST',
            url: "/concours-photo/vote-"+id+".html",
            success: function(data) {
              $('#nombreVotesPhoto-'+id).html(data);
            }
          });
        });
        
      });
    });

    // Sondage

    $('.doReponseSondage p.submit input').click(function(){
      
      var data = $(this).parent().parent('form').serialize();
      var p = $(this).parent('p.submit');
      $(p).html('Enregistrement...');
      $(p).parent().find('input').attr('disabled', 'disabled');

      $.ajax({
        type: 'POST',
        url: "/frontend_dev.php/sondage/repondre.html",
        data: data,
        success: function(message) {
          $(p).html(message);
        },
        error: function() {
          $(p).html('Une erreur est survenue.');
        }
      });
      
      return false;
    });
});


/***********************************************************************
* AJOUTS
***********************************************************************/


function initLightBox(name)
{
  jQuery("#overlayLightbox").show();
  jQuery("#"+name+"Lightbox").show();
}


function changeTitleBlocActusIfNecessary(elem)
{
  var theme = jQuery('#spanTheme').html();
  if (jQuery(elem).hasClass('sondage'))
  {
    jQuery('.blocActus > h2 > img').attr('width', 214).attr('src', '/themes/'+theme+'/images/titre-sondage-votre-avis.png').attr('alt', 'Sondage ! Donnez-nous votre avis');
  }
  else if (jQuery(elem).hasClass('actualite'))
  {
    jQuery('.blocActus > h2 > img').attr('width', 167).attr('src', '/themes/'+theme+'/images/titre-actualites-de-votre-centre.png').attr('alt', 'Actualit�s de votre centre');
  }
}
