﻿//Eric Tallman v.1.0

var isiStartPosFinal;
var currentPage;

$(document).ready(function () {


	//Get URL Filename
	var filename = location.pathname.substr(location.pathname.lastIndexOf("/") + 1, location.pathname.length);
	var filenameNoextension = filename.split('.');
	filenameNoextension = filenameNoextension[0];

	currentPage = filenameNoextension;

});


function selectNavPrimaryActive(nav) {
	$('.' + nav + ' .' + currentPage).each(function (index) {
		var primaryNavOn = 'a.' + $(this).attr('class');
		$(primaryNavOn).addClass('navOn');
	});
}


function selectNavSecondaryActive(options) {

	var options = options || {};
	options.navSecondaryContainer = options.navSecondaryContainer || "left";
	options.navSecondary = options.navSecondary || "leftNav";
	options.navSecondaryOnClass = options.navSecondaryOnClass || "active";
	options.navSecondaryLevelClassPrefix = options.navSecondaryLevelClassPrefix || "level";
	options.isMasterMenu = options.isMasterMenu || true;

	var activeNav = $('#' + options.navSecondary + ' .' + currentPage).addClass(options.navSecondaryOnClass);
	var navHtml = $('#' + options.navSecondaryContainer).html();

	//Set The Levels to the Parents
	applyDepth($('#' + options.navSecondaryContainer), 0, options);

	if (activeNav.size() > 0) {
		var search = true;
		//var firstNode = true;

		while (search) {
			//display everything in/next to this node for maximuim template support 

			if (activeNav.is('ul')) {
				activeNav.css({ 'display': 'block' });
			}

			if (activeNav.is('li')) {
				activeNav.children().css({ 'display': 'block' });
			}

			if (activeNav.attr('id') == options.navSecondary) {
				search = false;

				if (options.isMasterMenu) {
					var topNavOn = activeNav.attr('class');
					topNavOn = 'a.' + topNavOn;
					$(topNavOn).addClass('navOn');
				}
			}

			if (activeNav.parent().attr('id') == options.navSecondary) {
				activeNav.attr('id', 'topLevelActive');
			}

			activeNav = activeNav.parent();
		}
	}
}


function applyDepth($obj, x, options) {

	// Store our current jquery objects
	var $sub = $obj.children('ul').children('li:has(ul)');
	var $a = $sub.children('a');

	// Test is we need to continue loop - if not the function ends itself.
	if ($sub) {

		if (x !== 0) {
			$a.addClass(options.navSecondaryLevelClassPrefix + x);
		}

		else {
			$a.addClass(options.navSecondaryLevelClassPrefix);
		}

		//Incriment counter
		x++;

		$sub.each(function () {
			// Call the function one down the line...
			applyDepth($(this), x, options);
		});
	}
}

