
//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;
	}
}
function RSS2Banner(bannElement)
{
	if (bannElement == null)
	{
		//this.url = null;
		this.value = null;
	}
	else
	{
		//this.url = bannElement.getAttribute("url");
		this.value = bannElement.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;
	
	this.banner;
	this.derechos;
	this.authorInfo;
	this.subTitle;
	

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate", "subTitle","derechos","banner");
	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", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	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(xmlFileName,whaT,arti)
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhrrr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhrrr = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	
	xhrrr.open("GET",xmlFileName,true);
	xhrrr.setRequestHeader("Cache-Control", "no-cache");
	xhrrr.setRequestHeader("Pragma", "no-cache");
	xhrrr.onreadystatechange = function() {
		if (xhrrr.readyState == 4)
		{
			if (xhrrr.status == 200)
			{
				if (xhrrr.responseText != null)
					
					if(whaT==1)
						menu(xhrrr.responseXML);
					if(whaT==2)
						noticia(xhrrr.responseXML,arti);
				else
				{
					//alert("Failed to receive RSS file from the server - file not found.");
					//return false;
				}
			}
			else
				alert("Error code " + xhrrr.status + " received: " + xhrrr.statusText);
		}
	}

	//send the request
	xhrrr.send(null);
}
function menu(rssxml){
	RSS = new RSS2Channel(rssxml);
	//showRSS(RSS);

	subMn =	"  <table width='170' border='0' cellspacing='0' cellpadding='4'>"
	for (var i=0; i<RSS.items.length; i++) {
		if(RSS.items[i].description=="primera_seccion"){
			subMn +=	"   <tr>"
			subMn +=	"     <td align='left' background='images/imagensubmenu.jpg'><a href=javascript:ver('"+RSS.items[i].link+"') style='text-decoration:none'>"+RSS.items[i].title+"</a></td>";
			subMn +=	"   </tr>"
		}
	}
	subMn +=	"  </table>"
	$("subMenu1").innerHTML = subMn;
	
	subMn =	"  <table width='170' border='0' cellspacing='0' cellpadding='4'>"
	for (var i=0; i<RSS.items.length; i++) {
		if(RSS.items[i].description=="mundo"){
			subMn +=	"   <tr>"
			subMn +=	"     <td align='left' background='images/imagensubmenu.jpg'><a href=javascript:ver('"+RSS.items[i].link+"') style='text-decoration:none'>"+RSS.items[i].title+"</a></td>";
			subMn +=	"   </tr>"
		}
	}
	subMn +=	"  </table>"
	$("subMenu2").innerHTML = subMn;
	
	subMn =	"  <table width='170' border='0' cellspacing='0' cellpadding='4'>"
	for (var i=0; i<RSS.items.length; i++) {
		if(RSS.items[i].description=="vida"){
			subMn +=	"   <tr>"
			subMn +=	"     <td align='left' background='images/imagensubmenu.jpg'><a href=javascript:ver('"+RSS.items[i].link+"') style='text-decoration:none'>"+RSS.items[i].title+"</a></td>";
			subMn +=	"   </tr>"
		}
	}
	subMn +=	"  </table>"
	$("subMenu3").innerHTML = subMn;
	
	
	
	subMn =	" <table width='169' border='0' cellspacing='0' cellpadding='4'>"
	subMn +=	"	<tr><td align='left' background='images/imagensubmenu.jpg'><a href=javascript:verAnt('4Co') style='text-decoration:none'>Contacto</a>&nbsp;</td></tr>"
	subMn +=	"	<tr><td align='left' background='images/imagensubmenu.jpg'><a href='mailto:comentarios@americanueva.com' style='text-decoration:none'>Comentarios</a>&nbsp;</td></tr>"
	subMn +=	" </table>"
	$("subMenu4").innerHTML = subMn;	
	 
	subMn =	" <table width='170' border='0' cellspacing='0' cellpadding='4'>"
	subMn +=	"	<tr><td align='left' background='images/imagensubmenu.jpg'><a href=javascript:verAnt('5Pu') style='text-decoration:none'>Publicidad</a>&nbsp;</td></tr>"
	subMn +=	"<tr><td align='left' background='images/imagensubmenu.jpg'><a href=javascript:verAnt('5Su') style='text-decoration:none'>Suscripci&oacute;n</a>&nbsp;</td></tr>"
	subMn +=	"</table>"
	$("subMenu5").innerHTML = subMn;	
}

function noticia(rssxml,arti){
	RSS = new RSS2Channel(rssxml);
	
	$("divTitulo").innerHTML = RSS.items[arti].title;
	$("divNota").innerHTML = RSS.items[arti].description;
	
	
	if(RSS.items[arti].banner!=" ")
		$("divBan").innerHTML = '<img src="'+RSS.items[arti].banner+'" border="0"/>';
	
	$("idAutor").innerHTML = RSS.items[arti].author;
	$("idDerechos").innerHTML = RSS.items[arti].derechos;
	//$("authorInfo").innerHTML = RSS.items[arti].authorInfo;
	$("idSubtitulo").innerHTML = RSS.items[arti].subTitle;
	
}
var xhrrr;


