/**
 * Comments
 * @version 1.0.2
 */
Loader.scripts(["jquery-form", "jquery-color", "ajax", "validator", "string"], "comments_init");

var user_access = {};

function comments_init()
{
	auth_add_handlers(comments_on_login, comments_on_logout);

	$("#require_auth a").click(auth_form_show);

	$(".comment_actions a.reply").live("click", comments_show_reply);
	$(".comment_actions a.edit").live("click", comments_show_edit);
	$(".comment_actions a.delete").live("click", {'url': "/comment_delete_ajax/", 'success': comments_complete}, comments_post);
	$(".comment_actions a.delete_tree").live("click", {'url': "/comment_delete_tree_ajax/", 'success': comments_complete_tree}, comments_post);
	$(".comment_actions a.restore").live("click", {'url': "/comment_restore_ajax/", 'success': comments_complete}, comments_post);

	comments_bind($("#comment_form form"), true);
}

function comments_init_actions(access)
{
	user_access = access;

	$(".comment_actions").remove();

	if (user_access['allow_comments'] == 1 || user_access['is_admin'] == 1)
		$(".comment").each(comments_add_actions);
}

function comments_add_actions(stub, container)
{
	container = $(container);

	var actions_template = $(".actions_template:first").clone();

	var id = container.attr("id");
	var is_deleted = container.hasClass("deleted");

	if (user_access['allow_comments'] == 1 && is_deleted == false)
		actions_template.find(".reply").attr("href", "#" + id);
	else
		actions_template.children(".reply").remove();

	if (user_access['is_admin'] == 1)
	{
		if (is_deleted)
		{
			comments_set_action(actions_template, ".restore", "restore_", id);
			actions_template.children(".edit, .delete, .delete_tree").remove();
		}
		else
		{
			comments_set_action(actions_template, ".edit", "edit_", id);
			comments_set_action(actions_template, ".delete", "delete_", id);
			comments_set_action(actions_template, ".delete_tree", "deleteree_", id);
			actions_template.children(".restore").remove();
		}
	}
	else
		actions_template.children(".edit, .delete, .delete_tree, .restore").remove();

	actions_template.addClass("comment_actions");
	actions_template.removeClass("actions_template hide");
	container.children(".comment_text").after(actions_template);
}

function comments_set_action(actions_template, class_name, id_template, id)
{
	var action = actions_template.find(class_name);
	action.attr("href", "#" + id);
	action.attr("id", id_template + id);
}

function comments_bind(form, top_level)
{
	form.ajaxForm(
	{
		url: "/comment_ajax/",
		beforeSubmit: Validator.reset,
		success: function(data, status, xhr, form)
		{
			common_enable(form);

			form.find("textarea").val("");
			if (!top_level)
				form.parent().slideToggle("fast");

			if (!Ajax.check_error(data))
				return;

			comments_add(data, form);
		},
		dataType: "xml"
	});
}

function comments_bind_edit(form)
{
	form.ajaxForm(
	{
		url: "/comment_edit_ajax",
		beforeSubmit: function (data, form, options)
		{
			Validator.reset(data, form, options);
			var text = form.find("textarea").val().trim().nl2br();
			form.parent().data("text", text);
		},
		success: function (data, status, xhr, form)
		{
			common_enable(form);
			form.slideUp("fast");

			if (!Ajax.check_error(data))
				return;

			comments_edit(data, form);
		}
	})
}

function comments_add(data, form)
{
	var comment = $("comment", data).text();
	if (comment == "")
		return;

	var id = $("id", data).text();
	var parent_id = $("parent_id", data).text();

	var container;
	if (parent_id == 0)
		container = $("#comments_container").prepend(comment);
	else
		container = $("#comment" + parent_id).next().append(comment);

	var new_comment = container.find("#comment" + id).next();
	comments_add_actions(null, new_comment);

	var destination = new_comment.offset().top - form.height() - 20;
	$("html").animate({'scrollTop': destination}, 1100);

	comments_animate(new_comment);
}

