//TODO: aneverovsky: replace all method to action

function get_admin_cat_content(next){
	ajax("admin_ajax.php?action=get_admin_cat_content&next="+next, on_get_admin_cat_content);
}
function on_get_admin_cat_content(result) {
	$("blogCats").innerHTML = result.str;
}


function createNewPost(){
	ajax("admin_ajax.php?action=get_all_cat_content", on_createNewPost);
}
function on_createNewPost(result){
	document.getElementById("checkbox_allow_comments").checked = true;
	document.getElementById("checkbox_notify_replies").checked = true;
	document.getElementById("id").value = "";
 	document.getElementById("title").value = "";
	document.getElementById("author").value = result.author;
	document.getElementById("cat_content").innerHTML = result.categories;
	document.getElementById("best_cat_content").innerHTML = result.best_categories;
	document.getElementById("categories").value = "";
	if (result.is_html == "true") {
		FCKeditorAPI.GetInstance('body_post').SetHTML("");
	}else {
			document.getElementById("body_post").value = "";
	}
	FCKeditorAPI.GetInstance('extended_post').SetHTML("");
	document.getElementById("content2").style.display ="";
}

function refreshCatChoose(){
	if (document.getElementById("content2").style.display ==""){
		ajax("admin_ajax.php?action=get_all_cat_content", on_refreshCatChoose);
	}
}
function on_refreshCatChoose(result){
	document.getElementById("cat_content").innerHTML = result.categories;
	document.getElementById("best_cat_content").innerHTML = result.best_categories;
}


function showEditCatMenu(id){
	if (id){
	 	ajax("admin_ajax.php?id="+id+"&action=show_edit_cat_menu", onShowEditCatMenu);
	}
}
function onShowEditCatMenu(result){
	document.getElementById("edit_cat_menu").style.display = "";
	document.getElementById("edit_cat_id").value = result.id; 
	document.getElementById("edit_cat").value = result.str; 
	
}
function updateEditCatMenu(){
	var id = document.getElementById("edit_cat_id").value;
	var title = document.getElementById("edit_cat").value;
 	ajax("admin_ajax.php?id="+id+"&action=update_cat&title="+title, onUpdateEditCatMenu);

}
function onUpdateEditCatMenu(result){
	if (!result.str) alert("Update error!");
	else{
		document.getElementById("edit_cat_menu").style.display = "none";
		get_rigth_menu();
		refreshCatChoose();
	}
	
}

function delete_cat(id){
	var ask = "Are you sure I want to delete it?";
	if (window.confirm(ask)){
     ajax("admin_ajax.php?id="+id+"&delete=cat", on_delete_cat);
	}
}
function on_delete_cat(result) {
	
	if (!result.str) alert("Error to delete");
	else {
		refreshCatChoose()
		get_rigth_menu();
	}

}

function closeForm(){
	var ask = "Do you want to save?";
	if (window.confirm(ask)){
			save_post();	
	}
	document.getElementById("content2").style.display ="none";
}
function get_rigth_menu(){
	ajax("admin_ajax.php?get_menu=1", on_get_rigth_menu);
}
function on_get_rigth_menu(result) {
	document.getElementById("menu").innerHTML = result.str;                    
}

function cat_menu_draft(show_type,edit) {
	
	menu_display = document.getElementById("showings_draft_"+show_type).style.display;
    if (menu_display == "block")
    {
    	document.getElementById("showings_draft_"+show_type).style.display = "none"; 
    	document.getElementById("arr_title_draft_"+show_type).src = "../images/arr_right.png"; 
    }
	else if (menu_display == "none")
	{
			if(edit == true || edit == "true") edit = "&edit=true";
			else edit = "";
			ajax("admin_ajax.php?action=get_draft_post&show_type="+show_type+edit, on_cat_menu_draft);
			document.getElementById("showings_draft_"+show_type).style.display = "block";
			document.getElementById("arr_title_draft_"+show_type).src = "../images/arr_down.png"; 
	}
		
}
function on_cat_menu_draft(result) {
	document.getElementById("showings_draft_"+result.show_type).innerHTML = result.str;                    
}
function undelete_post(id){
	var ask = "Are you sure I want to undelete it?";
	if (window.confirm(ask)){
     ajax("admin_ajax.php?action=post_undelete&id="+id, on_undelete_post);
	}
}
function on_undelete_post(result) {
	
	if (!result.str) alert("Undelete Error");
	else {
		refreshCatChoose()
		get_rigth_menu();
		//document.getElementById("li_"+result.id).style.display="none";
		
	}
	
	//document.getElementById("showings_arch"+result.show_arch).innerHTML = result.str; 
}


function cat_menu(show_type,edit) {
	
	menu_display = document.getElementById("showings"+show_type).style.display;
    if (menu_display == "block")
    {
    	document.getElementById("showings"+show_type).style.display = "none"; 
    	document.getElementById("arr_title"+show_type).src = "../images/arr_right.png"; 
    }
	else if (menu_display == "none")
	{
			if(edit == true || edit == "true") edit = "&edit=true";
			else edit = "";

			ajax("admin_ajax.php?show_type="+show_type+edit, on_cat_menu);
			document.getElementById("showings"+show_type).style.display = "block";
			document.getElementById("arr_title"+show_type).src = "../images/arr_down.png"; 
		}
		
}
function on_cat_menu(result) {
	document.getElementById("showings"+result.show_type).innerHTML = result.str;                    
}
function cat_show(url,all){
	if (!all)all="";
	ajax("admin_ajax.php?show_cat="+url+"&all="+all, on_cat_show);
}

