var imgPath = "/static/images/";
var thumb_up_gray = imgPath + "default/icons/thumb_up_gray.png";
var thumb_up_green = imgPath + "default/icons/thumb_up_green.png";
var thumb_down_gray = imgPath + "default/icons/thumb_down_gray.png";
var thumb_down_red = imgPath + "default/icons/thumb_down_red.png";

$(document).ready(function() {
    $('.rating, .comment-rating').each(function(i) {
        $('.vote_up', $(this)).live('click', function() {
            var value_field = $('.value', $(this).parent().parent());
            var up_img = $('img', $(this));
            var down_img = $('.vote_down img', $(this).parent().parent());
            $.getJSON($(this).attr('href'), function(data) {
                if(data.status == 1) {
                    if(data.removed_prev == 1) {
                        up_img.attr('src', thumb_up_gray);
                    } else {
                        up_img.attr('src', thumb_up_green);
                    }
                    down_img.attr('src', thumb_down_gray);
                    value_field.html(data.current_rating);
                }
            });
            return false;
        });
        $('.vote_down', $(this)).live('click', function() {
            var value_field = $('.value', $(this).parent().parent());
            var down_img = $('img', $(this));            
            var up_img = $('.vote_up img', $(this).parent().parent());
            $.getJSON($(this).attr('href'), function(data) {
                if(data.status == 1) {
                    if(data.removed_prev == 1) {
                        down_img.attr('src', thumb_down_gray);
                    } else {
                        down_img.attr('src', thumb_down_red);
                    }
                    up_img.attr('src', thumb_up_gray);
                    value_field.html(data.current_rating);
                }
            });
            return false;
        });

        current_user_rating = $('.current_user_rating', $(this)).val();
        if(current_user_rating > 0) {
            $('.vote_up img', $(this)).attr('src', thumb_up_green);
        } else if(current_user_rating < 0) {
            $('.vote_down img', $(this)).attr('src', thumb_down_red);
        }
    });
});

