// Helpers
function gEBId(id) { return document.getElementById(id) };

function microAjaxJSON(query,callback) {
	microAjax(query,function (res) {
		var parsed_json;
		try {
			parsed_json = JSON.parse(res);
		} catch(err) {
			alert(res);
			return;
		}
		callback(parsed_json);
	});
}

// Admin simple actions
function select_fold(path) {
	gEBId("fold_path").value=path;
}

// Admin AJAX actions
function load_page_props(path) {
	gEBId("page_path").value=path;
	var url = "ajax.php?action=load_page_props&path=" + encodeURIComponent(path);
	microAjaxJSON(url, function (parsed_json) {
		gEBId("page_title").value = parsed_json.page_title;
		gEBId("page_description").value = parsed_json.page_description;
		gEBId("page_keywords").value = parsed_json.page_keywords;
	});
}

function load_media_props(path) {
	gEBId("media_path").value=path;
	var url = "ajax.php?action=load_media_props&path=" + encodeURIComponent(path);
	microAjaxJSON(url, function (parsed_json) {
		gEBId("media_title").value = parsed_json.media_title;
		gEBId("media_description").value = parsed_json.media_description;
		//gEBId("media_keywords").value = parsed_json.media_keywords;
	});
}

function save_page_props() {
	var path = gEBId("page_path").value;
	var page_title = gEBId("page_title").value;
	var page_description = gEBId("page_description").value;
	var page_keywords = gEBId("page_keywords").value;

	//TODO : check against regex

	var url = "ajax.php?action=save_page_props"
			+ "&path=" + encodeURIComponent(path)
			+ "&page_title=" + encodeURIComponent(page_title)
			+ "&page_description=" + encodeURIComponent(page_description)
			+ "&page_keywords=" + encodeURIComponent(page_keywords);

	microAjaxJSON(url, function (parsed_json) {
		if ( parsed_json.result != "OK" ) {
			alert("Error\nResult: " + parsed_json.result + "\nRequest: " + url);
			return;
		}
		//TODO : says to user that the work is done
	});
}

function save_media_props() {
	var path = gEBId("media_path").value;
	var title = gEBId("media_title").value;
	var description = gEBId("media_description").value;
	//var keywords = gEBId("media_keywords").value;

	//TODO : check against regex

	var url = "ajax.php?action=save_media_props"
			+ "&path=" + encodeURIComponent(path)
			+ "&title=" + encodeURIComponent(title)
			+ "&description=" + encodeURIComponent(description);
	//		+ "&keywords=" + encodeURIComponent(keywords);

	microAjaxJSON(url, function (parsed_json) {
		if ( parsed_json.result != "OK" ) {
			alert("Error\nResult: " + parsed_json.result + "\nRequest: " + url);
			return;
		}
		//TODO : says to user that the work is done
	});
}

function save_site_props() {
	//TODO
}

// Admin other actions (with page change or refresh)
function go_add_form(kind) {
	document.location = 'add.php?kind=' + encodeURIComponent(kind);
}

function go_add(kind,type) {
	var path = gEBId("fold_path").value;
	var name = gEBId("fold_add_name").value;
	// TODO : check name and path against regex
	var url = 'add.php?kind=' + encodeURIComponent(kind)
		+ '&action=add_' + encodeURIComponent(type)
		+ '&path=' + encodeURIComponent(path)
		+ '&name=' + encodeURIComponent(name);
	document.location = url;
}

function go_edit_page() {
	var path = gEBId("page_path").value;
	document.location = 'render.php?action=edit&page=' + encodeURIComponent(path);
}

function go_delete_page() {
	var path = gEBId("page_path").value;
	//TODO : confirmation, ajax query, if OK, confirm then refresh
}

function go_delete_media() {
	var path = gEBId("page_path").value;
	//TODO : confirmation, ajax query, if OK, confirm then refresh
}