//<script>
/********************************************************************************
 * Script Name : Jasfer
 * Version     : 1.00.A
 * Date		   : July 10, 2005
 * Jasfer or JavaScript Feed Reader is a JavaScript program to read RSS feeds 
 * 		and display it in a HTML file.
 * 
 *******************************************************************************/
///////////////////////////////////// Configurations //////////////////////////////
var feed_show_items = -1; //Show this much items - put a negative value(-1) if you want to show all items.

//The format in which the feed data will be displayed. The following replaces will be made
//	%LINK%		-	The contents of the <link> tag
//	%TITLE%		-	The contents of the <title> tag
//	%DESCRIPTION% -	The contents of the <description> tag
//	%DATE%		-	The contents of the <lastBuildDate> tag
//	%ITEMS%		-	The data about the items of this feed
var feed_template = '<div class="feed-details"><a href="%LINK%" class="feed-name">%TITLE%</a><div class="feed-description">%DESCRIPTION%</div><div><div class="feed-items">%ITEMS%</div></div>';

//The format in which an item will be displayed. The 'makeItem' constructor will replace the following text
//	%LINK%		-	The contents of the <link> tag inside the <item> tag
//	%TITLE%		-	The contents of the <title> tag inside the <item> tag
//	%DESCRIPTION% -	The contents of the <description> tag inside the <item> tag
//	%DATE%		-	The contents of the <pubDate> tag inside the <item> tag
//	%GUID%		-	The contents of the <title> tag inside the <item> tag
//	%ID%		-	An id for this item - will be in the format of "feed-item-0", "feed-item-1"
var item_template = '<div class="item-unit"><a href="%LINK%" class="item-name">%TITLE%</a><div class="item-description">%DESCRIPTION%</div></div>';

var feed_area = "feed_area";
///////////////////////////////////// Global Variables ////////////////////////////////
var feed_items = new Array();
var feed_total = 0,feed_nodes,feed_file="";
var feed_show_items_result=0; //The nubmer of items shown.
var feed_title,feed_link,feed_description,feed_date; //Variables for storing feed properties.
var feed_id = 0, feed_text = "";
var feed_type = "rss";

/////////////////////////////////////// Functions /////////////////////////////////////
//This is the function that must be called from the HTML File.
//Agrument : file - The location of the RSS feed.
//			 area - The id of the DIV element in which the feed should be shown
function showRSSFeed(file,area) {
	if(area) feed_area = area
	xmlLoad(file);
}

//This was actually given in the 'showRSSFeed' function - but Firefox exectued these functions
//	   even before the files were loaded - so I had to move them to another function which was
//	   called from 'xmlProcessor' function
function makeDisplay() {
	getFeedText();
	showFeed();
}

//Constructor Function for 'feed_items'
// Member variables...
// title 		-	The title of the current item - this is taken from contents of the <link> tag
// description	-	A description for this item - contents of the <description> tag 
// link			-	The link to this item - contents of the <link> tag
// date			-	The date in which the item was published - from <pubDate> tag
// guid			-	The GUID for this item
// text			-	The HTML code for inserting this item into to display area - as given in the 'item_template' variable
function makeItem(item) {
	var txt = "";
	//Put all items in their proper variables.
	//The ifs are given so that there won't be an error even if the stuff is not there.
	if(item.getElementsByTagName("title")[0]) 	this.title = item.getElementsByTagName("title")[0].firstChild.nodeValue;
	if(item.getElementsByTagName("description")[0])	this.desc = item.getElementsByTagName("description")[0].firstChild.nodeValue;
	if(item.getElementsByTagName("link")[0]) 	this.link = item.getElementsByTagName("link")[0].firstChild.nodeValue;
	if(item.getElementsByTagName("pubDate")[0])	this.date = item.getElementsByTagName("pubDate")[0].firstChild.nodeValue;
	if(item.getElementsByTagName("guid")[0])	this.guid = item.getElementsByTagName("guid")[0].firstChild.nodeValue;

	//Make a HTML text in the desired format.
	var text = "";
	if(this.title) { //If it is a valid item,
		text = item_template;
		text = text.replace(/%LINK%/g, this.link);
		text = text.replace(/%TITLE%/g, this.title);
		text = text.replace(/%DESCRIPTION%/g, this.desc);
		text = text.replace(/%DATE%/g, this.date);
		text = text.replace(/%ID%/g, "feed-item-"+feed_id);
		feed_id++;
	}
	this.text = text;
}

