
//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	//this.author;
	//this.comments;
	//this.pubDate;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "LINK", "description","NOMBRE","FECHA","TRABAJO","TEXTO","IMG1","IMG2","IMG3");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement!= null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS(url)
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	xhr.open('GET', url, true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				if (xhr.responseText != null)
					processRSS(xhr.responseXML);
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr.status + " received: " + xhr.statusText);
		}
	}

	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{
	//default values for html tags used
	var imageTag = "<img id='chan_image'";
	var startItemTag = "<div id='item'>";
	var startTitle = "<div id='item_title'>";
	var startLink = "<div id='item_link'>";
	var startDescription = "<div id='item_description'>";
	var endTag = "</div>";

		
	item_html = '<table width="164"  border="0" align="center" cellpadding="2" cellspacing="2">'

	//populate the items
	document.getElementById("chan_items").innerHTML = "";
	for (var i=0; i<RSS.items.length; i++)
	{
		item_html += "<tr><td><img src="+RSS.items[i].title +" onclick='muestra("+i+")'"+" style='cursor: pointer; cursor: hand;'></tr></td>"		
	}
	item_html += "</table>"
	 document.getElementById("chan_items").innerHTML += item_html;
	//we're done
	//document.getElementById("chan").style.visibility = "visible";
	return true;
}


function muestra( tnID )
{
		var html
		
		document.getElementById("resultado").innerHTML = "";
		document.getElementById("mensaje").innerHTML = "";
		
		html = '<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td><table width=100% border=0 cellspacing=0 cellpadding=0><tr><td bgcolor=#F8F8F8><table width=100% border=0 cellspacing=2 cellpadding=0><tr><td height=4></td><td></td></tr><tr>'
		html+=  "<td width=3%>&nbsp;</td><td width=97%><font color=#000000><b>"+RSS.items[tnID].NOMBRE+"</B></font></td></tr>"	
		html+=  "<td width=3%>&nbsp;</td><td width=97%><font color=#ff9900>"+RSS.items[tnID].LINK+"</font></td></tr>"	
		html+=  "<td width=3%>&nbsp;</td><td width=97%>"+RSS.items[tnID].TRABAJO+"</td></tr>"	
		html+=  "<td width=3%>&nbsp;</td><td width=97%>"+RSS.items[tnID].FECHA+"</td></tr><tr><td height=4></td><td></td></tr>"
		html+=  "</table></td><td width=7 bgcolor=#DCDCDC>&nbsp;</td></tr></table></td>"
		html+=  "</tr><tr><td><table width=100% border=0 cellspacing=10 cellpadding=2><tr><td width=20>&nbsp;</td>"
		html+=  "<td>"+RSS.items[tnID].TEXTO+"</td>"
		html+=  " <td width=20>&nbsp;</td></tr></table></td></tr><tr><td>"
		html+=  " <table width=100% border=0 cellspacing=2 cellpadding=2><tr>"
		html+=  "  <td><img src="+RSS.items[tnID].IMG1+"></td>"
		html+=  "  <td><img src="+RSS.items[tnID].IMG2+"></td>"
		html+=  "  <td><img src="+RSS.items[tnID].IMG3+"></td>"
		html+= "  </tr></table></td></tr></table></td></tr></table>"
		
		 document.getElementById("resultado").innerHTML = html;
		 
//		document.write(resultado)
}


var xhr;





