// This file contains the AJAX functions to support the AJAX-Enabled News Ticker
// ============================================================================
//
// NewsTicker object
//
// ============================================================================

function RssTicker(strFeedId, nCacheTime, strDivId, strDivClass, nDelay, strOptionalSwitch)
{
  	this.strFeedId = strFeedId;
  	this.nCacheTime = nCacheTime;
  	this.strDivId = strDivId;
  	this.strDivClass = strDivClass;
  	this.nDelay = nDelay;
  	this.strOptionalSwitch = -1;
  	this.theAjaxObj = getAjaxInterface();

  	// Begin the Ticker div.
  	document.write('<div id="' + strDivId + '" class="' + strDivClass + '">Initializing News Ticker...</div>');

  	// Fetch the RSS feed
  	this.getAjaxContent();
}

RssTicker.prototype.getAjaxContent = function()
{
  	if ( this.theAjaxObj==null )
  		return;

  	var myInstance = this;
  	var strParams = 'id=' + encodeURIComponent(this.strFeedId) + '&cacheTime=' + this.nCacheTime + '&bustCache=' + new Date().getTime();
  	this.theAjaxObj.onreadystatechange = function()
	{
	  	myInstance.initialize();
	}

	this.theAjaxObj.open('GET', 'rssfetch.php?' + strParams, true);
	this.theAjaxObj.send(null);
}

RssTicker.prototype.initialize = function()
{
  	// The AjaxObj must not be null
  	if ( this.theAjaxObj==null )
  		return;

  	// The AjaxObj must be ready
  	if ( this.theAjaxObj.readyState!=4 || this.theAjaxObj.status!=200 )
  		return;

  	// Begin parsing the reponse RSS feed
  	var theXMLData = this.theAjaxObj.responseXML;
  	if ( theXMLData.getElementsByTagName('item').length==0 )
  	{
  	  	// Failed to retrieved the RSS feed
  	  	document.getElementById( this.strDivId ).innerHTML = '<b>ERROR:</b> Failed to fetch RSS feed.<br/>';
  	  	return;
  	}

  	// Get our own instance
  	var myInstance = this;

  	// Get all items
  	this.theRssItems = theXMLData.getElementsByTagName('item');
  	this.nRssIndex = 0;

  	// Store the components of each item as it's attribute.
  	for (var i=0; i<this.theRssItems.length; i++)
  	{
  	  	this.theRssItems[i].setAttribute('title', this.theRssItems[i].getElementsByTagName('title')[0].firstChild.nodeValue);
  	  	this.theRssItems[i].setAttribute('link' , this.theRssItems[i].getElementsByTagName('link') [0].firstChild.nodeValue);
  	  	this.theRssItems[i].setAttribute('desc' , this.theRssItems[i].getElementsByTagName('description')[0].firstChild.nodeValue);
  	  	this.theRssItems[i].setAttribute('date' , this.theRssItems[i].getElementsByTagName('pubDate')[0].firstChild.nodeValue);
  	}

  	// Set onMouseOver functions
  	document.getElementById(this.strDivId).onmouseover = function()
  	{
  	  	myInstance.bIsMouseOver = true;
  	}

  	// Set onMouseOut function
  	document.getElementById(this.strDivId).onmouseout = function()
  	{
  	  	myInstance.bIsMouseOver = false;
  	}

  	// Start the message rotation
  	this.rotateRssItem();
}

RssTicker.prototype.rotateRssItem = function()
{
	var myInstance = this;
  	if ( this.bIsMouseOver )
  	{
  	  	setTimeout(function(){myInstance.rotateRssItem()}, 100);
  	  	return;
  	}

	// Fill in the div contents
  	var theDiv = document.getElementById(this.strDivId);
  	var theTime = new Date(Date.parse(this.theRssItems[this.nRssIndex].getAttribute('date')));
  	var strTime = '<div class="time">' + formatTime(theTime.getHours(), theTime.getMinutes()) + '</div>';
  	var strData = '<div class="data"><a title="' + this.theRssItems[this.nRssIndex].getAttribute('desc') + '" href="' + this.theRssItems[this.nRssIndex].getAttribute('link') + '" target="_blank">' + this.theRssItems[this.nRssIndex].getAttribute('title') + '</a></div>';
  	theDiv.innerHTML = strTime + strData;

	// Cycle the index pointer  	
  	this.nRssIndex = (this.nRssIndex+1<this.theRssItems.length) ? this.nRssIndex+1 : 0;

  	// Set for this to happen in another [delay] time.
  	setTimeout(function(){myInstance.rotateRssItem()}, this.nDelay);
}

function formatTimeComp(c)
{
	if ( c<10 )
		return '0' + c;
	return c;
}

function formatTime(h, m)
{
	return formatTimeComp(h) + ':' + formatTimeComp(m);
}
