
$(function()
	{

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

	/**
	 * Elements
	 */

	var elements = [];
	for(var i=1;i<=9;++i)
	{
		elements[i-1] = i;
	}
	var positions = [
		[-74,-40],[95,-40],[265,-40],
		[-74,145],[95,145],[265,145],
		[-74,330],[95,330],[265,330]
	];
	var count = 0;
	$.each(_.shuffle(elements),function(index,value)
		{
		$('.elements').append('<div style="position: absolute; right: '+positions[count][0]+'px; top: '+positions[count][1]+'px;" class="element_'+value+'" data-element="'+value+'"/>');
		count++;

		});

	/**
	 * Draggable style="background: none; background-color: #ffffff;"
	 */
	$('.elements > *').each(function()
		{
		var $this = $(this);

		$this.draggable(
			{
			cursor: 'move',
			opacity: .7,
			scope: $this.data('element')
			});
		});

	/**
	 * Droppable
	 */
	var counter = 0;

	$('.boxes > *').each(function()
		{
		var $this = $(this);

		$this.droppable(
			{
			accept: $('.elements > *'),
			addClasses: false,
			scope: $this.data('kind'),

			drop: function(event,ui)
				{
				++counter;

				$(event.target).addClass('ok').droppable('disable');
				$(event.target).css('background', '');
				if(!$("html").hasClass("ie8") ) {
					ui.helper.css('display','none').draggable('destroy');
				}



					if(counter === 9)
					{
						var url = window.location.pathname;
						var filename = url.substring(url.lastIndexOf('/')+1);

					       comand = filename.charAt(5);


						if (comand === '.')
						{

							$('#jplayer').jPlayer('setMedia', {mp3: './media/1.mp3'}).jPlayer('play');

						}
						else
						{

							$('#jplayer').jPlayer('setMedia', {mp3: './media/'+comand+'.mp3'}).jPlayer('play');

						}


					}

				}
			});
		});
	/**
	 * Hint
	 */
	$('a.hint').click(function(e)
		{
		e.preventDefault();
		$('div.hint').fadeToggle();
		});



	/**
	 * Check
	 */
	$('.footer_game .check').click(function(e)
		{
		e.preventDefault();

		if(counter === 9)
			{
			$('#jplayer_well').jPlayer('play');
			}
		else
			{
			$('#jplayer_wrong').jPlayer('play');
			}
		});
	});
