var cur_entry_page = 1;
var max_entry_pages = 1;
$(document).ready(function(){

	if($('[name=body]').html() != null)
		CKEDITOR.replace( 'body' );
});
function edit_blog_entry(article_id)
{
	var article_body;
	article_body = $.ajax({
		url:"../ajax/blog.ajax.php",
		type: "POST",
		data: "action=fetch_article_body&article_id="+article_id,
		dataType: "html",
		async: false
		}
	).responseText;
	
	var article_title;
	article_title = $.ajax({
		url:"../ajax/blog.ajax.php",
		type: "POST",
		data: "action=fetch_article_title&article_id="+article_id,
		dataType: "html",
		async: false
		}
	).responseText;
	
	$("#article_id").val(article_id);
	$("[name=title]:input").val(article_title);
	//var oEditor = FCKeditorAPI.GetInstance('body');
	//oEditor.SetData(article_body);
	
	CKEDITOR.instances.body.setData(article_body);
	// get related links and comments
	
	$.ajax({
		url:"../ajax/blog.ajax.php",
		type: "POST",
		data: {	'action' : 'fetch_rel_links_and_comments',
							'article_id':article_id,
							'editting': 1},
		dataType: "html",
		async: false,
		success:function(data, textStatus)
		{
				
			var json = eval("("+data+")");
			
			var comments = Base64.decode(json.comments);
			var rel_links = Base64.decode(json.rel_links);

			$("#rel_links_edit").html(rel_links);
			$("#comments_edit").html(comments);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
	
	
}

function add_new_blog()
{
	//$("#msg").hide();
	$("#article_id").val('');
	$("[name=title]").val('');
	//var oEditor = FCKeditorAPI.GetInstance('body');
	//oEditor.SetHTML('');
	CKEDITOR.instances.body.setData('');
	$("#rel_links_edit").html('');
	$("#comments_edit").html('');
	
}
function validate_blog_entry(blogBody, blogTitle)
{
	var body = blogBody;
	var title = blogTitle;
	var missingTitle=false;
	var valid = true;
	
	if(body==''&&title=='')
	{
		$.jGrowl("Please enter a title and body for your blog post.");
		missingTitle=true;
		valid=false;
	}
	else if(title=='')
	{
		$.jGrowl("Please enter a title for your blog post.");
		missingTitle=true;
		valid=false;
	}
	else if(body=='')
	{
		$.jGrowl("Please enter a body for your blog post.");
		valid=false;
	}
	return {"valid":valid,"missingTitle":missingTitle};
}
function save_blog(section_id)
{
	//var oEditor = FCKeditorAPI.GetInstance('body');
	//CKEDITOR.instances.body.setData(article_body);
	//var body =  urlencode(oEditor.GetData(true));
	var body =  urlencode(CKEDITOR.instances.body.getData());
	var title = urlencode($("[name=title]").val());
	var validation = validate_blog_entry(body, title);
	if(validation.valid) //check if all the inputs are valid
	{
		var user_id = $("#user_id").val();
		var article_id = $("#article_id").val();
		
		$.ajax({
			url:"../ajax/blog.ajax.php",
			type: "POST",
			data: "action=save_article&article_id="+article_id+"&title="+title+"&body="+body+"&user_id="+user_id+"&section_id="+section_id,
			dataType: "html",
			async: false,
			success: function(data)
			{
				var json = eval("("+data+")");
				
				$("#blog_entries").html(Base64.decode(json.entry_pages));
				$("#article_id").val(json.article_id);
				$("#comments_edit").html( Base64.decode(json.comments));
				$("#rel_links_edit").html(Base64.decode(json.rel_links));
				
				max_entry_pages = get_max_entry_pages(section_id);
				$("#current_entry_page").html("Page: "+cur_entry_page);
				$.jGrowl(json.msg);
				toggle_entry_paging();
			}
		});
	}
	else
	{
		titleElement = $("[name=title]");
		/* If the input was not valid, and there is no title, make the
		   title a missing field.*/
		
		titleElement.removeClass("missing_field"); /* Clear it first, just in case
													* they entered a title */
		
		if(validation.missingTitle)
			titleElement.addClass("missing_field");
	}
	
}
function delete_blog_entry(article_id, section_id)
{
	var answer = confirm("Are you sure you want to delete this blog entry?");
	if(answer){
	$.ajax(
	{
			url:"../ajax/blog.ajax.php",
			type: "POST",
			data: "action=delete_article&article_id="+article_id+"&section_id="+section_id,
			dataType: "html",
			async: false,
			success: function(data)
			{
				var json = eval("("+data+")");
				
				$.jGrowl(Base64.decode(json.msg));
				$("#blog_entries").html(Base64.decode(json.html));
				cur_entry_page=1;
				max_entry_pages = get_max_entry_pages(section_id);
				
				toggle_entry_paging();
				if(article_id == $("#article_id").val())
				{
					$("#article_id").val('');
					$("#comments_edit").html('');
					$("#rel_links_edit").html('');
					CKEDITOR.instances.body.setData('');
					$("[name=title]").val('');
				}
			}
		});
		
		
	}
}
function next_entry_page(section_id,edit)
{
	cur_entry_page++;
	/*
	$("#blog_entries").fadeOut("slow", function()
	{
		
	});*/
	$.ajax(
		{
			url:"../ajax/blog.ajax.php",
			type: "POST",
			data: "action=get_entry_page&section_id="+section_id+"&page="+cur_entry_page+"&edit="+edit,
			dataType: "html",
			async: false,
			success: function(data)
			{
				$("#blog_entries").html(data);//.fadeIn("slow");
				$("#current_entry_page").html("Page: "+cur_entry_page);
				
				toggle_entry_paging();
			}
		});

	
}
function prev_entry_page(section_id, edit)
{
	cur_entry_page--;
	/*
	$("#blog_entries").fadeOut("slow", function()
	{
		
	});*/
	$.ajax(
		{
			url:"../ajax/blog.ajax.php",
			type: "POST",
			data: "action=get_entry_page&section_id="+section_id+"&page="+cur_entry_page+"&edit="+edit,
			dataType: "html",
			async: false,
			success: function(data)
			{
				$("#blog_entries").html(data);//.fadeIn("slow");
				$("#current_entry_page").html("Page: "+cur_entry_page);
				toggle_entry_paging();
			}
		});
	
}
function toggle_entry_paging()
{
	if(cur_entry_page == 1)
		$("#prev_entry_page").hide();
	else
		$("#prev_entry_page").show();
	
	if(cur_entry_page == max_entry_pages || max_entry_pages ==0)
		$("#next_entry_page").hide();
	else
		$("#next_entry_page").show();
	if(max_entry_pages==0)
	{
		$("#blog_entries").html("There are no blog entries.");
		$("#current_entry_page").html("");
	}
	else
		$("#current_entry_page").html("Page: "+cur_entry_page);
}
function get_max_entry_pages(section_id)
{
	var data = "action=get_max_entry_pages&section_id="+section_id
	var pages = $.ajax({
		url:"../ajax/blog.ajax.php",
		data: data,
		success:function(data)
		{
			
		},
		type:"post",
		async: false
	}).responseText;
	
	return pages;
}
function add_comment()
{
	var data = "";
	$("#new_comment_form input:not(:button), textarea").each(function(){
		if($(this).attr("type") == "text" || $(this).attr("type") == "textarea")
			data += $(this).attr("name")+"="+urlencode($(this).val())+"&";	
		else
			data += $(this).attr("name")+"="+$(this).val()+"&";	
	})
	data += "action=add_comment";
	
	$("#comment_area").fadeOut("slow", function (){
		$.ajax({
				url:"../ajax/blog.ajax.php",
				data: data,
				success: function(data){
					var num_comments = $("#comment_area").html(data).find(".single_comment").size();
					$("#comment_header").html("Comments("+num_comments+")");
					
					$("#comment_area").fadeIn("slow");
				},
				type:"POST"
			});
	});
	
	
}
function flag_comment(comment_id,article_id)
{
	 var data = "action=flag_comment&comment_id="+comment_id+"&article_id="+article_id;
	 
	$("#comment_area").fadeOut("slow", function (){
		$.ajax({
				url:"../ajax/blog.ajax.php",
				data: data,
				success: function(data){
					var num_comments = $("#comment_area").html(data).find(".single_comment").size();
					$("#comment_header").html("Comments("+num_comments+")");
					$("#comment_area").fadeIn("slow");
				},
				type:"POST"
			});
	});
}
function add_rel_link()
{
	var article_id = $("#rel_link_form [name=article_id]").val();
	var url = $("#rel_link_form [name=rel_link_url]").val();
	
	
	
	$("#rel_link_area").fadeOut("slow",function(){
		$.ajax({
			url:"../ajax/blog.ajax.php",
			data: {
					'action': 'add_rel_link',
					'url' : url,
					'article_id' : article_id
					},
			success: function(data){
				$("#rel_link_area").html(data);
				$("#rel_link_header").html("Related Links ("+$("#rel_link_area li").size()+")");
				
				$("#rel_link_area").fadeIn("slow");
			},
			type:"POST"
		});
	});

}
function delete_link(link_id, article_id)
{
	$("#rel_link_area").fadeOut("slow",function(){
		$.ajax({
			url:"../ajax/blog.ajax.php",
			data: {
					'action' : 'delete_rel_link',
					'link_id' : link_id,
					'article_id' : article_id
					},
			success: function(data){
				$("#rel_link_area").html(data);
				$("#rel_link_header").html("Related Links ("+$("#rel_link_area li").size()+")");
				
				$("#rel_link_area").fadeIn("slow");
			},
			type:"POST"
		});
	});
}