// JavaScript Document
<!--
// ************************
// class portadaDinamica
//************************

var array_portadas = new Array();

//***************
// Constructor
//***************
function portadaDinamica (id_espacio, min_portadas, max_portadas, ultimo_id, color_i, color_f) {
	this.id_espacio = id_espacio;
	this.min_portadas_global = min_portadas;
	this.max_portadas_global = max_portadas;
	this.ultimoOrdenGlobal = ultimo_id;
	this.timerTopic = 20; // segundos
	this.div_fundido_en_curso = '';
	this.div_aparicion_en_curso = '';
	this.opacidad_en_curso = 100;
	this.html_div_nuevo_en_curso = '';
	this.color_aparicion_en_curso = '';
	this.http_request = false;
	this.posicion_array = 0;
	this.color_inicio = color_i;
	this.color_fin = color_f;

	//*************************
	// METODO iniciarTimer
	//*************************
	this.iniciarTimer = function() {
//		alert('ini');
//			setTimeout(this.httpRequestPortadaDinamica(this), this.timerTopic*1000); 
		this.posicion_array = array_portadas.length;
		array_portadas[this.posicion_array] = this;
		continuarTimer(this.posicion_array, 20000); 
	};

	//*************************
	// METODO hacer_borrado
	//*************************
	this.hacer_borrado = function(div_borrado) {
		if (document.getElementById(div_borrado)) {
			objeto_destino = document.getElementById(div_borrado);
			objeto_destino.parentNode.removeChild(objeto_destino);
//			document.getElementById(div_borrado).style.display='none';
		} else {
			if (document.getElementsByClassName('portada_dinamica_' + this.id_espacio).length > 0) {
				this.ultimo_div = document.getElementsByClassName('portada_dinamica_' + this.id_espacio).reverse()[0];
				this.ultimo_div.parentNode.removeChild(this.ultimo_div);
			}
		}
	};

	//*************************
	// METODO borrarTopic
	//*************************
	this.borrarTopic = function() {
		if (document.getElementById(this.div_fundido_en_curso)) {
			hacer_fundido(this.div_fundido_en_curso);
			hacer_aparicion();
		} else {
			// si el topic a borrar no esta en portada, borramos el ultimo
			this.div_fundido_en_curso = document.getElementsByClassName('portada_dinamica_' + this.id_espacio).reverse()[0].getAttribute('id');
			hacer_aparicion();
			hacer_fundido(this.div_fundido_en_curso);
		}
	};

	//*************************
	// METODO borrarTopic
	//*************************
	this.hacer_fundido = function(div_fundido) {
		if (document.getElementById(div_fundido).style.opacity) {
	//		opacidad_origen = div_fundido.style.opacity;
		} else {
			document.getElementById(div_fundido).style.opacity = 100;
		}
		this.div_fundido_en_curso = div_fundido;
		this.opacidad_en_curso = 100;
		fundido_transicion();
	};

	//*************************
	// METODO changeOpacity
	//*************************
	this.changeOpacity = function(id, opacity) {
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	};

	//*************************
	// METODO changeOpacity
	//*************************
	this.fundido_transicion = function (){
		if (this.opacidad_en_curso>0) {
			this.opacidad_en_curso = this.opacidad_en_curso - 5;
			changeOpacity (this.div_fundido_en_curso, this.opacidad_en_curso);
			setTimeout(fundido_transicion, 5); 
		}
		if (this.opacidad_en_curso<=0) {
			document.getElementById(this.div_fundido_en_curso).style.display='none';
		}
	};

	//*************************
	// METODO hacer_aparicion
	//*************************
	this.hacer_aparicion = function() {
//		var color = new RGBColor('DCC0C0');
		var color = new RGBColor(this.color_inicio);
		document.getElementById('cuerpo_portada_dinamica_' + this.id_espacio).innerHTML = this.html_div_nuevo_en_curso + document.getElementById('cuerpo_portada_dinamica_' + this.id_espacio).innerHTML;
		document.getElementById(this.div_aparicion_en_curso).style.backgroundColor = color.toHex();
//		var div_aparecer = this.div_aparicion_en_curso;
		aparicion_transicion(this.posicion_array); 
	};


} // fin constructor
// ******************************************************************************************************


