// frequency of updates
var updateInterval = 3000;

// url of the now playing include
var nowPlayingUrl = "nowplaying.php";

function nowPlayingElement() 
{
	if ( document.getElementById ) {
		return document.getElementById("nowPlayingText");
	} else {
		return document.all.nowPlayingText;
	}
}

function startTimer() 
{
	self.setTimeout ( "timerTick()", updateInterval )
}

function timerTick() 
{
	getNowPlaying ( nowPlayingUrl );
	startTimer();
}

function stopTimer() 
{
	self.clearTimeout();
}

function getNowPlaying ( url ) 
{

	// add a random parameter onto the url to get round cache
	url = url + "?parameter=" + Math.random();

	var http_request = false;
	
	// if the browser is mozilla, safari etc...
    if ( window.XMLHttpRequest )
    {
        http_request = new XMLHttpRequest();
        if  ( http_request.overrideMimeType ) 
        {
			http_request.overrideMimeType('text/plain');
		}
    } 
    // and if it's Windows
    else if ( window.ActiveXObject ) 
    {
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch ( e ) 
        {
            try 
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }

	// if we have failed to create an http request object,
	// set the now playing element to an error message and quit.
    if ( ! http_request ) 
    {
        nowPlayingElement().innerHTML = 'Not available!';
        return false;
    }
    
    // otherwise, open the URL and set a callback for when it is ready.
    http_request.onreadystatechange = function() { alertContents ( http_request ); };
    http_request.open ( 'GET', url, true );
    http_request.send ( null );

}

function alertContents ( http_request ) 
{
	
	if ( http_request.readyState == 4 ) 
	{
	
		if ( http_request.status == 200 ) 
		{
		
			nowPlayingElement().innerHTML = http_request.responseText;

		} else {

			nowPlayingElement() = 'Not available!';

		}
	}
}

