/**
 * CMS Javascript Functions
 * @author Gen25
 * @version 0.8.1
 * @copyright Copyright Gen25 2010
 */

// Menu
$(document).ready(function(){
	$('li', '.cms_toolbar').hover(
		function() {
			$(this).not('.menu_separator').addClass('active');
			$('ul', this).fadeIn('fast', function() {
				$(this).css('opacity', 0.8);
			});
		},
		function() {
		  	$(this).removeClass('active');
			$('ul', this).fadeOut('fast');
		}
	);
});

$(document).keyup(function(e) {
	if (e.keyCode == 27  && $("#cms_edit").is(":visible")) {
		hideEditArea(true);
	}
});

function loadSection(sectionid, lang) {
	showLoading();
	$.ajax({
		url: "ajax/getSection.php?sectionid=" + sectionid + "&lang=" + lang,
		success: function(data) {
			$("#section_" + sectionid).html(data);
			hideLoading();
		}
	});
}

function editCms() {
	showLoading();
	showEditArea();
	$.ajax({
		url: "ajax/processCms.php?action=edit",
		success: function(data) {
		$("#cms_edittitle").html(LANGITEM["edit_websettings"]);
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function updateCms(form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (!checkCmsUpdate()) {
		showMessage(LANGITEM["msg2"], "info");
		return false;
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processCms.php?action=update",
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
			location.reload(true);
		}
	});
}

function checkCmsUpdate() {
	var url = document.getElementById("url").value;
	if ((url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") || url.substr(url.length-1, 1) != "/") {
		alert(LANGITEM["checkCmsUpdate_msg"]);
		return false;
	}
	return true;
}

function selectPage(id, lang) {
	document.location = "index.php?pid=" + id + "&lang=" + lang;
}

function newPage() {
	showLoading();
	showEditArea();
	$.ajax({
		url: "ajax/processPage.php?action=new",
		success: function(data) {
			$("#cms_edittitle").html(LANGITEM['new_page']);
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function newPageInNewLang(id, oldLang, newLang) {
	showLoading();
	$.ajax({
		url: "ajax/processPage.php?action=insertNewLang&id=" + id + "&oldLang=" + oldLang + "&newLang=" + newLang,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["newPageInNewLang_msg1"], "check");
			} else {
				showMessage(LANGITEM["newPageInNewLang_msg2"], "error");
				handleError(data);
			}
			hideLoading();
			location.reload(true);
			document.location.href="index.php?pid="+id+"&lang="+newLang;
		}
	});

}

function editPage(id, lang) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["edit_page"]);
	$.ajax({
		url: "ajax/processPage.php?action=edit&id=" + id + "&lang=" + lang,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function insertPage(form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (!checkPageInsert()) {
		showMessage(LANGITEM["msg2"], "info");
		return false;
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processPage.php?action=insert",
		data: postVars,
		success: function(data) {
			/*
			Krijgt hier een id terug.
			Moet dus eigenlijk even checken of het numeriek is.

			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			*/
			hideEditArea(false);
			hideLoading();
			location.reload(true);
		}
	});
}

function checkPageInsert() {
	return true;
}

function updatePage(id, lang, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (!checkPageUpdate()) {
		showMessage(LANGITEM["msg2"], "info");
		return false;
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processPage.php?action=update&id=" + id + "&lang=" + lang,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
			location.reload(true);
		}
	});
}

function checkPageUpdate() {
	$.ajax({
		async: false,
		url: "ajax/processPage.php?action=checkExists&url=" + document.getElementById("url").value,
		success: function(data) {
			if (data == '1') {
				$("#url").addClass("cms_error");
				alert(LANGITEM["short_url"]);
				return false;
			} else {
				return true;
			}
		}
	});

	return true;
}

function editSetting(module) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["edit_settings"]);
	$.ajax({
		url: "ajax/processSetting.php?action=edit&module=" + module,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function updateSetting(module, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (eval("typeof check" + module + "SettingUpdate == 'function'")) {
		if (eval("!check" + module + "SettingUpdate()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processSetting.php?action=update&module=" + module,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
		}
	});
}

function newModule(module) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["new_element"]);
	$.ajax({
		url: "ajax/processModule.php?action=new&module=" + module,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function editModule(module, id) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["edit_element"]);
	$.ajax({
		url: "ajax/processModule.php?action=edit&module=" + module + "&id=" + id,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function insertModule(module, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (eval("typeof check" + module + "Insert == 'function'")) {
		if (eval("!check" + module + "Insert()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModule.php?action=insert&module=" + module,
		data: postVars,
		success: function(data) {
			/*
			Krijgt hier een id terug.
			Moet dus eigenlijk even checken of het numeriek is.

			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			*/
			hideEditArea(false);
			hideLoading();
		}
	});
}

function draftModule(module, id, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (eval("typeof check" + module + "Draft == 'function'")) {
		if (eval("!check" + module + "Draft()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModule.php?action=draft&module=" + module + "&id=" + id,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
		}
	});
}

function updateModule(module, id, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	if (eval("typeof check" + module + "Update == 'function'")) {
		if (eval("!check" + module + "Update()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModule.php?action=update&module=" + module + "&id=" + id,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
		}
	});
}

function newModuleLink(sectionid, lang, selectedmodule, selectedmoduleid) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["new_element"]);
	$.ajax({
		url: "ajax/processModuleLink.php?action=new&sectionid=" + sectionid + "&lang=" + lang + "&selectedmodule=" + selectedmodule + "&selectedmoduleid=" + selectedmoduleid,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function editModuleLink(id, lang, selectedmodule, selectedmoduleid, getVars) {
	showLoading();
	showEditArea();
	$("#cms_edittitle").html(LANGITEM["edit_element"]);
	$.ajax({
		url: "ajax/processModuleLink.php?action=edit&id=" + id + "&lang=" + lang + "&selectedmodule=" + selectedmodule + "&selectedmoduleid=" + selectedmoduleid + "&" + unescape(getVars),
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function insertModuleLink(sectionid, lang, form) {

	$('#cms_button_update').attr("disabled", "true");
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}

	module = form.elements['modulename'].value;
	if (eval("typeof check" + module + "Insert == 'function'")) {
		if (eval("!check" + module + "Insert()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	
	showLoading();
	postVars = makePostString(form);

	$.ajax({
		type: "POST",
		url: "ajax/processModuleLink.php?action=insert&sectionid=" + sectionid + "&lang=" + lang,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
			loadSection(sectionid, lang);
		}
	});
}

function updateModuleLink(id, lang, sectionid, form, getVars) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	module = form.elements['modulename'].value;
	if (eval("typeof check" + module + "Update == 'function'")) {
		if (eval("!check" + module + "Update()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModuleLink.php?action=update&id=" + id + "&lang=" + lang + "&" + unescape(getVars),
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
			loadSection(sectionid, lang);
		}
	});
}

function draftModuleLink(id, lang, sectionid, form, getVars) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	module = form.elements['modulename'].value;
	if (eval("typeof check" + module + "Draft == 'function'")) {
		if (eval("!check" + module + "Draft()")) {
			showMessage(LANGITEM["msg2"], "info");
			return false;
		}
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModuleLink.php?action=draft&id=" + id + "&lang=" + lang + "&" + unescape(getVars),
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
			loadSection(sectionid, lang);
		}
	});
}

function deleteModuleLink(id, lang, sectionid, getVars) {
	var r = confirm(LANGITEM["del_confirm"]);
	if (r == true) {
		showLoading();
		$.ajax({
			type: "POST",
			url: "ajax/processModuleLink.php?action=delete&id=" + id + "&lang=" + lang + "&" + unescape(getVars),
			success: function(data) {
				if (data == 1) {
					showMessage(LANGITEM["deleted"], "check");
				} else {
					showMessage(LANGITEM["del_err"], "error");
					handleError(data);
				}
				hideEditArea(false);
				hideLoading();
				loadSection(sectionid, lang);
			}
		});
	}
}

function moveModuleLink(id, lang, sectionid, direction) {
	showLoading();
	$.ajax({
		type: "POST",
		url: "ajax/processModuleLink.php?action=move&id=" + id + "&lang=" + lang + "&direction=" + direction,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["moved"], "check");
			} else {
				showMessage(LANGITEM["move_err"], "error");
				handleError(data);
			}
			hideLoading();
			loadSection(sectionid, lang);
		}
	});
}

function editModuleSettings(module) {
	showLoading();
	showEditArea();
	$.ajax({
		url: "ajax/processModule.php?action=editSettings&module=" + module,
		success: function(data) {
			$("#cms_edittitle").html(LANGITEM["edit_module_settings"]);
			$("#cms_edit").html(data);
			hideLoading();
		}
	});
}

function updateModuleSettings(module, form) {
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}
	showLoading();
	postVars = makePostString(form);
	$.ajax({
		type: "POST",
		url: "ajax/processModule.php?action=updateSettings&module=" + module,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				showMessage(LANGITEM["msg3"], "check");
			} else {
				showMessage(LANGITEM["msg4"], "error");
				handleError(data);
			}
			hideEditArea(false);
			hideLoading();
		}
	});
}

var editToolsTimer;
function showEditTools(id) {
	clearTimeout(editToolsTimer);
	$(".cms_edittools").css('display', 'none');
	document.getElementById("edittools_" + id).style.display = "block";
	//document.getElementById("modulelink_" + id).style.backgroundColor = "#f0f0f0";
}

function hideEditTools(id) {
	objectName = "edittools_" + id;
	//document.getElementById("modulelink_" + id).style.backgroundColor = "";
	document.getElementById("edittools_" + id).style.display = "block";
	editToolsTimer = setTimeout('document.getElementById(objectName).style.display = "none"', 2000);
}

function showEditArea() {
	$("#cms_editwrapper").fadeIn('fast', function() {
		$("#cms_editwrapper").css('opacity', 0.7);
	});
	$("#cms_edittitle").fadeIn('fast');
	$("#cms_editclose").fadeIn('fast');
	$("#cms_edit").fadeIn('fast');

	$("#cms_editwrapper").height($(window).height()-90);
	$("#cms_edit").height($(window).height()-160);
}

function hideEditArea(showConfirm) {
	if (showConfirm == true) {
		var close = confirm(LANGITEM["close_confirmation"])
	}
	if (showConfirm == false || close == true) {
		document.getElementById("cms_edit").innerHTML = "";

		$("#cms_editwrapper").fadeOut('fast');
		$("#cms_edittitle").fadeOut('fast');
		$("#cms_editclose").fadeOut('fast');
		$("#cms_edit").fadeOut('fast');
	}
}

function showMessage(message, type) {
	if (type == "info") {
		message = "<p style=\"margin:0px; padding:0px; color:#fff;\"><img src=\"img/icons/information.png\" alt=\"Info\" /> " + message + "</p>";
	} else if (type == "check") {
		message = "<p style=\"margin:0px; padding:0px; color:#fff;\"><img src=\"img/icons/accept.png\" alt=\"Check\" /> " + message + "</p>";
	} else if (type == "error") {
		message = "<p style=\"margin:0px; padding:0px; color:#fff;\"><img src=\"img/icons/error.png\" alt=\"Warning\" /> " + message + "</p>";
	}

	document.getElementById("cms_message").innerHTML = message;
	$("#cms_message").fadeIn("fast", function() {
		$("#cms_message").css('opacity', 0.8);
	});
	setTimeout("hideMessage();", 3000);
}

function hideMessage() {
	$("#cms_message").fadeOut("slow", function() {
			$("#cms_message").innerHTML = "";
		}
	);
}

function showLoading() {
	document.getElementById("cms_loading").innerHTML = "<img src=\"img/ajax-loader.gif\" alt=\"Loading...\" />";
	$("#cms_loading").show();
}

function hideLoading() {
	document.getElementById("cms_loading").innerHTML = "";
	$("#cms_loading").hide();
}

function handleError(message) {
	alert(message);
}

function makePostString(form) {
	postVars = "";
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].className == "editor") {
			var editor = tinyMCE.getInstanceById(form.elements[i].id);
			if(editor){
				value = editor.getContent();
			}
		} else {
			value = form.elements[i].value;
		}

		// Add to varstring if checked
		if (form.elements[i].type == "radio") {
			if (form.elements[i].checked == true) {
				postVars += form.elements[i].name + "=" + encodeURIComponent(value) + "&";
			}
		} else if (form.elements[i].type == "checkbox") {
			if (form.elements[i].checked == true) {
				postVars += form.elements[i].name + "=" + encodeURIComponent(value) + "&";
			} else {
				postVars += form.elements[i].name + "=&";
			}
		} else {
			postVars += form.elements[i].name + "=" + encodeURIComponent(value) + "&";
		}
	}

	return postVars;
}

function checkFormValues(form) {
	var ok = true
	for (var i = 0; i < form.elements.length; i++) {
		if ($(form.elements[i]).hasClass("cms_required_value") && form.elements[i].value == "") {
			$(form.elements[i]).addClass("cms_error");
			$(form.elements[i]).focus(function() {
				$(this).removeClass("cms_error");
			});
			ok = false;
		} else if ($(form.elements[i]).hasClass("cms_required_number") && !isNumeric(form.elements[i].value)) {
			$(form.elements[i]).addClass("cms_error");
			$(form.elements[i]).focus(function() {
				$(this).removeClass("cms_error");
			});
			ok = false;
		} else if ($(form.elements[i]).hasClass("cms_required_email") && isEmail(form.elements[i].value)) {
			$(form.elements[i]).addClass("cms_error");
			$(form.elements[i]).focus(function() {
				$(this).removeClass("cms_error");
			});
			ok = false;
		} else if ($(form.elements[i]).hasClass("cms_required_checked") && form.elements[i].checked == false) {
			$(form.elements[i]).addClass("cms_error");
			$(form.elements[i]).focus(function() {
				$(this).removeClass("cms_error");
			});
			ok = false;
		}
	}

	return ok;
}
