function __getKey(a, wordId) {	
	
	var key = false;	
	for (i in a) {	
		if (a[i] == wordId) {		
			key = i;
			break;		
		}	
	}	
	return key;
		
}


function __removeWord(a, key) {

	a.splice(key, 1);
	
}


function __isSelected(a, wordId) {

	key = __getKey(a, wordId);
	if (key) {
		return true;
	} else {
		return false;
	}
	
}	

	
$(document).ready(function(){
	
	var w = new Array();

	$('.word').toggle(	
		function () {
				
			if (w.length < 5) {
								
	        	$(this).css({"background-image":"url('/images/word-bg-selected.png')"});
	        	w.push($(this).attr('id'));
	        		        	
	        	if (w.length == 5) {        	
	        		$('#userform').show('slow');        	
	        	} 

			} else {
			
	        	alert("Legfeljebb 5 tulajdonság választható!");
	        	$(this).trigger('click');
	        	
	        }
	                	
      	},
      	function () {      	
      	
      		motorhead = __isSelected(w, $(this).attr('id'));
      	
      		if (w.length < 5 || motorhead) {
      	
	        	$(this).css({"background-image":"url('/images/word-bg.png')"});        	
	        	key = __getKey(w, $(this).attr('id'));  	
	         	__removeWord(w, key);
	         	
	       		$('#userform').hide('slow');
         	
         	}      	
         	        	
      	}
    ); 
    
    
    $('#submit').click(function() {   
    
    	$('#loading').show(200);
    
    	name = $('#name').val();
    	email = $('#email').val();
    	
    	var johariData = $('form').serialize();
    	johariData += "&w=" + w;    	
	
		$.ajax({  
			type: "POST",  
			url: "/process.php",  
			data: johariData,  
			success: function(response) {
				$('#loading').hide(500);
				$('#container').html(response);		 
			},
			error: function() {
				$('#container').text('Hiba történt, kérjük próbálja meg később!');
			}
		});    	
    
	    return false;
    
    });    
    
   	
});

