
$(function()
	{
	/**
	 * Configuration
	 */
	var elementsCount = 24;

	/**
	 * Elements
	 */
	var elements = [];
	for(var i=1;i<=elementsCount;++i)
		{
		elements[i-1] = (i % (elementsCount / 2)) + 1;
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	/**
	 * Click
	 */
	var counter = 0;

	var clickedPairs = -1;
	var pairs = {type: [0,0], element: [0,0]};

	var timeChange = 0;
	if($('main').data('time') > 0)
		{
		timeChange = 1;
		var time = $('main').data('time');

		$('.time').find('span').text(time);
		var interval = setInterval(function()
			{
			--time;

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

			if((time === 0) && (counter < elementsCount))
				{
				clearInterval(interval);

				$('.elements > *').off('click').addClass('covered');
				$('#jplayer_wrong').jPlayer('play');
				}
			},1000);
		}

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

		pairs.type[clickedPairs] = $(this).data('type');
		pairs.element[clickedPairs] = $(this).data('element');

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

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

				setTimeout(function()
					{
					for(var i=0;i<2;++i)
						{
						$('.elements > *').eq(pairs.element[i]-1).addClass('covered');
						}

					counter -= 2;

					clickedPairs = -1;
					pairs = {type: [0,0], element: [0,0]};

					if((timeChange === 0) || ((timeChange === 1) && (time > 0)))
						{
						$('.elements .covered').on('click',click);
						}
					},2000);
				}
			else
				{
				clickedPairs = -1;
				pairs = {type: [0,0], element: [0,0]};

				$('#jplayer_sound'+$(this).data('type')).jPlayer('play');
				}
			}

		if(counter === elementsCount)
			{
			if((timeChange === 1) && (time > 0))
				{
				clearInterval(interval);
				}

			$('#jplayer_well').jPlayer('play');
			}
		};

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