(function($, Lego) {

Lego.block['b-rate-search'] = function(params) {
    var uicon = $('.b-rate-icon_up',this),
        dicon = $('.b-rate-icon_down',this),
        u = $('.b-rate-search__up',this),
        d = $('.b-rate-search__down',this),
        rateurl = params.assessment_url,
        state = 0;

    u.click(function(){
        if (state != 1) {
            u.removeClass('b-rate-search__gray');
            d.addClass('b-rate-search__gray');
            uicon.addClass('b-rate-icon_upsel');
            dicon.removeClass('b-rate-icon_downsel');
            state = 1;
        } else {
            u.addClass('b-rate-search__gray');
            uicon.removeClass('b-rate-icon_upsel');
            state = 0;
        }
        sendrate();
    });

    d.click(function(){
        if (state != -1) {
            d.removeClass('b-rate-search__gray');
            u.addClass('b-rate-search__gray');
            dicon.addClass('b-rate-icon_downsel');
            uicon.removeClass('b-rate-icon_upsel');
            state = -1;
        } else {
            d.addClass('b-rate-search__gray');            
            dicon.removeClass('b-rate-icon_downsel');
            state = 0;
        }
        sendrate();
    });

    function sendrate() {
        if (!params.query || !params.target) return;

        var data = {
            q: params.query,
            url: params.target,
            r: state
        };
        $.get(rateurl,data,null,'jsonp');
    }
}

})(jQuery, window.Lego);
