// JavaScript Document
$(document).ready(function() {
		
		$("#ARBDenegarPeticiones").click(function(){
			
			var total = $(".ARPeticionesJugadorItem:checked").length + ($(".ARPeticionesParejaItem:checked").length*2);
			
			if (total == 0) return false;
			
		});
		
		$('#ARInvitarNombre,#ARInvitarEmail,#ARInvitarComentarios').click(function(){
			$("#ARTipoNuevoParticipante1").attr('checked','checked');
		});
		
		$('#ARNombreNuevoParticipante').focus(function(){																							   			$("#ARTipoNuevoParticipante2").attr('checked','checked');
		});
		
		$('#listaamigosinvitar').focus(function(){																							   			$("#ARTipoNuevoParticipante3").attr('checked','checked');
		});
		
		$("#partidoJugadores select").change( function(ev){

			var sel = $(ev.target);
			var tsel = Array();
			
			tsel = $('#partidoJugadores select:has(option[value="'+sel.val()+'"][selected])');
			
			tsel.each(function(i,el){
				if($(el).attr('id') != sel.attr('id'))
				{
					$(el).val(0);
					$(el).effect("highlight", {'color': '#D0FF1F'}, 2000);
				}
			});

		
		});
		
		//Retos por equipos modojuego 1
		
		$('#bsubire1').click(function(){
									  
			$('#jugadores_para1 option:selected').each(function(i,el){
				$('#jugadores_para2').find("option[value='"+$(el).val()+ "']").remove();
				$('#jugadores_equipo1').append($(el));
			});
									  
		});
		$('#bsubire2').click(function(){
									  
			$('#jugadores_para2 option:selected').each(function(i,el){
				$('#jugadores_para1').find("option[value='"+$(el).val()+ "']").remove();
				$('#jugadores_equipo2').append($(el));
			});
									  
		});
		$('#bbajare1,#bbajare2').click(function(){
			
			var idn = $(this).attr('id').substring($(this).attr('id').length-1,$(this).attr('id').length);

			$('#jugadores_equipo' + idn +' option:selected').each(function(i,el){
				$('#jugadores_para1').append($(this).clone());
				$('#jugadores_para2').append($(this).clone());
				$(this).remove();
				sortSelect('jugadores_para1');
				sortSelect('jugadores_para2');
			});
									  
		});
		
		$('#ARBAceptarJugadores').click(function(){
			$('#jugadores_equipo1 option').each(function(){$(this).attr("selected","selected");});
			$('#jugadores_equipo2 option').each(function(){$(this).attr("selected","selected");});
		});
		
		//Retos modalidad equipos
		$('#formAddEquipo').submit(function() {
		
				xajax_handler('retos','add_equipo',xajax.getFormValues(document.getElementById('formAddEquipo')),null);
				$('#ARNombreEquipo').val('');
				return false;
		
		});
	
});

function sortSelect(selectToSort) {
    var sortedVals = $.makeArray($('#' + selectToSort + ' option')).sort(function(a,b){
   		 return $(a).text() > $(b).text() ? 1: -1;
 	});

	$('#' + selectToSort).empty().html(sortedVals);
}

function ajustarJugadores()
{
	$('#jugadores_equipo1 option,#jugadores_equipo2 option').each(function(i,el){
		$('#jugadores_para1').find("option[value='"+$(el).val()+ "']").remove();
		$('#jugadores_para2').find("option[value='"+$(el).val()+ "']").remove();
	});
}

