/* hy home page version */

function getRSS(feed)
{
    /*A*/
    if (window.ActiveXObject) //IE
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) { //other
    /*
        xhr = new XMLHttpRequest();
    */
   try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
    alert("Permission UniversalBrowserRead denied.");
   }

    xhr = false;
    xhr = new XMLHttpRequest();
    if (xhr.overrideMimeType) {
      xhr.overrideMimeType('text/xml');
    }
    if (!xhr) {
      alert('Cannot create XMLHTTP instance');
      return false;
    }
    }
    else
        alert("your browser does not support AJAX");

    /*B*/

    xhr.open("GET",feed,true);

    /*C*/
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("Pragma", "no-cache");

    /*D*/
    xhr.onreadystatechange = function() {
	//		alert("readyState is " + xhr.readyState);
        if (xhr.readyState == 4)
        {
	    //	    alert("status is " + xhr.status);	    
            if (xhr.status == 0 || xhr.status == 200) // 0 locally
            {
                /*F*/
		//		alert("responseText is " + xhr.responseText);	    
                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);
        }
    }

    /*E*/
    xhr.send(null);
}

function processRSS(rssxml)
{
    //    alert("INTO ProcesRSS");
    RSS = new RSS2Channel(rssxml);
    //        alert("INTO showRSS");
    showRSS(RSS);
    //        alert("OUT OF processRSS");
}

function RSS2Channel(rssxml)
{
    /*A*/
    //        alert("INTO RSS2Channel A");
    /*required string properties*/
    this.title;
    this.link;
    this.description;

    /*optional string properties*/
    this.language;
    this.copyright;
    this.managingEditor;
    this.webMaster;
    this.pubDate;
    this.lastBuildDate;
    this.generator;
    this.docs;
    this.ttl;
    this.rating;

    /*optional object properties*/
    this.category;
    this.image;

    /*array of RSS2Item objects*/
    this.items = new Array();

    /*B*/
    //    alert("INTO RSS2Channel B");
    var chanElement = rssxml.getElementsByTagName("channel")[0];
    var itemElements = rssxml.getElementsByTagName("item");
    /*C*/
    //    alert("INTO RSS2Channel C items " + itemElements.length);
    for (var i=0; i<itemElements.length; i++)
    {
        Item = new RSS2Item(itemElements[i]);
        this.items.push(Item);
	//	alert("push item " + i);
	if (i >= 5) break; // max 5 items 
    }

    /*D*/
    //    alert("INTO RSS2Channel D");
    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++)
    {
	//	alert("INTO RSS2Channel D loop var " + i);
        tmpElement = chanElement.getElementsByTagName(properties[i])[0];
        if (tmpElement!= null)
            eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
    }

    /*E*/
    //    alert("INTO RSS2Channel E");
    this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
    this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
    //    alert("OUT OF RSS2Channel");    
}

function RSS2Category(catElement)
{
    if (catElement == null) {
        this.domain = null;
        this.value = null;
    } else {
        this.domain = catElement.getAttribute("domain");
        this.value = catElement.childNodes[0].nodeValue;
    }
}

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]+")");
    }
}

function RSS2Item(itemxml)
{
    /*A*/
    //    alert("INTO RSS2Item A");
    /*required properties (strings)*/
    this.title;
    this.link;
    this.description;

    /*optional properties (strings)*/
    this.author;
    this.comments;
    this.pubDate;

    /*optional properties (objects)*/
    this.category;
    this.enclosure;
    this.guid;
    this.source;

    /*B*/
    //    alert("INTO RSS2Item B");
    var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
    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");
    }

    /*C*/
    //    alert("INTO RSS2Item C category");
    this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
    //    alert("INTO RSS2Item C enclosure");
    this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
    //    alert("INTO RSS2Item C guid");
    this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
    //    alert("INTO RSS2Item C source");
    this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
    //    alert("OUT OF RSS2Item");
}

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)
{
    this.isPermaLink = null;
    this.value = null;
    if (guidElement != null) {
	//	alert("guid?" + guidElement);
        this.isPermaLink = guidElement.getAttribute("isPermaLink");
	if (guidElement.childNodes[0])
	    this.value = guidElement.childNodes[0].nodeValue;
    }
}

function RSS2Source(souElement)
{
    if (souElement == null) {
        this.url = null;
        this.value = null;
    } else {
	//	alert("sou?" + souElement);
        this.url = souElement.getAttribute("url");
	//	alert("OR sou children?" + souElement.childNodes[0]);
        this.value = souElement.childNodes[0].nodeValue;
    }
}

function showRSS(RSS)
{
    /*A*/
    var innerHTML = "";
    var startChanTag = "<li class=\"channel\">";
    var startChanTitle = "<span class=\"chan_title\" id='chan_title'>";
    var startChanPubDate = "<div id='pub_date'>";
    var startItemsTag = "<ul>";
    var startItemTag = "<li id='item'>";
    //    var startItemPubDate = "<div id='pub_date'>";
    var startItemPubDate = "<span>";
    var endChanTag = "</li>";
    var endChanTitle = "</span>";
    var endChanPubDate = "</div>";
    var endItemTag = "</li>";
    var endItemsTag = "</ul>";
    var endItemPubDate = "</span>";
    /*B*/
    //    alert("RSS title is " + RSS.title);
    if (RSS.title != null) {
	var titleid =  RSS.title.replace(/ /g,"_");
	//	alert("titleid is " + titleid);
	if (document.getElementById(titleid) != null) {
	    innerHTML = startChanTag;
	    if (RSS.link != null) innerHTML += "<a class=\"hidden\" href=\"" + RSS.link + "\">";
	    innerHTML += startChanTitle + RSS.title + endChanTitle;
	    if (RSS.link != null) innerHTML += "</a>";
	    if (RSS.pubDate == null) innerHTML += startChanPubDate + RSS.pubDate + endChanPubDate;
	    
	    innerHTML += "<ul>";
	    for (var i=0; i<RSS.items.length; i++) {
		// alert("chan_items at item " + i + " title " + RSS.items[i].title);
		if (RSS.items[i].title != null) {
		    item_html = startItemTag;
		    if (RSS.items[i].link != null) item_html += "<a ";
		    if (RSS.items[i].pubDate != null) item_html += "class=\"tooltip\""; 
		    if (RSS.items[i].link != null) item_html += "href=\"" + RSS.items[i].link + "\">";
		    if (RSS.items[i].pubDate != null) item_html += "<span>" + RSS.items[i].pubDate + "</span>";
		    item_html += RSS.items[i].title;
		    if (RSS.items[i].link != null) item_html += "</a>";
		    item_html += endItemTag;
		    //		    alert("item html is " + item_html);
		    innerHTML += item_html; /*D1*/
		}
	    }
	    innerHTML += "</ul>";
	    innerHTML += endChanTag;
	    document.getElementById(titleid).innerHTML = innerHTML;
	}
    }

    return true;
}


