var isIE = (-1 < (navigator.appVersion.indexOf( "MSIE" ))) ;

function setContent( war, wut ){
 if( isIE ){
  document.all[war].innerHTML = wut;
 }else{
  document.getElementById( war ).innerHTML = wut;
 }
}

function assert( test, info ){
 if( test ) return false ;
 alert( "Assert fail:  " + info ) ;
 return true ;
}

function log( s ){
 if( s.length > 70 ) s = s.substring( 0, 70 ) ;
 document.applets[0].log( s ) ;
}

ds = new Array() ;
var di, si ;	// demo index, step index:  ds[di][si] is current step

var stat = 0 ;	// 0 = stopped
				// 1 = waiting for not busy w/ timeout
				// 2 = pausing fixed period of time
				// -1 = script interp in progress

var atohan = 0, dtohan = 0, ptohan = 0 ;	// action, dog, poll timeout handles
var olcont ;
var sot = "" ;
var val = "", key = "" ;	// state:  key nonblank & val blank means waiting for val
var timeout = false ;

function begref( i ){
 return "<a href=\"javascript:beg(" + i + ");\">" ;
}
function rebegref( i ){
 return "<a href=\"javascript:rebeg(" + i + ");\">" ;
}
function doref(){
 return "<a href=\"javascript:doover();\">" ;
}
function spozref(){
 return "<a href=\"javascript:spoz();\">" ;
}

/**
 Parse "action" script step from ds[di][si++] until done or paused.
 */
function act(){				// interp script
 clearTimeout( dtohan ) ;
 for( stat = -1 ; stat < 0 ; ){
  var action = ds[di][si++] ;
//log( si + "," + action ) ;
  var actype = action.charAt( 0 ) ;
  action = action.substring( 1 ) ;
  if( actype == 'S' ){ //"Seek"
   if( action.charAt( 0 ) == '\\' ){		// full path
    sot = action ;
   }else{
    //while( action.startsWith( "..\\" ) ){	// climb relative
    while( action.length > 3 && action.substring( 0, 3 ) == "..\\" ){	// climb relative
     var io = sot.lastIndexOf( "\\" ) ;
     if( assert( io > 0, "..\\ rel to " + sot ) ){
      sot = "" ; action = "\\FliteMap" ;	// error
     }else{
      sot = sot.substring( 0, io ) ; action = action.substring( 3 ) ;
     }
    }
    sot = sot + "\\" + action ;
   }
   setAct( "|SEEK|" + sot ) ;
  }else if( actype == 'A' ){ //Pass action to applet
   setAct( action ) ;
  }else if( actype == 'C' ){ //Click a button
   document.applets[0].setKey( action ) ;
  }else if( actype == 'V' ){ //get a value from FliteMap
   val = "" ;
   key = action ;
   stat = 1 ;	// will wait for applet not busy
  }else if( actype == 'T' ){ //set Top of teaching window contents
//alert( "T" + action ) ;
   //document.all.hed.innerHTML = "<b>" + action + "</b>\n" ;
   setContent( "foot", "<b>" + action + "</b>\n" ) ;
  }else if( actype == 'D' ){ //Display content in teaching window
//alert( "D" + action ) ;
   if( action.charAt( 0 ) != ' ' ){
    olcont = "" ;
   }
   //document.all.bod.innerHTML = "<font color=gray>" + olcont + "</font>" + action + "\n" ;
   setContent( "main", "<font color=gray>" + olcont + "</font>" + action + "\n" ) ;
   olcont += action + "<br>" ;
  }else if( actype == 'P' ){ //Pause fixed period
   stat = 2 ;
   atohan = setTimeout( "act();", action + "000" ) ;
  }else if( actype == 'G' ){ //Goto script N
   go2( action ) ;
  }else if( actype == 'I' && timeout ){ //goto script N if Timeout
   go2( action ) ;
  }else if( actype == 'H' ){ //Halt script
   --si ;
   stat =  0 ;
  }
  if( stat == 1 ){
   timeout = false ;
   dtohan = setTimeout( "dog();", 12000 ) ;
   setPoll() ;
  }
 }  
}

function setAct( s ){
 document.applets[0].setAct( s ) ;
 stat = 1 ;	// will wait for applet not busy
}

function go2( i ){		// point script to a demo
 di = i ;
 si = 0 ;
}

var dstak ;
function doover(){		// restart last demo
 go2( dstak ) ;
 act() ;
}

