/* 
 * FeeLCoMz CMS AJAX (Require jQuery) 
 * By Rony Nofrianto
 * Copyright © 2009
 */

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();
		});    
    });		
	
	/* Post */
	$("#post-form").submit(function() {
		var errmsg = "";

		if ($("#post-cat").val() == "0") {
			errmsg += "Mohon pilih kategori!";		
		} else if ($("#post-text").val() == "") {
			errmsg += "Mohon isi deskripsi!";			
		} else if ($("#post-file").val() == "" && $("#post-link").val() == "") {
			errmsg += "Mohon masukkan file / link!";			
		}
		
		if (errmsg != "") {
			window.alert(errmsg);
			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;		
    });	
});

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);
		});		
    }
}

/* 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();		
	});
}