// #############################################################################
// Setup the main object
RotateNews_Obj = function()
{
	this.i = 0;
	this.news = new Array();
	
	// #############################################################################
	// Initialises the object and stores it properly
	this.init = function()
	{
		// Grab the news
		this.ajax_call('');
	}
	
	// #############################################################################
	// For usage with the "Next" button
	this.next = function()
	{
		// Determine length of array
		var length = (this.news.length - 1);
		
		// Fade out
		this.fade(0);
		
		if (this.i < length)
		{
			// Skip to the next article
			this.i++;
		}
		else
		{
			// Reset
			this.i = 0;
		}
	}
	
	// #############################################################################
	// For usage with the "Previous" button
	this.previous = function()
	{
		// Determine length of array
		var length = (this.news.length - 1);
		
		// Fade out
		this.fade(0);
		
		if (this.i > 0)
		{
			// Skip to the next article
			this.i--;
		}
		else
		{
			// Reset
			this.i = length;
		}
	}
	
	// #############################################################################
	// Fades the given article
	this.fade = function(opacityTo)
	{
		// Shorthand
		var news = this.news[this.i];
		var elem = YAHOO.util.Dom.get('dbtech_news_content');
		
		// Determine the easing
		var opacityFrom = (opacityTo == 0 ? 1 : 0);
		var easing = (opacityTo == 0 ? YAHOO.util.Easing.easeOut : YAHOO.util.Easing.easeIn);
		
		//alert(elem.id);
		// Begin fading out
		var anim = new YAHOO.util.Anim(elem, {
			opacity: {from: opacityFrom, to: opacityTo}
		}, 0.5, easing);
		
		// What to do on animation complete
		anim.onStart.subscribe(function() {
			if (opacityTo == 1)
			{			
				elem.innerHTML = news;
			}
		});
		
		// What to do on animation complete
		anim.onComplete.subscribe(function() {
			elem.innerHTML = news;
			
			if (opacityTo == 0)
			{
				// Fade in
				RotatingNews.fade(1);
			}
		});
		
		// Do the animation
		anim.animate();		
	}
	
	// #########################################################################
	// Shorthand for an ajax call
	this.ajax_call = function(extraparams)
	{
		return YAHOO.util.Connect.asyncRequest('POST', 'ajax.php', {
			success: this.ajax_completed,
			failure: this.handle_ajax_error,
			timeout: vB_Default_Timeout,
			scope: this
		}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=dbtech_rotatenews' + extraparams);		
	}	
	
	// #########################################################################
	// This should never happen.
	this.handle_ajax_error = function(ajax)
	{
		// Just pop it up
		alert(ajax.statusText);
	}

	// #########################################################################
	// Finalise fetching of content
	this.ajax_completed = function(ajax)
	{
		if (!ajax.responseXML)
		{
			// Empty response
			alert('Invalid response from server: ' + ajax.responseText);
			return false;
		}
		
		// All possible tags
		var tags = [
			'news'
		];
		for (var t = 0; t < tags.length; t++)
		{
			eval('var ' + tags[t] + ' = ajax.responseXML.getElementsByTagName("' + tags[t] + '");');
		}
		
		if (news.length)
		{
			// Begin storing the output
			var html = new Array();
			
			for (var i = 0; i < news.length; i++)
			{
				// Shorthand
				var _news 		= news[i];
				var message 	= PHP.trim(_news.firstChild.nodeValue);
				
				// Set the shout message
				this.news.push(message);
			}
		}
		
		// Fetch article with no animation
		YAHOO.util.Dom.get('dbtech_news_content').innerHTML = this.news[this.i];		
	}
}
