﻿$(function() {
$('.auto-submit-star').rating({ callback: setRatingValue });
$('.submit-star').rating({ callback: postRating });
});
function setRatingValue(value, link) {
    var oo = $(this).parent().parent()[0];    
    oo = $(oo).find('.ratingFields')[0];
    oo = $(oo).find('input')[0];    
    oo.value = value;
}
function postRating(value, link) {
    var oo = $(this).parent().parent()[0];
    oo = $(oo).find('.ratingFields')[0];
    oo = $(oo).find('input')[0];
    if (value == '') return;
    oo.value = value;    
    __doPostBack();
}
xInlineVoter = function() {
    var listen = false;
    return {
        InlineVoteDone: function(msg) {
            try {
                var results;
                eval('results = new Array(' + msg + ');');
                var selector = 'div' + results[1]
                var ratings = $(selector);
                $('input', ratings).rating('select', results[0]);
                alert(results[2]);
            }
            catch (err) {
                alert(err);
            }
            listen = true;
        }
            ,
        Listen: function(val) { listen = val; },
        InlineVoteHandler: function(r, i, j) {
            if (!listen) return;
            var url = ctx.HttpRoot + '/_layouts/xsolon/rating/vote.ashx';
            var oo = r;
            var whatisthjis = this;
            var o = $($(i).parents('div.xInRater')[0]);
            var rating = (r == '') ? '0' : r; //o.attr('xrating');
            var list = o.attr('list');
            var id = o.attr('itemid');
            $.ajax({ type: 'GET', url: url, data: 'list=' + list + '&id=' + id + '&vote=' + rating + "&d=" + new Date().getTime(), success: xInlineVoter.InlineVoteDone });
            listen = false;
        }
}//end return
    } ();             // end xInlineVoter

    $(function() {
        $('.inlineVoter').rating({ callback: xInlineVoter.InlineVoteHandler });
        $('div.xInRater').each(function(i) {
            var o = $(this);
            var rating = o.attr('xrating').replace(',', '.');
            var round = Math.round(rating * 10);
            if (round % 5 == 0) round = round / 10;
            else round = Math.round(rating);
            o = $('div.inRaterWrap', o);
            xInlineVoter.Listen(false); // just in case multiple instances
            $('input', o).rating('select', round.toString());
        });

        $('a.xraterLink').hide();
        $('div.inRaterWrap').show();
        $('div.inRaterWrap:first').hide();
        xInlineVoter.Listen(true);
    });       