/* 
 * FeeLCoMz CMS AJAX (Require jQuery) 
 * By Rony Nofrianto
 * Copyright © 2009 - Mar 2010
 */

function progress(type, mode) {
	if (type == "shout") {
		if (mode == "hide") {
			$("#shout-wait").show();
			$("#shout-isi").fadeOut();			
		}
		else {
			$("#shout-wait").hide();			
			$("#shout-isi").fadeIn();
		}
	} 
	else if (type == "comment") {
		if (mode == "hide") {
			$("#comment-wait").show();
			$("#comment-isi").fadeOut();			
		}
		else {
			$("#comment-wait").hide();
			$("#comment-isi").fadeIn();
		}	
	}
	else if (type == "post") {
		if (mode == "hide") {
			$("#post-wait").show();
			$("#admin-panel").fadeOut();			
		}
		else {
			$("#post-wait").hide();
			$("#admin-panel").fadeIn();
		}	
	}
	else if (type == "editbox") {
		if (mode == "hide") {	
			$("#post-list").fadeIn();			
			$("#edit-box").fadeOut();			
		} else {
			$("#post-list").fadeOut();			
			$("#edit-box").fadeIn();			
		}		
	}
}

/* Jika DOM udah siap dimanipulasi */
$(document).ready(function(){

	/* Kirim shouts */
	$("#shout-kirim").click(function() { 		
		var parameter = $("#shout-form").serialize();
		progress("shout", "hide");
		$.post("index.php?p=shout&shout=1", parameter, function(data){		
			progress("shout", "show");
			$("#shout-isi").html(data);
			$("#shout-text").val("");
			$("#shout-text").focus();
		});    
    });	

	/* Refresh shoutbox */
	$("#shout-refresh").click(function() {
		progress("shout", "hide");
		$.get("index.php?p=shout", function(data){
			progress("shout", "show");
			$("#shout-isi").html(data);
		});    
    });	
	
	/* Kirim Komentar */
	$("#comment-kirim").click(function() {
		if ($("#comment-text").val() == "") {
			window.alert("Mohon isi komentar!");			
			return false;
		}
		var parameter = $("#comment-form").serialize();
		progress("comment", "hide");
		$.post("index.php?o=addcomment", parameter, function(data){			
			progress("comment", "show");
			$("#comment-isi").html(data);			
			$("#comment-text").val("");
			$("#comment-text").focus();
		});    
    });		
	
	/* Article */
	$("#article-form").submit(function() {
		var errmsg = "";

		if ($("#article-judul").val() == "") {
			errmsg += "Mohon isi judul!";
		} else if ($("#article-cat").val() == "0") {
			errmsg += "Mohon pilih kategori!";		
		} else if ($("#article-desc").val() == "") {
			errmsg += "Mohon isi deskripsi!";			
		}
				
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#article-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
		
    });	

	/* Forum */
	$("#forum-form").submit(function() {
		var errmsg = "";

		if ($("#forum-judul").val() == "") {
			errmsg += "Mohon isi judul!";
		} else if ($("#forum-cat").val() == "0") {
			errmsg += "Mohon pilih kategori!";		
		} else if ($("#forum-desc").val() == "") {
			errmsg += "Mohon isi deskripsi!";			
		}
				
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#forum-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
		
    });	
    
	/* Page */
	$("#page-form").submit(function() {
		var errmsg = "";

		if ($("#page-judul").val() == "") {
			errmsg += "Mohon isi judul!";		
		} else if ($("#page-desc").val() == "") {
			errmsg += "Mohon isi deskripsi!";			
		}
				
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#page-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
		
    });	    
    
	/* Advertisement */
	$("#advertisement-form").submit(function() {
		var errmsg = "";

		if ($("#advertisement-judul").val() == "") {
			errmsg += "Mohon isi judul!";		
		} else if ($("#advertisement-desc").val() == "") {
			errmsg += "Mohon isi deskripsi!";			
		}
				
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#advertisement-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
		
    });	        

	/* Link */
	$("#link-form").submit(function() {
		var errmsg = "";

		if ($("#link-judul").val() == "" || $("#link-url").val() == "") {
			errmsg += "Mohon isi judul dan URL!";		
		}
		
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#link-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
    });	

	/* Category */
	$("#category-form").submit(function() {
		var errmsg = "";

		if ($("#cat-judul").val() == "" || $("#cat-type").val() == "0") {
			errmsg += "Mohon isi judul dan Type!";		
		}
		
		if (errmsg != "") {
			window.alert(errmsg);
			return false;
		}
		
		var parameter = $("#category-form").serialize();
		
		progress("post", "hide");
		$.post("index.php?post=1", parameter, function(data){		
			progress("post", "show");
			$("#admin-panel").html(data);
		});
		
		return false;		
    });
	
});

function hapus(type, id_post)
{
	var postID = "#c" + id_post;
	var jwb = window.confirm("Yakin ingin dihapus?");	
	
    if (jwb == true) {
		$(postID).fadeOut("slow");		
		$.get("?p=post&o=del&type=" + type + "&id=" + id_post, '', function(data){		
			$("#comment-info").html(data);
		});		
    }
}

/* Post Status */
function status(mode, type, id_post)
{
	var tanya = ""	
	
	if (mode == "approve") {
		tanya = "Yakin ingin diapprove?"				
	} else if (mode == "pending") {
		tanya = "Yakin ingin dipending?"		
	} else if (mode == "close") {
		tanya = "Yakin ingin ditutup?"		
	}
	
	var postID = "#c" + id_post;	
	var jwb = window.confirm(tanya);	
	
    if (jwb == true) {
		$(postID).fadeOut("slow");		
		$.get("?p=post&o=" + mode + "&type=" + type + "&id=" + id_post, '', function(data){		
			$("#comment-info").html(data);
		});		
    }
}

/* Petik post/komentar */
function quote(type, id_post)
{
	$.get("?p=post&type=" + type + "&id=" + id_post, '', function(data){		
		var str = $("#comment-text").val() + data;
		$("#comment-text").val(str);
		$("#comment-text").focus();		
	});
}

function userlist()
{
	$("#user-list").fadeOut("slow");		
	$.get("?p=post&o=userlist", '', function(data){
		$("#user-list").fadeIn("slow");		
		$("#user-list").html(data);
	});		
}

/* Post Lists Rename */
function mass_action(aksi)
{
	var errmsg = "";

	if (errmsg != "") {
		window.alert(errmsg);
		return false;
	}
	
	if (aksi == "rename") {
		$("#input-action").html("<input name='rename' type='hidden' value='1' />");
	} else if (aksi == "delete") {
		$("#input-action").html("<input name='hapus' type='hidden' value='1' />");
	}	
		
	var action = $("#posts-list-form").attr("action");		
	var parameter = $("#posts-list-form").serialize();
		
	progress("post", "hide");
	$.post(action, parameter, function(data){		
		progress("post", "show");
		$("#admin-panel").html(data);
	}); 		
}