/*//////////////////////////////////////////////////////////////////////////////////////////////////
RSS parser
----------

Using:

Call the 
	
function LoadRSS(url,callbackfn,errorfn)

function to load and parse rss feed in asyncron mode. If the parsing is successfull the callbackfn will be called and you
can use the global myRssObject variable to reach rss content. Otherwise the errorfn will be called.

Reach remote files:

Javascript is not able to reach rss feeds if they have a remote location. To solve this problem you should make a local copy
from the file with php functions, eq:

<?php
$myxml = domxml_open_file("http://origo.hu/contentpartner/rss/itthon/origo.xml");
$myxml->dump_file("local.xml");
?>

RSSObject
---------
title
link
description
image.title
image.url
image.link
items[0..items.length].title
items[0..items.length].link
items[0..items.length].description
items[0..items.length].pubdate

//////////////////////////////////////////////////////////////////////////////////////////////////*/

var _rss_callbackfn = "";
var _rss_errorfn = "";

function RSSImage(title, url, link) {
    this.title = title;
    this.url = url;
    this.link = link;
}

function RSSItem(title, link, description, category, pubdate) {
    this.title = title;
    this.link = link;
    this.description = description;
    this.category = category;
    this.pubdate = pubdate;
}

function RSSObject() {
    this.title = "";
    this.link = "";
    this.description = "";
    this.image = new RSSImage("", "", "");
    this.items = new Array();
}

var myRssObject = new RSSObject();

function LoadRSS(url, callbackfn, errorfn) {
    _rss_callbackfn = callbackfn;
    _rss_errorfn = errorfn;

    var xmlHttpRequest = null;
    try {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
        xmlHttpRequest = new XMLHttpRequest();
    }

    xmlHttpRequest.open("GET", url, false);
    xmlHttpRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
    try {
        xmlHttpRequest.send();
    }
    catch (e) {
        RSS_Error000();
        return;
    }
    if (location.hostname == "" && xmlHttpRequest.status == 0) {
        if (xmlHttpRequest.responseXML.documentElement == null) {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(xmlHttpRequest.responseText);
            xmlHttpRequest = new Object();
            xmlHttpRequest.responseXML = xmlDoc;
        }
    }
    RSS_Returned000(xmlHttpRequest);
}

function GetTextValue(item) {
    if (item == null)
        return "";
    return (item.textContent ? item.textContent : item.text);
}

function GetFirstItem(owner, item) {
    if (owner == null)
        return null;
    var a = owner.getElementsByTagName(item);
    return a[0];
}

function SearchNode(owner, item) {
    var a = 0;
    for (var i = 0; i < owner.childNodes.length; i++) {
        cni = owner.childNodes[i];
        if (cni.nodeName == item) {
            return cni;
        }
        else {
            obj = SearchNode(cni, item);
            if (obj != null)
                return obj;
        }
    }
    return null;
}

function RSS_Returned000(req) {
    myRssObject = new RSSObject();
    _rss = req.responseXML;
    if (_rss.childNodes.length == 0) {
        RSS_Error000();
        return;
    }
    _channel = _rss.getElementsByTagName("channel");
    if (_channel.length == 0) {
        RSS_Error000();
        return;
    }
    _title = _rss.getElementsByTagName("title");
    if (_title.length == 0) {
        RSS_Error000();
        return;
    }
    _link = _rss.getElementsByTagName("link");
    if (_link.length == 0) {
        RSS_Error000();
        return;
    }
    _desc = _rss.getElementsByTagName("description");
    if (_desc.length == 0) {
        RSS_Error000();
        return;
    }
    _items = _rss.getElementsByTagName("item");
    if (_items.length == 0) {
        RSS_Error000();
        return;
    }

    myRssObject = new RSSObject();
    myRssObject.title = GetTextValue(_title[0]);
    myRssObject.link = GetTextValue(_link[0]);
    myRssObject.description = GetTextValue(_desc[0]);

    _channel = SearchNode(_rss, "channel");

    _image0 = SearchNode(_channel, "image");
    myRssObject.image.title = GetTextValue(GetFirstItem(_image0, 'title'));
    myRssObject.image.url = GetTextValue(GetFirstItem(_image0, 'url'));
    myRssObject.image.link = GetTextValue(GetFirstItem(_image0, 'link'));

    for (var i = 0; i < _items.length; i++) {
        _title = GetTextValue(GetFirstItem(_items[i], "title"));
        _link = GetTextValue(GetFirstItem(_items[i], "link"));
        _description = GetTextValue(GetFirstItem(_items[i], "description"));
        _category = GetTextValue(GetFirstItem(_items[i], "category"));
        _pubdate = GetTextValue(GetFirstItem(_items[i], "pubDate"));
        myRssObject.items[myRssObject.items.length] = new RSSItem(_title, _link, _description, _category, _pubdate);
    }

    setTimeout(_rss_callbackfn + "();", 1);
}

function RSS_Error000() {
    setTimeout(_rss_errorfn + "();", 1);
}

