/**
 * @file
 * Custom javascript for the sub theme.
 */

(function ($) {
  Drupal.behaviors.NLexCountriesMenuAlterations = {
    attach: function (context, settings) {
      $('[data-toggle=offcanvas]').click(function () {
        $('.row-offcanvas').toggleClass('active');
      });

      // Initially collapse all collapsible links of the third countries menu.
      $('#block-menu-menu-third-countries ul ul').hide();
      // Disable clicking on the parent links of the third countries menu.
      $('#block-menu-menu-third-countries li.expanded > a').click(function (event) {
        event.preventDefault();
        $(this).parent().find('ul').slideToggle();
      });

      // Initially collapse all collapsible links of the left 1st level menu.
      $('.menu-name-menu-countries-menu.menu-level-1').parent().hide();
      // Disable clicking on the parent links of the left 1nd level menu.
      $('.menu-name-menu-countries-menu.menu-level-1').parent().parent().find('.panel-heading').click(function (event) {
        // Add expand/collapse functionality.
        $(this).parent().find('.panel-body').slideToggle();
      });
      $('.menu-name-menu-countries-menu.menu-level-1').parent().parent().find('.panel-heading').css('cursor', 'pointer');

      // Disable clicking on the parent links of the left 2nd level menu.
      $('.menu-name-menu-countries-menu.menu-level-2 li.expanded > a, .menu-name-menu-countries-menu.menu-level-2 li.collapsed > a').click(function (event) {
        event.preventDefault();
      });
      // Also add a negative tabindex so they cannot be selected.
      $('.menu-name-menu-countries-menu.menu-level-2 li.expanded > a, .menu-name-menu-countries-menu.menu-level-2 li.collapsed > a').attr('tabindex', '-1');
      // Add expanded class to countries div.
      $('.countriesListDiv .panel-heading').click(function () {
        $(this).toggleClass('expanded');
      });
      // Make Countries Menu a link so it can be focusable.
      $('.countriesListDiv .panel-heading').html('<a href="#">' + $('.countriesListDiv .panel-heading').html() + '</a>');
      // Add expanded class to subinfo div for basic pages.
      $('.node-type-page .subInfoDiv li.expanded').click(function () {
        $(this).toggleClass('linkexpand');
      });
      // Add bootstrap classes to front page country links.
      $('.front #block-menu-menu-countries-menu .panel-body li').addClass('col-xs-6 col-md-6 col-lg-4');

      $(".cntrMenuSecondHeading").nextAll('li').each(function () {
        $(this).addClass('lm-link');
      });

    }
  };

  Drupal.behaviors.NLexMenuAlterations = {
    attach: function (context, settings) {
      if ($("#labelForLearnMore").length) {
        $(".countryDiv ul").first().prepend('<li class="cntrMenuHeading">' + $("#labelForSearch").val() + '</li>');
        $(".countryDiv ul li.last").last().prev().before('<li class="cntrMenuSecondHeading">' + $("#labelForLearnMore").val() + '</li>');
        // Add bootstrap class to the panel-heading of the first countries menu.
        countryCode = $('.countriesListDiv li.active-trail a').attr('class');
        countryCode = countryCode.split(" ");
        countryCode = countryCode[0];
        $('.countryDiv .panel-heading').addClass(countryCode);
      }
   }
  };

  $(window).on("load", function () {
    var cookieMsg = $('a#cookie-notice');
    if ($(cookieMsg)) {
      $(cookieMsg).attr("href", "https://n-lex.europa.eu/n-lex/legal-notice/legal-notice#_Toc4");
    }
  });

  // Banner logic.
  Drupal.behaviors.NLexBannerModal = {
    attach: function (context, settings) {
      if ($(".subInfoDiv.bannersBlock").length) {
        $('#bannerModal').modal('hide');
        $(".subInfoDiv.bannersBlock .panel-body a.list-group-item").click(function (e) {
          if ($(this).find('img').length !== 0) {
            e.preventDefault();
            var normalImage = $(this).find('img').clone().wrap('<p>').parent().html();
            var cleanImage = $(this).find('img').clone().removeAttr('class width height').wrap('<p>').parent().html();
            var theMarkup = '<a href="//eur-lex.europa.eu/n-lex/">' + cleanImage + '</a>';
            $('#bannerModal .modal-body .left-section').html(normalImage);
            $('#bannerModal .modal-body .right-section').text(theMarkup);
            $('#bannerModal').modal('show');
          }
        });
      }
    }
  };
}(jQuery));

function activatedLanguage(lang){
  var m = document.getElementById("LanguageSelection").querySelectorAll("[hreflang]");
  for(var i=0; i<m.length; i++){
    m[i].classList.remove('active');
  }
  for(var i=0; i<m.length; i++){
    if(m[i].getAttribute("hreflang") === lang){
      m[i].classList.add('active');
    }
  }
}

function languageFullName(lang){
  var fullNameItem = document.getElementById("languageSelect").getElementsByClassName("hidden-xs").item(0);
  var m = document.getElementById("LanguageSelection").querySelectorAll("[hreflang]");
    for(var i=0; i<m.length; i++){
    if(m[i].getAttribute("hreflang") === lang){
      fullNameItem.innerHTML = m[i].getElementsByClassName("full-name-lang").item(0).innerHTML;
      return;
    }
  }
}

function activatedLanguageButton(div){
  var m = document.getElementById("dropdown-big").getElementsByTagName("LI");
  var actLang;
  for (var i = 0; i < m.length; i++) {
    if (m[i].classList.contains('active')) {
      actLang = m[i].innerText;
    }
  }
  document.getElementById(div).innerHTML = actLang + "<span class=\"caret\"></span>";
}
function fetchTranslatedDoc(obj,urlDoc){
  var arr = eval(urlDoc);
  arr.forEach(function(element){
    var link = document.createElement("a");
    link.download = name;
    link.href = element;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    delete link;
  })
function createTransBlock(position,strArr){

}
}
