$(document).ready(function(){
	var nav_open = '';
	if(!isMobile()){
		deskImage();
	}
	var time_updated = $('.alert').data('updated');
	if(Cookies.get('alert_updated') != time_updated){
		$('.alert').addClass('closed').find('.toggle span').html('open');
	}
	$('.alert .toggle').on('click',function(e){
		if($(this).parents('.alert').hasClass('closed')){
			$(this).find('span').html('close');
		}else{
			$(this).find('span').html('open');
		}
		Cookies.set('alert_updated',time_updated);
		$(this).parents('.alert').toggleClass('closed');
	});
	$('section.collapsable').each(function(e){
		if ($(this).height() > 500){
			$(this).append("<a class='view-more' href='#'>View More</a>");
			$(this).toggleClass('short');
		}
	});
	$('.view-more').on('keypress click',function(e){
		if (e.which === 13 || e.type === 'click') {
			e.preventDefault();
			$(this).parents('section').toggleClass('short');
			if($(this).parents('section').hasClass('short')){
				$(this).html('View More');
			}else{
				$(this).html('View Less');
			}
		}
	});
	$('.accordion h3').on('keypress click',function(e){
		$(this).toggleClass('active');
		var selector = $(this).next();
		$(this).next().toggleClass('open');
	});
	$('.drop-toggle').on('keypress click',function(e){
		if (e.which === 13 || e.type === 'click') {
			e.preventDefault();
			$(this).parents('.drop-container').toggleClass('open');
		}
		if (e.type === 'click') {
			$(this).blur();
		}
	});
	$(".featured .slide img").each(function(i, img) {
		$(img).css({
			left: ($(img).parent().width() - $(img).width()) / 2
		});
	});
	$('.subnav > h2').on('keypress click',function(e){
		if(isMobile() || isTablet()){
			e.preventDefault();
			if (e.which === 13 || e.type === 'click') {
				$(this).parents('.subnav').toggleClass('open');
				$('body').toggleClass('subnav-open');
			}
			if (e.type === 'click'){
				$(this).blur();
			}
		}
	});
	$('.subnav .sn-toggle').on('keypress click',function(e){
		if (e.keyCode === 13 || e.type === 'click') {
			e.preventDefault();
			$(this).parent().toggleClass('open');
		}
		if (e.type === 'click'){
			$(this).blur();
		}
	});
	$('.subsection button').on('keypress click',function(e){
		if (e.which === 13 || e.type === 'click') {
			$(this).parents('.subsection').toggleClass('open');
		}
		if (e.type === 'click'){
			$(this).blur();
		}
	});
	$('.tabs-container ul.tabheaders li a').on('click',function(e){
		e.preventDefault();
		var a = $(this).attr('href');
		var b = a.substring(1,a.length);
		if(isMobile()){
			$(this).parent().siblings().removeClass('open');
			$(this).parent().addClass('open');
			if($(this).parents('.tabs-container').hasClass('collapsable')){
				if(!$(this).parents('.tabheaders').siblings('#'+b).hasClass('open')){
					$(this).parents('.tabheaders').siblings('div.contents').each(function(i){
						$(this).removeClass('open');
					});
					$(this).parents('.tabheaders').siblings('#'+b).addClass('open');
					$(this).parents('.tabheaders').addClass('collapsed');
				}else{
					$(this).parents('.tabheaders').siblings('div.contents').each(function(i){
						$(this).removeClass('open');
					});
					$(this).parents('.tabheaders').siblings('#'+b).removeClass('open');
					$(this).parents('.tabheaders').removeClass('collapsed');
					$(this).parent().removeClass('open');
				}
			}else{
				if(!$(this).parents('.tabheaders').siblings('#'+b).hasClass('open')){
					$(this).parents('.tabheaders').siblings('div.contents').each(function(i){
						$(this).removeClass('open');
					});
					$(this).parents('.tabheaders').siblings('#'+b).addClass('open');
					$(this).parents('.tabheaders').addClass('collapsed');
				}
			}
		}else{
			$(this).parent().siblings().removeClass('open-desk');
			$(this).parent().addClass('open-desk');
			$(this).parents('.tabheaders').siblings('div.contents').each(function(i){
				$(this).removeClass('open-desk');
			});
			$(this).parents('.tabheaders').siblings('#'+b).addClass('open-desk');
		}
	});
	$('.tabaccordion > li > h3').on('keypress click', function() {
		if (isMobile()) {
			$(this).parent().siblings().removeClass('acc-active');
			$(this).parent().toggleClass('acc-active');
		}else{
			$(this).parent().siblings().removeClass('tab-active');
			$(this).parent().addClass('tab-active');
		}
		var ul_h = $(this).parents('li').outerHeight();
		var div_h = $(this).next().outerHeight();
		var tot_h = ul_h + div_h;
		if (isMobile()) {
			$(this).parents('ul.tabaccordion').css('height','auto');
			$('html,body').animate({
				scrollTop: $(this).parents('li').offset().top-60
			});
		}else{
			$(this).parents('ul.tabaccordion').css('height',tot_h);
		}
	});
	equalHeight('.tabaccordion > li');
	$('.tabaccordion > li > div').each(function(){
		$(this).css('top',$(this).parent().height());
	});
	equalHeight('.matrix.overlay-cta > div .container');
	equalHeight('.owl-btn-wrapper .owl-btn');
	tabAccordion();
});


