
Ape.Element.game_memory = function(){
	var self = this;
	
	var openFirst = 'none';
	var openSecond = 'none';
	var of = 'none';
	var os = 'none';
	
	var openpairs = 0;
	var icons = 0;
	
	var working = 0;
	var ivwPixelStack = new Array();
	var animationSpeed_front = 250;
	var animationSpeed_back  = 250;
	
	var waitingTimeToClose = 500;
	var tryouts = 0;
	
	var params = '';
	
	this.start = function(count){
		
		$('#gamememory_area div.icon div.back').bind('click.memory_switch', memorySwitch);
		
		$('#gamememory_area div.icon').each(function(){
			
			icons++;
			
		});
		
	}
	
	function memorySwitch(event){
		//Ape.debug('was clicked: ' + this.id + ' captured by: ' + event.target.toString());
		
		var showFrontID = $(this).parent().attr('id').split('_').pop();

		if(working === 0){
			working = 1;
			
		
			tryouts = tryouts+1;
			
			$('#game_finised_link').attr('href', '?step=form&game_parameter=tryouts:'+tryouts+'&Done=1');
	
			
			//showBack();
				
			if(openFirst !== 'none'){
			openSecond = showFrontID;
		
		}else{
			openFirst = showFrontID;
		}
				
		$('#game_side_back_'+showFrontID).animate({
					width: "0px",
					left: "60px"
			}, animationSpeed_front, '', function(){
				
				$('#game_side_back_'+showFrontID).hide();
				$('#game_side_front_'+showFrontID).animate({
							width: "121px",
							left: "0px"
					}, animationSpeed_front, '', function(){
						
						if(	openFirst !== 'none' && openSecond !== 'none' ){
			
							if($('#game_side_id_'+openFirst).val() === $('#game_side_id_'+openSecond).val()){
								
								openpairs++;
								
								of = 'none';
								os = 'none';
								
								openFirst = 'none';
								openSecond = 'none';
								
								if(openpairs === (icons/2)){
									
									$('#game_finished').show();
										window.setTimeout(gotoFormURL, 1000);
								
								}else{
									working = 0;
								}
							}else{
	
								of = openFirst;
								os = openSecond;
							
								window.setTimeout(showBack, 1000);
								
								openFirst = 'none';
								openSecond = 'none';
								
								working = 1;
								
							}
						}else{
							working = 0;
						}
						
					} );
					}
				
			);
			
		}
	};
	function showBack(){
		
		$('#game_side_front_'+of).animate({
			width: "0px",
			left: "60px"
			}, animationSpeed_back, '', function(){
				$('#game_side_back_'+of).animate({
					width: "121px",
					left: "0px"
				}, animationSpeed_back, '', function(){
					of = 'none';
				})
			}
		);
		
		$('#game_side_front_'+os).animate({
			width: "0px",
			left: "60px"
			}, animationSpeed_back, '', function(){
				$('#game_side_back_'+os).animate({
					width: "121px",
					left: "0px"
				}, animationSpeed_back, '', function(){
					os = 'none';
					working = 0;
				})
			}
		);
	
	};
	
	function gotoFormURL(){
		if(!abgelaufen){
			if($('#game_finised_link').attr('href') != undefined && $('#game_finised_link').attr('href') != ''){
				window.location.href = $('#game_finised_link').attr('href');
			}
		}
	}
}

