
$(function()
	{
	window.gameOn = true;

	/**
	 * jPlayer
	 */
	 if($('main').data("time") > 0) {
		$('.footer').append('<span class="description">Pozostały czas: <span id="OneHourCountDown"></span>  </span>');
	 }
	$('#jplayer').on($.jPlayer.event.ready,function()
		{
		$('#jplayer').jPlayer('setMedia', {mp3: './media/file1.mp3'});
		});

	/**
	 * Elements
	 */

	var elements = [];
	for(var i=1;i<=16;++i)
	{
		elements[i-1] = i;
	}
	var positions = [
		[-82,-38],[52,-38],[185,-38],[318,-38],
		[-82,100],[52,100],[185,100],[318,100],
		[-82,235],[52,235],[185,235],[318,235],
		[-82,370],[52,370],[185,370],[318,370],
	];
	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++;

		});

	$('.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 === 16)
				{
				$('#jplayer_well').jPlayer('play');
				window.gameOn = false;
				}
				}
			});
		});
	/**
	 * Hint
	 */
	$('a.hint').click(function(e)
		{
		e.preventDefault();
		$('div.hint').fadeToggle();
		});
});
