/**
 * RSS JavaScript Ticker Object
 * Author: George at JavaScriptKit.com / DynamicDrive.com
 * Created: Feb 5th, 2006
 * Updated: Feb 5th, 2006
 */

function createAjaxObj()
{
	var httprequest = false;
	
	if( window.XMLHttpRequest )
	{ // if not IE
		httprequest = new XMLHttpRequest();
		
		if( httprequest.overrideMimeType )
		{
			httprequest.overrideMimeType( 'text/xml' );
		}
	}
	else if( window.ActiveXObject )
	{ // if IE
		try
		{
			httprequest = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e )
		{
			try
			{
				httprequest = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e )
			{}
		}
	}

	return httprequest;
}


/**
 * Main RSS Ticker Object function
 * rss_fetch( RSS_id, cachetime, divId, divClass, delay, optionalswitch )
 */
function rss_fetch( RSS_id, nbr, folder, div_id )
{
	var cachetime  = 60;
	this.url_php   = folder;
	this.nbr_news  = nbr;
	this.RSS_id    = RSS_id;           // Array key indicating which RSS feed to display
	this.cachetime = 60;               // Time to cache feed, in minutes. 0=no cache.
	this.ajaxobj   = createAjaxObj();
	this.rss_content = "";
	this.divID = div_id;

	this.getAjaxcontent();
}


/**
 * getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
 */
rss_fetch.prototype.getAjaxcontent = function()
{
	if( this.ajaxobj )
	{
		var instanceOfTicker = this;
		var parameters       = "id=" + encodeURIComponent( this.RSS_id ) + "&cachetime=" + this.cachetime + "&bustcache=" + new Date().getTime();
		
		this.ajaxobj.onreadystatechange = function()
		{
			instanceOfTicker.initialize();
		}
		
		this.ajaxobj.open( 'GET', this.url_php + "rssfetch.php?" + parameters, true );
		
		this.ajaxobj.send( null );
	}
}


/**
 * initialize()- Initialize ticker method.
 * -Gets contents of RSS content and parse it using JavaScript DOM methods
 */
rss_fetch.prototype.initialize = function()
{
	var instanceOfTicker = this;

	if( this.ajaxobj.readyState == 4 )
	{ //if request of file completed
		if( this.ajaxobj.status == 200 )
		{ //if request was successful
			var xmldata = this.ajaxobj.responseXML;
			
			if( xmldata.getElementsByTagName( "item" ).length == 0 )
			{ //if no <item> elements found in returned content
				this.rss_content = "<li><strong>Error:</strong> Fetching remote RSS feed! " + this.ajaxobj.responseText + "</li>";
			}
			else
			{
				this.feeditems = xmldata.getElementsByTagName( "item" );
	
				var cnt_feed = 0;
	
				//Cycle through RSS XML object and store each peice of the item element as an attribute of the element
				for( var i = 0; i < this.feeditems.length; i++ )
				{
					if( this.feeditems[ i ].getElementsByTagName("title")[0] != null && cnt_feed < ( this.nbr_news ) )
					{

						if( 0 == cnt_feed )
						{
							this.rss_content += '<li class="first">';
						}
						else
						{
							this.rss_content += '<li>';
						}

						this.rss_content += '<a href="' + this.feeditems[ i ].getElementsByTagName("link")[0].firstChild.nodeValue + '">' + this.feeditems[ i ].getElementsByTagName("title")[0].firstChild.nodeValue + '</a>';

						if( 0 == cnt_feed )
						{
							this.rss_content += '<br />' + this.feeditems[ i ].getElementsByTagName("description")[0].firstChild.nodeValue;
						}

						this.rss_content += '</li>';
	
						cnt_feed++;
					}
				}
			}
		}
	}

	if( "" != this.rss_content )
	{
		document.getElementById( this.divID ).innerHTML = this.rss_content;
		return;
	}

}