//*************************
// FUNCION aparicion_transicion
//*************************
function aparicion_transicion (posicion_objeto) {
	var color_destino = new RGBColor(array_portadas[posicion_objeto].color_fin);
	var div_aparecer = array_portadas[posicion_objeto].div_aparicion_en_curso;
	var color = new RGBColor(document.getElementById(div_aparecer).style.backgroundColor);
	if (color.r != color_destino.r || color.g != color_destino.g || color.b != color_destino.b) {
		if (color.r<color_destino.r) color.r=color.r+1;
		if (color.r>color_destino.r) color.r=color.r-1;	
		if (color.g<color_destino.g) color.g=color.g+1;
		if (color.g>color_destino.g) color.g=color.g-1;	
		if (color.b<color_destino.b) color.b=color.b+1;
		if (color.b>color_destino.b) color.b=color.b-1;
		document.getElementById(div_aparecer).style.backgroundColor = color.toHex();
		setTimeout('aparicion_transicion('+posicion_objeto+');', 7); 
	} else {
		array_portadas[posicion_objeto].div_aparicion_en_curso = '';
	}
}
  
//*************************
// FUNCION continuarTimer
//*************************
function continuarTimer (posicion_objeto){
	httpRequestPortadaDinamica (posicion_objeto);
//	try {
		setTimeout('continuarTimer('+posicion_objeto+');', 20000); 
//	} catch (e) {
//	}
} 
  
