// JavaScript Navigation Trail

var trailMenu = new Object();
trailMenu["syllabus"] = "Syllabus";
trailMenu["practical_exam"] = "Practical Exam";
trailMenu["animations"] = "Animations";
trailMenu["atlas"] = "Atlas";
trailMenu["facial_muscles"] = "Facial Muscles";
trailMenu["figures"] = "Syllabus Figures";
trailMenu["oculo_reflex"] = "Oculo Reflex";
trailMenu["outlines"] = "Outlines";
trailMenu["parasymp_reflex"] = "Parasympathetic Reflex";
trailMenu["pathways"] = "Pathways";
trailMenu["glossary"] = "Glossary";
trailMenu["kw"] = "kw";
trailMenu["hyperbrain"] = "HyperBrain"; // home folder by definition if run from local drive


LocalTrailDef=false; // declare a global var
trail=false; // init a global var

function makeTrailMenu() {
    var parseStart, volDelim, parseEnd;
    var output = "<span style='font-family:Arial, Helvetica, sans-serif; font-size:11px; color:#5C1610; padding:4px'>";
    var linkStyle = "color:#555555";
    var path = location.pathname;
    var separator = "&nbsp;&raquo;&nbsp;";
    var re = /\\/g;
    if ( re.test ( path ) ) {
    	// document.write ( "<p>Dealing with Win path, " + path + "\n" );
    	
    }
    path = path.replace(re, "/");
    // document.write(path + "\n<br>");
    var leaftrail;
    var i;
    var leaves = path.split("/");
    if (leaves[leaves.length-1] == "index.php" || leaves[leaves.length-1] == "index.htm" || leaves[leaves.length-1] == "index.html") {
        parseEnd = leaves.length -1;
    } else {
        parseEnd = leaves.length;
    }
    // Begin alternative branch if a local disk based installation is requested
    // if (location.protocol.indexOf("file:") != -1) {
    if (location.protocol.indexOf("file:") === 0) {
        // parseStart = 1;
        // Beginning at the end of the "leaves" array ("rightLim"), iterate backwards until no
        //  defined trailMenu array members can be associated with the "leaves" array value
        //  (which will become the new parseStart value)
        // If file-based, the site should always end in "index.html", as there is no
        //  automatic/redirect server mapping for directory-based URLs.
        parseEnd = leaves.length -2;
        localURLCtr = leaves.length-1;
        LocalTrailDef = leaves[localURLCtr]; // global var picked up later by the sub-head include
        volDelim = "/";
        flagMoreMenuDefs = true;
        leafLvl = 0;
        lookBack = 0;
        leaf_output = '';
        for ( i=parseEnd; i >= 0; i-- ) {
        	// document.write("<p>Examining " + leaves[i] + "\n");
        	if ( ! parseStart ) {
        		parseStart = i;
        		// document.write("<p>Start at: " + parseStart + "; end at: " + parseEnd + "\n");
        	}
		if ( ! trail ) { trail = "index.html"; }
        	if ( ! trailMenu[leaves[i]] || leaves[i] == "hyperbrain" ) {
			flagMoreMenuDefs = false;
		}
		if ( ! flagMoreMenuDefs ) {
			trail = leaves[i] + "/" + trail;
        		// document.write("<p>" + trail + "\n");
        	} else {
        		// By definition, folder hyperbrain is always the home folder if it exists
			if ( leaves[i] == "hyperbrain" ) { continue; }
			if ( leaftrail == undefined ) { leaftrail = "index.html"; }
			if ( leafLvl > 0 ) { leaftrail = "../" + leaftrail; }
			// document.write("<p>Starting leaftrail: " + leaftrail + "\n");
			if ( leaves[leaves.length-1] != "index.html" ) {
			leaf_output = "</a>" + separator + leaf_output;
			leaf_output = trailMenu[leaves[i]] + leaf_output;
			leaf_output = "<a href='" + leaftrail + "' style='" + linkStyle + "'>" + leaf_output;
			}
			// document.write("<p>Leaftrail output: " + output + "\n");
			leafLvl++;
			// Get the current local path composed of right-end path atoms
			localURLCtr--;
			LocalTrailDef = leaves[localURLCtr] + '/' + LocalTrailDef; // global var picked up later by the sub-head include script, sub-header.js
        	}
        }
        trail = location.protocol + "//" + location.hostname + trail;
        // document.write("<p>Finished: " + trail + "\n");
	begin_output = '<a href="' + trail + '" style="' + linkStyle + '">';
	begin_output += "Home";
	begin_output += "</a>";
	if ( leaf_output ) { output = output + begin_output + separator + leaf_output; } else {output = output + begin_output + separator; }
	output += document.title + "</span>";
	return output;
    } else if (location.protocol.indexOf("file:") != -1) {
    	parseStart = 1;
        volDelim = "/";
    } else {
    	// Installation is Web-server based
        parseStart = 0;
        volDelim = "";
    }
    trail = location.protocol + "//" + location.hostname;
    for (var i = parseStart; i < parseEnd; i++) {
        // document.write ( "'" );
        if (i == parseStart) {
            trail += "/" + leaves[i] + volDelim;
            output += "<a href='" + trail + "' style='" + linkStyle + "'>";
            output += "Home";
        } else if (i == parseEnd - 1) {
            output += document.title;
            separator = "";
        } else {
            trail += leaves[i] + "/";
            output += "<a href='" + trail + "' style='" + linkStyle + "'>";
            output += trailMenu[leaves[i]];
        }
        output += "</a>" + separator;
    }
    output += "</span>";
    return output;
}