function comments_edit(data, form)
{
	if ($("error", data).text() != "")
		return;

	var container = form.parent();
	container.html(container.data("text"));
	form.remove();
	comments_animate(container.parent());
}

function comments_on_login(data)
{
	var allow_comments = $("allow_comments", data).text();
	var allow_votes = $("allow_votes", data).text();
	var is_admin = $("is_admin", data).text();

	user_access = {
		'allow_comments':	allow_comments,
		'is_admin':		is_admin
	};

	comments_init_actions(user_access);

	if (allow_comments || allow_votes)
		$("#comment_form").show();

	if (allow_votes)
		$("#comment_votes").show();

	if (allow_comments)
		$("#comment_comments").show();

	$("#require_auth").hide();
}

function comments_on_logout(data)
{
	$(".comment_actions").remove();
	$("#comment_form, #comment_votes, #comment_comments").hide();
	$("#require_auth").show();
	user_access = {};
}

function comments_show_reply(data)
{
	var fields = comments_get_fields(data);
	var reply_container = $("#reply_" + fields['id']);

	if (reply_container.find("form").length > 0)
	{
		reply_container.slideToggle("slow");
		return false;
	}

	reply_container.hide();
	reply_container.html($("#reply_template").html());

	var root_id_index = "root_id";
	if (fields['root_id'] == 0)
		root_id_index = "id";

	reply_container.find(".root_id").attr("value", fields[root_id_index]);
	reply_container.find(".parent_id").attr("value", fields['id']);
	reply_container.show("slow");

	comments_bind(reply_container.find("form"), false);

	reply_container.find("textarea").focus();
	return false;
}

function comments_show_edit(data)
{
	var fields = comments_get_fields(data);
	var edit_container = $('#comment_text_' + fields['id']);
	if (edit_container.find("form").length > 0)
	{
		edit_container.slideUp("fast");
		edit_container.html(edit_container.data("text").nl2br());
		edit_container.slideDown("fast");
		return false;
	}

	var text = edit_container.html().br2nl();
	edit_container.data("text", text);

	edit_container.hide();
	edit_container.html($("#edit_template").html());

	edit_container.find(".id").attr("value", fields['id']);
	edit_container.find(".text").val(text);
	edit_container.show("slow");

	comments_bind_edit(edit_container.find("form"));

	edit_container.find("textarea").focus();
	return false;
}

function comments_post(data)
{
	var params = data.data;

	var fields = comments_get_fields(data);
	$.post(params['url'], fields, params['success'], "xml");

	return false;
}

function comments_complete(data)
{
	var container = comments_old_container(data);
	var new_comment	= comments_new_container(data);

	comments_replace(container, new_comment);
	container.toggleClass("deleted");

	comments_add_actions(null, container);

	comments_animate(container);
}

function comments_complete_tree(data)
{
	var container = comments_old_container(data);
	var new_comment	= comments_new_container(data).eq(2);

	container.replaceWith(new_comment);

	comments_add_actions(null, new_comment);

	new_comment.find(".comment").each(comments_add_actions);

	comments_animate(new_comment);
}

function comments_replace(container, new_comment)
{
	var class_names = [".comment_header", ".comment_text", ".comment_reply"];
	for (var id in class_names)
	{
		var comment_element = container.find(class_names[id]).first();
		var new_comment_element = new_comment.find(class_names[id]);
		comment_element.replaceWith(new_comment_element);
	}

	container.children(".comment_actions").remove();
}

function comments_animate(container)
{
	container.animate({'backgroundColor': "#e0e0e0"}, "slow");
	container.animate({'backgroundColor': "white"}, "fast", function()
	{
		container.css("backgroundColor", "transparent");
	});
}

function comments_get_fields(data)
{
	var array = $(data.target).attr("href").split("_");
	return {
		'type': array[0].substring(1),
		'root_id': array[1],
		'parent_id': array[2],
		'id': array[3],
		'pid': $('#pid').text()
	};
}

function comments_new_container(data)
{
	return $($("comment", data).text());
}

function comments_old_container(data)
{
	var id = $("id", data).text();
	return $("#comment" + id).next();
}

$(comments_init);
