muziArray = Array("hracy","dvojhry","stvorhry","mixy", "y");
zenyArray = Array("hraci","dvojhri","stvorhri","mixi", "i");
counter = 0;
counter2=1;

/*zmeni z pridaj hraca na dalsi krok*/
$(function() {
	
	$('#button_add_hrac').click(function() {
		text="<select id=\"ucast\" name=\"ucast[] \"> </select>";
		
		option= '<option class="" selected="selected" value="17"> </option>';
		//$(this).parent().append(text);
	});
	
	addHrac('ucast', 'meno') ;

	$(".input_autocomplete").live('keyup' ,function() {
		var input =$(this);
		
		//if ($(this).val().length <3 ) return;
		availableTags= spustiAjaxMeno($(this).val() ,function(results) {
			
				availableTags = results.split("~"); 
				
				//2 lebo vzdy je meno~... teda [ meno, ""]
				if (availableTags.length==2) {
					//ak je tam iba jeden vysledok napis ho rovno
					input.val(availableTags[0]);					
				}
				//mam nove data, nacitaj znova autocompleter
				else {
				$( ".input_autocomplete" ).autocomplete({
					source: availableTags
				});
				}
			});
	});
	
	/*
	 * prekopiruje vsetkych hracov do selectu
	 */
	$('input[name="meno[]"]').live('change' ,function() {
		//select[name="teamA[]"] ,select[name="teamB[]"]
		$('select').each(function() {
			$(this).find('option').remove().end();
		});
		
		$('input[name="meno[]"]').each(function() {
			text=$(this).val();
			value=$(this).val().substring( $(this).val().indexOf("(")+1,$(this).val().indexOf(")")); 
			$('select').each(function() {
				$(this).append("<option value='" + value +"'>" + text + "</option>");
			});
	      });
	});
	
	zastavEnter();
	
	/*
	 * Datepicker
	 */
	$( "#datum" ).datepicker({ dateFormat: 'dd-mm-yy' }).val();
	 
});

function spustiAjaxMeno( meno , odozva ){
	 $.post('/plugins/trener/ajax.php',
	  { 
	    "str": meno
	  },
	  function(data,status){
		  odozva(data);
	  });
}


function zastavEnter(){
		$(".input_autocomplete").keydown(function(event){
			    if( (event.keyCode == 13)  ) {
			      event.preventDefault();
			      
			      /*zober vsetky autocompleteri a ak su vsetky 
			       * hidnute tak vtedy pridaj riadok
			       * inak by sa enter potvrdzujuci vyber dal chapat ako pridaj riadok
			       */
			      ok=true;
			      $(".ui-autocomplete").each(function() {
			    	  if ($(this).css("display")=="block") ok=false;
			      });
			      if (ok) {
			    	  addHrac('ucast', 'meno');
			    	  $('#ucast input').last().focus();
			    	  zastavEnter();
			      }
			      
			      return false;
			    }
			  });
	}

function addHrac(toElement, name , count= null) {
	if (name=='meno'){
	var content= '<div class= "u"> <label>Meno/priezvisko</label>'
	+	'<input id="ucast'+counter+ '" class="input_large input_autocomplete" type="text" name="'+name+'[]" autocomplete="off"><a class="submit input_delete" onclick="vymaz($(this).parent());" href="#" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </a><br></div> ';
	+ $('#'+toElement).append(content);
	
	
	}else {
		content ='<label id="'+name+count+'[]_label" for="'+name+count+'[]">'+name+'</label> <select name="'+name+count+'[]"> <option class="a" selected="selected" value="">Hrac musí byť na treningu</option> </select> <br>';
		$(toElement).parent().append(content);
	}
	
	counter++;	
	
}

function vymaz(element){
	if (element.find(" input ").text() == "") {
		element.remove();
		return false;
	}
	if (confirm('vymazat?')) element.remove();
	else return false;
}

function addZapas() {
	var content= '<fieldset > <legend>Zápas</legend> <label for="teamA'+counter2+'[]" id="teamA[]_label'+counter2+'">team A</label> <select name="teamA'+counter2+'[]" id="teamA'+counter2+'[]" > <option class="a" value="" selected="selected">Hrac musí byť na treningu</option> </select><br /> <input type="button" name="button_add_m" id="button_add_teamA" class="submit input_expand" value="Pridaj ďalšieho hráča" onclick="addHrac( this, \'teamA\','+counter2+' );" /><br> <label for="teamB'+counter2+'[]" id="teamB[]_label'+counter2+'">team B</label> <select name="teamB'+counter2+'[]" id="teamB'+counter2+'[]" > <option class="a" value="" selected="selected">Hrac musí byť na treningu</option> </select><br /> <input type="button" name="button_add_m" id="button_add_teamB" class="submit input_expand" value="Pridaj ďalšieho hráča" onclick="addHrac(this, \'teamB\','+counter2+' );" /><br>';
	content += '</div><label for="score'+counter2+'" id="score'+counter2+'_label">Score</label>'
	+'<input type="text" name="score'+counter2+'" id="score'+counter2+'" class="input_small" value="" maxlength="16"  /><br />'
	+'<label for="vytaz0" id="vytaz'+counter2+'_label">Vytaz</label>'
	+'<input type="radio" name="vytaz'+counter2+'" id="vytaz'+counter2+'_A" value="A" class="optionbox" checked="checked" /> team A &nbsp; <input type="radio" name="vytaz'+counter2+'" id="vytaz'+counter2+'_B" value="B" class="optionbox" /> team B<br />  </fieldset>';
	$('form fieldset:last').parent().append(content);
	$('#pocetZapasov').attr('value', counter2);
	counter2++;
	
}





