var maestroID = -1;

$(document).ready(function() {
	loadMaestroID();
});


function loadMaestroID() {
	maestroID = sessionStorage.getItem("maestroID");
	if(maestroID === null) {
		maestroID = -1;
		sessionStorage.setItem("maestroID", maestroID);
	}
	maestroID = Number(maestroID);
	console.log("loaded maestroID : " + maestroID);
}

function saveMaestroID(maestroID) {
	sessionStorage.setItem("maestroID", maestroID);
	// console.log("saved maestroID : " + maestroID);
}

function getParentDirectory(url) {
	var directories = url.split("/");
	if(directories.length < 2)
		return "";

	return directories[directories.length - 2];
}

function getPage(url) {
	var directories = url.split("/");
	if(directories.length < 1)
		return "";

	return directories[directories.length - 1];
}

function goHome() {
	var url = window.location.href;

	if(getParentDirectory(url) === "maestro") {
		if(getPage(url) !== "main_menu.html")
			location.href = "./../maestro/main_menu.html";

		return;
	}

	// parent directory is "main"
	if(getPage(url) === "faq.html") {
		if(maestroID === -1) {
			location.href = "./../main/index.html";
		} else {
			location.href = "./../maestro/main_menu.html";
		}
	} else if(getPage(url) !== "index.html")
		location.href = "./../main/index.html";
}

function logout() {
	sessionStorage.clear();
	maestroID = -1;
	sessionStorage.setItem("maestroID", maestroID);
	location.href = './../main/index.html';
}

function showErrorMessage(message, type) {
	console.log("showErrorMessage : " + message);
	var messageBox = $("#message_box");

	if(!messageBox)
		return;

	var alertType = "";
	switch(type) {
		case "success":
		alertType = "alert-success";
		break;
		case "warning":
		alertType = "alert-warning";
		break;
		case "info":
		alertType = "alert-info";
		break;
		case "error":
		default:
		alertType = "alert-danger";
	}
	messageBox.html(
		'<div class="alert ' + alertType + '" role="alert">'
			+ '<button type="button" class="close" data-dismiss="alert">&times;</button>'
			+ message
		+ '</div>'
	);
}