
$(function()
	{

	var elements = [];
	var counter = 0;

	var sounds = ['flute','guitar','violin','piano','drum','cello'];
	var i = 0;
	for(i=1;i<=6;++i)
		{
		$('#jplayer_sound'+i).jPlayer(
			{
			ready: function()
				{
				$(this).jPlayer('setMedia', {mp3: './media/'+sounds[$(this).data('value')]+'.mp3'});
				},
			play: function()
				{
				$(this).jPlayer('pauseOthers', 0);
				},

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

	for(var i=1;i<=6;++i) for(var j=1;j<=2;++j)
		{
		elements[counter++] = {'kind': i, 'item': j};
		}


	$.each(_.shuffle(elements),function(index,value)
		{
		$('.elements').append('<div class="type_'+value.kind+'_'+value.item+' covered" data-kind="'+value.kind+'" data-item="'+value.item+'"/>');
		});

	var counter = 0;

	var clickedPairs = -1;
	var pairs = {kind: [0,0], item: [0,0]};

	var click = function()
		{
		++counter;
		++clickedPairs;

		pairs.kind[clickedPairs] = $(this).data('kind');
		pairs.item[clickedPairs] = $(this).data('item');

		if ($(this).data('item') === 1)
		{
			console.log('aaaaa')
			$('#jplayer_sound'+$(this).data('kind')).jPlayer('play');
		}

		$(this).removeClass('covered').off('click');

		if(clickedPairs === 1)
			{
			if(pairs.kind[0] !== pairs.kind[1])
				{
				$('.elements > *').off('click');

				setTimeout(function()
					{
					$('.type_'+pairs.kind[0]+'_'+pairs.item[0]).addClass('covered');
					$('.type_'+pairs.kind[1]+'_'+pairs.item[1]).addClass('covered');

					counter -= 2;

					clickedPairs = -1;
					pairs = {kind: [0,0], item: [0,0]};

					$('.elements .covered').on('click',click);
					},2000);
				}
			else
				{
				clickedPairs = -1;
				pairs = {kind: [0,0], item: [0,0]};
				}
			}

		if(counter === 12)
			{
			$('#jplayer_well').jPlayer('play');
			}
		};

	$('.elements > *').click(click);
	});
