// PHP array_search equivalent JS function - ADH20081201
// -----------------------------------------------------
Array.prototype.ArraySearch = function(val) {
   for(var i = 0; i < this.length; i++) {
      if(this[i] == val)
		return i;
   }
   return false;
}
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_ticker( RSS_id, cachetime, divId, divClass, delay, optionalswitch )
 * Italian version : ADH20081204
 */
function rss_ticker(strStartFeed)
{
	if (!strStartFeed) strStartFeed = 'vnunet.es'; // default value
	var cachetime = 60;
	var divId     = 'nme_ticker_news';
	var divClass  = 'nme_ticker';

	this.feeds     = new Array();
	this.feeds_pos = 0;
	 // ADH20081201 this.exclude   = 'startfeed';
	this.url_php = '/nme/'; // ADH20081201

	this.RSS_id = strStartFeed; // ADH20081201
	this.cachetime = cachetime; // Time to cache feed, in minutes. 0=no cache.
	this.tickerid = divId;     // ID of ticker div to display information
	this.delay = 3000;
	this.logicswitch  = ( typeof optionalswitch != "undefined" ) ? optionalswitch : -1;
	this.mouseoverBol = 0;         // Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer      = 0;
	this.ajaxobj      = createAjaxObj();

	document.write( '<div id="nme_ticker"><div id="nme_ticker_links"><a href="http://www.vnunet.es/">VNUnet</a> | <a href="http://www.siliconnews.es/">Silicon News</a> | <a href="http://www.gizmodo.es/">Gizmodo</a> | <a href="http://www.theinquirer.es/">The Inquirer</a> | <a href="http://descargas.vnunet.es/">Descargas</a> | <a href="http://www.channelinsider.es/">Channel Insider</a> | <a class="nme_last" href="http://www.eweekeurope.es/">eWEEK Europe</a> | <strong>Lo último:&nbsp;</strong></div><span id="' + divId + '">Initializing ticker...</span></div>' );
	this.feeds[0] = 'vnunet.es';
	this.feeds[1] = 'siliconnews.es';
	this.feeds[2] = 'gizmodo.es';
	this.feeds[3] = 'theinquirer.es';
	this.feeds[4] = 'descargas.vnunet.es';
	this.feeds[5] = 'channelinsider.es';
	this.feeds[6] = 'eweekeurope.es';
	// change the default start feed, if necessary - ADH20081201
	if (strStartFeed != 'vnunet.es') {
		var iStartFeed = this.feeds.ArraySearch(strStartFeed);
		if (iStartFeed != false) {
			var strOldStart = this.feeds[0];
			this.feeds[0] = strStartFeed;
			this.feeds[iStartFeed] = strOldStart;
		}
	}

	for( var i = 0; i < this.feeds.length; i++ )
	{
		if( this.RSS_id == this.feeds[ i ] )
		{
			this.feeds_pos = i;
		}
	}

	this.getAjaxcontent();
}


/**
 * getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
 */
rss_ticker.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_ticker.prototype.initialize = function()
{
	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
				document.getElementById( this.tickerid ).innerHTML = "<strong>Error:</strong> Fetching remote RSS feed!<br />" + this.ajaxobj.responseText;

				return;
			}

			var instanceOfTicker = this;

			this.feeditems = xmldata.getElementsByTagName( "item" );

			//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++ )
			{
				this.feeditems[ i ].setAttribute( "ctitle",       this.feeditems[ i ].getElementsByTagName( "title" )[0].firstChild.nodeValue );
				this.feeditems[ i ].setAttribute( "clink",        this.feeditems[ i ].getElementsByTagName( "link" )[0].firstChild.nodeValue );
				this.feeditems[ i ].setAttribute( "cdescription", this.feeditems[ i ].getElementsByTagName( "description" )[0].firstChild.nodeValue );
			}
			document.getElementById( this.tickerid ).onmouseover = function()
			{
				instanceOfTicker.mouseoverBol = 1;
			}

			document.getElementById( this.tickerid ).onmouseout = function()
			{
				instanceOfTicker.mouseoverBol = 0;
			}

			this.rotatemsg();
		}
	}
}


/**
 * rotatemsg()- Rotate through RSS messages and displays them
 */
rss_ticker.prototype.rotatemsg = function()
{
	var instanceOfTicker = this;

	if( this.mouseoverBol == 1 )
	{ //if mouse is currently over ticker, do nothing (pause it)
		setTimeout( function() { instanceOfTicker.rotatemsg() }, 100 );
	}
	else
	{
		var tickerDiv     = document.getElementById( this.tickerid );
		var tickercontent ='<a href="' + this.feeditems[ this.pointer ].getAttribute( "clink" ) + '">' + this.feeditems[ this.pointer ].getAttribute( "ctitle" ) + '</a>';

		if ( this.logicswitch == "showdescription" )
		{
			tickercontent += "<br />" + this.feeditems[ this.pointer ].getAttribute( "cdescription" );
		}

		tickerDiv.innerHTML = tickercontent;

		if( this.pointer < this.feeditems.length - 1 )
		{
			this.pointer++;
		}
		else
		{
			this.pointer = 0;

			this.nextfeedid();
			return this.getAjaxcontent();
		}

		setTimeout( function() { instanceOfTicker.rotatemsg() }, this.delay ); //update container every second
	}
}


/**
 * nextfeedid()- select the next RSS_id to display
 */
rss_ticker.prototype.nextfeedid = function()
{
	this.feeds_pos++;

	if( this.feeds_pos == this.feeds.length )
	{
		this.feeds_pos = 0;
	}

//alert( this.feeds_pos );
	
	this.RSS_id = this.feeds[ this.feeds_pos ];
}
