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

var models_base = {'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 newvote(div_id, model_slug, user_id, direction){
    // по началу дисейблим все
    $('#' + div_id + ' .up').replaceWith('<a href="javascript:non();" class="up up-disabled"></a>');
    $('#' + div_id + ' .down').replaceWith('<a href="javascript:non();" class="down down-disabled"></a>');

    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);

		var el_up 	= $('#' + div_id + ' .up');
		var el_down = $('#' + div_id + ' .down');
		var up_enable_str = '<a href="javascript:newvote(\'' + div_id + '\', \'' + model_slug + '\', \''+user_id+'\',\'up\')" class="up"></a>';
		var down_enable_str = '<a href="javascript:newvote(\'' + div_id + '\',\'' + model_slug + '\', \''+user_id+'\',\'down\')" class="down"></a>'
        if (jsonResult.superuser_mode == 'True') {
			el_up.replaceWith(up_enable_str);
			el_down.replaceWith(down_enable_str);
        }

		if (jsonResult.rating_votes != '0' || jsonResult.free_voting) {
			if (jsonResult.vote.is_upvote || (!jsonResult.vote.is_upvote && !jsonResult.vote.is_downvote)) {
				el_down.replaceWith(down_enable_str);
			}
			if (jsonResult.vote.is_downvote || (!jsonResult.vote.is_upvote && !jsonResult.vote.is_downvote)) {
				el_up.replaceWith(up_enable_str);
			}
		}
        try { update_rating_votes(parseInt(jsonResult.rating_votes), jsonResult.free_voting); } catch(e) {};

    });
}

