$(function () {
    var zad = 0,
            mixZad,
            mixBtn,
            results = [48, 17, 50, 19, 46],
            cont = [
                ['30 + 3 &middot; 6', 48],
                ['(90 &minus; 56) : 2', 17],
                ['5 &middot; (76 &minus; 66)', 50]
            ];

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

    mixZad = shuffleArray(cont);
    mixBtn = shuffleArray(results);

    for (var i = 0; i < 5; i++) {
        $('main').append('<div class="btn btn' + i + '" data-value="' + mixBtn[i] + '"><div class="wynalazek w' + mixBtn[i] + '"></div><span>' + mixBtn[i] + '</span></div>');
    }

    $('.btn').click(function () {
        if ($(this).data('value') === $('.cont').data('value')) {
            $('#jplayer_well').jPlayer('play');
            $('.cont').animate({width: 0}, 1000);
            zad++;
            if(zad <3){
            setTimeout(function () {
                $('.cont').animate({width: 460}, 1000).html(mixZad[zad][0]).data('value', mixZad[zad][1]);
            }, 1000);
            
        } else{
            $('.cont').remove();
            $('.btn').unbind('click');
        }
        $('.w' + $(this).data('value')).fadeIn(600);
        } else {
            $('#jplayer_wrong').jPlayer('play');
        }
    });

    $('.cont').html(mixZad[zad][0]).data('value', mixZad[zad][1]);
});
