//start the clock refresh

new Ajax.PeriodicalUpdater('somafmtime', '/recent/asbestos.time.html', {
  method: 'get', frequency: 60, decay: 1});


//Get the Stream ID from query
var streamID = gup("startstream");	

// channelName = streamID with speed srtipped;
var channelRawName = streamID.replace(/^fw\//,'');
var channelName = channelRawName.replace(/[0-9]*$/,'');


// NOTE: if startstream doesn't exist; channel name will be based on query string only
// this is for the recently played page that doesn't auto-start things.
// qs is the query string, and apache sees it (not the browser), we define this in the header with a SSI.

if ( (channelName == null ) ||(channelName == "undefined" ) ||(channelName == "" ) ) {
	channelName = qs;
	}

// reload the playing info pages when they get updated.

/**
 * Set of functions for checking a trigger file and pulling the actual content
 * and placing it into a target when a change is detected.
 *
 * @version 1.0
 * @author Andrew Christoffersen
 * @website www.websix.ca
 */
 
 /* Thanks Andrew! -- Rusty */
 
PlaylistUpdater('playinc', '/recent/' + channelName + '.html', '/recent/' + channelName + '.test.html', 80000)




var gUpdateDiv;
var gContentURL;
var gCheckURL;
var gCheckInterval;
var gCurrentCheck = '';

//Call only this function when setting up.
function PlaylistUpdater(updateDiv, contentURL, checkURL, checkInterval)
{
	gUpdateDiv = updateDiv;
	gContentURL = contentURL;
	gCheckURL = checkURL;
	gCheckInterval = checkInterval;	

	setTimeout('check()', gCheckInterval);
}

//called every x seconds to check the trigger file
function check()
{
	new Ajax.Request(gCheckURL,
		{
			method:'get',
			onSuccess: checkResponse
		}
	);

	setTimeout('check()', gCheckInterval);
}

//looks at the response from the trigger file and determines if it's time
//to update the content
function checkResponse(transport)
{
	var content = transport.responseText;
	if (gCurrentCheck != content)
	{
		gCurrentCheck = content;
		new Ajax.Request(gContentURL,
			{
				method:'get',
				onSuccess: function(t)
				{
					$(gUpdateDiv).innerHTML = t.responseText;
				}
			}
		);
	}
}





// This seems to need to be set after the periodicalupdater runs
// Were we called with "startstream=" ? Then
if (streamID) {
	// alert("Starting " + streamID);
	window.setTimeout("startStream(streamID);", 2000);
	}

function startStream(location)  {
	var newlocation  = '/' + location + '.pls';


	// loadIframe('busvan', newlocation);

	 window.location.href = newlocation;		
	
	}


function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec(qs);  // qs is set from quesy string echoed by ssi in top-player-ajax.html
	if( results == null )
		return "";
	else
		return results[1];
}

function InitPage() {
	// alert('Page Loaded.');

}

function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}
