/* =========================================================================
global variables
========================================================================== */
//watchers for major breakpoint changes - move from small screen to large screen layout/styles
//these match up to
(function() {
  var mq = {
    end: window.matchMedia("(max-width: 799px)")
  }

  //container ID/class names called by specific functions
  var selectors = {
    searchForm: '.search-form',
    advancedSearchForm: '.advanced-search-form',
    pageWrap: '#page',
    socialShare: '.social-share',
    socialShareMore: '.share-more'
  }

  /* =========================================================================
  search form panel
  ========================================================================== */
  //make search form expandable only on small screens
  function searchFormExpandable() {
    if (mq.end.matches) {
      $(selectors.searchForm).expandable('revive');
      $(selectors.advancedSearchForm).expandable('revive');
    } else {
      $(selectors.searchForm).expandable('kill');
      $(selectors.searchForm).children('div').removeAttr('style');
      $(selectors.advancedSearchForm).expandable('kill');
      $(selectors.advancedSearchForm).children('div').removeAttr('style');
    }
    return;
  }
  searchFormExpandable();
  mq.end.addListener(searchFormExpandable);

  /* =========================================================================
  slideout filters for search results on small screens
  ========================================================================= */
  if ($('#search-results').length == 1) window.APP.MODELS.FilterSlideOut.create({
    breakpoint: 800,
    animationSpeed: 200,
    pageWrapId: 'page',
    filterType: 'search',
    openToggle: 'Filter Results',
    closeToggle: 'Close'
  });

  /* =========================================================================
  social share open/close toggle
  ========================================================================== */
  $(selectors.socialShare)
    .on('click', selectors.socialShareMore, function() {
      var parent = $(this).parents(selectors.socialShare);
      parent.toggleClass('share-open');
      var moreText = $(this).attr('data-more-text');
      var lessText = $(this).attr('data-less-text');
      //on large screens, move the second list items into the first list, instead of sliding the list down
      if (parent.hasClass('share-open')) {
        $(this).text(lessText);
      } else {
        $(this).text(moreText);
      }
      return;
    });
})();

/* =========================================================================
Menu Toggle
========================================================================== */

$('.menu-toggle').click(function() {
  $('.brand-toggle').slideToggle('fast');
  $('.menu-toggle').toggleClass('x');
});

/* =========================================================================
Hero Slider
========================================================================== */
$('.hero-slider').slick({
  dots: false,
  infinite: true,
  speed: 500,
  fade: true,
  autoplay: true,
  cssEase: 'linear'

});

/* =========================================================================
Working for us slider
========================================================================== */


$('.blue-box-slider').slick({
  dots: true,
  infinite: true,
  speed: 300,
  slidesToShow: 1,
  slidesToScroll: 1,
  infinite: true,
  autoplay: true,
  arrows: true,

});

/* =========================================================================
Inside Company slider
========================================================================== */


$('.inside-company-slider').slick({
  dots: true,
  infinite: true,
  speed: 300,
  slidesToShow: 1,
  slidesToScroll: 1,
  infinite: true,
  autoplay: true,
  arrows: true,

});

/* =========================================================================
Location Toggle
========================================================================== */
$('.location-toggle-btn').click(function() {
  $('.grid-toggle').slideToggle('fast');
  $(this).toggleClass('open');
  var text = $(this).text() == 'More locations' ? 'Less locations' : 'More locations';
  $(this).text(text);
});


/* =========================================================================
Brand Location Toggle
========================================================================== */


$(document).ready(function() {
  $('.brand-location-btn').click(function() {
    if ($(window).width() < 800) {
      $(this).next('.brand-location-toggle').toggleClass("toggle-open");
      $(".brand-location-toggle").not($(this).next()).removeClass('toggle-open');
      $(this).toggleClass('active');
      $(".brand-location-btn").not($(this)).removeClass('active');
    } else {
      $(this).next('.brand-location-toggle').addClass("toggle-open");
      $(".brand-location-toggle").not($(this).next()).removeClass('toggle-open');
      $(this).addClass('active');
      $(".brand-location-btn").not($(this)).removeClass('active');
    }
  });
});


// If window opens greater than 800 open up tabs

$(document).ready(function() {
  if ($(window).width() > 800) {

    if (!$(".brand-location-toggle").hasClass("toggle-open")) {
      $(".brand-location-toggle:first-of-type").addClass("toggle-open");
    }

    if (!$(".brand-location-btn").hasClass("active")) {
      $(".brand-location-btn:first-of-type").addClass("active");
    }
  }
});

// If window is resized from mobile to over 800 open up tabs

$(window).resize(function() {
  if ($(window).width() > 800) {

    if (!$(".brand-location-toggle").hasClass("toggle-open")) {
      $(".brand-location-toggle:first-of-type").addClass("toggle-open");
    }
    if (!$(".brand-location-btn").hasClass("active")) {
      $(".brand-location-btn:first-of-type").addClass("active");
    }
  }
});

// south africa header button

$(document).ready(function() {
  $('.south-africa-header-button').click(function() {

    $('.south-africa-toggle').toggleClass("toggle-open");
    $(".brand-location-toggle").not($(".south-africa-toggle")).removeClass('toggle-open');
    $(".south-africa-btn").toggleClass('active');
    $(".brand-location-btn").not($(".south-africa-btn")).removeClass('active');


  });
});

// UK header button

$(document).ready(function() {
  $('.uk-header-button').click(function() {

    $('.uk-toggle').toggleClass("toggle-open");
    $(".brand-location-toggle").not($(".uk-toggle")).removeClass('toggle-open');
    $(".uk-btn").toggleClass('active');
    $(".brand-location-btn").not($(".uk-btn")).removeClass('active');


  });
});


// Toronto header button

$(document).ready(function() {
  $('.canada-header-button').click(function() {

    $('.toronto-toggle').toggleClass("toggle-open");
    $(".brand-location-toggle").not($(".toronto-toggle")).removeClass('toggle-open');
    $(".toronto-btn").toggleClass('active');
    $(".brand-location-btn").not($(".toronto-btn")).removeClass('active');


  });
});

// Italy header button

$(document).ready(function() {
  $('.italy-header-button').click(function() {

    $('.milan-toggle').toggleClass("toggle-open");
    $(".brand-location-toggle").not($(".milan-toggle")).removeClass('toggle-open');
    $(".milan-btn").toggleClass('active');
    $(".brand-location-btn").not($(".milan-btn")).removeClass('active');


  });
});




/* =========================================================================
Smooth scroll
========================================================================== */

$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
