$(document).ready(function(){
    $(".entry-rating-form").addClass("hidden");
    $(".rating-possibilites").removeClass("hidden");


    function rating_on_until(node,mouseover)
    {
        if (node.hasClass("rating-selected") || mouseover)
        {
            var current_star = parseInt(node.attr("name").replace("rating-star-",""));

            var ratings_list = node.parent().children(".rating-star").addClass("rating-off");

            $.each(ratings_list,function(i,val){
                if ((i+1)<=current_star)
                    $(val).removeClass("rating-off");
            });
        }
        else
        {
            var ratings_list = node.parent().children(".rating-star").addClass("rating-off");
        }
    }

    $(".rating-star").mouseover(function(){
        rating_on_until($(this),true);
    });

    $(".rating-star").click(function(){

        $(this).parent().children(".rating-selected").removeClass("rating-selected");
        var node = $(this).addClass("rating-selected");
        var current_star = node.attr("name").replace("rating-star-","");

        var form = $(node).parent().parent().children("form");
        var checked_node = form.children("p").children("input[name=rating]:checked");
        checked_node.attr("checked","");
        var selected_node = form.children("p").children("input[name=rating][value="+current_star+"]");
        selected_node.attr("checked","checked");

        var container = $(this).parent();
        var url = form.get(0).action;
        var data = $(':input', form).serialize();
        $('#entry-current-rating-' + $('.entry-pk', form).get(0).value).html('...')
        $.post(url, data, function(data) {
        	var id = $(data).get(0).id;
        	$('#'+id).replaceWith(data);
        }, 'html');

        return false;

    });

    $(".rating-star").mouseout(function(){

        var node = $(this).parent().children(".rating-selected");
        if (!node.attr("name"))
        {
            var node = $(this).parent().children(":first");
        }

        rating_on_until(node,false);

        return false;

    });

});