$(document).ready(function(){

	$("div.dato_guardar").hide();
	$(".icono_edicion").click(function(){
		$(".dato_guardar").each(function(){
			if ($(this).is(":visible")){
				$(this).hide();
				$(this).parent().find("span.dato_editable").show();
			}
		});
		$(this).hide();
		$(this).parent().parent().find("div.dato_guardar").show();
		$(this).parent().parent().find("span.dato_editable").hide();
	});
/*
	$(".dato_real").css('display','none');
	var texto="";
	$(document).click(function(event){
		var target = $(event.target);
		if (target.find(".table_list").length != 0) {
			$(target).find("select").each(function(){
				if ($(this).parent("div.dato_real").css('display') =="block"){

					if ($(this).attr("name").substr(0,4)=='hour'){//select de horas 
						if ($(this).val()<10) texto ="0"+$(this).val();else texto=$(this).val();	
						cantidad=$(this).next().val();
						if ($(this).next().val()<10) texto +=":00";else texto +=":"+$(this).next().val();
					}
					
					if (texto=="")	$(this).parent().parent().find("span").text($(this).find('option:selected').text());
					else{
						$(this).parent().parent().find("span").text(texto);
						texto="";
					}
						$(this).parent().parent().find("div").css('display','none');
						$(this).parent().parent().find("span").css('display','block');
				}
			});

			$(target).find("input").each(function(){
				if ($(this).parent("div.dato_real").css('display') =="block"){
					if ($(this).attr("name").substr(0,5)=='fecha'){//input de fecha 
						$(this).parent().parent().find("span").text($(this).val());
					}					
					$(this).parent().parent().find("div").css('display','none');
					$(this).parent().parent().find("span").css('display','block');
				}
			});
		}
    });

	$('.select_tecnicos').change(function(){
		$(this).parent().parent().find("span.edit_dato").text($(this).find('option:selected').text());
		$(this).parent().parent().find("span.edit_dato").css('display','block');
		$(this).parent().parent().find("div").css('display','none');
	});

	$('.edit_dato').click(function(){
		var texto="";
		$("div.dato_real:visible").find("input").each(function(){$(this).parent().parent().find("span.edit_dato").text($(this).val());});
		$("div.dato_real:visible").find("select").not(".select_tecnicos").each(function(){
			var valor=$(this).val();
			if (valor.length==1) valor="0"+valor;
			if (texto=="") texto =valor;
			else texto += ":"+valor;
			$(this).parent().parent().find("span.edit_dato").text(texto);
		});
		$("div.dato_real").hide();
		$("span.edit_dato").show();
		$(this).hide();
		$(this).parent().find("div").show();
		ver_campo_tabla(this);
	});

	function ver_campo_tabla(object){			
			var texto="";
		$(".dato_real").not(object).each(function(){

			if ($(object).css('display') =="block"){

				$(object).find("select").each(function(){
					if (($(object).attr("name").substr(0,4)=='hour')&&($(object).attr("name").substr(0,8)!='hour_fin')){//select de horas iniciales
						if ($(object).val()<10) texto="0"+$(object).val();else texto=$(object).val();
					}
					if (($(object).attr("name").substr(0,6)=='minute')&&($(object).attr("name").substr(0,10)!='minute_fin')){//select de minutos iniciales
						if ($(object).val()<10) texto +=":00";else texto +=":"+$(object).val();
						$(object).parent().parent().find("span").text(texto);
					}

					if ($(object).attr("name").substr(0,8)=='hour_fin'){//select de horas iniciales
						if ($(object).val()<10) texto="0"+$(object).val();else texto=$(object).val();
					}
					if ($(object).attr("name").substr(0,10)=='minute_fin'){//select de minutos iniciales
						if ($(object).val()<10) texto +=":00";else texto +=":"+$(object).val();
						$(object).parent().parent().find("span").text(texto);
					}

					if ($(object).attr("name").substr(0,15)=='select_tecnicos'){//select de tecnicos
						$(object).parent().parent().find("span").text($(object).find('option:selected').text());
					}
				});
				$(object).find("input").each(function(){
					if ($(object).attr("name").substr(0,5)=='fecha'){//input de la fecha
						$(object).parent().parent().find("span").text($(object).val());
					}
				});
			}
		});
	}
	$('[rel="tecnico"]').click(function(){
		var cod=$(this).parent().find("span").text();
		var element=document.getElementById("tecnico"+cod).style;
		var element2=document.getElementById("tecnicosel"+cod).style;
		if (element.display =="block"){
			element.display ="none";
			element2.display ="block";				
		}else{
			element.display ="block";
			element2.display ="none";
		}
	});
*/
	$("#intervenciones_calendar .cls_mover_fecha").click(function(){
		var date = $(this).attr("rel");
		$("#intervenciones_calendar").load("intervenciones_calendar.php",{date: date});
	});
	$("#intervenciones_calendar td.ocupado_inicio").click(function(){
		$("#intervenciones_act").toggle();
		if ($("#intervenciones_act").css("display")!="none"){
			var date = $(this).attr("rel");
			$("#intervenciones_act").load("./autocomplete/ajax_listados.php?list_tip=17",{date: date});
		}		
	});
	$("#intervenciones .dia_citas").click(function(){
		var fecha = $(this).children("span").attr("rel");
		$("#intervenciones_content").load("intervenciones_citas.php",{date: fecha});
	});

	$("#intervenciones_calendar .hoy").click(function(){
		var fecha = $(this).children("span").attr("rel");
		$("#intervenciones_calendar").load("intervenciones_calendar.php",{date: fecha});
		$("#intervenciones_content").load("intervenciones_citas.php",{date: fecha});
	});
	
	
	$("#intervenciones_calendar2 .cls_mover_fecha2").click(function(){
		var date = $(this).attr("rel");
		$("#intervenciones_calendar2").load("intervenciones_calendar2.php",{date: date});
	});
	
});

