// Default JavaScript Functions and Initiations
$(document).ready(function() {

  $('.slider-container').slick({
    dots: true,
    infinite: true,
    speed: 300,
    slidesToShow: 1,
    arrows: true,
  });


  // Mobile menu button
  $('#mobile-menu-button').on('click', function(){
    event.stopPropagation();
    if(!$(this).hasClass('open')) {
      $('#mobile-menu-button, .mobile-menu, overlay').addClass('open');
    } else {
      $('#mobile-menu-button, .mobile-menu, overlay').removeClass('open');
    }
  });

  if($('body').hasClass('single-gls_webinars')) {

    var $i = 0;
    $('.wpProQuiz_resultsList li ').each(function() {
      $i++;
      $(this).addClass('row-' + $i);
      $(this).children('div').addClass('none');
    });


    $('.wpProQuiz_infopage').on('click', '.wpProQuiz_button', function(){
      $( '<h2>Sorry you failed</h2>').appendTo('.wpProQuiz_resultsList li.row-2' );

      var $firstName = $('.wpProQuiz_resultsList li.row-1 [data-form_id=3]').html();
      var $lastName = $('.wpProQuiz_resultsList li.row-1 [data-form_id=4]').html();
      var $email = $('.wpProQuiz_resultsList li.row-1 [data-form_id=5]').html();

      var $courseTitle = $('h1').html();
      $( '<h2 class="text-center">Congratulations you passed</h2><a class="congrats col-12 col-6-m center" href="/certificate?co='+ $courseTitle + '&amp;em=' + $email  + '&amp;fn=' + $firstName +'&amp;ln=' + $lastName +' ">Click here to view your certificate</a>').appendTo('.wpProQuiz_resultsList li.row-1' );
      console.log($('.wpProQuiz_resultsList li.row-1 > div'));
    });


  }



  $('.mobile-menu, .hamburger-menu').on('click',  function(event){
    event.stopPropagation();
  });

  $('html').on('click', function() {
    if($('.mobile-menu').hasClass('open')) {
      $('.mobile-menu, #mobile-menu-button, overlay').removeClass('open');
    }
  });

  // var $i = 1;
  // $('.load-more').live('click', function(e) {
  // 	e.preventDefault();
  // 	var $maxPages = $('.max-pages').text();
  //
  // 	$i++;
  // 	var link = jQuery(this).attr('href');
  // 	var segements = link.split("/");
  // 	segements[segements.length - 2] = "" + $i;
  // 	var newurl = segements.join("/");
  //
  // 	$.ajax({
  // 		url: newurl,
  // 		success: function(data) {
  // 			data=$(data).find('.more-content .results .col-12');
  // 			data.each(function(){
  // 				$(this).addClass('hidden');
  // 			});
  // 			$('.more-content .results').append(data);
  // 			$('.more-content .results .hidden').each(function(){
  // 				$(this).fadeIn();
  // 			});
  // 		}
  //
  // 	});
  //
  // 	if($i >= $maxPages) {
  // 		$('.load-more').fadeOut('slow');
  // 	}
  // });

  // http://codepen.io/Lewitje/pen/YybQEP original copy from Lewi Hussay updated to work with multiple divs
  // Example usage use data-match-height="groupName"> on anything!!!

  var matchHeight = (function () {

    "use strict";

    function matchHeight(){
      //get all matched height attr
      var groupName = $('[data-match-height]');
      var groupHeights = [];

      // for each attr set the min height to auto this makes it responsive
      $(groupName).each(function(){

        var dataName = $(this);

        var key = dataName.data('match-height');

        //create an array of heights
        if(!(key in groupHeights)){
          groupHeights[key] = [];
        }

        dataName.css('min-height', 'auto');

        dataName.each(function() {

          groupHeights[key].push(dataName.outerHeight());

        });

        return groupHeights.key;

      });

      var obj = groupHeights;

      for (var index in obj) {
        if (!obj.hasOwnProperty(index)) {
          continue;
        }

        var minHeight = Math.max.apply(null, obj[index]);

        //if window is bigger then 600px set new height else set height to auto
        if ($(window).width() > 500) {
          $('[data-match-height="'+index+'"]').css('min-height', minHeight);
        }
      }
    }

    function eventListeners(){
      $(window).on('resize', function() {
        matchHeight();
      });
    }

    function init() {
      eventListeners();
      matchHeight();
    }

    return {
      init: init
    };

  }());

  $(document).ready(function() {
    if ($(window).width() > 500) {
      matchHeight.init();
    }
  });

  function slick_load(){
    $('.slick > div').css('height', $('.slick-active').height());
  }

  $(window).load(function() {
    setTimeout(slick_load, 200);

    if ($(window).width() > 500) {
      matchHeight.init();
    }
  });

}); // end document ready
