var URL = {
	ajax : "1",
	id : "0",
	url: "",
	nom: ""
}




$(document).ready(function() {
	$(".lnk_bor").css("cursor","pointer").bind("click",function(){evento_eliminar($(this).attr("id"))});
// 	$("#lnk_nue").css("cursor","pointer").bind("click",function(){evento_nuevo()});
	$("#lnk_nue").css("cursor","pointer").bind("click",function() {evento_preparar_nuevo()});
});


function evento_nuevo(url,nom) {
	URL.ajax = "enlace_nuevo";
	URL.url = url;
	URL.nom = nom;
	$.getJSON("",URL,function(jRes2){
// 		alert(jRes2.res);
		if (jRes2.res=="ERROR") {
  			alert(jRes2.res_er);
		} else {
			$("ul#listado_enlaces").append(jRes2.res_txt);
			$("#lnk_li_"+jRes2.id_enl+" .lnk_bor").css("cursor","pointer").bind("click",function(){evento_eliminar($(this).attr("id"))});
		}
	});
}


function evento_preparar_nuevo() {
	var html_inicio = $("#lnk_nue").html();
	$("#lnk_nue").css("cursor","pointer").unbind("click");
	var html_edicion = "<label for=\"nue_url\">Url:</label><input type=\"text\" value=\"\" id=\"nue_url\" class=\"inp_url\" /><br />";
	html_edicion += "<label for=\"nue_nom\">Nombre: </label><input type=\"text\" value=\"\" id=\"nue_nom\" class=\"inp_url\" /><br />";
	html_edicion += "<img src=\"img/iconos/aceptar.gif\" id=\"lnk_ic_acept\" title=\"Aceptar\" />";
	html_edicion += "<img src=\"img/iconos/cancelar.gif\" id=\"lnk_ic_cancel\" title=\"Cancelar\" />";
	$("#lnk_nue").html(html_edicion);
	$("#lnk_ic_acept").css("cursor","pointer").bind("click",{html_inicio:html_inicio},function() {
		evento_nuevo($("#nue_url").val(),$("#nue_nom").val());
		$("#lnk_nue").html(html_inicio);
		$("#lnk_nue").css("cursor","pointer").bind("click",function() {evento_preparar_nuevo()});
	});
	$("#lnk_ic_cancel").css("cursor","pointer").bind("click",{html_inicio:html_inicio},function() {
		$("#lnk_nue").html(html_inicio);
		$("#lnk_nue").css("cursor","pointer").bind("click",function() {evento_preparar_nuevo()});
	});

}


function evento_eliminar(id_attr) {
	URL.ajax = "enlace_eliminar";
	URL.id = id_attr;
	$.getJSON("",URL,function(jRes){
		if (jRes.res=="OK") {
			$("#lnk_li_"+jRes.id_enl).hide();
 			alert(jRes.res_txt);
		} else {
 			alert(jRes.res_txt);
		}
	});
}




// function editar_() {
// 		$(this).css("cursor","pointer").bind("click",function() {
// 			URL.ajax = "enlace_editar";
// 			URL.indice = i;
// 			URL.id = $(this).attr("id");
// 			$.getJSON("",URL,function(jRes){
// 				$("#lnk_texto_"+jRes.id_enl).html("modificado");
// 			});
// 		});
// }





// alert(document.location);
// 	$(".lnk_ed").each(function(i) {
// 		$(this).css("cursor","pointer").bind("click",function() {
// // 			document.title += $(this).parent("li").children("span.lnk_texto").attr("id");
// 			var html_edicion = "<input type=\"text\" value=\"\" id=\"inp_ed\" />";
// 
// 
// 		var sID = $(this).attr("id").toString();
// 			alert(sID.replace(/[^0-9]*/,""));
// 
// 
// 
// 
// 			$(this).parent("li").children("span.lnk_texto").html(html_edicion);
// 
// 		});
// 	});

//  	if ($("#cuerpo_documentos").is("div")) {
// 		$("#secciones li a").each(function(i) {
// 			$(this).click(function() {
// 				URL1.ajax = "categoria";
// 				URL1.indice = i;
// 				$.getJSON($(this).attr("href"),URL1,function(jRes){
// 					$("#secciones li").removeClass();
// 					$("#secciones li a:eq("+jRes.indice+")").parent("li").addClass("seleccionado");
// 					$("#documentos").fadeOut("slow",function(){
// 						$(this).html(jRes.html_gordo).fadeIn("slow");
// 					});
// 					$("#imagen_desc").fadeOut("slow",function(){
// 						$(this).attr("src",jRes.img_src).fadeIn("slow");
// 					});
// 				});
// 				return false;
// 			});
// 		});
//  	}
