function displayContents(rawMarkdown, section) {
	var pageContents = marked(rawMarkdown);
	document.getElementById(section).innerHTML = pageContents;
}

function attachEditor(markdownFile) {
	document.getElementById("midaswiki-editor").style.display = "none";
	document.getElementById("content").style.display = "block";

	//determine if history page or talk page
	var location = window.location.hash;
	var pageType = "";
	if(location != "" && typeof location != typeof undefined) {
		pageType = location.split('#')[1].split("-")[0];
		location = location.split("#")[1].split("-")[1];
	}

	var header = document.getElementById("controls");
	header.innerHTML = "";

	var controlContainer = document.createElement('ul');
	controlContainer.className += "tabs"; /* causes a jQuery error, need to investigate */
	if(pageType != "history" && pageType != "edit") {
		var historyTab = document.createElement("li");
		historyTab.className += "tab";
		var historyLink = document.createElement("a");
		historyLink.innerHTML = "History";
		historyLink.href = "#history-"+markdownFile;
		historyTab.appendChild(historyLink);
		//controlContainer.appendChild(historyTab);
	}

	if(pageType != "history" && pageType != "talk" && pageType != "edit") {
		var talkTab = document.createElement("li");
		talkTab.className += "tab";
		var talkLink = document.createElement("a");
		talkLink.innerHTML = "Talk";
		talkLink.href = "#talk-"+markdownFile;
		talkTab.appendChild(talkLink);
		controlContainer.appendChild(talkTab);
	}

	if(pageType != "edit" && pageType != "history") {
		var editTab = document.createElement("li");
		editTab.className += "tab";
		var editLink = document.createElement("a");
		editLink.innerHTML = "Edit";
		editLink.href = "#edit-"+markdownFile;
		editTab.appendChild(editLink);
		controlContainer.appendChild(editTab);
	}

	header.appendChild(controlContainer);
}

function displayEditor(raw, markdownFile) {
	document.getElementById("midaswiki-editor").style.display = "block";
	document.getElementById("content").style.display = "none";
	var editfield = document.getElementById("midaswiki-edit");
	editfield.value = raw;
}

function pageReload() {
	var location = window.location.hash.toLowerCase().replace("../", "");
	if(location == "" || typeof location == typeof undefined) {
		location = "#/index.md";
	}
	location = location.split('#')[1];
	var pageType = location.split("-")[0];
	var markdownFile = location.split("-")[1];

	document.getElementById("midaswiki-file").value = markdownFile;

	var noCacheHeaders = {
		"Pragma": "no-cache",
		"Cache-Control": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
		"Expires": 0,
		"Last-Modified": new Date(0)//, // January 1, 1970
		//"If-Modified-Since": new Date(0)
	};

	var promise = ajaxGetRaw("/markdown/header.md", noCacheHeaders);
	promise.success = function(response) {
		displayContents(response.trim(), "header");
	};
	promise.failure = function(response) {
		console.error("Error retrieving header");
	};

	//console.log(pageType);
	if(pageType == "edit") {
		
		var promise = ajaxGetRaw("/markdown"+ markdownFile, noCacheHeaders);
		promise.success = function(response) {
			displayEditor(response.trim(), markdownFile);

		};
		promise.failure = function(response) {
			console.error("Error building editor for " + markdownFile);
			displayEditor(markdownFile, markdownFile);
		};

	} else {
		//attachEditor(location);
		var promise = ajaxGetRaw("/markdown"+ location, noCacheHeaders);
		promise.success = function(response) {
			displayContents(response.trim(), "content");
		};
		promise.failure = function(response) {
			console.error("Error retrieving " + location);
			displayContents("Page not found: " + location, "content");
		};
	}

	var promise = ajaxGetRaw("/markdown/footer.md", noCacheHeaders);
	promise.success = function(response) {
		displayContents(response.trim(), "footer");
	};
	promise.failure = function(response) {
		console.error("Error retrieving footer");
	};
}

document.addEventListener("DOMContentLoaded", function(event) {
	pageReload();
});

window.addEventListener("hashchange", function(event) {
	pageReload();
});

document.getElementById("midaswiki-save").addEventListener("click", function(event) {
	var editfield = document.getElementById("midaswiki-edit");
	var markdownFile = document.getElementById("midaswiki-file").value;
	var data = {"markdown": editfield.value, "filename": markdownFile};
	var savePromise = ajaxPost("/api/", data);
	savePromise.success = function(response) {
		window.location.hash = "#" + markdownFile;
	};			
});
document.getElementById("midaswiki-cancel").addEventListener("click", function(event) {
	var markdownFile = document.getElementById("midaswiki-file").value;
	window.location.hash = "#" + markdownFile;
});


