$(function ()
{	

	var zindex = 10, counter = 1, y = 500;
	var i = 0;
	for(i=1;i<=8;i++)
		{
		$('#jplayer_sound'+i).jPlayer(
			{
			ready: function()
				{
				$(this).jPlayer('setMedia', {mp3: './media/807-'+$(this).data('value')+'.mp3'});
				},

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

			swfPath: '../../styles/flash',
			supplied: 'mp3'
			});
		}
	
	$('#jplayer').on($.jPlayer.event.ended,function(event){
		$('#jplayer_sound'+counter).jPlayer('play');
	});
	var drop = function (event, ui) {
		var x = ui.draggable.data('value');
		var y = $(this).data('value');
			
		if (x === y && y === counter) {
			var top = $(this).position().top;
			var left = $(this).position().left;
			$('#t'+y).fadeIn();
			
			ui.draggable
				.draggable({revert: false})
				.draggable("option", "disabled", true);
			ui.draggable.animate({
				top:  top,
				left: left
			}, 500); 
			if(counter < 9) 
			{
				$('#jplayer_sound'+counter).jPlayer('play');
			}  
			$('#jplayer_sound'+counter).on($.jPlayer.event.ended,function(event)
			{
			$('#jplayer_sound'+counter).jPlayer('play');
			});
			counter++;
			if (counter === 9){
			setTimeout(function(){$('#jplayer_well').jPlayer('play');},2000);
			}
		} else {
			$('#jplayer_wrong').jPlayer('play');
		}
	};
			
	$('.dragg').draggable({
		start: 
			function () {
				zindex++;
				$(this).css('z-index', zindex);
			},
		revert: true,
		revertDuration: 800
	});
	$('.dropp').droppable({
		accept: ".dragg",
		tolerance: "pointer",
		drop: drop
	});	
});
