
function ArticleHandling() { }

ArticleHandling.current_article_id_ = null;

ArticleHandling.init = function(article_id)
{
	var article = articles[article_id2index(article_id)];

	ArticleHandling.current_article_id_ = article_id;

	open_dialog("#newArticleDialog");

	if (ArticleHandling.current_article_id_)
	{
		$('#articleTitleInput').attr("value", article.Name);
		$('#articleTextInput').attr("value", article.Text);
	}
	else
	{
		$('#articleTitleInput').attr("value", "");
		$('#articleTextInput').attr("value", "");
	}
	
	ArticleHandling.check_form();
	
	$('#articleTitleInput').bind("change", function(e) { ArticleHandling.check_form(); });
	$('#articleTextInput').bind("change", function(e) { ArticleHandling.check_form(); });
	$('#articleTitleInput').bind("keyup", function(e) { ArticleHandling.check_form(); });
	$('#articleTextInput').bind("keyup", function(e) { ArticleHandling.check_form(); });
	
	$('#articleForm').bind("submit", function(e)
	{
		e.preventDefault();
		
		var args = $('#articleForm').serialize();
		
		ArticleHandling.hide();
		
		jQuery.getJSON("functions/post_article.php?" + args, function(data)
		{
			if (data.Status.Success)
			{
				open_articles(article);
			}
			else
			{
				alert(data.Status.Error);
			}
		});
	});
	
	$('#articleTextInput').tinymce({
		// Location of TinyMCE script
		script_url : 'js/tiny_mce/tiny_mce.js',

		// General options
		theme : "simple",
		width : 786,
		height : 300,
		paste_auto_cleanup_on_paste : true,

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,justifyfull"
	});
	
	//$('.mceLayout').corner('3px');
}

ArticleHandling.remove = function(article_id)
{
	var article = articles[article_id2index(article_id)];

	if (confirm("Are you sure you want to delete the article \"" + article.Name + "\""))
	{
		var args = "Id=" + article_id; 
	
		jQuery.post("functions/del_article.php", args, function(data)
		{
			if (data.Status.Success)
			{
				update_articles(data);
			}
			else
			{
				alert(data.Status.Error);
			}
		}, "json");
	}
}

ArticleHandling.hide = function()
{
	tinyMCE.getInstanceById('articleTextInput').remove();
	//$('#articleTextInput').remove();
	close_dialog('#newArticleDialog');
}

ArticleHandling.save = function(publish)
{
	if (!ArticleHandling.check_form())
	{
		alert("Article must have a title and some content");
		return;
	}
	
	var args = $('#articleForm').serialize();
	
	args += "&Published=" + publish;
	
	if (ArticleHandling.current_article_id_)
	{
		args += "&Id=" + ArticleHandling.current_article_id_;
	}
	
	ArticleHandling.hide();
	
	jQuery.post("functions/save_article.php", args, function(data)
	{
		if (data.Status.Success)
		{
			update_articles(data);
		}
		else
		{
			alert(data.Status.Error);
		}
	}, "json");
	
}

ArticleHandling.check_form = function()
{
	var buttons = $('#newArticleDialog').dialog('option', 'buttons');

	if ($('#articleTitleInput').attr("value") == "" || $('#articleTextInput').attr("value") == "")
	{
		//buttons.Save.attr("disabled", "disabled");
		//buttons.Publish.attr("disabled", "disabled");
		return false;
	}
	else
	{
		//buttons.Save.removeAttr("disabled");
		//buttons.Publish.removeAttr("disabled");
		return true;
	}
}

