<?php
/*
This file contains code based on and/or copied from Divi by Elegant Themes - see ../../functions.php for full credit
This file modified by Jonathan Hall, Ben Hussenet, Anna Kurowska, and Stephen James
Last modified 2020-02-26
*/

echo "jQuery(document).ready(function($){";

// ----- CLICK TO CALL --------
if ( et_get_option( 'swtch-header-3' ) == 'on' ) {?>
$unformattednum = $('#et-info-phone').text();
$formattednum = $unformattednum.replace(/-|\s/g,"");
$("#et-info-phone").wrapInner("<a href=tel:" + $formattednum + "></a>");
<?php 
}


// ----- Open Social Media Links In New Tab --------
if ( et_get_option( 'swtch-header-4' ) == 'on' ) {?>

$('.et-social-icon a').attr('target','_blank');


<?php } 

// ----- Different Logo On Scroll --------
$option = et_get_option( 'swtch-header-6' );
// Re-written based on version of switch in DS 3.0.0
if ( ! empty( $option ) ) {?>
// This switch contains code copied from WP and Divi Icons Pro by Aspen Grove Studios and modified
var MO = window.MutationObserver ? window.MutationObserver : window.WebkitMutationObserver;
if (MO) {
	var $header = $('#main-header');
	var $logo = $header.find('#logo');
	var logoOriginalSrc = $logo.attr('src');
	(new MO(function(events) {
		$logo.attr('src', $header.hasClass('et-fixed-header') ? '<?php echo(esc_url($option)); ?>' : logoOriginalSrc);
	})).observe($header[0], {attributes: true});
}
<?php
}

// ----- Collapsible submenus

if ( et_get_option( 'swtch-mobile-8' ) == 'on' ) {?>
function ds_setup_collapsible_submenus() {
    var $menu = $('.et_mobile_menu'),
	top_level_link = '.et_mobile_menu .menu-item-has-children > a';

	$menu.find('a').each(function() {
		$(this).off('click');

		if ( $(this).is(top_level_link) ) {
			$(this).attr('href', '#');
		}

		if ( ! $(this).siblings('.sub-menu').length ) {
			$(this).on('click', function(event) {
				$(this).parents('.mobile_nav').trigger('click');
			});
		} else {
			$(this).on('click', function(event) {
				event.preventDefault();
				$(this).parent().toggleClass('visible');
			});
		}
	});
}

$(window).load(function() {
	setTimeout(function() {
		ds_setup_collapsible_submenus();
	}, 700);
});

<?php
}

if (et_get_option('swtch-main-2')=='on') {
?>
var $preloader = $('.ds_preloader');
var preloaderTime = 0;
var removePreloaderInterval = setInterval(function() {
	preloaderTime += 200;
	if ($preloader.css('opacity') == 0 || preloaderTime >= 4000) {
		clearInterval(removePreloaderInterval);
		$preloader.remove();
	}
},200);
<?php
}
// Stop the footer floating on empty pages

if (et_get_option('swtch-footer-3')=='on') {
	?>

    function stickyFooter(){
    var footer = $("footer");
    var position = footer.position();
    var height = $(window).height();
    height = height - position.top;
    height = height - footer.outerHeight();
    if (height > 0) {
    footer.css({'margin-top' : height+'px'});
    }
    }
    stickyFooter();
    $(window).resize(function(){
    stickyFooter();
    });

	<?php
}


echo "});";
