/**
 * "Memory"
 *
 * Ajaxbasiertes Memory-Spiel
 *
 * @copyright cekom GmbH <mail@cekom.de>
 * @package Laurin Custom-Modul
 * @author Michael Suszek <suszek@cekom.de>
 *
 * mehr info in readme.txt
**/

var card_height = '109'; //bildchengröße
var card_width = '109'; //bildchengröße
var firstmove = 0;


$.ajax('/custom/memory/xhr_mem.php?action=start');

function mem_start(){
	//schicke Befehl für Spielfeld-Erstellung
	$('#mem_field').load('/custom/memory/xhr_mem.php?action=start'); //spielfeld schon vorher erzeugen
	$.ajax('/custom/memory/xhr_mem.php?action=start');
	// $('#mem_start_button').remove();

	//$('#mem_field').load('/custom/memory/xhr_mem.php?action=intoHighscorefield');
	$('.lrn-el-951').replaceWith('<div class="lrn-element lrn-richtext lrn-el-951"><h1>Weihnachts-Walnuss-Bildpaare-Finden</h1><p>Los geht&#039;s: Decken Sie so schnell wie m&ouml;glich die zusammen geh&ouml;renden Bildpaare rund um die kalifornische Walnuss auf! Doch Vorsicht: wir haben es nicht so leicht gemacht!</p></div>');
	
}//function

function lookCard(tid){
	
	if(tid == firstmove){
		//ignorieren
		return 0;
	}

	$.getJSON('/custom/memory/xhr_mem.php?show='+tid, 
		function (data){
		/*
			rückgabewerte (json) ['',0,0,0,0,0]:
				0: 'pic-dateiname des aktuellen zugs', 
				1: 'position der ersten karte', 
				2: 'position der zweiten karte oder 0',
				3: 'treffer (1) / niete (0)',
				4: 'zug-nr',
				5: 'fertig (1 / 0)'
		*/
			if(data[2] > 0){
				firstmove = 0;
			}else{
				firstmove = data[1];
			}
			
			//-umdrehen:
			$('#'+tid+' img').animate({width: '0', height: card_height+'px'},300,
			function(){
				$('#'+tid+' img').attr('src','/custom/memory/cards/'+data[0]);
				$('#'+tid+' img').animate({width: card_width+'px', height: card_height+'px'},300);
			});
			//-treffer / niete:
			
			if(data[2] > 0) { // = 2. zug
				if(data[3] == 1){
					setTimeout("removecard("+data[1]+")",1000);
					setTimeout("removecard("+data[2]+")",1000);
				}else{
					setTimeout("hidecard("+data[1]+")",1000);
					setTimeout("hidecard("+data[2]+")",1000);
				}
				$('#mem_zugnr').text(data[4]);
			}
			if(data[5] == 1){
				$('#mem_field').load('/custom/memory/xhr_mem.php?action=intoHighscorefield');
				$('#mem_field').removeClass('secondEndPic');
				$('.lrn-el-951').replaceWith('<div class="lrn-element lrn-richtext lrn-el-951"><h1>Weihnachts-Walnuss-Bildpaare-Finden</h1><p>Gratulation, Sie haben es geschafft!<br />Wahrscheinlich essen Sie täglich Ihre Portion Walnüsse?<br />Nur noch das Teilnahme-Formular ausfüllen, und schon sind Sie dem Weihnachtstraum einer original KitchenAid Artisan ein ganzes Stück näher!</p></div>');
				/* $('.lrn-el-951').replaceWith('<div class="lrn-element lrn-richtext lrn-el-951"><h1>Weihnachts-Walnuss-Bildpaare-Finden</h1><p>Nur noch das Teilnahme-Formular ausf&uuml;llen, und schon sind Sie dem Weihnachtstraum einer original KitchenAid Artisan ein ganzes St&uuml;ck n&auml;her!</p></div>'); */
			}
		});
}


function dataintoHighscore(){
	if(checkFormularFilled()){
		$.post('/custom/memory/xhr_mem.php?action=setHighscore',
			$('#highvalues').serialize(),
			function(data) {
				$('#mem_field').html(data);
				$('#mem_field').addClass('secondEndPic');
				$('.lrn-el-951').replaceWith('<div class="lrn-element lrn-richtext lrn-el-951"><h1>Weihnachts-Walnuss-Bildpaare-Finden</h1></div>');
			}
		);
	}
}

function checkFormularFilled(){
	error = 0;
	$('.mem_textfeld').each(function(index) {
		if(this.value == ''){
    		error++;
    	}
	});
	if(error == 0) {
		return 1;
	}else{
		alert("Bitte füllen Sie das Formular vollständig aus.");
		return 0;
	}
}

function hidecard(id){
	
	$('#'+id+' img').animate({width: '0', height: card_height+'px'},300,
	function(){
			$('#'+id+' img').attr('src','/custom/memory/cards/card_back.jpg');
			$('#'+id+' img').animate({width: card_width+'px', height: card_height+'px'},300);
		});
}

function removecard(id){
	$('#'+id+' img').slideUp('fast');
}
