// global variables

var old_parsed=null;

function startUpdate(ms)
{
	window.setInterval("reloadData()",ms);
}


function reloadData() {
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		req=new XMLHttpRequest();
		// fix for MSIE
		if (req.overrideMimeType) req.overrideMimeType("text/html; charset=ISO-8859-1");
	}
	else
	{// code for IE6, IE5
		req=new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("GET","realtime_status.txt",true);
	// workaround for MSIE caching problem
	req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	req.onreadystatechange=function() { if (this.readyState==4 && this.status==200) updateLoadedValues(this.responseText); }
	try { req.send(null);} catch(err) {}

  	return false;
}

function updateLoadedValues(raw_data){
	var parsed=raw_data.split(";");

	function set_element(name, value)
	{
		document.getElementById(name).innerHTML=value;
	}

	function set_img(name, value)
	{
		document.getElementById(name).src=value;
	}
	function chk(i)
	{
		return !old_parsed || old_parsed[i]!=parsed[i];
	}

	if (chk(1) || chk(2) || chk(3)) set_element('statID',
			parsed[3]==99?"<font color=#ee0000>PRIORITY</font>":
			parsed[1]==1?"STAND-BY":
			parsed[2]==0?"<font color=#666666>IDLE</font>":
			parsed[2]==1?"<font color=#efb506>BUFFERING</font>":
			"<font color=#68e52b>PLAYING</font>"
		);
	if (chk(3)) set_element('srcID',parsed[3]==99?"":("URL"+parsed[3]));
	if (chk(4)) set_element('chanID',parsed[4]);
	if (chk(5)) set_img('shflID',"o"+parsed[5]+".gif");
	if (chk(6)) set_img('rptID',"o"+parsed[6]+".gif");
	if (chk(7)) {
		set_element('volID',parsed[7]+" %");
		update_vu_meter(vuV,parsed[7]);
	}
	if (chk(8)) set_element('bitrID',parsed[8]+" kbps");
	if (chk(9)) {
		set_element('bufID',parsed[9]+" B");
		update_vu_meter(vuB,parsed[9]);
	}
	if (chk(10)) {
		set_element('peaklID',(parsed[10]<=-127?"-&infin;":parsed[10])+" dB");
		update_vu_meter(vuL,parsed[10]);
	}
	if (chk(11)) {
		set_element('peakrID',(parsed[11]<=-127?"-&infin;":parsed[11])+" dB");
		update_vu_meter(vuR,parsed[11]);
	}
	for (i=0;i<8;i++)
		if (chk(12+i)) set_img('o'+i+'ID',"o"+parsed[12+i]+".gif");
	if (chk(20)) set_element('strID',parsed[20]);
	if (chk(21)) set_element('titlID',parsed[21]);

	old_parsed=parsed;
}
