﻿/*
** SideMenu.js
*/
$(function () {
	// Current url offset.  Note, we need to rely on the directory name
	// to identify the active menu group, so we strip off the page name
	// from the url.
	var root = (this.location.protocol + "//" + this.location.host + "/").toLowerCase();
	var path = this.location.href.toLowerCase();
	path = path.substring(0, path.lastIndexOf("/") + 1);

	// If 'root === path' then we are on the Home page and therefore
	// we don't want to expand any menus.  If this should ever change
	// then we will need to modify the following if statement to allow
	// for both situations.	Note, the issue her is that the root folder
	// will always match all the sub-folders, therefore we must manage
	// it directly.
	if (root !== path) {
		// Find all the top level item links
		$("ul.sideMenu > li > a").each(function () {
			var href = this.href.toLowerCase();
			if (href.indexOf(path) >= 0) {
				$(this).parent().children("ul").addClass("open").show();
			}
		});
	}

	// Check each link to see if they match the current location
	var currentUrl = Jes.Uri.getBaseUrl().toLowerCase();
	$("ul.sideMenu a").each(function () {
		var href = Jes.Uri.getBaseUrl(this.href).toLowerCase();
		if (currentUrl === href) {
			var $a = $(this).addClass("current");
			$a.parent().parents("li").each(function () {
				$(this).children("a").eq(0).addClass("current");
			});
		}
	});

	// Harcode the /index.aspx page to open The Course Overview menu
	if (currentUrl === "/index.aspx") {
		$("ul.sideMenu li.course ul").addClass("open").show();
		$("ul.sideMenu li.course a").addClass("current");
		$("ul.sideMenu li.course ul li a").removeClass("current");
		$("ul.sideMenu li.course ul li.overview a").addClass("current");
	}

});
