$(document).ready(function () {
  /*//pimpage radio   //on stop ça, trop de sideffect partout
    $("input:radio").not(".radio_ignored").wrap("<label class='container_rd'></label>");
    $("input:radio").not(".radio_ignored").after("<span class='checkmark_rd'></span>");

    //pimpage checkbox
    $("input:checkbox").not(".checkbox_ignored").wrap("<label class='container_chkbox'></label>");
    $("input:checkbox").not(".checkbox_ignored").after( "<span class='checkmark'></span>" );
    */

  //validate required checkbox
  if ($(".required_checkbox")[0]) {
    $(".required_checkbox_submit").click(function () {
      checked = $("input.required_checkbox:checkbox:checked").length;

      if (!checked) {
        alert("Case à cocher obligatoire.");
        return false;
      }
    });
  }

  /*-------------TABLE RESPONSIVE ---------*/
  // Setup responsive table
  var header = Array();
  //first get the data from the headings
  $(".responsive_style_table thead th").each(function (i, v) {
    header[i] = $(this).text();
  });
  //now loop through table rows and apply headings to the td in each row
  $(".responsive_style_table tbody tr").each(function (i, v) {
    var myRow = $(this);

    myRow.find("td").each(function (j) {
      $(this).attr("data-thead", header[j]);
    });
  });

  //-----------------------------------------------------------------------------------------------

  //Augmenter/diminuer la taille du texte                          accueil
  var fontsize = 100;

  $("#TextTransform #BigText").click(function () {
    fontsize = fontsize + 15;

    $(".container_content").css("font-size", fontsize + "%", "!important");
    $(".container_content p").css("font-size", fontsize + "%", "!important");
    $(".container_content a").css("font-size", fontsize + "%", "!important");
    $(".container_content h1").css("font-size", fontsize + "%", "!important");
    $(".container_content h2").css("font-size", fontsize + "%", "!important");
    $(".container_content h3").css("font-size", fontsize + "%", "!important");
    $(".container_content h4").css("font-size", fontsize + "%", "!important");
    $(".container_content h5").css("font-size", fontsize + "%", "!important");
    $(".container_content h6").css("font-size", fontsize + "%", "!important");

    $("#accueil").css("font-size", fontsize + "%", "!important");
    $("#accueil p").css("font-size", fontsize + "%", "!important");
    $("#accueil a").css("font-size", fontsize + "%", "!important");
    $("#accueil h1").css("font-size", fontsize + "%", "!important");
    $("#accueil h2").css("font-size", fontsize + "%", "!important");
    $("#accueil h3").css("font-size", fontsize + "%", "!important");
    $("#accueil h4").css("font-size", fontsize + "%", "!important");
    $("#accueil h5").css("font-size", fontsize + "%", "!important");
    $("#accueil h6").css("font-size", fontsize + "%", "!important");
  });

  $("#TextTransform #SmallText").click(function () {
    fontsize = fontsize - 15;
    $(".container_content").css("font-size", fontsize + "%", "!important");
    $(".container_content p").css("font-size", fontsize + "%", "!important");
    $(".container_content a").css("font-size", fontsize + "%", "!important");
    $(".container_content h1").css("font-size", fontsize + "%", "!important");
    $(".container_content h2").css("font-size", fontsize + "%", "!important");
    $(".container_content h3").css("font-size", fontsize + "%", "!important");
    $(".container_content h4").css("font-size", fontsize + "%", "!important");
    $(".container_content h5").css("font-size", fontsize + "%", "!important");
    $(".container_content h6").css("font-size", fontsize + "%", "!important");

    $("#accueil").css("font-size", fontsize + "%", "!important");
    $("#accueil p").css("font-size", fontsize + "%", "!important");
    $("#accueil a").css("font-size", fontsize + "%", "!important");
    $("#accueil h1").css("font-size", fontsize + "%", "!important");
    $("#accueil h2").css("font-size", fontsize + "%", "!important");
    $("#accueil h3").css("font-size", fontsize + "%", "!important");
    $("#accueil h4").css("font-size", fontsize + "%", "!important");
    $("#accueil h5").css("font-size", fontsize + "%", "!important");
    $("#accueil h6").css("font-size", fontsize + "%", "!important");
  });

  $("a[rel^='prettyPhoto']").prettyPhoto({
    animationSpeed: "normal",
    opacity: 0.8,
    deeplinking: false,
    allow_resize: true /* Resize the photos bigger than viewport. true/false */,
    default_width: 500,
    default_height: 344,
    showTitle: true,
    social_tools: "",
  });
  $(".photo_product_slider").owlCarousel({
    autoPlay: 5000,
    stopOnHover: true,
    navigation: false,
    pagination: true,
    paginationSpeed: 1000,
    goToFirstSpeed: 2000,
    singleItem: true,
    autoHeight: true,
    transitionStyle: "fade",
    navigationText: ["", ""],
  });

  $("ul.dropdown-menu [data-toggle=dropdown]").on("click", function (event) {
    event.preventDefault();
    event.stopPropagation();
    $(this).parent().siblings().removeClass("open");
    $(this).parent().toggleClass("open");
  });
  $("#mobile_spc_menu_btn").click(function (e) {
    $(this).toggleClass("Active");
    $("#menu_spc").slideToggle("fast");
    e.preventDefault();
  });

  /* ================ Show Login box. ================ */
  $(".login-btn").prepend('<b class="tri hidden"></b>');
  $(".login-btn").click(function (e) {
    e.preventDefault();
    /*VG-066092-0*/
    $(window).scrollTop(0);
    $(this).toggleClass("active_hd");
    $(".login-box").slideToggle();
    $(".login-btn").find(".tri").toggleClass("visible");
    $(".close-login").click(function (e) {
      e.preventDefault();
      $(".login-btn").toggleClass("active_hd");
      $(".login-box").slideUp();
      $(".login-btn").find(".tri").removeClass("visible");
    });
  });

  /* ================ Back to top button. ================ */
  var winScroll = $(window).scrollTop();
  if (winScroll > 1) {
    $("#to-top").css({ bottom: "10px" });
  } else {
    $("#to-top").css({ bottom: "-100px" });
  }
  $(window).on("scroll", function () {
    winScroll = $(window).scrollTop();

    // PARALLAX background Animation.
    var y = parseInt($(".parallax").css("background-position-y"));
    var newY = -(winScroll * 0.05) + "px";
    $(".parallax").css("background-position-y", newY);

    //  Show Hide back to top button.
    if (winScroll > 1) {
      $("#to-top").css({ opacity: 1, bottom: "10px" });
    } else {
      $("#to-top").css({ opacity: 0, bottom: "-100px" });
    }
  });

  $("#to-top").click(function () {
    $("html, body").animate({ scrollTop: "0px" }, 800);
    return false;
  });

  /* ================ Show Hide Search box. ================ */
  /*
        $('.top-search a').click(function(){
            if($(this).parent().find('.search-box').is(':visible')){
                $('.search-box').fadeOut(300);
                $(this).parent().removeClass('selected');
                return false;
            }else{
                $('.search-box').fadeIn(300);
                $(this).parent().addClass('selected');
                return false;
            }
        });
        $(document).mouseup(function(e){
            if($('.search-box').is(':visible')){
                var targ = $(".search-box");
                if (!targ.is(e.target) && targ.has(e.target).length === 0){
                $('.search-box').fadeOut(300);
                $('.top-search').removeClass('selected');
                }
            }
        });
    */

  /*$( function() {
        $( ".accordion" ).accordion({
          heightStyle: "content",
          collapsible: true,
          active: false
        });
      } );*/

  /* ================ Album Photos ================ */
  $(".filter-button").click(function () {
    var value = $(this).attr("data-filter");

    if (value == "all") {
      //$('.filter').removeClass('hidden');
      $(".filter").show("1000");
    } else {
      //            $('.filter[filter-item="'+value+'"]').removeClass('hidden');
      //            $(".filter").not('.filter[filter-item="'+value+'"]').addClass('hidden');
      $(".filter")
        .not("." + value)
        .hide("3000");
      $(".filter")
        .filter("." + value)
        .show("3000");
    }
  });

  if ($(".filter-button").removeClass("active")) {
    $(this).removeClass("active");
  }
  $(this).addClass("active");

  //video embed full width
  $(".video_full_width").each(function () {
    $(this).removeAttr("style");
    $(this).wrap("<div class='fullwidth_video_container'></div>");
  });

  $(".lara_log_taxo").click(function () {
    var id = $(this).attr("data_interet");
    $.ajax({
      type: "POST",
      url: "/gi2-modules/standard/formation/client/framework/ajax_interet.php",
      data: { id: id },
      success: function (response) {
        //alert(response);
      },
    });
  });

  $(".formation_favoris").click(function () {
    var id = $(this).attr("data-id");
    var item = $(this);
    $.ajax({
      type: "POST",
      url: "/gi2-modules/standard/formation/client/framework/ajax_favoris.php",
      data: { id: id },
      success: function (response) {
        if (response == 1) {
          $(item).removeClass("far");
          $(item).addClass("fas"); // alert('1');
        } else {
          $(item).removeClass("fas");
          $(item).addClass("far"); // alert('0');
        }
      },
    });
  });

  $('a[data-target="#membershipDesc"]').click(function (e) {
    console.log("module.js");
    $("#main_nav").css("z-index", "0");
  });
});

