/**
* Actualizaci?n de Contenido
* Autor: Thomas Woodard
* Copyright: 2007 Ministerio del Poder Popular para la Comunicaci?n y la Informaci?n
* Licencia GPL
*/
function substituirContenido(obj,accion){
	// Si hay timer, detenerlo ya que no se sabe cuanto tiempo puede tardar para recibir la respuesta
	if(obj.timer!=undefined&&obj.timer.interval!=null){
		obj.timer.cargandoImagen = true;
		obj.timer.stop();
	}
	// Mostrar ícono de "..cargando"
	if(obj.mostrarCargando==true){
		//alert(obj.style.left);
		obj.imgCargando = new Image();
		obj.imgCargando.src = 'clase_modulos/mod_rotacion_ajax/js/nulo.gif';
		obj.imgCargando.style.position = "absolute";
		posImgCargado = getPos(obj);
		obj.imgCargando.style.top = (posImgCargado[0]+3)+"px";
		obj.imgCargando.style.left = (posImgCargado[1]+3)+"px";
		obj.appendChild(obj.imgCargando);
	}
	url = obj.reldir+'clase_modulos/mod_rotacion_ajax/nvo_contenido.php?do='+accion+'&mod='+obj.mod+'&cont='+obj.cont
	// Procesar respuesta
	obj.mostrarContenidoNuevo= function(requestJson){
		if(this.mostrarCargando==true){
			this.removeChild(obj.imgCargando);
		}
		eval(requestJson.responseText);
		if(!data) {return false;}
		this.RequestData = data;
		if(this.id == data.divId) {
			obj.timer.start(false);
			obj.timer.cargandoImagen = false;
			return false;
		}
		this.funcMostrar = function(obj, data){
			$(obj.id+"_img").id = data.divId+"_img";
			if(data.anteTitulo!=undefined)$(obj.id+"_antetitulo").innerHTML = data.anteTitulo;
			if(data.anteTitulo!=undefined)$(obj.id+"_antetitulo").id = data.divId+"_antetitulo";
			if(data.titulo!=undefined)$(obj.id+"_titulo").innerHTML = data.titulo;
			if(data.url!=undefined)$(obj.id+"_titulo").href = data.url;
			$(obj.id+"_titulo").id = data.divId+"_titulo";
			if(data.Resumen!=undefined)$(obj.id+"_resumen").innerHTML = data.Resumen;
			if(data.Resumen!=undefined)$(obj.id+"_resumen").id = data.divId+"_resumen";
			if(data.url!=undefined)$(obj.id+"_url").href = data.url;
			$(obj.id+"_url").id = data.divId+"_url";
			$(obj.id+"_mas").href = data.url;
			$(obj.id+"_mas").id = data.divId+"_mas";
			obj.cont = data.cont;
			obj.id = data.divId;
			// Reanudar timer
			if(obj.timer!=undefined && ((obj.mostrarCargando==true && obj.timer.cargandoImagen == true)||obj.mostrarCargando==false)){
				obj.timer.start(false);
				obj.timer.cargandoImagen = false;
			}
			if(obj.mostrarActualizado==true){
				new Effect.Highlight(obj.id, {startcolor:'#fffbc3', duration:3})
			}
			//Effect.Appear(obj)
		}
		if($(this.id+"_img")==undefined){
			obj.funcMostrar(obj, data);
		}else if(data.imgSrc===null){
			obj.funcMostrar(obj, data);
			$(this.id+"_img").src = "";
			$(this.id+"_img").parentDivId = this.id;
		} else {
			$(this.id+"_img").src = data.imgSrc;
			$(this.id+"_img").parentDivId = this.id;
			$(this.id+"_img").onload = function (){
				obj = $(this.parentDivId);
				data = obj.RequestData;
				obj.funcMostrar(obj, data);
			}
		}
	}
	// Ajax request
	obj.myAjax = new Ajax.Request(
		url, {
			method: 'get', onComplete: obj.mostrarContenidoNuevo.bind(obj)
		}
	);
}
/**
* Actualización Periódica
*/
function inicio_act(obj,accion,segs){
	if(obj.timer==undefined){
		obj.timer = new Object();
		obj.timer.args = new Object();
		obj.timer.segs = segs;
		obj.timer.args.objeto = obj;
		obj.timer.args.accion = accion;
		obj.timer.evento = function(){
			try {
				substituirContenido(this.args.objeto,this.args.accion);
			}catch(er){}
		}
		obj.timer.interval = window.setInterval(obj.timer.evento.bind(obj.timer), obj.timer.segs * 1000);
		obj.timer.frecuencia = function(segs){
			this.segs = segs;
			this.stop();
			this.interval = window.setInterval(this.evento.bind(this), this.segs * 1000);
		}
		obj.timer.stop = function(){
			clearInterval(this.interval);
			this.interval = null;
		}
		obj.timer.start = function(ejecutarEvento){
			if(ejecutarEvento==undefined) ejecutarEvento=true;
			this.stop();
			if(ejecutarEvento)this.evento();
			this.interval = window.setInterval(this.evento.bind(this), this.segs * 1000);
		}
	}
}
/**
* Cambio de estado de la actualización
*/
function act_swiche(boton,forzarStop){
	if(boton==undefined) boton=window.event.srcElement;
	obj = boton.parentNode.parentNode;
	if(obj.timer!=undefined){
		if(obj.timer.interval!=null||forzarStop!=undefined){
			boton.parentNode.getElementsByTagName("IMG")[1].style.display="none";
			boton.parentNode.getElementsByTagName("IMG")[2].style.display="";
			obj.timer.stop();
		} else {
			boton.parentNode.getElementsByTagName("IMG")[2].style.display="none";
			boton.parentNode.getElementsByTagName("IMG")[1].style.display="";
			boton.style.display="none";
			obj.timer.start();
		}
	} else return false;
}
