$(document).ready(function() {
	hijackVoteForms("host");
});

// Capture the submit function of each form
function hijackVoteForms(prefix) {
	var voteForms = $("form." + prefix + "vote");
	voteForms.submit(function(event) {
		// Instead of letting the browser submit on its own, perform an AJAX request
		event.preventDefault();
		
		var form = $(this);
		var id = /(\d+)$/.exec(this.id)[1];
		var action = /(up|down|clear)vote/.exec(form.attr("action"))[1];
		
		var upForm = $("#" + prefix + "up" + id);
		var upArrow = $("#" + prefix + "uparrow" + id);
		
		var downForm = $("#" + prefix + "down" + id);
		var downArrow = $("#" + prefix + "downarrow" + id);
		
		$.post(form.attr("action"),
		{},
		function(data){
			if (data.success === true)
			{
				// By default, nothing is selected
				var upArrowType = "grey";
				var upFormAction = "up";
				var downArrowType = "grey";
				var downFormAction = "down";
				if (action == "up")
				{
					// NO MATTER WHAT, UPARROW TURNS WHITE / CLEAR, DOWNARROW BECOMES GREY/DOWN
					updateForm(upForm, "clear");
					updateArrow(upArrow, "up", "mod");
					updateForm(downForm, "down");
					updateArrow(downArrow, "down", "grey");
					
				}
				else if (action == "down")
				{
					// NO MATTER WHAT, UPARROW TURNS GREY / UP, DOWNARROW BECOMES WHITE/CLEAR
					updateForm(upForm, "up");
					updateArrow(upArrow, "up", "grey");
					updateForm(downForm, "clear");
					updateArrow(downArrow, "down", "mod");
				}
				else
				{
					// A clear was just recorded.
					updateForm(upForm, "up");
					updateArrow(upArrow, "up", "grey");
					updateForm(downForm, "down");
					updateArrow(downArrow, "down", "grey");
				}
				
				updateScore(prefix, id, data.score);
			}
			else
			{	
				$("body").append('<div id="dialog"></div>');
				if (data.error_message == "Not authenticated.") {
					
					$("#dialog").attr("title", "You are not logged in").html('<p>You must <a href="/accounts/login">log in</a> or <a href="/accounts/register/">register</a> to vote.</p>').dialog({
						bgiframe: true,
						height: 140,
						modal: true,
						close: function() {$("#dialog").remove();}
					});
					//location.href = "http://djangofriendly.com/accounts/login/";
				}
				else {
					$("#dialog").attr("title", "There was an error recording your vote").html('<p>Your vote has not been recorded</p>').dialog({
						bgiframe: true,
						height: 140,
						modal: true,
						close: function() {$("#dialog").remove();}
					});
					//alert("Error voting: " + data.error_message);
				}
			}
		}, "json");
		
		
	});
};
function updateForm(form, newDirection) {
	var oldDirection = /(up|down|clear)vote/.exec(form.attr("action"))[1];
	var oldAction = form.attr("action");
	newAction = oldAction.replace(/(?:up|down|clear)vote/, newDirection + "vote");
	form.attr("action", newAction);
}
function updateArrow(arrow, direction, color) {
	var re = new RegExp("a" + direction + "(?:mod|grey)\\.gif");
	var old_src = arrow.attr("src");
	var new_src = old_src.replace(re, "a" + direction + color + ".gif");
	arrow.attr("src", new_src);
	/*
	var img = $("#" + prefix + direction + "arrow" + id);
	var re = new RegExp("a" + direction + "(?:mod|grey)\\.gif");
	old_src = img.attr("src");
	new_src = old_src.replace(re, "a" + direction + state + ".gif");
    img.attr("src", new_src);
	*/
};
/*
function updateFormAction(direction, prefix, id, a, dform, action) {
	new_action = action.replace(/(?:up|down|clear)vote/, a + "vote");
	dform.attr("action", new_action);
	
};
*/
function updateScore(prefix, id, score) {
	var scoreElement = $("#" + prefix + "score" + id);
	var totalElement = $("#" + prefix + "total" + id);
	
	scoreElement.fadeOut("fast");
	totalElement.fadeOut("fast", function() {
		scoreElement.html(score.score);
		new_title = "after " + score.num_votes + " vote" + pluralize(score.num_votes);
		scoreElement.attr("title", new_title); 
		totalElement.html(score.num_votes + " vote" + pluralize(score.num_votes));
		
		totalElement.fadeIn("fast");
		scoreElement.fadeIn("fast");
	});

	
	
};
function pluralize(value)
{
	if (value != 1)
	{
		return "s";
	}
	return "";
};