//---------------------------------------------------------------------------------------------------------------------------
function go_to_page3(page_num) {
  var mode = "";
  if (document.getElementById("spc_mod") != null) {
    mode = document.getElementById("spc_mod").value;
  }
  var show_per_page = parseInt($("#show_per_page").val(), 0);
  start_from = page_num * show_per_page;
  end_on = start_from + show_per_page;
  if (mode == "table") {
    $("#list_item")
      .children("tbody")
      .children(".oneItem_wrapper")
      .css("display", "none")
      .slice(start_from, end_on)
      .css("display", "table-row");
  } else {
    $("#list_item").children(".oneItem_wrapper").css("display", "none");
    $("#list_item")
      .children(".oneItem_wrapper")
      .slice(start_from, end_on)
      .css("display", "block");
    $("#list_item")
      .children(".oneItem_wrapper")
      .slice(start_from, end_on)
      .addClass("d-block");
    $("#list_item")
      .children(".oneItem_wrapper")
      .removeClass("d-block")
      .slice(start_from, end_on)
      .removeClass("d-none");
  }
  $(".page[longdesc=" + page_num + "]")
    .addClass("active")
    .siblings(".active")
    .removeClass("active");
  $("#current_page").val(page_num);

  $("a.page").css("display", "none");
  $("a.page").removeClass("prev_nav");
  $("a.active").css("display", "inline-block");

  $("a.active").prev().css("display", "inline-block");
  $("a.active").prev().prev().css("display", "inline-block");

  $("a.active").next().css("display", "inline-block");
  $("a.active").next().next().css("display", "inline-block");

  $("a.prev").next().css("display", "inline-block");
  $("a.prev").next().next().css("display", "inline-block");

  $("a.next").prev().css("display", "inline-block");
  $("a.next").prev().prev().css("display", "inline-block");

  if (page_num >= 5) {
    $("a.active").prev().prev().addClass("prev_nav");
  }
  $("html,body").animate(
    {
      scrollTop: $("#main_wrapper_interne").offset().top,
    },
    "fast"
  );
}