function on_cat_show(result) {
   document.getElementById("content").innerHTML = result.str;                    
}

function arch_show(uri){
	ajax("admin_ajax.php?uri="+uri, on_arch_show);
}
function on_arch_show(result) {
 document.getElementById("content").innerHTML = result.str;                          
}

function arch_menu(show_type,uri,edit) {
	
	menu_display = document.getElementById("showings_arch"+show_type).style.display;
    if (menu_display == "block")
    {
    	document.getElementById("showings_arch"+show_type).style.display = "none"; 
    	document.getElementById("arr_title_arch"+show_type).src = "../images/arr_right.png"; 
    }
	else if (menu_display == "none")
	{
			if(edit == "true") edit = "&edit=true";
			else edit = "";
		
			ajax("admin_ajax.php?uri="+uri+"&show_arch="+show_type+edit, on_arch_menu);
			document.getElementById("showings_arch"+show_type).style.display = "block";
			document.getElementById("arr_title_arch"+show_type).src = "../images/arr_down.png"; 
	}
		
}

function on_arch_menu(result) {
	document.getElementById("showings_arch"+result.show_arch).innerHTML = result.str;
}

function closeForm(){
	var ask = "Do you want to save?";
	if (window.confirm(ask)){
			save_post();	
	}
	document.getElementById("content2").style.display ="none";
	}

function save_post(is_html){
	var categories = document.getElementById("categories").value;
	if (categories==""){
		alert('The "Tags" can`t be empty!');
		return false;
	}
	
	var id = document.getElementById("id").value;
	var title = document.getElementById("title").value;
	
	if (document.getElementById("checkbox_allow_comments").checked) {
		var allow_comments ="true";
	}else{
		var allow_comments ="false";
	}
	
	if (document.getElementById("checkbox_notify_replies").checked) {
		var notify_replies ="true";
	}else{
		var notify_replies ="false";
	}
	
	/*if (document.getElementById("checkbox_moderate_comments").checked) {
		var moderate_comments = "true";
	}else{
		var moderate_comments = "false";
	}*/
	if (is_html == "true") {
		var body = (FCKeditorAPI.GetInstance('body_post').GetXHTML());
		//var body = escape(FCKeditorAPI.GetInstance('body_post').GetXHTML());
	}else {
		var body = (document.getElementById("body_post").value);
		//var body = escape(document.getElementById("body_post").value);
	}
	var extended = escape(FCKeditorAPI.GetInstance('extended_post').GetXHTML());

	url = "admin_ajax.php?save=1&id="+id+
			"&title="+title+
			"&allow_comments="+allow_comments+
			"&notify_replies="+notify_replies+
			"&body="+body+
			"&extended="+extended+
			"&categories="+categories;
	
	ajax(url, on_save_post,"POST");
}

function on_save_post(result) {
	//document.getElementById("title_"+result.id).innerHTML=result.title;
	if (result.str>0) {
			refreshCatChoose()
			get_rigth_menu();
			document.getElementById("id").value = result.str;
			alert("Post was saved!"); 
	}
	
}
function delete_post(id){
	var ask = "Are you sure I want to delete it?";
	if (window.confirm(ask)){
     ajax("admin_ajax.php?id="+id+"&delete=post", on_delete_post);
	}
}
function on_delete_post(result) {
	
	if (!result.str) alert("Error to delete");
	else {
		refreshCatChoose()
		get_rigth_menu();
		//document.getElementById("li_"+result.id).style.display="none";
		
	}
	
	//document.getElementById("showings_arch"+result.show_arch).innerHTML = result.str; 
}
function get_cat_content(next){
	ajax("admin_ajax.php?action=get_cat_content&next="+next, on_get_cat_content);
}
function on_get_cat_content(result) {
	document.getElementById("cat_content").innerHTML = result.str;
}
function get_best_cat_content(next){
	ajax("admin_ajax.php?action=get_best_cat_content&next="+next, on_get_best_cat_content);
}
function on_get_best_cat_content(result) {
	document.getElementById("best_cat_content").innerHTML = result.str;
}	
function edit_post(id){
	ajax("admin_ajax.php?id="+id+"&edit_arch_show=1", on_edit_post);
}
function on_edit_post(result) {
	if (result.allow_comments=="true")document.getElementById("checkbox_allow_comments").checked = true;
	else document.getElementById("checkbox_allow_comments").checked = false;
	if (result.notify_replies=="true")document.getElementById("checkbox_notify_replies").checked = true;
	else document.getElementById("checkbox_notify_replies").checked = false;
	/*if (result.moderate_comments=="true")document.getElementById("checkbox_moderate_comments").checked = true;
	else document.getElementById("checkbox_moderate_comments").checked = false;
	*/
	document.getElementById("id").value = result.id;
 	document.getElementById("title").value = result.title;
	document.getElementById("author").value = result.author;
	document.getElementById("cat_content").innerHTML = result.categories;
	document.getElementById("best_cat_content").innerHTML = result.best_categories;
	document.getElementById("categories").value = result.categories_name;
	if (result.is_html == "true") {
		FCKeditorAPI.GetInstance('body_post').SetHTML(result.str);
	}else {
			document.getElementById("body_post").value = result.str;
	}
	FCKeditorAPI.GetInstance('extended_post').SetHTML(result.extended);
	document.getElementById("content2").style.display ="";
	}
