var fullstring;
var xmlList;
var xmlurl;
var xmlhttp;
var list;
var x;
var i=0;
var hiLite;
var fileView;
var listView;
var t;
var power=0;
var lastClick;
var thisClick;
var counter=0;
var message=0;
var endchar;
var writeout;
var sound=0;
var hiID="link0";
var loID;
var clock;

function startclock() {
	var thetime=new Date();
	var nhours=thetime.getHours();
	var nmins=thetime.getMinutes();
	var nsecn=thetime.getSeconds();
	var nmonth=thetime.getMonth();
	nmonth+=1;
	var ntoday=thetime.getDate();
	var nyear=thetime.getYear();
	if (nsecn<10)
	 nsecn="0"+nsecn;
	if (nmins<10)
	 nmins="0"+nmins;
	if (nyear<=99)
	  nyear= "19"+nyear;
	if ((nyear>99) && (nyear<2000))
	 nyear+=1900;
	document.getElementById('clock').innerHTML=ntoday+"/"+nmonth+"-"+nyear+" "+nhours+":"+nmins+":"+nsecn;
	clock=setTimeout('startclock()',1000);
} 


function EvalSound(soundobj) {
  if (sound==1) {
	  var thissound=document.getElementById(soundobj);
	  thissound.Play();
  }
}

function typeWriter(speed) {
	if (message==0) {
		return;
	}
	if (speed==0) {
		speed=8;
	}
        var nextchar=message.charAt(counter);
        if (writeout) {
                writeout=writeout + nextchar;
        } else {
                writeout=nextchar;
        }
	if (sound==1) { 
		EvalSound('click'); 
	}
        document.getElementById('output').innerHTML=writeout;
        if (counter<endchar) {
                counter++;
                t=setTimeout("typeWriter()",speed);
        }
}

function sound_onoff() {
	if (power==0) {
        	alert("Check powersource");
	} else {
		if (sound==1) {
	                document.getElementById('soundbtn').style.background = "#111";
	                document.getElementById('soundbtn').style.borderStyle = "outset";
			sound=0;
		} else {
	                document.getElementById('soundbtn').style.borderStyle = "inset";
	                document.getElementById('soundbtn').style.background = "#4a0";
			sound=1;
		}
	}
}

function clickButton() {
	EvalSound('clickon');
	if (lastClick) {
		document.getElementById(lastClick).style.borderStyle = "outset";
		document.getElementById(lastClick).style.background = "#777";
		document.getElementById(lastClick).style.color = "#001";
	}
	if (thisClick) {
		document.getElementById(thisClick).style.background = "#555";
		document.getElementById(thisClick).style.borderStyle = "inset";
		document.getElementById(thisClick).style.color = "#4a0";
	}
}

function power_onoff() {
	var state=power;
	clearTimeout(t);
	if (state!=0) {
		EvalSound('clickon');
		power=0;
		clearTimeout(clock);
		document.getElementById('command').innerHTML="HW_PWRBTN.state_change[0]";
		document.getElementById('output').innerHTML="";
		document.getElementById('mainoverlay').style.zIndex = "3";
		document.getElementById('mainoverlay').style.background = "#111";
		document.getElementById('header-shadow').style.color = "#555";
		document.getElementById('header').style.color = "#111";
		thisClick=0;
		if (sound==1) {
	                document.getElementById('soundbtn').style.background = "#111";
		}
		clickButton();
	} else {
		EvalSound('clickon');
		power=1;
		if (sound==1) {
	                document.getElementById('soundbtn').style.background = "#4a0";
		}
		startclock();
		document.getElementById('mainoverlay').style.zIndex = "1";
		document.getElementById('mainoverlay').style.background = "#002";
		document.getElementById('header-shadow').style.color = "#009";
		document.getElementById('header').style.color = "#004";
		document.getElementById('command').innerHTML="HW_PWRBTN.state_change[1]";
		message="Initializing... Done.<br>System ready. Loading data";
		writeout='';
		endchar=message.length;
		counter=0;
                t=setTimeout("typeWriter()",1000);
		var l=setTimeout("loadXML()", 2500);
	}
}