function previous3() {
  new_page = parseInt($("#current_page").val(), 0) - 1;
  //if there is an item before the current active link run the function
  if ($(".active").prev(".page").length == true) {
    go_to_page3(new_page);
  }
}

function next3() {
  new_page = parseInt($("#current_page").val(), 0) + 1;
  //if there is an item after the current active link run the function

  go_to_page3(new_page);
}
function go_to_page(page_num) {
  var show_per_page = parseInt($("#show_per_page").val(), 0);
  start_from = page_num * show_per_page;
  end_on = start_from + show_per_page;
  $("#wrapper_list_paginate")
    .children()
    .css("display", "none")
    .slice(start_from, end_on)
    .css("display", "block");
  $(".page[longdesc=" + page_num + "]")
    .addClass("active")
    .siblings(".active")
    .removeClass("active");
  $("#current_page").val(page_num);
}

function previous() {
  new_page = parseInt($("#current_page").val(), 0) - 1;
  //if there is an item before the current active link run the function
  if ($(".active").prev(".page").length == true) {
    go_to_page(new_page);
  }
}

function next() {
  new_page = parseInt($("#current_page").val(), 0) + 1;
  //if there is an item after the current active link run the function
  if ($(".active").next(".page").length == true) {
    go_to_page(new_page);
  }
}
$(document).ready(function () {
  if (document.getElementById("show_per_page"))
    var c = document.getElementById("show_per_page").value;

  function change_per_page(show_per_page) {
    var mode = "";
    if (document.getElementById("spc_mod") != null) {
      mode = document.getElementById("spc_mod").value;
    }
    var current_page = document.getElementById("current_page").value;
    if (mode == "table") {
      number_of_items = $("#list_item")
        .children("tbody")
        .children(".oneItem_wrapper").length;
    } else {
      var number_of_items = $("#list_item").children(".oneItem_wrapper").length;
    }

    var number_of_pages = Math.ceil(number_of_items / show_per_page);

    $("#current_page").val(current_page);
    $("#show_per_page").val(show_per_page);

    var navigation_html = '<a class="prev" onclick="previous3()">Précédent</a>';
    var current_link = 0;
    while (number_of_pages > current_link) {
      navigation_html +=
        '<a class="page" onclick="go_to_page3(' +
        current_link +
        ')" longdesc="' +
        current_link +
        '">' +
        (current_link + 1) +
        "</a>";
      current_link++;
    }
    navigation_html += '<a class="next" onclick="next3()">Suivant</a>';

    $(".controls_pagination").html(navigation_html);
    $(".controls_pagination .page:first").addClass("active");
    if (mode == "table") {
      $("#list_item")
        .children("tbody")
        .children(".oneItem_wrapper")
        .css("display", "table-row");
      $("#list_item")
        .children("tbody")
        .children(".oneItem_wrapper")
        .slice(0, show_per_page)
        .css("display", "block");
    } else {
      $("#list_item").children().removeClass("d-block"); // d-flex
      $("#list_item").children().addClass("d-none");

      $("#list_item").children().slice(0, show_per_page).removeClass("d-none");
      $("#list_item")
        .children(".oneItem_wrapper")
        .slice(0, show_per_page)
        .addClass("d-block"); // d-flex
    }

    $("a.next").prev().css("display", "inline-block");
    $("a.next").prev().prev().css("display", "inline-block");
  }

  $("#showPerPage").on("change", function () {
    change_per_page(this.value);
  });
});