//*************************
// FUNCION httpRequestPortadaDinamica
//*************************
function httpRequestPortadaDinamica (posicion_objeto) {
	array_portadas[posicion_objeto].http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		array_portadas[posicion_objeto].http_request = new XMLHttpRequest();
		if (array_portadas[posicion_objeto].http_request.overrideMimeType) {
			array_portadas[posicion_objeto].http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			array_portadas[posicion_objeto].http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				array_portadas[posicion_objeto].http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!array_portadas[posicion_objeto].http_request) {
	//	alert('Falla :( No es posible crear una instancia XMLHTTP');
		return false;
	}

	array_portadas[posicion_objeto].http_request.onreadystatechange = function insertarPortadaDinamica (){
	// METODO insertarPortadaDinamica
	if (array_portadas[posicion_objeto].http_request.readyState == 4) 
	{
		if (array_portadas[posicion_objeto].http_request.status == 200) 
		{
			respuesta_xml = array_portadas[posicion_objeto].http_request.responseText;
			//if (respuesta_xml.length>10) {
			//	alert(respuesta_xml.length + '*' + respuesta_xml);			
			//}
			if(window.ActiveXObject){
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.loadXML(respuesta_xml);
			} else if (document.implementation && document.implementation.createDocument) {
				xmlDoc = document.implementation.createDocument("","",null);
				var parser=new DOMParser();
				xmlDoc=parser.parseFromString(respuesta_xml,"text/xml");
			} else {
				alert ('Su navegador no puede soportar este script');
			}

			if (xmlDoc.firstChild != null) {
				var longitudXML = xmlDoc.getElementsByTagName('topics')[0].childNodes.length;
				for(var i=0;i<=longitudXML-1;i++) {
					id_topic = xmlDoc.getElementsByTagName('topic')[i].getAttribute('topic_id');
					mensaje_id = xmlDoc.getElementsByTagName('topic')[i].getAttribute('message_id');
					url = xmlDoc.getElementsByTagName('topic')[i].getAttribute('url');
					titulo = xmlDoc.getElementsByTagName('topic')[i].getAttribute('title');
					texto = xmlDoc.getElementsByTagName('topic')[i].getAttribute('text');
					imagen = xmlDoc.getElementsByTagName('topic')[i].getAttribute('image');
					respuestas = xmlDoc.getElementsByTagName('topic')[i].getAttribute('responses');
					lecturas = xmlDoc.getElementsByTagName('topic')[i].getAttribute('readers');
					fecha = xmlDoc.getElementsByTagName('topic')[i].getAttribute('date');
					respuesta_id = xmlDoc.getElementsByTagName('topic')[i].getAttribute('response_id');
					respuesta_imagen = xmlDoc.getElementsByTagName('topic')[i].getAttribute('response_image');
					ultima_respuesta = xmlDoc.getElementsByTagName('topic')[i].getAttribute('last_response');
					if (respuesta_id)
						array_portadas[posicion_objeto].ultimoOrdenGlobal = respuesta_id;
					else
						array_portadas[posicion_objeto].ultimoOrdenGlobal = mensaje_id;
				}
				titulo = unescape(titulo.replace(/\[BLOBIC:AMP\]/g, "&"));
				titulo = titulo.replace(/\[BR\]/g,'\r\n'); 
				texto = unescape(texto.replace(/\[BLOBIC:AMP\]/g, "&"));
				texto = texto.replace(/\[BR\]/g,'\r\n'); 
				ultima_respuesta = unescape(ultima_respuesta.replace(/\[BLOBIC:AMP\]/g, "&"));
				ultima_respuesta = ultima_respuesta.replace(/\[BR\]/g, '\r\n');

				html = '';
				html = html + '<div class="portada_dinamica" id="topic_/' + array_portadas[posicion_objeto].id_espacio +'/_/' + id_topic +'/" name="topic" style="display:table; width:100%">';
				html = html + '<span class="encuadreNoticia"><span class="tituloNoticia">';
				html = html + '<a href="'+ url +'" class="tituloNoticiaEnlace"><font size="5">' + titulo + '</font></a>';
				html = html + '</span>';
				if (imagen.length>0) html = html + '<img src="http://images.blobic.com' + imagen + '" alt="'+ titulo +'" border="0">';
				html = html + '<span class="textoNoticia">'+ texto +'</span>';
				html = html + '<a href="' + url + '" class="links"><b>' + respuestas + ' respuestas</b></a>';
				html = html + '<span class="clear"></span>';
				html = html + '</span>';
				html = html + '<span class="textoNoticia">ULTIMA RESPUESTA<br>';
				if (respuesta_imagen.length>0) html = html + '<img src="http://images.blobic.com' + respuesta_imagen + '" border="0">';
				html = html + ultima_respuesta +'</span>';	
				html = html + '</div>';
				array_portadas[posicion_objeto].html_div_nuevo_en_curso = html;

				id_espacio = array_portadas[posicion_objeto].id_espacio;
				array_portadas[posicion_objeto].hacer_borrado('topic_/' +id_espacio + '/_/' + id_topic + '/');
				array_portadas[posicion_objeto].div_aparicion_en_curso = 'topic_/' +id_espacio + '/_/' + id_topic + '/';
				array_portadas[posicion_objeto].hacer_aparicion();
			}
		} else {
 //		alert(http_request.status + ' ' + 'Hubo problemas con la petición.');
		}
	}
};


	array_portadas[posicion_objeto].http_request.open('GET', '/foro_ajax/?t=1&i='+ array_portadas[posicion_objeto].min_portadas_global +'&a=' + array_portadas[posicion_objeto].max_portadas_global + '&o=' + array_portadas[posicion_objeto].ultimoOrdenGlobal, true);
	array_portadas[posicion_objeto].http_request.send(null); 
//	setTimeout(this, this.timerTopic*1000); // reactiva el timer 
//	this.iniciarTimer(); // reactiva el t imer
//	alert('fin');
//	setTimeout(this.httpRequestPortadaDinamica(this), this.timerTopic*1000); 


}
  
  
  
//-->