function clickUp() {
	if (x.lenght==0 || !(x) || fileView==1 || power==0 || !(list)) {
		return;
	}
	if (sound==1) { 
		EvalSound('click'); 
	}
	var loLite=hiLite;
	if (hiLite==0) {
		hiLite=(x.length-1);
	} else {
		hiLite=hiLite-1;
	}
	loID='link'+loLite;
	hiID='link'+hiLite;
	document.getElementById(loID).style.color='#999';
	document.getElementById(hiID).style.color='#fff';
	document.getElementById('command').innerHTML='HW_BTN1[1].state_change';
}

function clickDown() { 
	if (x.lenght==0 || !(x) || (fileView==1) || power==0 || !(list)) {
		return;
	}
	if (sound==1) { 
		EvalSound('click'); 
	}
	var loLite=hiLite;
	if (hiLite==(x.length-1)) {
		hiLite=0;
	} else {
		hiLite=hiLite+1;
	}
	loID='link'+loLite;
	hiID='link'+hiLite;
	document.getElementById(loID).style.color='#999';
	document.getElementById(hiID).style.color='#fff';
	document.getElementById('command').innerHTML='HW_BTN[2].state_change';
}

function helpTxt() {
	if (power==1) {
		alert('Click up/down/enter keys to navigate');
	}
}

function clickExe() {
	if (x.lenght==0 || !(x) || power==0) {
		return;
	}
	if (sound==1) { 
		EvalSound('click'); 
	}
	clearTimeout(t);
	if (fileView==1) { // We were displaying file content, return to listview
		listView=1;
		fileView=0;
		document.getElementById('output').innerHTML=list;
		document.getElementById('command').innerHTML='return';
	} else { // We were in listview, show selected entry
		document.getElementById(hiID).style.color='#0f2';
		list=document.getElementById('output').innerHTML;
		var openFile=(x[hiLite].getElementsByTagName("filename")[0].childNodes[0].nodeValue);
		document.getElementById('command').innerHTML='display ' + openFile;
		showItem(openFile);
		fileView=1;
		listView=0;
	}
}

function loadXML() {
	// code for IE
	if (window.ActiveXObject)
	{
		xmlList=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation.createDocument)
	{
		xmlList=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	xmlList.async=false;
	xmlList.load("./content/datalist.xml");
	x=xmlList.getElementsByTagName("data");
	list=new Array();
	list.push("<ul class='filelist'>");
	for (i=0;i<x.length;i++) {
		title=(x[i].getElementsByTagName("filename")[0].childNodes[0].nodeValue);
		cdate=(x[i].getElementsByTagName("cdate")[0].childNodes[0].nodeValue);
		ctime=(x[i].getElementsByTagName("ctime")[0].childNodes[0].nodeValue);
		list.push("<li class='filelist' id='link" + i + "'>" + title + " <span style=\42font: 300 12px arial;\42><i>Changed " + cdate + " at " + ctime + "</i></span></li >");
	}
	list.push("</ul>Ready");
	hiLite=0;
	document.getElementById('output').innerHTML='';
	document.getElementById('command').innerHTML="list";
	printLines();
}

function printLines() {
	var string=list.shift();
	if (string) {
		if (fullstring) {
			fullstring=fullstring + string;
		} else {
			fullstring=string;
		}
		document.getElementById('output').innerHTML=fullstring;
		var f=setTimeout("printLines()", 200);
	} else {
		fullstring=0;
		return;
	}
}


function showItem(item) {
  xmlurl="http://mistr.oslogate.net/content/" + item;
  xmlhttp=null;
  if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
	  xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject) {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=state_Change;
	  xmlhttp.open("GET",xmlurl,true);
	  xmlhttp.send(null);
  }
  else {
	  alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change() {
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
	//    document.getElementById('output').innerHTML=xmlhttp.responseText;
	message="<span id='displayFile'>" + xmlhttp.responseText + "</span>";
    }
  else
    {
	//    document.getElementById('output').innerHTML="Problem retrieving data:" + xmlhttp.statusText;
	message="<h1>Item unavailable</h1><hr>" + xmlhttp.statustext + "<hr>Sorry for the inconvenience.";
    }
  document.getElementById('output').innerHTML=message;
  }
}

