var comments = new function()
{
	var o = this;
	var type_id = null; // Get this from the page that calls it
	var type_name = null; // Get this from the page that calls it
	var profanity = null; // Get this from the page that calls it

	this.replies_toggle = function(el, discussion_id)
	{
		var replies = $("#reply_to_"+discussion_id);
		if(replies.is(':visible'))
		{
			replies.slideUp('fast');
			el.innerHTML = 'Show Replies';
		}
		else
		{
			replies.slideDown('fast');
			el.innerHTML = 'Hide Replies';
		}
	}
	this.replies_show_all = function(el)
	{
		el = $(el);
		var el_replies = el.parents('.discussion_replies').find('.more_replies');
		el.parent().remove();
		el_replies.slideDown('fast');
	}

	// Gets the form box
	this.reply_start = function(el, discussion_id)
	{
		if(!user.id)
			return non_user('Post a Comment');

		el = $(el);
		discussion_id = discussion_id ? discussion_id : 0;


		var url = '/article/a_comment_form.php';
		var data = {type_id : o.type_id, type_name : o.type_name};

		if(discussion_id) // if it's a reply
			data.parent_id = discussion_id;
		else
		{
			$.scrollTo(
				"#discussion",
				{
					duration: 800,
					easing: 'easeOutBounce',
					offset: -200
				}
			);
		}

		// The box exists just leave it there
		if($('#form_discussion_'+discussion_id).size())
			return;

		$.getJSON(url, data, function(data) { o.reply_start_done(el, data, discussion_id); } );
	}
	// Gets the form and displays it
	this.reply_start_done = function(el, data, discussion_id)
	{

		var el_form = $(data.content).hide();

		if(discussion_id)
			el_form.appendTo(el.parents('.avatar'))
		else
		{
			el_form.prependTo($('#discussion'));
		}

		el_form.fadeIn();

		var options = {
			dataType: 'json',
			success: function(data) { o.reply_submit_done(el, data); }
		};
		el_form.find('form').ajaxForm( options );
	}

	// Display the post
	this.reply_submit_done = function(el, data)
	{
		if(data.error)
		{
			alert(data.error);
			return;
		}

		var el_post = $(data.post).hide().fadeTo(0.1, 0).show();

		if(data.post_data.parent_id) // It's a reply
		{
			var reply_container = $("#reply_to_" + data.post_data.parent_id);
			var child_count = $('#children_count_'+ data.post_data.parent_id); // Add to the counter
			var viewing_count = $('#viewing_count'); // Add to the counter
			var replies_count = $('#replies_count'); // Add to the counter
			var fade_time = 4000;

			el_post.appendTo(reply_container);
			child_count.html( child_count.html() * 1 + 1 );
			viewing_count.html( viewing_count.html() * 1 + 1 );
			replies_count.html( replies_count.html() * 1 + 1 );
			reply_container.find('.view_all_replies').remove();
			reply_container.find('.more_replies').show();
		}
		else
		{
			el_post.append('<div class="discussion_replies" id="reply_to_'+data.post_data.id+'"></div>'); // Need a container for the replies
			el_post.prependTo($("#discussion"));
			var fade_time = 1000;
		}

		//el_post.fadeTo(fade_time, 1);
		$.scrollTo(
			"#discussion_"+data.post_data.id,
			{
				duration: 800,
				easing: 'easeOutBounce',
				offset: -200,
				onAfter: function() { el_post.fadeTo(fade_time, 1); }
			}
		);

		// Remove the discussion form
		var form_name = '#form_discussion_'+(data.post_data.parent_id ? data.post_data.parent_id : '0');
		$(form_name).remove();
	}

	// EDITS 

	// Handles editing a reply
	this.reply_edit_start = function(discussion_id)
	{
		// The box exists just leave it there
		if($('#form_discussion_'+discussion_id).size())
			return;

		var el = $('#content_box_'+discussion_id);


		var url = '/article/a_comment_form.php';
		var data = {
			type_id: o.type_id,
			type_name : o.type_name,
			id: discussion_id
		};

		$.getJSON(url, data, function(data) { o.reply_edit_start_done(el, data, discussion_id); } );
		return;
	}
	// Regets the data
	this.reply_edit_cancel = function(discussion_id)
	{
		var url = '/article/a_comment_form.php';
		var data = {
			type_id: o.type_id,
			type_name : o.type_name,
			id: discussion_id,
			cancel: true
		};
		$('#content_box_'+discussion_id).load(url, data);
	}
	this.reply_edit_start_done = function(el, data, discussion_id)
	{
		if(data.error)
		{
			alert(data.error);
			return;
		}

		el.html(data.content);

		var options = {
			dataType: 'json',
			success: function(data) { o.reply_edit_submit_done(el, data); }
		};
		el.find('form').ajaxForm( options );
	}
	this.reply_edit_submit_done = function(el, data)
	{
		if(data.error)
		{
			alert(data.error);
			return;
		}
		el.html(data.post_data.comment_raw.replace(/\n/g, "<br>"));
	}

	
	// Reply delete
	this.reply_delete = function(id, parent_id)
	{
		var dialogue = parent_id ? "Delete this?" :  "Delete this and all it's replies?";
		if(!confirm(dialogue))
			return;

		var data = {
			delete_me: true,
			id: id, 
			type_id: o.type_id,
			type_name : o.type_name
		};
		var url = '/article/a_comment_submit.php';
		$.post(url, data);

		if(!parent_id)
			$('#reply_to_'+id).slideUp('fast', function() { $(this).remove() } );
		$('#discussion_'+id).slideUp('fast', function() { $(this).remove() } );
	}

	// SHOW ALL! Woooooooo
	this.show_all = function(comment_submit)
	{
		if (typeof comment_submit == "undefined") comment_submit=true;

		var html = '' +
		           '<div style="text-align: center; font-size: 30px;">' +
						  'Loading all comments<br />' +
						  '<img src="http://cdn-www.cracked.com/ui/images/loadingAnimation.gif" />' +
		           '</div>' +
		           '';
		var el = $('#comments_section').html(html);
		var data = {
			type_id: o.type_id,
			type_name : o.type_name,
			profanity : o.profanity,
			comment_submit: comment_submit
		};
		el.load('/article/a_comment_show_all.php', data);
	}

	this.user_delete = function(user_id) {
		if (confirm("are you sure? all comments made by this user will be deleted!")) {
			var data={
				action: "delete_user",
				user_id: user_id
			}; 
			$.post("/article/a_comment_submit.php", data, function() {
				window.location.reload(true);
			});
		}
		return;
	}
}