$( window ).resize(function() {
	equalHeight('.tabaccordion li');
	$('.tabaccordion li > div').each(function(){
		$(this).css('top',$(this).parent().height());
	});
	if(isMobile()){
		equalHeight('.matrix.overlay-cta > div .container');
		$('.tabaccordion').css('height','auto');
	}else{
		deskImage();
		tabAccordion();
	}
});
$(window).scroll(function() {
	stickyHeader();
	if ($(this).scrollTop() > 150) {
		$('.back-to-top:hidden').stop(true, true).fadeIn();
	} else {
		$('.back-to-top').stop(true, true).fadeOut();
	}
});
function isMobile() {
	if(window.innerWidth <= 719) {
		return true;
	} else {
		return false;
	}
}
function isTablet() {
	if(window.innerWidth <= 1120) {
		return true;
	} else {
		return false;
	}
}
function equalHeight(path){
	$(path).height("auto");
	if (!isMobile()){
		var max = Math.max.apply(null, 
			$(path).map(function () {
				return $(this).height();
			})
		);
	}
	$(path).height(max);
}
function deskImage(){
	$(".mobile-image").each(function(){
		if(!$(this).next().is('img')){
			var alt = $(this).attr('alt');
			var txt1 = $(this).attr('src');
			//var txt2 = txt1.lastIndexOf('.');
			//var txt3 = txt1.slice(0,txt2) + "-desk" + txt1.slice(txt2);
			var txt3 = txt1.replace('-mobi.','.');
			$(this).after("<img class='desk-image' alt='"+alt+"' src='"+txt3+"' />");
		}
	});
}
function tabAccordion(){
	$('.tabaccordion').each(function(e){
		if(isMobile()){
			$(this).css('height','auto');
		}else{
			var ul_h = $(this).children('li').outerHeight();
			var open_h = $(this).find('.tab-active > div').outerHeight() + ul_h+20;
			$(this).css('height',open_h);
		}
	});
}
function subnavToggles(){
	//$('.currentbranch');
}
function stickyHeader(){
	var topPos = $(document).scrollTop();
	if(120 < topPos){
		$('body').addClass('sticky');
	}else{
		$('body').removeClass('sticky');
	}
}

// Engage by Snap


// (function() {
// 	var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
// 	se.src = '//storage.googleapis.com/code.snapengage.com/js/f85dc742-dcf2-469a-a71e-53c2ebf86ce7.js';
// 	var done = false;
// 	se.onload = se.onreadystatechange = function() {
// 	  if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
// 	    done = true;
// 	    /* Place your SnapEngage JS API code below */
// 	    /* SnapEngage.allowChatSound(true); Example JS API: Enable sounds for Visitors. */
		
// 	  }
// 	};
// 	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
// })();

                     
// Anchor Scroll for Mobile Devices, addition from VPM, added by T.McGowan
function anchorScroll(){
     if(window.location.hash) {
         window.scrollBy(0, -150);
   }
}



