$(document).ready(function() {
  // geneos specifically requires a different chunk file '../Data/Tocs/website_main_toc_Chunk0.js' because of legacy naming conventions
	require(['../../Data/Tocs/Master_Chunk0.js'],
			function(toc) {
				var name = window.location.pathname
        // need to add an element with class version-picker in non versioned projects master page to carry the product name info
        var $project = jQuery('.version-picker');
        // method of fetching path will vary between versioned and non-versioned projects e.g. op5 vs capacity planner. We use a switch statement to handle this for flexibility (may be new cases for plugin repos)
        switch($project.data('product')) {
          case "capacityPlanner":
            var path = "/" + name.split("capacity-planner/").pop()
            break;
          default:
            var path = "/" + name.split(".0/").pop()
						
						if (path == ("/" + window.location.pathname)) {
							path = "/" + name.split(".1/").pop()
						}
        } 
				
					var this_index = Number(toc[path].i)
					var prev_index = this_index - 1
					var next_index = this_index + 1

					for (key in toc) {
							if (toc[key].i == prev_index) {
									var prev_topic = String(toc[key].t)
							}
							if (toc[key].i == next_index) {
									if (String(toc[key].t) == "undefined") {
											next_index += 1
									} else {
											var next_topic = String(toc[key].t)
									}
							}
					}

					var prev_btn = $('button.prev-btn');
					var next_btn = $('button.next-btn');

					if (typeof prev_topic !== "undefined") {
							prev_btn.html('PREVIOUS TOPIC:<br>' + prev_topic);
					} else {
							for (key in toc) {
									if (toc[key].i == prev_index - 1) {
											var prev_topic = String(toc[key].t)
									}
							}
							if (typeof prev_topic == "undefined") {
									prev_btn.html('PREVIOUS TOPIC:<br> Home Page');
							} else {
									prev_btn.html('PREVIOUS TOPIC:<br>' + prev_topic)
							};
					}
					if (typeof next_topic !== "undefined") {
							next_btn.html('NEXT TOPIC:<br>' + next_topic);
					} else {
							for (key in toc) {
									if (toc[key].i == next_index + 1) {
											var next_topic = String(toc[key].t)
									}
							}
							if (typeof next_topic == "undefined") {
									next_btn.html('NEXT TOPIC:<br> Home Page');
							} else {
									next_btn.html('NEXT TOPIC:<br>' + next_topic)
							};

					}
			})
});