var loading= 	'<div class="loading_gif">'+
					'<img src="gs/loader_gif_deg_big_text.gif"></img>'+
					'</div>';
			
function showMessageBox(message) {
	if(message.length > 0) {
		$('#messages').html(message).css('top', $(document).scrollTop()+50).jqmShow();
		setTimeout('closeMessageBox()', 1500);
	}
}
function closeMessageBox() {
	$('#messages').jqmHide();
}
function showErrorBox(message) {
	$('#errors').html(message).css('top', $(document).scrollTop()+50).jqmShow({overlay:0});
}
function closeErrorBox() {
	$('#errors').jqmHide();
}
function closeAllBoxes() {
	closeMessageBox();
	closeErrorBox();
	closeDialogBox();
	closeLoginWindow();
}
function dialogBox(template, params) {
	var url = "_get.php?template="+template;
	$('#dialog').html(loading).load(url, params).css('top', $(document).scrollTop()+50).jqmShow();
}
function closeDialogBox() {
	$('#dialog').jqmHide();
}

function showLoginWindow(){
	$('#loginwindow').html(loading).load("_get.php", {template: "login"}, function(){
		$("#login").submit(function(){
			$(this).ajaxSubmit({dataType:"json",success:function(response){
				if(response.status=='1'){
					showMessageBox("You were logged in!");
					closeLoginWindow();
					user_authenticated = true;
					updateThingsWithLoginLogout();
				}else{
					showErrorBox("There was an error logging you in!<br />" + response.errors);
				} return false;
			}});
			return false;
		});
	}).css('top', $(document).scrollTop()+50).jqmShow();
}

function closeLoginWindow() {
	$('#loginwindow').jqmHide();
}

function updateThingsWithLoginLogout(){
	updateLeftNav();
	updateSection();
	updateEditor();
	updateSubHeader();
}

function updateSubHeader(){
	$("#subheader").load("_get.php",{template:"subheader_getter",inner:true});
}

function updateLeftNav(){
	$("#left_nav").load("_get.php",{template:"left_nav"});
}

function updateSection(){
	
}

function showRegisterInputs(){
	$("input[value='login']").remove();
	        $('#register_controls').show();
			$('#register_controls .action_register').removeAttr("disabled");
			$('#register_controls .action_login').attr("disabled","true");
	        $("#register").click(function(){
	                $("#login").ajaxSubmit({dataType:"json",success:function(response){
	                        if(response.status=='1' || response.status == 1){
	                                closeLoginWindow();
	                                $('#messages').html("CONGRATULATIONS! You now have full privileges to use ABSML.").css('top', $(document).scrollTop()+300).jqmShow();
	                                setTimeout('closeMessageBox()', 4000);
	                                updateThingsWithLoginLogout();
	                        }else{
	                                showErrorBox("There was an error registering your new account!" + response.errors);
	                        } return false;
	                }});
	                return false;
	        });
	
}

function logout(){
	$.get("_do.php",{action:"logout"},function(response){
		if(response=='1'){
			showMessageBox("You were successfully logged out.");
			updateThingsWithLoginLogout();
			user_authenticated = false;
			currently_editing = false;
		}return false;
	});
}

//page editing functions

function resetEditor(){
	editingPage = false;
	editingSection = false;
}

function editThisPage(){
	editingPage = true;
	$("#content .inner_content").load('_get.php', {template:"pageedit_getter",page:currentPage}, function(){
		$("#content .inner_content .section_page_list").sortable({delay:60});
		$(".trash").click(function(e){
			$li = $(this).parent();
			$li.children(".trash").hide();
			$li.children("div.clear").before("<p style='float:rigth;' class='confirm'>Delete Input? <a nohref class='no'>no</a> <a nohref class='yes'>yes</a></p>");
			var id = $li.attr("id");
			$li.children(".confirm").children(".yes").click(function(){
				removeSection(id);
			});
			$li.children(".confirm").children(".no").click(function(){
				$li.children(".confirm").remove();
				$li.children(".trash").show();
			});
		});
		
	});
}

