$(function () {
    var task = 1,
            zindex = 10,
            result = [1, 2, 3, 4, 5],
            mix,
            dropped = 0,
            drop = function (event, ui) {
                if (ui.draggable.data('value') === $(this).data('value')) {
                    dropped++;
                    ui.draggable
                            .draggable({revert: false})
                            .draggable("option", "disabled", true);
                    ui.draggable.animate({
                        top: $(this).position().top,
                        left: $(this).position().left
                    }, 900);
                    task++;
                    if (task <= result.length) {
                        $('.dropp').data('value', 0);
                        $("#drop" + mix[task - 1]).data('value', task);
                        $('#jplayer_sound2').jPlayer('setMedia', {mp3: './media/806-0' + mix[task - 2] + 'L.mp3'});
                        $('#jplayer_sound2').jPlayer('play');
                        $('#jplayer_sound2').on($.jPlayer.event.ended, function (event) {
                            if (mix[task - 1] === undefined) {
                                $('#jplayer_sound').jPlayer('setMedia', {mp3: '../../media/well.mp3'});
                            } else {
                                $('#jplayer_sound').jPlayer('setMedia', {mp3: './media/806-0' + mix[task - 1] + '.mp3'});
                            }
                            $('#jplayer_sound').jPlayer('play');
                        });
                    } else {
                        $('#jplayer_sound2').jPlayer('setMedia', {mp3: './media/806-0' + mix[task - 2] + 'L.mp3'});
                        $('#jplayer_sound2').jPlayer('play');
                    }

                } else {
                    $('#jplayer_wrong').jPlayer('play');
                    $('#jplayer_wrong').on($.jPlayer.event.ended, function (event) {
                        $('#jplayer_sound').jPlayer('play');
                    });
                }
            };

    function shuffleArray(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;


        }

        return array;
    }

    mix = shuffleArray(result);
    console.log(mix);
    $(".dragg")
            .draggable({
                start: function () {
                    zindex++;
                    $(this).css('z-index', zindex);
                },
                revert: true,
                revertDuration: 900
            });

    $(".dropp").droppable({
        accept: ".dragg",
        tolerance: "pointer",
        drop: drop
    });

    $("#drop" + mix[0]).data('value', task);

    $('#jplayer_sound').jPlayer({
        ready: function () {
            $(this).jPlayer('setMedia', {mp3: './media/806-0' + mix[0] + '.mp3'});
        },
        play: function ()
        {
            $(this).jPlayer('pauseOthers');
        },
        swfPath: '../../styles/flash',
        supplied: 'mp3',
        cssSelectorAncestor: '#jp_container_sound'
    });
    $('#jplayer_sound2').jPlayer({
        ready: function () {
            $(this).jPlayer('setMedia', {mp3: './media/806-0' + mix[0] + 'L.mp3'});
        },
        play: function ()
        {
            $(this).jPlayer('pauseOthers');
        },
        swfPath: '../../styles/flash',
        supplied: 'mp3',
        cssSelectorAncestor: '#jp_container_sound'
    });
    $('#jplayer').on($.jPlayer.event.ended, function (event) {
        $('#jplayer_sound').jPlayer('play');
    });

});
