function map() {
  var $carte = $('#carte');
  if ( $carte.length ) {

    var mymap = L.map('carte').setView([44.7365818, 4.9776488], 13);

    var greenIcon = L.icon({
        iconUrl: '/user/themes/lecampus/images/mappoint.svg',

        iconSize:     [38, 95], // size of the icon
        iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
        popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
    });


    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
    attribution: '© <a href="https://www.mapbox.com/about/maps/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback/" target="_blank">Improve this map</a></strong>',
    tileSize: 512,
    maxZoom: 18,
    zoomOffset: -1,
    id: 'mapbox/streets-v11',
    accessToken: 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw'
  }).addTo(mymap);

  	// L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  	// 	maxZoom: 18,
  	// 	id: 'mapbox.streets'
  	// }).addTo(mymap);

  	L.marker([44.7365818, 4.9776488], {icon: greenIcon}).addTo(mymap);

  	function onMapClick(e) {
  		popup
  			.setLatLng(e.latlng)
  			.setContent("You clicked the map at " + e.latlng.toString())
  			.openOn(mymap);
  	}
  	mymap.on('click', onMapClick);
  }
}

function filteritem() {
  var $card = $('.body-wrapper > .card');
  var $cat = $('.cat a');
  var $img = $('.body-wrapper .img');

  $(".filters .btn").click(function(e) {
    // e.preventDefault();

    var filter = $(this).attr("data-filter");
    var self = $('.body-wrapper');

    self.masonryFilter({
        filter: function () {
            if (!filter) return true;
            return $(this).attr("data-filter") == filter;
        }
    });
});

  var $grid = $('#archive .body-wrapper, #calendrier .body-wrapper').masonry({
    // columnWidth: 200,
    itemSelector: '.card',
    gutter: 40,
    transitionDuration: '0.2s'
  });


  $grid.imagesLoaded().progress(function() {
      $grid.masonry();
  });

}

function slide() {
  $('.gal').bxSlider({
    responsive: true,
    infiniteLoop: true,
    preloadImages:'visible',
    pager:false,
    auto:true
  });


  $('.gal-salles').slidesjs({
    width: 800,
    height: 533,
    navigation: true,
    pagination: false
  });

}

function links() {
  var $a = $('.dropmenu > ul > li > .no-click');
  var $map = $('a#carte');

  $a.on('click', function(e) {
    e.preventDefault();
  })

  $map.on('click', function(e) {
    e.preventDefault();
  })
}

function clickbutton() {
  var $button = $('#devis-form .radio label');

  $button.on('click', function(){
    $(this).parents('.form-data').find('.check').removeClass('check');
    $(this).parent('.radio').addClass('check');
  });

}

function clicksenddevis() {
  var $ok = $('.ok');
  var $send = $('#send-valide');

  $ok.on('click', function () {
    $(this).parent('#send-valide').remove();
  })

}

function addclass() {
  var $map = $('a#carte');

  $map.parents('.content_s').addClass('map')

  var $reco_click = $('#reco .title');
  var $reco_txt = $('#reco .txt');

  $reco_click.on('click', function() {
    $reco_txt.toggleClass('open');
    $(this).toggleClass('open');
    $('html, body').animate({scrollTop:$('#reco').position().top}, 'slow');
  })

  var $out = $('.opt-out .txt');

  $out.on('click', function() {
    $(this).parent('.opt-out').toggleClass('open');
  })
}

function mapsalles() {
  var $salles = $('.fond svg path');
  var $content = $('.content .content_salle');
  arrayid = [];
  $('.content .content_salle#salle_trois_becs').css("visibility", "visible");
  $('.fond svg path#salle_trois_becs').css("fill", "#0093a3");

  $salles.on('click', function(idsalle) {
    var idsalle = this.id;

    arrayid.push(idsalle);
    $salles.css("fill", "white");
    $(this).css("fill", "#0093a3");
    $('.content .content_salle').css("visibility", "hidden");
    $('.content .content_salle#'+arrayid).css("visibility", "visible");
    arrayid.length = 0;
  });

  var $close = $('.content_salle .header-salles img');
  $close.on('click', function () {
    $(this).parents('.content_salle').css("visibility", "hidden");
    $salles.css("fill", "white");
  })
};

function burger() {
  $('.navTrigger').click(function(){
    $(this).toggleClass('active');
    $('.dropmenu').toggleClass('active');
  });
}

function ajaxformulaire() {
  $(".ok").on('click', function () {
    $(this).parent('#send-valide').remove();
    location.reload();
  });

  var form = $('#devis-form');
  form.submit(function(e) {
      // prevent form submission
      e.preventDefault();

      // submit the form via Ajax
      $.ajax({
          url: form.attr('action'),
          type: form.attr('method'),
          dataType: 'html',
          data: form.serialize(),
          success: function(result) {
              // Inject the result in the HTML
              $('#form-result').html(result);
          }
      });
  });

}


function open_pack() {
  var $pack = $(".nos_pack .pack .__thumb");
  var nbrDiv = $pack.length;
  var widtWind = $( window ).width();

  if(widtWind <= 530){
    var count = 1;
  } else if (widtWind <= 1024) {
    var count = 2;
  } else if (widtWind <= 1200) {
    var count = 3;
  } else {
    var count = 4;
  }
  $pack.on('click',function(e) {

    var href = $(this).find('a').attr('href') ;
    if ( href == '#') {
      e.preventDefault();
    }

    var idThis = $(this).parent('.pack').attr('id');
    var $more = $('.__more#' + idThis);

    $('.__more').removeClass('visible');
    $('.triangle').removeClass('visible');

      var i = count;

      $more.addClass('visible');
      $(this).find('.triangle').addClass('visible')

  })

  var $close = $('.__more .close');
  $close.on('click' , function(e) {
    console.log('clsick');
    $(this).parents('.__more').removeClass('visible');
    $('.triangle').removeClass('visible');
  })

}

jQuery(document).ready(function(){
  tarteaucitron.init({
    "privacyUrl": "/charte-cookies", /* Privacy policy url */

    "hashtag": "#gestion_des_cookies", /* Open the panel with this hashtag */
    "cookieName": "gestion_des_cookies", /* Cookie name */

    "orientation": "bottom", /* Banner position (top - bottom) */
    "showAlertSmall": false, /* Show the small banner on bottom right */
    "cookieslist": false, /* Show the cookie list */

    "adblocker": false, /* Show a Warning if an adblocker is detected */
    "AcceptAllCta" : true, /* Show the accept all button when highPrivacy on */
    "highPrivacy": true, /* Disable auto consent */
    "handleBrowserDNTRequest": true, /* If Do Not Track == 1, disallow all */

    "removeCredit": false, /* Remove credit link */
    "moreInfoLink": true, /* Show more info link */
    "useExternalCss": true, /* If false, the tarteaucitron.css file will be loaded */

    //"cookieDomain": ".my-multisite-domaine.fr", /* Shared cookie for multisite */

    "readmoreLink": "/cookiespolicy" /* Change the default readmore link */
  });

  ajaxformulaire();
  addclass();
  mapsalles();
  clickbutton();
  clicksenddevis();
  links();
  slide();
  filteritem();
  map();
  burger();



  if ($('section').is("#nos-packs")) {
    open_pack();
  }

});