function addSection(){
	$.post("_do.php",{action:"addSection",p:currentPage},function(){
		$("#content .inner_content").load('_get.php', {template:"pageedit_getter",page:currentPage}, function(){
			$("#sortable_section_page_list").sortable('enable');
			$(".trash").click(function(e){
				$li = $(this).parent();
				$li.children(".trash").hide();
				$li.children("div.clear").before("<p style='float:rigth;' class='confirm'>Delete Input? <a nohref class='no'>no</a> <a nohref class='yes'>yes</a></p>");
				var id = $li.attr("id");
				$li.children(".confirm").children(".yes").click(function(){
					removeSection(id);
				});
				$li.children(".confirm").children(".no").click(function(){
					$li.children(".confirm").remove();
					$li.children(".trash").show();
				});
			});
		});
		loadSectionNavAjax();
	})
}

function removeSection(s_id){
	$.post("_do.php",{action:"removeSection",p:currentPage, s:s_id},function(response){
		if(response=='1'){
			showMessageBox("The section was successfully removed.");
			$("#content .inner_content").load('_get.php', {template:"pageedit_getter",page:currentPage}, function(){
				$("#sortable_section_page_list").sortable('enable');
				$(".trash").click(function(e){
					$li = $(this).parent();
					$li.children(".trash").hide();
					$li.children("div.clear").before("<p style='float:rigth;' class='confirm'>Delete Input? <a nohref class='no'>no</a> <a nohref class='yes'>yes</a></p>");
					var id = $li.attr("id");
					$li.children(".confirm").children(".yes").click(function(){
						removeSection(id);
					});
					$li.children(".confirm").children(".no").click(function(){
						$li.children(".confirm").remove();
						$li.children(".trash").show();
					});
				});
			});
			loadSectionNavAjax();
		} else {
			showErrorBox("There was a problem removing this section."+ response);
		}return false;
	})
}

function saveThisPage(){
	var order = [];
	for(var i = 0;i< $("#sortable_section_page_list li").length; i++){
		order[i] = $("#sortable_section_page_list li:eq("+i+")").attr("id");
	}
	var order_json = JSON.stringify(order);
	$.post("_do.php",{action:"savePage",page:currentPage,section_order:order_json},function(response){
		if(response.status == '1' || response.status == 1){
			showMessageBox("The page was successfully saved.");
			$("#page_save").hide();
			$("#page_edit").show();
			loadSectionContentAjax();
			loadSectionNavAjax();
			editingPage = false;
		} else {
			showErrorBox("There was a problem saving your edits."+response.errors);
		};
	},"json")
}

function editThisSection(){
	editingSection = true;
	var title = $("#content .page_nav .pages .page_inner_wrapper .current p").text();
	$("#content .page_nav .pages .page_inner_wrapper .current p").remove();
	var input = "<input type='text' class='title_editor' value='"+title+"'></input>";
	$("#content .page_nav .pages .page_inner_wrapper .current").append(input);
	$("#content .inner_content").html("Loading editor, please wait...").load('_get.php', {template:"section_edit_form",page:currentPage,section:currentSection});
}

function saveThisSection(){
	var section_contents = $("#content .section_editor").val();
	var section_title = $("#content .page_nav .pages .page_inner_wrapper .current .title_editor").val();
	var error = "";
	if(section_contents == ""){
		error += "Please enter some body text!<br />";
	};
	if(section_title == ""){
		error += "Please enter a title.<br />";
	}
	if(error != ""){
		showErrorBox(error);
		return false;
	} else {
		$.post("_do.php",{action:"saveSection",page:currentPage,section:currentSection,title:section_title,contents:section_contents},function(response){
			if(response.status == 1 || response.status == "1"){
				$("#content .section_editor").val(response.content);
				$("#content .page_nav .pages .page_inner_wrapper .current .title_editor").val(response.title);
				return true;
			} else {
				showErrorBox("There was a problem saving your edits!");
				return false;
			}
		},"json");
	}	
}

function stopEditingSection(){
	saveThisSection();
	var section_title = $("#content .page_nav .pages .page_inner_wrapper .current .title_editor").val();
	$("#content .page_nav .pages .page_inner_wrapper .current .title_editor").remove();
	$("#content .page_nav .pages .page_inner_wrapper .current").append("<p>"+section_title+"</p>");
	$("#content .inner_content").html("Loading editor, please wait...").load('_get.php', {template:"section_getter",p:currentPage,s:currentSection});
	loadSectionNavAjax();
	editingSection = false;
}
