/*
 * $Id$
 *
 * Голосование
 */

var models_base = {'blog': '/blog/blog/vote/', 'entity': '/entities/vote/', 'user': '/accounts/user/vote/', 'comment': '/blog/comment/vote/', 'entry': '/blog/entry/vote/', 'mfile': '/mserver/mfile/vote/'};

function non() { }

function entry_vote(entry_id, direction){
    var url = _ENTRY_VOTE_BASE + entry_id + '/' + direction + 'vote/';
    $.post(url, {}, function (data, textStatus) {
        var jsonResult = eval('(' + data + ')');
        if(direction == 'clear') {
            var voted_for = $('#b'+entry_id+' div.vote .voted');
            voted_for.removeClass('voted');
            var new_dir = voted_for.hasClass('up') ? 'up' : 'down';
            var other_dir = new_dir == 'up' ? 'down' : 'up';
            var other_vote = $('#b'+entry_id+' div.vote .' + other_dir);
            voted_for.replaceWith('<a href="javascript:entry_vote(\''+entry_id+'\',\''+new_dir+'\')" class="'+new_dir+'"><img src="'+_ARROW_IMG_BASE+new_dir+'.png"></img></a>');
            other_vote.replaceWith('<a href="javascript:entry_vote(\''+entry_id+'\',\''+other_dir+'\')" class="'+other_dir+'"><img src="'+_ARROW_IMG_BASE+other_dir+'.png"></img></a>');
        }
        else if(direction == 'up') {
            var voted_for = $('#b'+entry_id+' div.vote .up');
            var other_vote = $('#b'+entry_id+' div.vote .down');
            voted_for.replaceWith('<a href="javascript:entry_vote(\''+entry_id+'\',\'clear\')" class="up voted"><img src="'+_ARROW_IMG_BASE+'up_gray.png"></img></a>');
            other_vote.replaceWith('<a href="javascript:entry_vote(\''+entry_id+'\',\'down\')" class="down"><img src="'+_ARROW_IMG_BASE+'down.png"></img></a>');
        }
        else {
            var voted_for = $('#b'+entry_id+' div.vote .down');
            var other_vote = $('#b'+entry_id+' div.vote .up');
            voted_for.replaceWith('<a href="javascript:vote(\''+entry_id+'\',\'clear\')" class="down voted"><img src="'+_ARROW_IMG_BASE+'down_gray.png"></img></a>');
            other_vote.replaceWith('<a href="javascript:vote(\''+entry_id+'\',\'down\')" class="up"><img src="'+_ARROW_IMG_BASE+'up.png"></img></a>');
        }
        $('#b'+entry_id+' p.score').text(jsonResult.score.score);
    });
}

function vote(div_id, model_slug, user_id, direction){
    var url = models_base[model_slug] + user_id + '/' + direction + 'vote/';
    $.post(url, {}, function (data, textStatus) {
        var jsonResult = eval('(' + data + ')');
        if(direction == 'clear') {
            var voted_for = $('#' + div_id + ' .rbutton_plus_i');
            var other_vote = $('#' + div_id + ' .rbutton_minus_i');
            voted_for.replaceWith('<a href="javascript:vote(\'' + div_id + '\', \'' + model_slug + '\', \''+user_id+'\',\'up\')" class="rbutton rbutton_plus"></a>');
            other_vote.replaceWith('<a href="javascript:vote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'down\')" class="rbutton rbutton_minus"></a>');
            var voted_for = $('#' + div_id + ' .rbutton_plus');
            var other_vote = $('#' + div_id + ' .rbutton_minus');
            voted_for.replaceWith('<a href="javascript:vote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'up\')" class="rbutton rbutton_plus"></a>');
            other_vote.replaceWith('<a href="javascript:vote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'down\')" class="rbutton rbutton_minus"></a>');
        }
        else if(direction == 'up') {
            var voted_for = $('#' + div_id + ' .rbutton_plus');
            var other_vote = $('#' + div_id + ' .rbutton_minus');
            voted_for.replaceWith('<a href="javascript:non();" class="rbutton rbutton_plus_i"></a>');
            other_vote.replaceWith('<a href="javascript:vote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="rbutton rbutton_minus"></a>');
        }
        else {
            var voted_for = $('#' + div_id + ' .rbutton_minus');
            var other_vote = $('#' + div_id + ' .rbutton_plus');
            voted_for.replaceWith('<a href="javascript:non();" class="rbutton rbutton_minus_i"></a>');
            other_vote.replaceWith('<a href="javascript:vote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="rbutton rbutton_plus"></a>');
        }
        $('#' + div_id + ' #user_score').text(jsonResult.score.score);
    });
}

