﻿$(document).ready(function() {
    $('ul#top30 li a.vote_up').click(function() {
		$(this).parent('span').parent('li').children('span.loading').fadeIn('fast');
        s_id = $(this).parent('span').parent('li').attr('id').substring(2);
        $(this).parent('span').fadeOut('fast');
        voteUp(s_id);
		$(this).parent('span').parent('li').children('span.loading').fadeOut('fast');
    });

    $('ul#top30 li a.vote_down').click(function() {
		$(this).parent('span').parent('li').children('span.loading').fadeIn('fast');
        s_id = $(this).parent('span').parent('li').attr('id').substring(2);
        $(this).parent('span').fadeOut('fast');
        voteDown(s_id);
		$(this).parent('span').parent('li').children('span.loading').fadeOut('fast');
    });

    $('ul#top30 li a.vote_up_none').hover(
            function() {
                $(this).children('span').css({ 'opacity': 0.7, 'top': $(this).offset().top + 8, 'left': $(this).offset().left - 145 });
                $(this).children('span').fadeIn('fast');
            },
            function() { $(this).children('span').fadeOut('fast'); });

    $('ul#top30 li a.vote_down_none').hover(
            function() {
                $(this).children('span').css({ 'opacity': 0.7, 'top': $(this).offset().top + 8, 'left': $(this).offset().left + 14 });
                $(this).children('span').fadeIn('fast');
            },
            function() { $(this).children('span').fadeOut('fast'); });
});


function voteUp(song_id) {
    $.ajax({
        type: 'POST',
        url: 'top30.aspx/voteUp',
        contentType: 'application/json; charset=utf-8',
        data: '{"song_id":"' + song_id + '"}',
        dataType: 'json',
        success: function(result) {
            // alert(result.d + ' всего голосов ' + song_id);
            var pos = document.cookie.indexOf("topVoteUp=");
            if (pos != -1) {
                var start = pos + 10;
                var end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                var countVote = document.cookie.substring(start, end);
                if (countVote == '3') {
                    $('.vote_up').remove();
                    $('.vote_up_none').fadeIn('fast');
                }
            }
            $('span#allVotes' + song_id).html(result.d);
            $('span#allVotes' + song_id).fadeIn('fast');
        },
        error: AjaxFailed
    });
}

function voteDown(song_id) {
    $.ajax({
        type: 'POST',
        url: 'top30.aspx/voteDown',
        contentType: 'application/json; charset=utf-8',
        data: '{"song_id":"' + song_id + '"}',
        dataType: 'json',
        success: function(result) {
            //alert(result.d + ' всего голосов ' + song_id);
            var pos = document.cookie.indexOf("topVoteDown=");
            if (pos != -1) {
                var start = pos + 12;
                var end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                var countVote = document.cookie.substring(start, end);
                if (countVote == '3') {
                    $('.vote_down').remove();
                    $('.vote_down_none').fadeIn('fast');
                }
            }
            $('span#allVotes' + song_id).html(result.d);
            $('span#allVotes' + song_id).fadeIn('fast');
        },
        error: AjaxFailed
    });
}

function AjaxSucceeded(result) {
    alert(result.d);
}
function AjaxFailed(result) {
    alert('Error: ' + result.status + ' ' + result.statusText);
}
