function showEditor(page) {
	showWait(true);
	new Ajax.Request('functions/page-getter.php?page='+page, {
		onSuccess: function(transport) {
			$('action').value = 'functions/page-saver.php';
			$('page').value = page;
			var editor = FCKeditorAPI.GetInstance('full_text');
			editor.SetData(transport.responseText);
			scroll(0,0);
			$('editor-container').show();
		}
	});
}

function savePage() {
	var oEditor = FCKeditorAPI.GetInstance('full_text');
	oEditor.UpdateLinkedField();
	data = $('edit-form').serialize();
	container_div = $('page').value;
	full_text = $('full_text').value;
	new Ajax.Request('functions/page-saver.php', {
			parameters: data,
			method: 'post',
			onSuccess: function(transport) {
				$(container_div).innerHTML = full_text;
				$('editor-container').hide();
				showWait(false);
			}
		}
	);	 
	return true;
}

function editBlog(blog) {
	showWait(true);
	new Ajax.Request('functions/blog-getter.php?blog='+blog, {
		onSuccess:function(transport) {
			$('action').value = 'functions/blog-saver.php';
			$('blog_id').value=blog;
			$('editor-container').show();
			var editor = FCKeditorAPI.GetInstance('full_text');
			editor.SetData(transport.responseText);
			scroll(0,0);
		}
	});
}

function saveBlog() {
	var oEditor = FCKeditorAPI.GetInstance('full_text');
	oEditor.UpdateLinkedField();
	data = $('edit-form').serialize();
	full_text = $('full_text').value;
	new Ajax.Request('functions/blog-saver.php', {
			parameters: data,
			method: 'post',
			onSuccess: function(transport) {
				$('blog-content').innerHTML = full_text;
				$('editor-container').hide();
				showWait(false);
			}
		}
	);	 
	return true;
}

function addBlog() {
	showWait(true);
	$('action').value = 'functions/blog-saver.php';
	$('blog_id').value='new';
	$('page').value = '';
	$('editor-container').show();
	var editor = FCKeditorAPI.GetInstance('full_text');
	editor.SetData('');
	scroll(0,0);
}

function saveNewBlog() {
	var oEditor = FCKeditorAPI.GetInstance('full_text');
	oEditor.UpdateLinkedField();
	data = $('edit-form').serialize();
	new Ajax.Request('functions/blog-saver.php', {
			parameters: data,
			method: 'post',
			onSuccess: function(transport) {
				window.location = 'blog.php';
			}
		}
	);
}


function save() {
	if ($('action').value =='functions/page-saver.php') {
		savePage();
	}
	if ($('action').value == 'functions/blog-saver.php') {
		if ($('blog_id').value == 'new') saveNewBlog();
		if ($('blog_id').value != 'new') saveBlog();
	} 
}



function saveMetas() {
	data = $('metas').serialize();
	new Ajax.Request('functions/meta-saver.php', {
			parameters: data,
			method: 'post',
			onSuccess: function(transport) {
				document.location='functions/redirect.php?req=' + document.location;
			},
			onFailure: function() {
				alert('Error! Unable to save meta data');
			}
		}
	);
}


function showWait(trueOrFalse) {
	if (trueOrFalse == true) {
		$$('object').each(function(o){o.hide();});
		$('wait').show();
		return;
	}
	$('wait').hide();
	$$('object').each(function(o){o.show();});
}


function logout() {
	new Ajax.Request('functions/logout.php', {
		onSuccess: function(transport) {
			location.reload(true);
		}
	});
}

function deleteBlog(blog) {
	if (!confirm('Are you sure?')) return false;
	new Ajax.Request('functions/delblog.php?blog='+blog, {
		method: 'post',
		onSuccess: function(transport) {
			alert('Blog deleted');
			window.location = 'blog.php';
		}
	});
}