var __dbMessage = "";
var __messageid = 0;
var __genericmessageid = 0;
var __frequency = 5000;
var __newMsgTxt = "";
var __rotate = 1;

function nextMessage( )
{
	// Next message
	__messageid++;
	
	// We will need this to find the appropriate messages in the XML file
	var productid = document.getElementById( "__printerid" ).innerHTML;
	
	if ( window.XMLHttpRequest )
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Grab the next message for this printer from the XML file
	xmlhttp.open( "GET", "/recommend.php?productid=" + productid + "&messageid=" + __messageid, false );
	xmlhttp.send();
	
	// If we have printer-specific messages left to display...
	if ( xmlhttp.responseText )
	{
		// Display message
		__newMsgTxt = xmlhttp.responseText;
		
		fadeMessage();		
		setTimeout( "nextMessage()", __frequency );
	}
	else
	{
		// Finished showing printer-specific messages so now show generic messages
		__genericmessageid++;
		xmlhttp.open( "GET", "/recommend.php?productid=ALL" + "&messageid=" + __genericmessageid, false );
		xmlhttp.send();
		
		// If we have generic messages left to display...
		if ( xmlhttp.responseText )
		{				
			// Display message
			__newMsgTxt = xmlhttp.responseText;
			
			fadeMessage();
			setTimeout( "nextMessage()", __frequency );
		}
		else
		{
			// All generic messages have been shown so re-display the DB msg and start again
			__messageid = 0;
			__genericmessageid = 0;
			__newMsgTxt = __dbMessage;
			
			fadeMessage();
			setTimeout( "nextMessage()", __frequency );				
		}
	}
}

function startMessageTimer()
{
	if ( __rotate == 0 )
		return;
	
	// Grab the first message displayed for this printer which will have come from the DB 
	// We will need this to re-display after the last XML msg has been displayed
	__dbMessage = document.getElementById( "specialhead" ).innerHTML;

	setTimeout( "nextMessage()", __frequency );
}

function fadeMessage()
{
	$("#specialhead").fadeOut( 100, 			
		function()
		{
			document.getElementById( "specialhead" ).innerHTML = __newMsgTxt; 
			$("#specialhead").fadeIn( 100 );
		} 
	); 
}
