$(function()
	{
	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 turn = 0;
	var names = ['Mieszko I','Bolesław Chrobry','Mieszko II Lambert'];

function makeTurn(){
	$('.wrapper').addClass('turn'+turn);
	{

		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,
				stack: '.elements > *',
				scope: $this.data('element')
				});
			});

		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('destroy');
					$(event.target).css('background', '');
					if(!$("html").hasClass("ie8") ) {
						ui.helper.draggable('destroy').remove();
					};
					if (counter === 16)
					{
						$('#jplayer_well').jPlayer('play')
						$('.name').text(names[turn]).fadeIn();
						if (turn < 2)
						{
						setTimeout(function(){
						$('.name').fadeOut();
						$('.boxes > *').removeClass('ok').css('background','none');
						turn++;
						makeTurn();
						},5000)
						}

					}
					}
				});
			});

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

});
