tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	language : "es",
	plugins : "inlinepopups"
});


			
function agregarFavoritos(){ 
	var url="http://www.solarelement.es";  
	var titulo="SolarElement";
	if (window.sidebar&&window.sidebar.addPanel){
		window.sidebar.addPanel(titulo,url,"");
	} else if (document.all) {
		window.external.AddFavorite(url,titulo);
	}
}


function cargarTexto(texto){
	var ed = tinyMCE.get('contenido');
	ed.setContent(texto);
}

function obtenerTexto(){
	var ed = tinyMCE.get('contenido');

	return ed.getContent();
}

function mensajes(capa,mensaje){
	$("#"+capa).html(mensaje).css("display","block");
	esconder(capa,5000);
}

function esconder(capa,tiempo){
	setTimeout("$('#"+capa+"').fadeOut()",tiempo);
}

function borrarNoticia(){
	$.post('ajax.php',{accion: 'borrarNoticia',id: $("#noticias option:selected").val()},function(data){
				eval(data);
			});
}

$(function(){
	$("#secciones").change(function(){
		if($("#secciones option:selected").val() > 0){
			$.post('ajax.php',{accion: 'cargarTexto',id: $("#secciones option:selected").val()},function(data){
				cargarTexto(data);
			});
			$.post('ajax.php',{accion: 'cargarSub',id: $("#secciones option:selected").val()},function(data){
				var limite = 0;
				$.each(data,function(){
					limite++;
				});
				if(limite > 1){
					$("#subSecciones").removeOption(/./);
					$("#subSecciones").addOption(data);
					$("#selectSubSecciones").css("display","block");
					$("#subSecciones").unbind("change").bind("change",function(){
						if($("#subSecciones option:selected").val() > 0){
							$.post('ajax.php',{accion: 'cargarTexto',id: $("#subSecciones option:selected").val()},function(data){
								cargarTexto(data);
							});
						}
					});
				}
				else{
					$("#subSecciones").removeOption(/./);
					$("#selectSubSecciones").css("display","none");
				}
			},'json');
		}
		else{
			$("#subSecciones").removeOption(/./);
			$("#selectSubSecciones").css("display","none");
		}
	});
	
	$("#noticias").change(function(){
		
			$.post('ajax.php',{accion: 'cargarNoticias',id: $("#noticias option:selected").val()},function(data){
				cargarTexto(data);
			});
	});
	
	$("#enviar").click(function(){
		if($("#subSecciones option:selected").val() == 0 || $("#subSecciones option:selected").val() == undefined){
			$.post('ajax.php',{accion: 'guardarTexto',id: $("#secciones option:selected").val(),texto: obtenerTexto()},function(data){
				eval(data);
			});
		}
		else{
			$.post('ajax.php',{accion: 'guardarTexto',id: $("#subSecciones option:selected").val(),texto: obtenerTexto()},function(data){
				eval(data);
			});
		}
	});
	
	$("#enviarNoticia").click(function(){
			$.post('ajax.php',{accion: 'actualizarNoticia',id: $("#noticias option:selected").val(),texto: obtenerTexto()},function(data){
				eval(data);
			});
	});
	
	$("#nuevaNoticia").click(function(){
			$.post('ajax.php',{accion: 'nuevaNoticia',titulo: $("#titulo").val(),texto: obtenerTexto()},function(data){
				eval(data);
			});
	});

	$("#idioma").change(function(){
		$.post('ajax.php',{accion: 'cambiarIdioma',idioma: $("#idioma option:selected").val()},function(data) {
			if (data.ok) {
				$("#secciones").removeOption(/./).addOption(data.secciones);
				$("#selectSubSecciones").css("display","none");
				cargarTexto("");
			} else {
				alert('Ocurrió un error al intentar cambiar el idioma');
			}
		},'json');
	});
	
	$("#idiomaNoticias").change(function(){
		$.post('ajax.php',{accion: 'cambiarIdiomaNoticias',idioma: $("#idiomaNoticias option:selected").val()},function(data) {
			if (data.ok) {
				$("#noticias").removeOption(/./).addOption(data.noticias);
				$("#selectSubSecciones").css("display","none");
				cargarTexto("");
			} else {
				alert('Ocurrió un error al intentar cambiar el idioma');
			}
		},'json');
	});

	$("#cargando").jqm().ajaxStart(function(){
		$(this).jqmShow();
	}).ajaxStop(function(){
		$(this).jqmHide();
	});	
});

