﻿/*KCCHurTrackRSS2Reader.js*/
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", "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");
    }

    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");
    var i;
    for (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 (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 RSS2GetFeed()
{
    var urlFeed = urlRSS2Feed[ndxRSS2Feed];
    //call the right constructor for the browser being used
    if (window.ActiveXObject)
        xhrRSS2 = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        xhrRSS2 = new XMLHttpRequest();
    else
    {
        if(xhrRSS2doErrorAlert)
            alert("not supported");
    }

    //prepare the XMLHttpRequest object
    xhrRSS2.open("GET",urlFeed,true);
    xhrRSS2.setRequestHeader("Cache-Control", "no-cache");
    xhrRSS2.setRequestHeader("Pragma", "no-cache");
    xhrRSS2.onreadystatechange = function() {
        if (xhrRSS2.readyState == 4)
        {
	        if (xhrRSS2.status == 200)
	        {
		        if (xhrRSS2.responseText != null)
		        {
			        processRSS(xhrRSS2.responseXML);
		            RSS2DisplayDIV(true);
			    }
		        else
		        {
	                if(xhrRSS2doErrorAlert)
			            alert("Failed to receive RSS file from the server - file not found.");
			        return false;
		        }
	        }
	        else
	        {
	            if(xhrRSS2doErrorAlert)
	            {
		            alert("Error code " + xhrRSS2.status + " received: " + xhrRSS2.statusText);
		        }
		        RSS2DisplayDIV(false);
		    }
        }
    }

    //send the request
    xhrRSS2.send(null);
}

function RSS2DisplayDIV(show)
{
    document.getElementById("idRssChannelDIV").style.display = show ? "block" : "none";
    var oLBL = document.getElementById("idCurrentEventLBL");
    if(oLBL)
        oLBL.style.display = show ? "none" : "block";
}

//processes the received rss xml
function processRSS(rssxml)
{
    RSS = new RSS2Channel(rssxml);
    RSS2ShowFeed(RSS);
}

//shows the RSS content in the browser
function RSS2ShowFeed(RSS)
{
    gotRSS2Feed = false;
    //default values for html tags used
    var imageTag = "<img id='idRssChannel_image'";
    var startItemTag = "<div id='item'>";
    var startTitle = "<div id='idRssItem_title'>";
    var startLink = "<div id='idRssItem_link'>";
    var startDescription = "<div id='idRssItem_description'>";
    var endTag = "</div>";

    //populate channel data
    var properties = new Array("title","link","description","pubDate","copyright");
    var i;
    for (i=0; i<properties.length; i++)
    {
        eval("document.getElementById('idRssChannel_"+properties[i]+"').innerHTML = ''");
        curProp = eval("RSS."+properties[i]);
        if (curProp != null)
	        eval("document.getElementById('idRssChannel_"+properties[i]+"').innerHTML = curProp");
    }

    //show the image
    document.getElementById("idRssChannel_image_link").innerHTML = "";
    if (RSS.image.src != null)
    {
        document.getElementById("idRssChannel_image_link").href = RSS.image.link;
        document.getElementById("idRssChannel_image_link").innerHTML = imageTag
	        +" alt='"+RSS.image.description
	        +"' width='"+RSS.image.width
	        +"' height='"+RSS.image.height
	        +"' src='"+RSS.image.url
	        +"' "+"/>";
    }

    //populate the items
    document.getElementById("idRssChannel_items").innerHTML = "";
    //var sMap = "	<a HREF='http://www.karenclarkandco.com/hurricanes/'>map...</a>";
    for (i=0; i<RSS.items.length; i++)
    {
        var sItemHtml;
        var sLinkURL = RSS.items[i].link;
        var sMap = "<br/><br/><a href='"+ sLinkURL +"'>Click&gt; Interactive map ...</a>";
        sLinkURL = "";
        sItemHtml = startItemTag;
        sItemHtml += (RSS.items[i].title == null) ? "" : startTitle + RSS.items[i].title + endTag;
        sItemHtml += "<br/>";
        sItemHtml += (RSS.items[i].link == null)  ? "" : startLink  + sLinkURL + endTag;
        sItemHtml += (RSS.items[i].description == null) ? "" : startDescription + RSS.items[i].description + sMap + endTag;
        sItemHtml += endTag;
        document.getElementById("idRssChannel_items").innerHTML += sItemHtml;
        gotRSS2Feed = true;
    }

    //we're done
    //document.getElementById("idRssChannelDIV").style.display = gotRSS2Feed ? "block" : "none";
    //document.getElementById("idCurrentEventLBL").style.display = gotRSS2Feed ? "none" : "block";
    RSS2DisplayDIV(gotRSS2Feed);
    return true;
}

var xhrRSS2;
var gotRSS2Feed = false;
var xhrRSS2doErrorAlert = false;
var urlRSS2Feed =  new Array();
urlRSS2Feed[0] = "../../hurricanes/KCCHurricaneTrackerRSS2.xml";
//urlRSS2Feed[1] = "../rss/KCCHurricaneTrackerRSS2_v2.xml";
var ndxRSS2Feed = 0;
var ndxRSS2FeedReloadMinutes = 0; //was 60
var retryRSS2 = 1;
function doRetryRSS2()
{
    if(gotRSS2Feed==false && retryRSS2>0)
    {
        urlRSS2Feed[0] = "../../HurricaneTracker/KCCHurricaneTrackerRSS2.xml";
        RSS2GetFeed();
        retryRSS2--;
    }
}

function doInitRSS2()
{
    if(true)
    {
        //$('idRssChannelDIV').style.display="none";
        var n = ndxRSS2Feed;
        RSS2GetFeed();
        setTimeout('doRetryRSS2()',5000);
        
        //--- set up for next refresh (basically cycle through available feed;
        if(ndxRSS2FeedReloadMinutes > 0)
        {
            var numFeeds = urlRSS2Feed.length;
            var milliseconds = ndxRSS2FeedReloadMinutes*60000;
            ndxRSS2Feed = ((n+1)%numFeeds);
            setTimeout('doInitRSS2()',milliseconds);
        }
    }
    else
    {
    }
}


