jQuery(document).ready(function($) {

	$.fn.extend({
		tx_spmemory: {
			messages: {
				congratulation: 'Glückwunsch',
				message_rounds_and_time_elapsed: 'Du hast {0} Züge benötigt und dafür {1} Sekunden gebraucht.',
				message_position_in_highscore: 'Momentan bist Du auf Platz {0} in der Highscore-Liste.',
				card_alt_closed: 'Verdeckte Karte',
				card_alt_open: 'Motiv {0}'
			}
		}
	});

	$().tx_spmemory.format = function(source, params) {
		if ( arguments.length == 1 )
			return function() {
				var args = $.makeArray(arguments);
				args.unshift(source);
				return $.tx_spmemory.format.apply( this, args );
			};
		if ( arguments.length > 2 && params.constructor != Array  ) {
			params = $.makeArray(arguments).slice(1);
		}
		if ( params.constructor != Array ) {
			params = [ params ];
		}
		$.each(params, function(i, n) {
			source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
		});
		return source;
	};

	var lastlastX = null;
	var lastlastY = null;
	var lastX = null;
	var lastY = null;
	var lastStatus = null;

	$('.tx-spmemory-pi1 a:has(img.hiddenCard)').click(function() {
		var $this = $(this);
		var $img = $this.children('img');
		var $row = $img.closest('tr');
		var $table = $row.closest('table');

		if (!$img.hasClass('hiddenCard')) {
			return false;
		}

		var row = $('tr',$table).index($row);
		var col = $('img',$row).index($img);

		$.ajax({
			url: 'index.php',
			type: 'POST',
			data: {
				eID: 'sp_memory',
				col: col,
				row: row
			},
			dataType: 'json',
			success: function(data) {

				// open current card
				$img
					.attr('src',$img.attr('src').replace(/\/[^\/]*$/,'/pic' + data['answer'] + '.gif'))
					.attr('alt', $().tx_spmemory.format($().tx_spmemory.messages.card_alt_open, data['answer']))
					.attr('title', $().tx_spmemory.format($().tx_spmemory.messages.card_alt_open, data['answer']))
					.removeClass('hiddenCard')
					.addClass('openCard');

				// close all open cards on every odd click
				if (data['status'] == 'first_card') {
					$('img.openCard', $table).not($img).each(function() {
						$(this)
							.attr('src',$(this).attr('src').replace(/\/[^\/]*$/,'/back.gif'))
							.attr('alt', $().tx_spmemory.messages.card_alt_closed)
							.attr('title', $().tx_spmemory.messages.card_alt_closed)
							.addClass('hiddenCard')
							.removeClass('openCard');
					});
				}

				if ((data['status'] == 'pair') || (data['status'] == 'solved')) {
					$('img.openCard', $table).addClass('foundCard').removeClass('openCard');
				}

				$('.round').html(data['round']);
				$('.found').html(data['found']);
				if (data['status'] == 'solved') {
					var $link1 = $('.tx-spmemory-pi1 .restartLink').clone().wrap('<p></p>').parent();
					var $link2 = $('.tx-spmemory-pi1 .highscoreLink').clone().wrap('<p></p>').parent();
					var text = '<div title="' + $().tx_spmemory.messages.congratulation + '">';
					text +=	'<p>'
						+ $().tx_spmemory.format(
							$().tx_spmemory.messages.message_rounds_and_time_elapsed,
							data['round'],
							data['time']
						)
						+ '</p>';
					if (data['position']) {
						text += '<p>'
							+ $().tx_spmemory.format(
								$().tx_spmemory.messages.message_position_in_highscore,
								data['position']
							)
							+ '</p>';
					};
					text += '</div>';

					$(text)
						.append($link1)
						.append($link2)
						.insertAfter($table)
						.dialog({
							bgiframe: true,
							modal: true,
							buttons: {
								OK: function() {
									$(this).dialog('close');
								}
							}
						});
				}
			}
		});

		return false;
	})
});

