
$(window).load(function(){
	$('#showconds').click(function(){
		if($("#conditions").is(":hidden"))
			$("#conditions").slideDown(600, function(){ $('span.plus').html('-'); });
		else
			$("#conditions").slideUp(600, function(){ $('span.plus').html('+'); });
	});
	$('select#participantes').change(function(){
		if(this.value == "0"){			
			$('div#dinamicsets div#sets').empty().parent('display', 'none');
			$('div.modul__reservar_reservar').css('visibility', 'hidden');
			$('#samecourse').css('display', 'none');		
		}else if(this.value == "1"){
			$("#dinamicsame").empty();
			doforone();	
			$('select.typecourse').change(function(){
				if(this.value == '0'){
					$(this).parent().next().empty();
				}else if(this.value == ' Windsurf'){
					var level = $(this).parent().next();
					level.html($('#patternquerya').html())
					$("p.step1", level).css('display', 'block');				
					level.css('display', 'block');
				}else{
					var level = $(this).parent().next();
					level.html($('#patternqueryb').html())
					$("p.step1", level).css('display', 'block');
					level.css('display', 'block');
				}
			});
		}else{
			$('div#dinamicsets div#sets').empty().parent().css('display', 'none');		
			$('div.modul__reservar_reservar').css('visibility', 'hidden');
			$('#dinamicsame').html($("#patternsame").html());
			$('#dinamicsame div:first').attr('id', 'samecourse');
			$('#samecourse input.allthesame').click(function(){
				$('div#dinamicsets div#sets').empty();
				doformore(parseInt($('select#participantes').val()));
				$('select.typecourse').change(function(){
					if(this.value == '0'){
						$(this).parent().next().empty();
					}else if(this.value == ' Windsurf'){
						var level = $(this).parent().next();
						level.html($('#patternquerya').html())
						$("p.step1", level).css('display', 'block')
						level.css('display', 'block');
					}else{
						var level = $(this).parent().next();
						level.html($('#patternqueryb').html())
						$("p.step1", level).css('display', 'block');
						level.css('display', 'block');
					}
				});
			});
		}
	}).val("0");
	$('a.boton_reservar_reservar').click(function(){ $('form#reserva').submit(); });
	$("input[type='radio']").each(function(){ this.checked = false; })
});

function doforone(){
	var clon = $('#patterncourses').clone();
	$('label', clon).each(function(){ var t = $(this); var x = t.attr('for'); t.attr('for', x.replace('X', '')); });
	$('input, select, textarea', clon).each(function(){ var t = $(this); var x = t.attr('id'); t.attr('id', x.replace('X', '')); var y = t.attr('name'); t.attr('name', y.replace('X', '')); });
	$('input#nombre_y_apellido_part', clon).parent().remove();
	$('strong, br, #participante, #firststep', clon).remove();
	$('div#dinamicsets div#sets').empty();
	$('div#dinamicsets div#sets').append(clon.html()).parent().css('display', 'block');
	$('input.fecha').mask("99/99/9999");	
	$('div#dinamicsets div#sets div.reservedata').css('display', 'none');
	//$('div.modul__reservar_reservar').css('visibility', 'visible');	
}

function doformore(n){
	var sets = $('div#dinamicsets div#sets');
	var clon = null;
	for(z = 0; z < n; z++){
		clon = $('#patterncourses').clone();
		$('label', clon).each(function(){ var t = $(this); var x = t.attr('for'); t.attr('for', x.replace('X', z + 1)); });
		$('strong span.npart', clon).each(function(){ this.innerHTML = z + 1; });		
		$('input, select, textarea', clon).each(function(){ var t = $(this); var x = t.attr('id'); t.attr('id', x.replace('X', z + 1)); var y = t.attr('name'); t.attr('name', y.replace('X', z + 1)); });
		sets.append(clon.html());
	}
	$('input.fecha').mask("99/99/9999");		
	sets.parent().css('display', 'block')
	$('div#dinamicsets div#sets div.reservedata').css('display', 'none');
	//$('div.modul__reservar_reservar').css('visibility', 'visible');		
}

function getSection(){
	var section = window.location.href.split('#');
	return (section.length == 2) ? section[1] : null;
}

function goTo(obj, replya, replyb){
	var container = $(obj).parent().parent();
	var goa = null, gob = null;
	if(obj.className == 'awcourse'){ // exception
		if(obj.options[obj.selectedIndex].className == 'windsurf-perfeccionamiento3'){
			 showform(container, obj.options[obj.selectedIndex].className);
			 return;
		}
	}
	if(replyb == null){
		$('.' + replya, container).css('display', 'block');
	}else{
		var tmp	= replya.split('#');
		replya = tmp[0];
		if(tmp.length == 2)
			goa = tmp[1]
		tmp	= replyb.split('#');
		replyb = tmp[0];
		if(tmp.length == 2)
			gob = tmp[1];
		if(obj.value == 's'){
			if(replya == 'reservedata')	
				showform(container, goa);
			else{
				$($(obj).parent()).nextAll('p').css('display','none');
				$('.' + replya, container).css('display', 'block');
			}
		}else if(obj.value == 'n'){
			if(replyb == 'reservedata')	
				showform(container,  gob);
			else{
				$($(obj).parent()).nextAll('p').css('display','none');
				$('.' + replyb, container).css('display', 'block');
			}
		}else{
			resetQuery();
		}
	}
}

function showform(container, oitem){
	var container =  container.parent();
	if(oitem){
		$('.reservedata option.' + oitem, container).each(function(){
			this.innerHTML +=  oitem != 'nodisponible' ? ' &raquo; ' + $('#recommended').val() : '';
			this.selected = true;
		});
	}
	$('div.questionary', container).css('display', 'none');
	$('div.questionary input, div.questionary select', container).attr('disabled', 'disabled');
	$('div.reservedata', container).css('display', 'block');
	$('a.qrepeat', container).css('display', 'block');
	$('div.modul__reservar_reservar').css('visibility', 'visible');		
	$('div.ptypecourse', container).css('display', 'none');
}
function resetQuery(ac){
	var pater = $(ac).parent().parent();
	$(ac).css('display', 'none');
	$('div.ptypecourse').css('display', 'block');
	$('div.questionary p', pater).css('display', 'none');
	$('div.questionary input, div.questionary select', pater).attr('disabled', '');
	$('select.typecourse', pater).val('0').trigger('change');	
	$('div.reservedata', pater).css('display', 'none');
	$('div.reservedata select.cursos', pater).html($('#patterncourses #cursoX').html());
	$('div.modul__reservar_reservar').css('visibility', 'hidden');		
}