function newvote(div_id, model_slug, user_id, direction){
    var url = models_base[model_slug] + user_id + '/' + direction + 'vote/';
    $.getJSON(url, function (jsonResult) {
        var object_rating = jsonResult.score.score;
        $('#rating_' + model_slug + '_' + user_id).text(jsonResult.score.score);

        var class_name = 'neutral';
        if (object_rating < 0) {
            class_name = 'negative';
        } else if (object_rating > 0) {
            class_name = 'positive';
        }
        var rating_wrap = $('#rating_wrap_' + model_slug + '_' + user_id);
        rating_wrap.removeClass("positive neutral negative");
        rating_wrap.addClass(class_name);

        if(direction == 'clear') {
            var voted_for = $('#' + div_id + ' .up');
            var other_vote = $('#' + div_id + ' .down');
            if (jsonResult.rating_votes == '0'  && jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
                other_vote.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            else {
                voted_for.replaceWith('<a href="javascript:newvote(\'' + div_id + '\', \'' + model_slug + '\', \''+user_id+'\',\'up\')" class="up"></a>');
                other_vote.replaceWith('<a href="javascript:newvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'down\')" class="down"></a>');
            }
        }
        else if(direction == 'up') {
            var voted_for = $('#' + div_id + ' .up');
            var other_vote = $('#' + div_id + ' .down');
	    if (jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
            }
            if (jsonResult.rating_votes == '0' && jsonResult.superuser_mode != 'True') {
                other_vote.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            else {
                other_vote.replaceWith('<a href="javascript:newvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="down"></a>');
            }
        }
        else {
            var voted_for = $('#' + div_id + ' .down');
            var other_vote = $('#' + div_id + ' .up');
            if (jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            if (jsonResult.rating_votes == '0' && jsonResult.superuser_mode != 'True') {
                other_vote.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
            }
            else {
                other_vote.replaceWith('<a href="javascript:newvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="up"></a>');
            }
        }
    try { update_rating_votes(parseInt(jsonResult.rating_votes)); } catch(e) {};


    });
}

function gvote(div_id, model_slug, user_id, direction){
    var url = models_base[model_slug] + user_id + '/' + direction + 'vote/';
    $.getJSON(url, function (jsonResult) {
        var object_rating = jsonResult.score.score;
        $('#rating_' + model_slug + '_' + user_id).text(jsonResult.score.score);

        if(direction == 'clear') {
            var voted_for = $('#' + div_id + ' .up');
            var other_vote = $('#' + div_id + ' .down');
            if (jsonResult.rating_votes == '0'  && jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
                other_vote.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            else {
                voted_for.replaceWith('<a href="javascript:gvote(\'' + div_id + '\', \'' + model_slug + '\', \''+user_id+'\',\'up\')" class="up"></a>');
                other_vote.replaceWith('<a href="javascript:gvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'down\')" class="down"></a>');
            }
        }
        else if(direction == 'up') {
            var voted_for = $('#' + div_id + ' .up');
            var other_vote = $('#' + div_id + ' .down');
    	    if (jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
            }
            if (jsonResult.rating_votes == '0' && jsonResult.superuser_mode != 'True') {
                other_vote.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            else {
                other_vote.replaceWith('<a href="javascript:gvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="down"></a>');
            }
        }
        else {
            var voted_for = $('#' + div_id + ' .down');
            var other_vote = $('#' + div_id + ' .up');
            if (jsonResult.superuser_mode != 'True') {
                voted_for.replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');
            }
            if (jsonResult.rating_votes == '0' && jsonResult.superuser_mode != 'True') {
                other_vote.replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
            }
            else {
                other_vote.replaceWith('<a href="javascript:gvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'clear\')" class="up"></a>');
            }
        }
    try { update_rating_votes(parseInt(jsonResult.rating_votes)); } catch(e) {};


    });
}

