// JavaScript Document
var xhra;
function clear()
{
    xhra=new Array();
}
function getRSS(filename, ChannelDiv)
{
    var xhr;

    xhr = $.get(filename,  function() {
        
        var clearit=true;
        
        for(index=0;index<xhra.length;index++) 
        {  
            if (xhra[index][0].readyState == 4)
            {
                if (xhra[index][0].status == 200)
                {
                    if (xhra[index][0].responseText != null)
                    {
                        processRSS(xhra[index][0].responseXML,xhra[index][1]);
                        xhra[index][2]=1;
                    }
                    else
                    {
                        xhra[index][2]=1;
                        return false;
                    }
                }
            }
            
            if (xhra[index][2]==0) clearit=false;
        }
        if (clearit) clear();  
    });

    var x=new Array;
    x.push(xhr);
    x.push(ChannelDiv);
    x.push(0);
    xhra.push(x);
}

function processRSS(rssxml, ChannelDiv)
{
    var RSS = new RSS2Channel(rssxml);
    showRSS(RSS, ChannelDiv);
}
function RSS2Channel(rssxml)
{
    /*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();

    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);
    }

    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)
        {
            if (tmpElement.childNodes[0]!=null)
            {
                eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
            }
            else
            { 
                eval("this."+properties[i]+"=''");
            }
         }
    }

    this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
    this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}
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)
{
    /*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;

    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)
        {
            if (tmpElement.childNodes[0]!=null)
            {
                eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
            }
            else
            { 
                eval("this."+properties[i]+"=''");
            }
         }
    }

    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]);
}
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 showRSS(RSS, ChannelDiv)
{
    var startItemTag = "<div id='item'>";
    var startTitle = "<div id='item_title'>";
    var startDescription = "<div id='item_description'>";
    var endTag = "</div>";
    
    var html=""; //"<table cellspacing='0' border='0'>";
    var begindetail="<tr><tr>";
    var enddetail="</td></tr>";
    var endhtml="</table>";

    document.getElementById(ChannelDiv).innerHTML = "";
    var items=shuffle(RSS.items);
    for (var i=0; i<items.length; i++)
    {
        html += startItemTag
        html += (items[i].title == null) ? "" : startTitle + items[i].title + endTag;
        html += (items[i].description == null) ? "" : startDescription + items[i].description + endTag;
        html += endTag;
    }
    document.getElementById(ChannelDiv).innerHTML += html; /*D1*/

    return true;
}
function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

