$(function () {
    var task = 1;
    var result = {
        1: 2,
        2: 2,
        3: 2,
        4: 2,
        5: 1,
        6: 1,
        7: 2,
        8: 2,
        9: 1,
        10: 2
    };
    var sent = {
        1: ['I’m angry.', 'She’s sad.'],
        2: ['She’s happy.', 'They’re worried.'],
        3: ['I’m excited.', 'We’re happy.'],
        4: ['He’s sad.', 'You’re excited.'],
        5: ['He’s scared.', 'They’re scared.'],
        6: ['It’s worried.', 'She’s scared.'],
        7: ['We’re happy.', 'I’m happy.'],
        8: ['We’re happy.', 'It’s excited.'],
        9: ['You’re angry.', 'It’s sad.'],
        10: ['You’re happy.', 'They’re sad.']
    };

    $('#jplayer_sound').jPlayer({
        ready: function () {
            $(this).jPlayer('setMedia', {mp3: './media/606-1.mp3'});
        },
        swfPath: '../../styles/flash',
        supplied: 'mp3',
        cssSelectorAncestor: '#jp_container_sound'
    });

    $('#jplayer').on($.jPlayer.event.ended, function (event) {
        $('.dzieci').fadeOut(600);
        $('#txt1').text(sent[task][0]);
        $('#txt2').text(sent[task][1]);
        $('#jplayer_sound').jPlayer('play');
        
        $(".bt").click(function () {
            if ($(this).data('value') !== result[task]) {
                $('#jplayer_wrong').jPlayer('play');
            } else {
                $(this).addClass('ok');
                var id = $(this).attr('id');
                var no = id.replace('bt', '');
                $('#txt' + no).addClass('oktxt');
                task++;
                if (task < 11) {

                    setTimeout(function () {
                        $('#txt1').text(sent[task][0]);
                        $('#txt2').text(sent[task][1]);
                        $('.bt').removeClass('ok');
                        $('td').removeClass('oktxt');
                        $('#jplayer_sound').jPlayer('setMedia', {mp3: './media/606-' + task + '.mp3'});
                        $('#jplayer_sound').jPlayer('play');
                    }, 1000);

                }
                if (task === 11) {
                    $(".bt").unbind('click');
                    $('.dzieci').fadeIn(600);
                    $('#jplayer_well').jPlayer('play');
                }
            }
        });

    });
    
});