//Create the HTML for displaying the feed in the desired format - and return the HTML code
function getFeedText() {
	var text = feed_template;
	//Replace all the fields with the correct text
	text = text.replace(/%LINK%/, feed_link);
	text = text.replace(/%TITLE%/, feed_title);
	text = text.replace(/%DESCRIPTION%/, feed_description);
	text = text.replace(/%DATE%/, feed_date);

	//Insert all the items
	var item_text = "";
	feed_show_items_result = (feed_show_items < 0 ) ? feed_items.length : feed_show_items; //The number of items shown is decided here
	for(var i=0; i<feed_show_items_result; i++) {
		item_text = item_text + feed_items[i].text;
	}
	text = text.replace(/%ITEMS%/, item_text);

	feed_text = text;
	return text;
}


//Show the feed details.
function showFeed() {
	document.getElementById(feed_area).innerHTML = feed_text;
}

//Process the xml data
function xmlProcessor(xmlDoc) {
	if(!xmlDoc) {
		feedError();
	}
	
	/*
	//Get the feed type-won't work right now.
	feed_type = (xmlDoc.getElementsByTagName("rdf:RDF").length) ? "rdf:RDF" : "rss" ;
	
	if(feed_type == "rdf:RDF") { //Handle RDF Formats - :TODO: what if it is in lower case
		feed_nodes = data.documentElement.childNodes
	} else { //RSS format
		feed_nodes = data.documentElement.childNodes.item(0).childNodes
	}
	*/

	feed_channel_nodes = xmlDoc.getElementsByTagName("item"); //Get all the items

	feed_total = feed_channel_nodes.length; //Number of sofwares
	feed_items = new Array();

	var count = 0;
	for(var i=0; i<feed_total; i++) {
		//Get all the items in the feed.
		if((feed_channel_nodes[i].childNodes.length > 1) && feed_channel_nodes[i].nodeName == "item") {
			feed_items[count++] = new makeItem(feed_channel_nodes[i]);
		}
	}

	var txt = "";
	//Try to get the feed details like the title of the feed and a link.
	if(xmlDoc.getElementsByTagName("title")[0]) feed_title = xmlDoc.getElementsByTagName("title")[0].firstChild.nodeValue;
	if(xmlDoc.getElementsByTagName("description")[0]) feed_description = xmlDoc.getElementsByTagName("description")[0].firstChild.nodeValue;
	if(xmlDoc.getElementsByTagName("link")[0]) feed_link = xmlDoc.getElementsByTagName("link")[0].firstChild.nodeValue;
	if(xmlDoc.getElementsByTagName("date")[0]) feed_date = xmlDoc.getElementsByTagName("date")[0].firstChild.nodeValue;
	
	makeDisplay();
}

/////////////////////////////////////// XML File Loading ///////////////////////////////////////
//Firefox only function - happens when a XML file is loaded
function loadHandler () {
	xmlProcessor(this); //Call the Commen function with 'this' data.
}

//Load the xml file - using different method for different browsers  
function xmlLoad(xml_file) {
	//Initializations
	feed_id = 0;
	feed_total = 0;
	
	var xmlDocument = "";
	feed_file = xml_file;
	if(document.implementation.createDocument) {//Firefox
		xmlDocument = document.implementation.createDocument('', '', null);
		xmlDocument.load(xml_file);
		xmlDocument.addEventListener('load', loadHandler, false); //This function will happen when the file is loaded
	}
	else { //IE
		var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
		xmlDocument.async = false;
		var loadResult = xmlDocument.load(xml_file); 
		if (loadResult) {
		    // process xml document with DOM methods e.g.
						xmlProcessor(xmlDocument)
		} else {
			feedError();
			return false;
		}
	}
	return true;
}

function feedError() {
	feed_text = '<span style="color:red">Feed "'+feed_file+'" not found.</span>';
	showFeed();
	return false;
}

/*********************************** To Do ********************************************* /
 * Make it object oriented
 * Should support the RDF standard.
 * Documentation
 *  
 ***************************************************************************************/
