/**
 * Custom scripts to support small pieces of functionality.
 */

 // Adding toggle class to main nav and open class to nav-toggle; adding toggle class to sub-menu's trigger
jQuery(document).ready(function ($){
	
	$( ".menu-toggle" ).click(function() {
		$('.main-navigation').toggleClass('toggled');
    $('.search').toggleClass('toggled');
    $('.nav-toggle span').toggleClass('open');
	});

  $( ".carat-container" ).click(function() {
    $('.sub-menu').toggleClass('toggled');
    $('.carat-container').toggleClass('toggled');
  });

  // Removing classes when browser window exceeds 740px wide
  $(window).resize(function() {
      if ($(window).width() > 740) {
        $('.main-navigation').removeClass('toggled');
        $('.search').removeClass('toggled');
        $('.nav-toggle span').removeClass('open');
        $('.sub-menu').removeClass('toggled');
        $('.carat-container').removeClass('toggled');
  } 

  }).resize(); // trigger resize event initially

});

// governs behavior for select a location menu
(function ($) {
  jQuery.extend({
    getUrlVars : function () {
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
      }
      return vars;
    },
    getUrlVar : function (name) {
      return jQuery.getUrlVars()[name];
    }
  });
  $(document).ready(function () {
    // Find Select a Location Toggle Menu
    // Extra hide so that toggle responds correctly the first time
    $('.footer-content .select-a-location ul').hide().addClass('is-closed');
    $( ".footer-content .select-a-location" ).prepend( "<p class=\"location-selector\">Select a Location <span class=\"icon-arrow-down\"></span></p>" );    
  
    // Toggle
    $('.select-a-location .location-selector').click(function(){
      if($('.footer-content .select-a-location ul').hasClass('is-closed')) {
        $('.footer-content .select-a-location ul').fadeIn(300).removeClass('is-closed').addClass('is-open');
        
      } else if($('.select-a-location ul').hasClass('is-open')) {
        $('.footer-content .select-a-location ul').fadeOut(300).removeClass('is-open').addClass('is-closed');
        
      }
    });
});
})(jQuery);
