$(function()
{

	var ok = 0;
	var left = 0;
	var top = 0;	
	
	var wrong = document.createElement('audio');
	var well = document.createElement('audio');
	wrong.setAttribute('src', '../../media/wrong.mp3');
	well.setAttribute('src', '../../media/well.mp3');
	
	$('#jplayer').on($.jPlayer.event.ended,function(event)
		{
		var src = _.last(event.jPlayer.status.src.split('/'));
$('.dym').fadeIn();
		if(src === 'file.mp3')
			{
			setTimeout(function()
				{
				$('#jplayer').jPlayer('setMedia', {mp3: './media/txt.mp3'}).jPlayer('play');
				$('.next').fadeIn();
				},1000);
			}
		});
			
	
			
	var revert = function()
			{
				$('.wrong').fadeIn("fast")
				.addClass('animated wobble')
				.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
					$(this).removeClass('animated wobble');
				});
				setTimeout(function()
				{$('.wrong').fadeOut()},2000);
				wrong.play();
				return true;
			}
			
        $(".dragg").each(function(){
            $(this).draggable({revert: revert,scope: $(this).data('type'), stack: ".dragg" });
        })
	
        $('.target').each(function(){
		$(this).droppable({
                        scope: $(this).data('type'),
			addClasses: false,
			drop: function(event,ui)
				{
					top = $(this).position().top + ($(this).height() - ui.draggable.height());
						left = $(this).position().left + ($(this).width() - ui.draggable.width());
						if ($(this).data('type') === 'j' || $(this).data('type') === 'g') top -= 30;
						ok++;
						if (ok === 7)
						{
						$('.well').fadeIn()
						.addClass('animated rubberBand');
						well.play();
						}
						ui.draggable.animate({left:left, top:top},{duration: 500, easing: 'easeOutBounce'})
							.draggable({revert:false,disabled:true})
							.css('z-index',$(this).data('value')*100);

				}
		});
	});
	
});