//1. INICIO
function inicio(buscar,idregistro){
	if (idregistro != ''){
		obtener_registro(idregistro);
	}else if(buscar !=''){
		buscar_registros(1,buscar);
	}else{
		mostrar_registros(1);
	}

	$("#amd_receptor").html('En espera...');
}

function obtener_registro(idcarrera) {
    $.ajax({
        url: 		'async.php',
        type: 		'POST',
        dataType: 	"xml",
        data: 		{ operacion: 1, idcarrera: idcarrera},
        beforeSend: function(){ac_loading("#carrera_title");},
        error: 		function(){ac_error("#loading");},
        success: 	function(xml){cargar_carrera(xml);},
        complete: 	function(){ac_complete("#loading");}
    });
}	

function cargar_carrera(xml){
	var i = 1;
	$("#ProgramaContent").html('');
	$(".nombre_carrera").html($(xml).find('nombre').text());
    $("#descripcion_prog").html($(xml).find('descripcion').text());
    $("#requisitos_prog").html($(xml).find('requisitos').text());
    $("#perfil_prog").html($(xml).find('perfil').text());
    $(xml).find('cuatrimestre').each(function(){
    	var cuatri = $(this).text();
    	//Agregamos el UL Cuatrimestre
    	$('<ul id="cuatri'+ i +'"></ul>')
            .html('')
            .appendTo('#ProgramaContent');
            
       	$('<li class="cuatrimestre"></li>')
		    .html('<span>'+ i +'&deg;</span> Cuatrimetre')
		    .appendTo('#cuatri'+i); 
            
	        $(this).find('curso').each(function(){
				var curso = $(this).text();
				//Agregamos el LI curso
		    	$('<li class="curso"></li>')
		            .html(curso)
		            .appendTo('#cuatri'+i);        	
	        });
		i++;
    });

}

function votar(valor){
	    $.ajax({
        url: 		'votar.php',
        type: 		'POST',
        dataType: 	"html, script",
        data: 		{ valor: valor},
        beforeSend: function(){ac_loading("#carrera_title");},
        error: 		function(){ac_error("#loading");},
        success: 	function(response){$("#proyecto"+valor).html(response);},
        complete: 	function(){ac_complete("#loading");}
    });
}

//8.CANCELAR
function cancelar(){
	$("#amd_receptor").html('Operacion cancelada');
}

//9.CUSTOM AJAX
	//9.1 Cargando
	function ac_loading(div_carga, nombref){
			console.group(nombref + ' - Procedimiento en Loading');
			console.time(div_carga);
        	$(div_carga).html('<img src="../ac-design/images/loading.gif" alt="Loading"/>');
            $(div_carga).fadeIn("slow", function(){});
	}
	//9.2 Error
	function ac_error(div_carga){
			$(div_carga).html('ERROR, ALGO SUCEDIO');
			console.error("Error en llamada AJAX"); 
	}
	//9.3 Completado
	function ac_complete(div_carga){
            console.info("Completado en");  
            console.timeEnd(div_carga);
            console.groupEnd();   
            $(div_carga).fadeOut("slow", function(){});      
	}	
			