function input_numeric_only(evt, allow_negatif) {
  var theEvent = evt || window.event;

  // Handle paste
  if (theEvent.type === "paste") {
    key = event.clipboardData.getData("text/plain");
  } else {
    // Handle key press
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode(key);
  }
  if (allow_negatif == "1") {
    var regex = /[0-9]|\.|\-/;
  } else {
    var regex = /[0-9]|\./;
  }
  if (!regex.test(key)) {
    theEvent.returnValue = false;
    if (theEvent.preventDefault) theEvent.preventDefault();
  }
}

function changeBgNews(color, id) {
  var element = document.getElementById(id);
  element.style.background = "" + color + "";
}

//modal img
$(function () {
  $(".image_fullsize").on("click", function () {
    if ($(this).attr("src")) {
      var src = $(this).attr("src");
    } else {
      var src = $(this).find("img").attr("src");
    }

    $(".imagepreview").attr("src", src);
    var desc = $(this).attr("alt");
    if (!desc) {
      desc = $(this).attr("title");
    }
    $("#text_img_fullsize").html(desc);
    $("#imagemodal").modal("show");
  });
});

function isValidDate(dateString) {
  var regEx = /^\d{4}-\d{2}-\d{2}$/;
  if (!dateString.match(regEx)) return false; // Invalid format
  var d = new Date(dateString);
  var dNum = d.getTime();
  if (!dNum && dNum !== 0) return false; // NaN value, Invalid date
  return d.toISOString().slice(0, 10) === dateString;
}

function check_double_submit(id, form, txt) {
  btn_sbm = document.getElementById(id);
  btn_sbm.disabled = true;
  btn_sbm.value = txt;
  // btn_sbm.style.display ='none';
  return true;
}

function home_event_tab(evt, tabName) {
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("home_event_tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the button that opened the tab
  document.getElementById(tabName).style.display = "block";
  evt.currentTarget.className += " active";
}

function cart_login() {
  $(".login-box").slideToggle();
  $("html, body").animate({ scrollTop: 0 }, "slow");
}

function show_password(id_input_pass) {
  var x = document.getElementById(id_input_pass);
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}

jQuery(function ($) {
  //$("#date").mask("99/99/9999");
  $(".mask_phone_full").inputmask("(999) 999-9999 #9999");
  $(".mask_phone").inputmask("(999) 999-9999");
  $(".mask_postcode").inputmask("a9a 9a9");

  //$(".mask_courriel").mask("999-99-9999");
});
//---------------------------------------------------------------------------------------------------------------------------
