/*-
 * Copyright (c) 2006-2010 Xin LI
 * All rights reserved.
 *
 * This license does NOT permit any distribution under ANY version of
 * GNU Public License.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Distribution or modification of this source code under any GNU
 *    Public License is explicitly forbidden.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Phantasm: delphijweb/www/rss.js,v 1.19 2010/02/08 04:09:37 delphij Exp $
 */

var RSSRequestObject = false; // XMLHttpRequest Object
var _RSS_url = 'http://www.delphij.net/atom.xml';

/*
 * Callback handler for readystatechange
 */
function _cbStateChange() {
	// Take action only when data is properly parsed.
	if (RSSRequestObject.readyState==4) {
		if (RSSRequestObject.responseText.toLowerCase() != 'invalid') {
			var xmlTree = RSSRequestObject.responseXML;
			var node = xmlTree.documentElement;

			// Get Channel information
			var channel = node;
			var content = '';

			// Browse items
			var items = channel.getElementsByTagName('entry');
			for (var n=0; (n < items.length) && (n < 8); n++) {
				var itemTitle = items[n].getElementsByTagName('title')[0].firstChild.data;
				var itemLink = items[n].getElementsByTagName('link')[0].getAttribute('href');
				var itemCategory, itemDate, tDate;
				var itemDescription = "" + items[n].getElementsByTagName('summary').item(0).firstChild.data;
				var htmlTag = /<[^>]*>/g;
				try {
					itemCategory = items[n].getElementsByTagName('category').item(0).firstChild.data;
				} catch (e) {
					itemCategory = "";
				}
				try {
					itemDescription = itemDescription.replace(htmlTag, "");
					var tPubStamp = items[n].getElementsByTagName('published').item(0).firstChild.data;
					tDate = new Date(tPubStamp);
					if (itemCategory === "")
						itemDate = ' [' + tDate.getFullYear() + '/' + (tDate.getMonth()+1) + '/' + tDate.getDate() + ']';
					else
						itemDate = ' [' + itemCategory + ', ' + tDate.getFullYear() + '/' + (tDate.getMonth()+1) + '/' + tDate.getDate() + ']';
				} catch (e) {
					if (itemCategory !== "")
						itemDate = ' [' + itemCategory + ']';
				}

				content += '<li><a href="'+itemLink+'" title="'+itemDescription+'">'+itemTitle+'</a>'+itemDate+'</li>';
			}

			content += '</ul>';
			// Display the result
			document.getElementById("contents").innerHTML = content;
		} else {
			// Tell the reader that there was error requesting data
			document.getElementById("contents").innerHTML = "Error requesting data.";
		}
		Show('contents');
	}
}

function _cbErr() {
	Hide('contents');
	window.setInterval("_syncRSS()", 3600000);
}

/*
 * Initialize the instance
 */
function RSSInit()
{
	window.setInterval("_syncRSS()", 3600000);

	if (window.XMLHttpRequest) {
		RSSRequestObject = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}

	_syncRSS();
}


/*
 * Main AJAX RSS reader request
 */
function _syncRSS() {
	document.getElementById("contents").innerHTML = "Requesting data ...";

	try {
		// Prepare the request
		RSSRequestObject.open("GET", _RSS_url, true);
		// Set the onreadystatechange function
		RSSRequestObject.onreadystatechange = _cbStateChange;
		// Send
		RSSRequestObject.send(null);
	} catch (E) {
		document.getElementById("contents").innerHTML = "Error requesting data ...";
		Show('contents');
		window.setInterval("_cbErr()", 5000);
	}
}

function Show(id) {
	var el = GetObject(id);
	el.style.display='';
}

function Hide(id) {
	var el = GetObject(id);
	el.style.display='none';
}

function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}