function beg( i ){		// launch a demo
 timeout = false ;
 go2( dstak = i ) ;
 act() ;
}

function scrinter(){	// kill timeouts
 clearTimeout( ptohan ) ;
 clearTimeout( atohan ) ;
}

function rebeg( i ){	// kill timeouts and launch a demo
 scrinter() ;
 beg( i ) ;
}

function spoz(){	// pause script, offer resume or stop
 scrinter() ;
 var report = "Script paused at " ;
 for( tsi = si-1 ; report.length < 60 && tsi < ds[di].length ; ++tsi ){
  if( tsi >= 0 ) report = report + ds[di][tsi] + ","
 }
 setContent( "dbug", "<font color=red>" + report + "..</font><br/><a href=\"javascript:rebeg(0);\">STOP</a> | <a href=\"javascript:srez();\">RESUME</a>" ) ;
}

function srez(){	// resume script
 atohan = setTimeout( "act();", "1000" ) ;
 setPoll() ;
}



function setPoll(){		// sked poll()
 stat = 1 ;
 ptohan = setTimeout( "poll()", 250 ) ;
}

function poll(){		// check for applet no longer busy
 if( key != "" && val == "" ){	// we're waiting on a property
  val = document.applets[0].getProp( key ) ;
  if( val != "" ){
//log( key + "=" + val ) ;
   act() ;				// resume script
  }else{
   setPoll() ;			// keep looking
  }
 }else{
  var stus = document.applets[0].getStat() ;	// bit 2 = interrupted, bit 1 = in-prog, bit 0 = last seek failed
  if( stus > 3 ){		// script interrupted by user taking control
   rebeg( STI_DIY ) ;	// kill all timeouts, present DIY instructions
  }else if( stus < 2 ){	// no longer busy
   clearTimeout( dtohan ) ;
   if( stus != 0 ) log( "step " + si + " seek fail @ " +
    //"...\\" + document.applets[0].getProp( "l" ) ) ;
    document.applets[0].getProp( "f" ) ) ;
   act() ;				// resume script
  }else{
   setPoll() ;
  }
 }
}

function dog(){			// handle applet task timeout
 clearTimeout( ptohan ) ;
log( "timeout @ ds[" + di +"][" + si + "]=" + ds[di][si-1] ) ;
 timeout = true ;
 act() ;
}

function init(){
 var plet = document.applets[0] ;
 if( plet != null ) plet.getStat() ;
 beg( 0 ) ;
}
window.onunload = init ;    // fix script continuing while applet restarts in Mozilla

var DI_MAN = 0 ;	// main menu
//var DI_DIY = 1 ;	// DIY instructions
var DI_DEM = 1 ;
var DI_TOH = 2 ;	// timeout handler

var DIYguts = ""
 + "<font size=2><b>Take Control</b></font><br/>"
 + "To navigate, click the round button on your left - right edge to drill "
 + "in, top and bottom edges to scroll, left edge to climb back.&nbsp; "
 + "As you navigate, you will find underlined links coming into view.&nbsp; "
 + "When you've moved one to the center of the small display area, "
 + "click the center of the round button to select it." ;
 
// Main Menu
if( ENABLE_SELFRUN ){
 ds[DI_MAN] = new Array(
 "T" + "<font size=2>Auto-Pilot</font><br/>" + begref( 1 ) + "Take the self-running tour</a>.\n",
 "D" + DIYguts,
 "H") ;
}else{
 ds[DI_MAN] = new Array(
 //"T" + "<font size=2>Auto-Pilot</font><br/>" + begref( 1 ) + "Take the self-running tour</a>.\n",
 "D" + DIYguts,
 "H") ;
}


//// DIY instructions
//ds[DI_DIY] = new Array(
// "T" + rebegref( DI_MAN ) + "back to demo menu</a>",
// "D" + DIYguts,
// "H") ;
 
// generic timeout handling:
ds[DI_TOH] = new Array(
 "D"
 + "<b>Timeout.</b>&nbsp; The mJetz emulation is experiencing network problems."
 + "<br/>&nbsp;<br/>&nbsp;" + doref() + "restart demo</a>\n"
 + "<br/>&nbsp;<br/>or <b>" + begref( DI_MAN ) + "stop demo</a></b>\n",
 "H" ) ;

//200610 define nav keys, duh:
var kR = "C5" ; var kL = "C2", kU = "C1", kD = "C6", kS = "C8", kM= "C15" ;
var kREL = "C0" ; //key release - needed for arrows, not for select or menu
