//&L(0,"*",1);
// 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];
	}
	function update_inputs(inp)
	{
		var hwt=&LState(3,"%u",6);;

		/* disable certain inputs depending on the HW type */
		/* input IDs:
			129=line mono
			1=line stereo
			7=line ext ADC
			130=mic
			4=SPDIF opt AES/EBU
			5=SPDIF coax
		 */
		var ann100_200_table=	{129:'0',1:'X',7:'X',130:'0',4:'X',5:'X'};
		var in100_table=	{129:'0',1:'0',7:'X',130:'X',4:'0',5:'0'};
		var ann1000_table=	{129:'0',1:'0',7:'X',130:'0',4:'X',5:'X'};
		var ex1000_table=	{129:'0',1:'0',7:'0',130:'X',4:'0',5:'X'};
		var ann155_table=	{129:'X',1:'X',7:'X',130:'0',4:'X',5:'X'};
		var ex500_table=	{129:'0',1:'0',7:'0',130:'X',4:'X',5:'X'};
		var in_new_table=	{129:'0',1:'0',7:'X',130:'X',4:'X',5:'X'};
		var table;
		switch (hwt)
		{
			case 7:	//Annuncicom 100: mic in, line mono
			case 19: //Annuncicom 200: mic in, line mono
			table=ann100_200_table;
			break;

			case 8:	//Instreamer 100: line stereo, line mono, SPDIF optical, SPDIF coax
			table=in100_table;
			break;

			case 17: //Annuncicom 1000: mic in, line stereo, line mono
			table=ann1000_table;
			break;

			case 21: //Exstreamer 1000: line stereo, line mono, AES-EBU, line Ext ADC
			table=ann1000_table;
			break;

			case 32: //Annuncicom 155: mic in
			table=ann155_table;
			break;

			case 35: //Exstreamer 500: line stereo, line mono, line Ext ADC
			table=ex500_table;
			break;

			case 43: //Instreamer new: line stereo, line mono
			table=in_new_table;
			break;
		}

		for(i in table)
		{
			if (table[i]=='X') set_img('inp'+i,"oX.gif");
			else set_img('inp'+i,(parseInt(i,10)==parseInt(inp,10)?'o1.gif':'o0.gif'));
		}
	}

	if (chk(1)) set_element('statID', parsed[1]==3?"ENCODING":"IDLE");
	if (chk(2)) set_img('statimgID',parsed[2]==0?"o0.gif":"o1.gif");
	if (chk(3)) {
		set_element('peakliID',(parsed[3]<=-127?"-&infin;":parsed[3])+" dB");
		update_vu_meter(vuLI,parsed[3]);
	}
	if (chk(4)) {
		set_element('peakriID',(parsed[4]<=-127?"-&infin;":parsed[4])+" dB");
		update_vu_meter(vuRI,parsed[4]);
	}
	if (chk(5)) {	/* CTS */
		set_img('ctsID',"o"+parsed[5]+".gif");
	}
	if (chk(6)) {	/* RTS */
		set_img('rtsID',"o"+parsed[6]+".gif");
	}
	if (chk(7)) {	/* audio format */
		set_img('afmtID',"o"+parsed[7]+".gif");
	}
	if (chk(8)) {	/* input source */
		update_inputs(parsed[8]);
//		set_element('inpsrcID',parsed[8]==7?"Line Ext ADC":parsed[8]==5?"SPDIF coaxial":parsed[8]==4?"SPDIF optical or AES/EBU":parsed[8]==130?"Mic":parsed[8]==129?"Line mono":"Line stereo");
	}
	var n_inputs=&LState(3,"%u",10);;
	for (i=0;i<n_inputs;i++)
		if (chk(10+i)) set_img('o'+i+'ID',"o"+parsed[10+i]+".gif");

	old_parsed=parsed;
}
