function init_profile_votes()
{	
	var container = $('#voting');
	var textVotes = container.find("#current_votes");
	var errorP = container.find("#vote_error");
	var arrowUp = container.find("#arrow_up");
	var arrowDown = container.find("#arrow_down"); 
	var form = $('#voting form'); 
	var currentAction = '';
	
	function add_message(is_error, score) {				
		if (is_error) {
			errorP.innerHTML = score;
			errorP.removeattr("disabled");
		} else {			
			errorP.attr("disabled","disabled");			
			textVotes.text(score.score);
			if (currentAction == 'up') {
				arrowUp.attr("src", "/imgs/aupmod.png");
				arrowUp.css("cursor", "default");
				arrowUp.attr("disabled", "disabled");
				arrowUp.attr("title", "You sad you like his");
				arrowDown.attr("src", "/imgs/adowngrey.png");
				arrowDown.css("cursor", "pointer");
				arrowDown.removeAttr("disabled");
				arrowDown.attr("title", "Are You change your mind and you hate his?");
			}
			else if (currentAction == 'down') {
				arrowUp.attr("src", "/imgs/aupgrey.png");
				arrowUp.css("cursor", "pointer");
				arrowUp.removeAttr("disabled");
				arrowUp.attr("title", "Are You change your mind and you like his?");
				arrowDown.attr("src", "/imgs/adownmod.png");
				arrowDown.css("cursor", "default");
				arrowDown.attr("disabled","disabled");
				arrowDown.attr("title", "You sad you hate his");
			}
		}		
	}
	
	function on_response(data, action) {		
		try {
			response = eval("(" + data + ")");
		} catch(err) {
			msg = "error: response parse exception"
		}		
		if (response.error) {
			add_message(true, response.error);
		} else {
			add_message(false, response.score);
		}
	}
	
	form.submit( function() {		
		var patt=new RegExp("(up|down|clear)");				
		currentAction = patt.exec(this.id)[0]; 
		var patt2=new RegExp("\\d+");		
		var userId = patt2.exec(this.id)[0];		
		$.post('/vote/user/' + userId + '/' + currentAction + 'vote', on_response);
		return false;
	})	
}

$(document).ready(function(){
	init_profile_votes();
})

/*    doVote: function(e)
    {
        Event.stop(e);
        var form = Event.element(e);
        var id = /(\d+)$/.exec(form.id)[1];
        var action = /(up|down|clear)vote/.exec(form.action)[1];
        new Ajax.Request(form.action, {
            onComplete: VoteHijacker.processVoteResponse(this.prefix, id, action)
        });
    }
};

VoteHijacker.processVoteResponse = function(prefix, id, action)
{
    return function(transport)
    {
        var response = transport.responseText.evalJSON();
        if (response.success === true)
        {
            var upArrowType = "grey";
            var upFormAction = "up";
            var downArrowType = "grey";
            var downFormAction = "down";

            if (action == "up")
            {
                var upArrowType = "mod";
                var upFormAction = "clear";
            }
            else if (action == "down")
            {
                var downArrowType = "mod";
                var downFormAction = "clear";
            }

            VoteHijacker.updateArrow("up", prefix, id, upArrowType);
            VoteHijacker.updateArrow("down", prefix, id, downArrowType);
            VoteHijacker.updateFormAction("up", prefix, id, upFormAction);
            VoteHijacker.updateFormAction("down", prefix, id, downFormAction);
            VoteHijacker.updateScore(prefix, id, response.score);
        }
        else
        {
            alert("Error voting: " + response.error_message);
        }
    };
};

VoteHijacker.updateArrow = function(arrowType, prefix, id, state)
{
    var img = $(prefix + arrowType + "arrow" + id);
    var re = new RegExp("a" + arrowType + "(?:mod|grey)\\.png");
    img.src = img.src.replace(re, "a" + arrowType + state + ".png");
};

VoteHijacker.updateFormAction = function(formType, prefix, id, action)
{
    var form = $(prefix + formType + id);
    form.action = form.action.replace(/(?:up|down|clear)vote/, action + "vote");
};

VoteHijacker.updateScore = function(prefix, id, score)
{
    var scoreElement = $(prefix + "score" + id);
    scoreElement.innerHTML = score.score + " point" + VoteHijacker.pluralize(score.score);
    scoreElement.title = "after " + score.num_votes + " vote" + VoteHijacker.pluralize(score.num_votes);
};

VoteHijacker.pluralize = function(value)
{
    if (value != 1)
    {
        return "s";
    }
    return "";
}; */