$(function () {
    
	var time = 30, counter = 0, z = 1;
	function rubic(){
		setTimeout(function(){
				$('.timer').html("czas: " + time+ " s");
				if(time != 0){
					
					if(z == 1){
						time--;
						rubic();
					}
				}
				else{location.reload();
				}
				
			}, 1000);
	}
	$( ".start" ).click(function(){
		rubic();
		$(this).fadeOut(900);
		$(".ekran").fadeOut(900);
	});
		var zindex = 10;
		var drop = function (event, ui) {
			var x = ui.draggable.data('value');
			var y = $(this).data('value');
			
			if (x === y) {			
					var top = $(this).position().top;
					var left = $(this).position().left;				
					$('#jplayer_well').jPlayer('play');
					ui.draggable
							.draggable({revert: false})
							.draggable("option", "disabled", true);	
					ui.draggable.animate({
						top: top, 
						left: left
					}, 900);
					counter++;
					if(counter == 6){
						z = 0;
						rubic();
					}
			} else {
				$('#jplayer_wrong').jPlayer('play');
			}
		};
			
				
	$('.dragz').draggable({
		start: function () {
			zindex++;
			$(this).css('z-index', zindex);
		},
		revert: true,
		revertDuration: 800
	});
	$('.dropz').droppable({
		accept: ".dragz",
		tolerance: "pointer",
		drop: drop
	});
	

	$('#jplayer').on($.jPlayer.event.ended,function(event){
		$('.sound').fadeIn();
		$('#jplayer_sound').jPlayer({
			ready: function()
				{
				$(this).jPlayer('setMedia', {mp3: './media/song.mp3'});
				},
			swfPath: '../../styles/flash',
			supplied: 'mp3',
			cssSelectorAncestor: '#jp_container_sound'
		});
	});
});

