<div id="module_<?php echo($module_instance_id); ?>" class="module menu-module <?php echo($module_class); ?>">
	<?php
	if($title && $title_heading_level)
	{
		?><<?php echo($title_heading_level); ?> class="module-title"><?php echo($title); ?></<?php echo($title_heading_level); ?>><?php
	}
	?>
	<nav role="navigation">
		<div class="container-fluid">
			<div data-nav="priority"><?php $module->menu(); ?></div>
		</div>
	</nav>

	<div class="nvmenu-overlay" id="nvmenu-overlay-<?php echo($module_instance_id); ?>">
		<div class="nvmenu-overlay-container">
			<nav class="nvmenu nvmenu-vertical" role="navigation">
				<div class="row">
					<div class="col-8 nvmenu-title"></div>
					<div class="col-4 text-right">
						<a href="#" onclick="close_nvmenu<?php echo($module_instance_id); ?>(event)" class="nvmenu-close" title="Close This Menu"><em class="fas fa-times-circle" aria-hidden="true"></em><span class="d-none">Close This Menu</span></a>
					</div>

					<?php
					if($content_above)
					{
						?><div class="col-12"><?php echo($content_above); ?></div><?php
					}

					?>
					<div class="col-12 nvmenu-items"></div>
					<div class="col-12"></div>
					<?php
					if($search)
					{
						?>
						<div class="col-12">
							<fieldset>
								<?php
								if($search_legend)
								{
									?><legend><?php echo($search_legend); ?></legend><?php
								}
								?>
								<form action="<?php echo($search_action); ?>" method="get" class="form-row">
									<div class="col-11"><input type="search" placeholder="Search" aria-label="Search Term" name="q" value="<?php echo(html_escape($this->input->get_post("q"))); ?>" class="form-control" /></div>
									<div class="col-1"><button type="submit" class="btn btn-tertiary" title="<?php echo($search_legend); ?>"><em class="fas fa-fw fa-search" aria-hidden="true"></em><span class="d-none">Search</span></button></div>
								</form>
							</fieldset>
						</div>
						<?php
					}
					?>
					<div class="col-12"><div id="nvmenu-content-below"><?php echo($content_below); ?></div></div>
				</div>
			</nav>
		</div>
	</div>
</div>

<script>
var nvmenu_content_below = jQuery("#nvmenu-content-below").html();

jQuery("#module_<?php echo($module_instance_id); ?> .nvmenu-overlay-container").off("click").on("click", function(e)
{
	try
	{
		e.stopPropagation();
	}
	catch(e)
	{
		// For IE
		try
    	{
    		window.event.cancelBubble = true;
    	}
    	catch(e){}
	}
});

jQuery("#module_<?php echo($module_instance_id); ?> .nvmenu-overlay").off("click").on("click", function(e)
{
	try
	{
		e.stopPropagation();
	}
	catch(e)
	{
		// For IE
		try
    	{
    		window.event.cancelBubble = true;
    	}
    	catch(e){}
	}

	close_nvmenu<?php echo($module_instance_id); ?>(e);
});

jQuery(document).ready(function()
{
	<?php
	if($responsive)
	{
		?>
		window.navPriority('#module_<?php echo($module_instance_id); ?> div[data-nav="priority"]',
		{
			dropdownLabel: "<?php echo(addslashes($more_text)); ?>",
			menuLabel: "<?php echo(addslashes($menu_text)); ?>",
			threshold: <?php echo($threshold); ?>,
			containerWidthOffset: <?php echo($container_width_offset); ?>,
			dropdownMenuTemplate: '<li data-nav-priority-menu class="nav-item navbar-nav-more" aria-hidden="true">' +
		        '<a id="{{dropdownMenuId}}" onclick="open_nvmenu_overflow<?php echo($module_instance_id); ?>(event)" href="#" class="nav-link navbar-toggle-more" role="button" aria-haspopup="true" aria-expanded="false" data-nav-priority-toggle>' +
		          '{{dropdownLabel}}' +
		        '</a>' +
		        '<ul style="display: none" aria-labelledby="{{dropdownMenuId}}" class="nvmenu-overflow"></ul>' +
		      '</li>'
		});
		<?php
	}
	?>
});

function close_nvmenu<?php echo($module_instance_id); ?>(e)
{
	try
	{
		e.preventDefault();
	}
	catch(e){}

	jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?>").fadeOut(500);
}

function open_nvmenu<?php echo($module_instance_id); ?>(e)
{
	// jQuery("#nvmenu-content-below").removeClass("padded");

	// if(nvmenu_content_below != "" && nvmenu_content_below != "0")
	// {
	// 	jQuery("#nvmenu-content-below").addClass("padded").html(nvmenu_content_below);
	// }

	try
	{
		e.preventDefault();
	}
	catch(e){}

	var target = jQuery(e.target);

	if(!target.hasClass("nav-link"))
	{
		target = target.parents(".nav-link").eq(0);
	}

	// This allows deep linking if a parent item was clicked from a main menu bar.
	if(target.hasClass("dropdown-toggle"))
	{
		target = target.siblings(".nvmenu-overlay-data-items").eq(0).find("li.first .nav-link").eq(0);
	}

	var content_below = target.siblings('.nvmenu-content-below').eq(0).html();

	// if(content_below != "" && content_below != "0")
	// {
	// 	jQuery("#nvmenu-content-below").addClass("padded").html(content_below);
	// }

	jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?>").css({height: jQuery(document).outerHeight(true) + "px"});

	// If the overlay is already visible, slide it sideways. Otherwise, fade it in.
	if(jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?>:visible").size() > 0)
	{
		jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-overlay-container").fadeOut(250, function()
		{
			jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-title").html(target.siblings(".nvmenu-overlay-data-title").eq(0).html());
			jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-items").html(target.siblings(".nvmenu-overlay-data-items").eq(0).html());
			jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-overlay-container").fadeIn(250);
		});
	}
	else
	{
		jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-title").html(target.siblings(".nvmenu-overlay-data-title").eq(0).html());
		jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?> .nvmenu-items").html(target.siblings(".nvmenu-overlay-data-items").eq(0).html());
		jQuery("#nvmenu-overlay-<?php echo($module_instance_id); ?>").fadeIn(500);
	}
	jQuery(window).scrollTop(0);
}

function open_nvmenu_overflow<?php echo($module_instance_id); ?>(e)
{
	var items = jQuery("#module_<?php echo($module_instance_id); ?> .nvmenu-overflow > li");
	var more_menu = '<ul class="row">';

	for(var i=0; i<items.size(); i++)
	{
		items.eq(i).addClass("col-12 col-md-6 col-lg-4 col-xl-3");
		more_menu += items.eq(i)[0].outerHTML;
		items.eq(i).removeClass("col-12 col-md-6 col-lg-4 col-xl-3");
	}

	more_menu += '</ul>';
	jQuery("#module_<?php echo($module_instance_id); ?> .nvmenu-items").html(more_menu);

	open_nvmenu<?php echo($module_instance_id); ?>(e);
}
</script>
