
$(function()
	{
	/**
	 * Elements
	 */

	var counter=0;
	var elements=[];
	var taskOrder=0;
	var points=0;
	var interval;
	var time=10;



	$('#jplayer').on($.jPlayer.event.ready,function()
		{
		$('#jplayer').jPlayer('setMedia', {mp3: './media/file2.mp3'});
		});




	for(var i=1;i<=10;++i)
		{
		elements[i-1] = Math.floor(Math.random() * (4)) + 1;;
		}

$.each(_.shuffle(elements),function(index,value)
		{
		$('.elements').append('<div class="element_'+value+'"/>');

		});

function newTask(){
	$('.elements > *').remove();
	clearInterval(interval);
	time=10;
	i = 1;
	$.each(_.shuffle(elements),function(index,value)
		{
		$('.elements').append('<div class="element_'+value+'" data-id="'+value+'" data-order="'+i+'" />');
		i++;
		});


	$('.time').text(time);
	interval = setInterval(function()
					{
					if (time>0) --time;

					$('.time').text(time);

					if((time === 0))
						{
						$('#jplayer_wrong').jPlayer('play');
						$("#jplayer_wrong").bind($.jPlayer.event.ended, function() {
							location.reload();
							});
						}
					},1000);




	/**
	 * Click
	 */
	$('.elements > *').each(function()
		{
		$(this).click(function()
			{
				if (taskOrder > 0)
				{

					if (taskOrder === $(this).data('order'))
					{
						$('#jplayer_well').jPlayer('play');
						points += 1;
						$(this).addClass('ok');
						clearInterval(interval);
						taskOrder = 0;
						$('.points').text(points);
					} else $('#jplayer_wrong').jPlayer('play');

				} else  alert('Najpierw kliknij przycisk "przeczytaj liczbę"');
                       });
                });

	}; //end newTask


	/**
	*Sound
	*/

       for(var i=1;i<=10;++i)
		{
		$('#jplayer_sound'+i).jPlayer(
			{
			ready: function()
				{
				$(this).jPlayer('setMedia', {mp3: './media/2/'+i+'.mp3'});
				},

			play: function()
				{
				$(this).jPlayer('pauseOthers');
				},

			swfPath: '../../styles/flash',
			supplied: 'mp3'
			});
		}



		$('#jplayer_sound1').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound1').jPlayer('setMedia', {mp3: './media/2/1.mp3'});
		});

		$('#jplayer_sound2').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound2').jPlayer('setMedia', {mp3: './media/2/2.mp3'});
		});

		$('#jplayer_sound3').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound3').jPlayer('setMedia', {mp3: './media/2/3.mp3'});
		});

		$('#jplayer_sound4').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound4').jPlayer('setMedia', {mp3: './media/2/4.mp3'});
		});

		$('#jplayer_sound5').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound5').jPlayer('setMedia', {mp3: './media/2/5.mp3'});
		});

		$('#jplayer_sound6').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound6').jPlayer('setMedia', {mp3: './media/2/6.mp3'});
		});

		$('#jplayer_sound7').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound7').jPlayer('setMedia', {mp3: './media/2/7.mp3'});
		});

		$('#jplayer_sound8').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound8').jPlayer('setMedia', {mp3: './media/2/8.mp3'});
		});

		$('#jplayer_sound9').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound9').jPlayer('setMedia', {mp3: './media/2/9.mp3'});
		});

		$('#jplayer_sound10').on($.jPlayer.event.ready,function()
		{
		$('#jplayer_sound10').jPlayer('setMedia', {mp3: './media/2/10.mp3'});
		});





	$('.btn').click(function()
		{


			var rand=Math.floor(Math.random() * (10)) + 1;


				taskOrder = 11 - rand;
				console.log(rand);
				$('#jplayer_sound'+rand).jPlayer('play');
				newTask();

		});






	/**
	 * Check
	 */
	$('.footer_game .check').click(function(e)
		{
		e.preventDefault();
		if(counter === 6)
			{
			$('#jplayer_well').jPlayer('play');
			}
		else
			{
			$('#jplayer_wrong').jPlayer('play');
			}
		});
	});
