var activeTopic = null;
var topics = null;

// When loading topics and text from server
function loadTopics() {
	activeTopic = null;
	$.ajax({
		dataType: "json",
		url: "help/help-topics.txt",
		success: function(response) {
			if (response) {
				topics = response["topics"];
			} else {
				topics = [
					{title: "Not Available", text: "Not Available"}
				];
			}
			listTopics();
			showTopic(0);
			displayDoc(800, 550);
		},
		error: function (response) {
			displayError(lang["couldnotdooperation"]);
		}
	});
	return false;
}

// When clicking a topic to display it
function showTopic(id) {
	if (activeTopic != null) {
		$("#topic_"+activeTopic).removeClass("active");
	}
	activeTopic = id;
	$("#topic_"+activeTopic).addClass("active");
	$.ajax({
		dataType: "html",
		url: "help/"+language+"/"+topics[activeTopic]["url"],
		success: function(response) {
			if (response) {
				$("#divDocString").html(response);
			} else {
				$("#divDocString").html("Not Available");
			}
		},
		error: function (response) {
			$("#divDocString").html("Not Available");
		}
	});
}

// When listing topics
function listTopics() {
	var string = "";
	for (var i = 0;i < topics.length; i++) {
		string += "<div id=\"topic_"+i+"\">"+topics[i][language]["title"]+"</div>";
	}
	$("#divDocTableOfContents").html(string);
	// Assign on-click to each topic
	for (var i = 0;i < topics.length; i++) {
		$("#topic_"+i).click(function (){
			showTopic(this.id.split("_")[1]);
		});
	}
}

var helpLoaded = true;
