function errorReport (error) {
	alert('ERROR: ' +error);
}
function initFB() {
	FB_RequireFeatures(["XFBML"], function(){
		FB.init("a43a192ad5f85cd2068b4ee9be5ca2aa", "xd_receiver.htm",{"reloadIfSessionStateChanged":true});
	});
}
function fbConnect() {
	FB.init("a43a192ad5f85cd2068b4ee9be5ca2aa", "xd_receiver.htm",{"reloadIfSessionStateChanged":true});
}
$(document).ready(
	function(){	

		$("a[name='cancel']").hide();
		$("a[name='save']").hide();
		$("[id^='deleteConfirm']").hide();
		$("[id^='comments']").hide();
		$("[id^='commentForm']").hide();
		/*
		function ajax_formValidation(elementID, variable) {
			value = document.getElementById(elementID).value;
			$.post(
				"ajax.register.php", 
				{ variable: variable, value: value },
				function (response) {
					if (response == "TRUE") {
					
					} else {
						// error report
					}
				});
		}
		*/
		$("a[name='showComments']").click(
			function(){
				$("[id='comments" +this.id+ "']").slideToggle("slow");
				return false;
			}
		);
		$("a[name='addComments']").click(
			function(){
				$("[id='commentForm" +this.id+ "']").slideToggle("slow");
				return false;
			}
		);
		$("a[name='edit']").click(
			function(){
				$("span[id='postBody" +this.id+ "']").html('<textarea class="ckeditor" cols="80" id="editor'+this.id+'" name="postBody" rows="10">' +$("span[id='postBody" +this.id+ "']").html()+ '</textarea>');
				$("a[name='save'][id='" +this.id+ "']").show();
				$("a[name='cancel'][id='" +this.id+ "']").show();
				$("a[name='edit'][id='" +this.id+ "']").hide();
				CKEDITOR.replace('editor'+this.id);
				return false;
			}
		);
		$("a[name='lock']").click(
			function(){
				$.post(
					"./ajax/ajax.blog.php", 
					{ action: 'lock', id: this.id },
					function (data) {
						eval("var data="+response);
						if (data.success==1) {
							$("a[name='unlock'][id='" +data.id+ "']").show();
							$("a[name='lock'][id='" +data.id+ "']").hide();
						} else errorReport(data.error);
					},
					"json"
				);
				return false;
			}
		);
		$("a[name='unlock']").click(
			function(){
				$.post(
					"./ajax/ajax.blog.php", 
					{"action": 'unlock', "id": this.id },
					function (data) {
						if (data.success==1) {
							$("a[name='lock'][id='" +data.id+ "']").show();
							$("a[name='unlock'][id='" +data.id+ "']").hide();
						} else errorReport(data.error);
					},
					"json"
				);
				return false;
			}
		);
		$("a[name='submitComment']").click(
			function(){
				var subject, text;
				text = $("textarea[name='commentBody'][id='" +this.id+ "']").val();
				subject = $("input[name='commentHeader'][id='" +this.id+ "']").val();
				$.post(
					"./ajax/ajax.blog.php",
					{"action": "new", "id": this.id, "header": subject, "data": text},
					function (data) {
						if (data.success==1) {
							$("textarea[name='commentBody'][id='" +data.postid+ "']").val('');
							$("input[name='commentHeader'][id='" +data.postid+ "']").val('');
							$("[id='commentForm" +data.postid+ "']").slideToggle("slow");
							var count = parseInt($("#sumComments" +data.postid).html(), 10);
							$("#sumComments" +data.postid).html(count + 1);
							$("[id='comments" +data.postid+ "']").prepend(data.content);
							$("#deleteConfirm" +data.id).hide();
							$("a[name='cancel'][id='" +data.id+ "']").hide();
							$("a[name='save'][id='" +data.id+ "']").hide();
							$("[id='comments" +data.postid+ "']").show("slow");
						} else {alert(data.error);}
					},
					"json"
					);
				return false;	
			}
		);
		$("a[name='save']").click(
			function(){
				var content;
				CKEDITOR.instances['editor' + this.id].updateElement();
				content = CKEDITOR.instances['editor' + this.id].getData();
				$.post(
					"./ajax/ajax.blog.php", 
					{ action: 'save', id: this.id, data: content },
					function (data) {
						if (data.success==1) {
							$("a[name='save'][id='" +data.id+ "']").hide();
							$("a[name='cancel'][id='" +data.id+ "']").hide();
							$("a[name='edit'][id='" +data.id+ "']").show();
							$("span[id='postBody" +data.id+ "']").html(content);
							CKEDITOR.instances['editor' + data.id].destroy();
						} else errorReport(data.error);
					},
					"json"
				);
				return false;			
			}
		);
		$("a[name='cancel']").click(
			function(){	
				$("a[name='save'][id='" +this.id+ "']").hide();
				$("a[name='cancel'][id='" +this.id+ "']").hide();
				$("a[name='edit'][id='" +this.id+ "']").show();
				$("span[id='postBody" +this.id+ "']").html(CKEDITOR.instances['editor' + this.id].getData());
				CKEDITOR.instances['editor' + this.id].destroy();
				return false;
			}
		);
		$("a[name='delete']").click(
			function(){
				$("#post" +this.id).slideUp('slow');
				$("[id^='deleteConfirm']").hide();
				$("#deleteConfirm" +this.id).slideDown('slow');
				return false;			
			}
		);
		$("a[name='deleteConfirm']").click(
			function(){
				$.post(
					"./ajax/ajax.blog.php", 
					{ action: 'delete', id: this.id },
					function (data) {
						if (data.success==1) {
							$("#deleteConfirm" +data.id).hide();
							var count = parseInt($("#sumComments" +data.id).html(), 10);
							$("#sumComments" +data.id).html(count - 1);							
						} else errorReport(data.error);
					},
					"json"
				);
				return false;	
			}
		);
		$("a[name='deleteCancel']").click(
			function(){
				$("#deleteConfirm" +this.id).slideUp('fast');
				$("#post" +this.id).slideDown('slow');
				return false;			
			}
		);
	}
);

