// (c) 2001, D.Holmes

// curr_ms, prev_ms			set in calling file
// server_H, server_M, server_S

var server_time = "";
var curr_sec    = 0;
var ms_adj      = 0.000;
var ms_rem      = 0.000;

var printing;

var rMax		=  1;
var cLen		= 11;
var cellWidth		= 11;
var cellHeight		= 15;

var imgdir		= "/d.holmes/photos/5x7small/gB/";
var colourcode		= "-gB";	// -gB (grow/lblue on white) or -b (blue) or -B (bright blue) or -y2 (yellow)

var fileprefix		= imgdir;
var filesuffix		= colourcode + ".gif";

var cellbackground	= imgdir + "cell-bgnd" + colourcode + ".gif";
var cursorAnim		= "cursor-anim" + colourcode + "_t.gif";
var spincursor		= "Sun-cw" + colourcode + "_t.gif";


var imgprefix	= "asc";

//var cursorimg	= new Image();
//cursorimg.src	= imgdir + cursorAnim;
//var NotAChar       = 999;
//var NotACharImg    = new Image();
//NotACharImg.src    = "5x7/ascii-999_t.gif";

var rIndex     =  0;
var cIndex     =  0;
var sIndex     =  0;
var sCount     =  1;
var currString = "";

var clockBlink = false;
var clkIntervalID;
var testLCDid;

/*
       0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel
       8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si
      16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb
      24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us
      32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '
      40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /
      48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7
      56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?
      64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G
      72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O
      80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W
      88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _
      96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g
     104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o
     112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w
     120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del

for (imgs = 32; imgs <= 126 ; imgs++) {
        eval("var " + imgprefix + imgs + " = new Image()\;");
}
for (imgs = 32; imgs <= 126 ; imgs++) {
        eval(imgprefix + imgs + ".src = \"" + fileprefix + imgs + filesuffix + "\"\;");
}

*/

function precache() {
	var c = 0;
	currString = eval("Text1");
	textLen = currString.length;
	while (c < textLen) {
		target = eval(imgprefix + currString.charCodeAt(c));
		source = fileprefix + currString.charCodeAt(c) + filesuffix;
		target.src = source;
		c++;
	}
}

for (imgs = 32; imgs <= 126 ; imgs++) {
	eval("var " + imgprefix + imgs + " = new Image();");
       	eval(imgprefix + imgs + ".onload = countimg;");
}
//precache();

var numcached = 0; // 95 blocks mainLoop

function countimg() {
	numcached++;
	//window.status = numcached;
}

function LCDclock2() {
	clearTimeout(clkTimeoutID);
	clearRow(0);
	clkIntervalID = setInterval("LCDclock()",2000);
	scrollR();
}

function LCDclock() {

	var c = 0;
	var numstring = "";

	client_now = new Date();
	curr_ms    = client_now.getTime();
	ms_diff    = curr_ms - prev_ms;			// ms since last update
	prev_ms    = curr_ms;				// note now for next time thru

	ms_adj = ms_diff + ms_rem;					// 2.nnn +/- prev round error
	old_ms_rem = ms_rem;
	ms_rem = Math.floor(((ms_adj/1000) - Math.round(ms_adj/1000)) * 1000);


	server_S = Math.round(server_S + Math.round(ms_adj/1000));

	if (server_S > 59) {
		server_M = server_M + Math.floor(server_S / 60);
		server_S = server_S % 60;
	}
	if (server_M > 59) {
		server_H = server_H + Math.floor(server_M / 60);
		server_M = server_M % 60;
	}
	server_H = ((server_H > 23) ? server_H - 24 : server_H);

        server_time  = (server_H < 10 ? "0" + server_H.toString() : server_H.toString());
	server_time += ((clockBlink) ? " " : ":");
        server_time += (server_M < 10 ? "0" + server_M.toString() : server_M.toString());
	//server_time += ((clockBlink) ? " " : ":");
        //server_time += (server_S < 10 ? "0" + server_S.toString() : server_S.toString());

	clockBlink  = !clockBlink;
	clockLen = server_time.length;

	while (!printing && c < clockLen) {
		//target = "lcd" + rMax + "c" + (cLen - clockLen + c);	// align-right
		target = "lcd" + rMax + "c" + (c + 3);			// arbitrary - c must be pre-set or 0 for align-left
		source = fileprefix + server_time.charCodeAt(c) + filesuffix;
		document[target].src = source;
		c++;
	}
	/*
	window.status =  "st: " + server_time + " ms: " + ms_diff + " adj: " + ms_adj +
		" sec: " + Math.round(ms_adj/1000) + " oldrem: " + old_ms_rem +
		" rem: " + ms_rem;
	*/
}
	

function mainLoop() {
	var c = 0;

	if (numcached < 95) {
		return;
	} else {
		clearInterval(mainLoopID);
	}
/*
	currString = eval("Text1");
	textLen = currString.length;
	while (c < textLen) {
		target = "lcd" + rMax + "c" + c;
		source = fileprefix + currString.charCodeAt(c) + filesuffix;
		document[target].src = source;
		c++;
	}
*/
	randomizeID = setInterval("randomize()",25);
}

var randcount = 0;

function randomize() {
	var c = 0;
	currString = eval("Text1");
	textLen = currString.length;
	if (randcount <= textLen) {
		while (c < textLen) {
			target = "lcd" + rMax + "c" + c;
			source = fileprefix + currString.charCodeAt(c) + filesuffix;
			if (c == (randcount % textLen)) {
				document[target].src = source;
			} else {
				document[target].src = asc32.src;
			}
			c++;
		}
	} else if (randcount <= (textLen * 2) ) {
		while (c < textLen) {
			target = "lcd" + rMax + "c" + c;
			source = fileprefix + currString.charCodeAt(c) + filesuffix;
			if (c == (randcount % textLen) || c == ((randcount % textLen) - 2)) {
				document[target].src = source;
			} else {
				document[target].src = asc32.src;
			}
			c++;
		}
	} else if (randcount <= ((textLen * 3) - 1) ) {
		while (c < textLen) {
			target = "lcd" + rMax + "c" + c;
			source = fileprefix + currString.charCodeAt(c) + filesuffix;
			if (c == (randcount % textLen)) {
				document[target].src = source;
			} else {
			//	document[target].src = asc32.src;
			}
			c++;
		}
	} else {
		clearInterval(randomizeID);
		while (c < textLen) {
			target = "lcd" + rMax + "c" + c;
			source = fileprefix + currString.charCodeAt(c) + filesuffix;
			document[target].src = source;
			c++;
		}
		return;
	}
	//window.status = randcount;
	randcount++;
}

function afterload() {

	for (imgs = 32; imgs <= 126 ; imgs++) {
       		eval(imgprefix + imgs + ".src = \"" + fileprefix + imgs + filesuffix + "\";");
	}

	rMax-- ;
	printing = false;
	mainLoopID = setInterval("mainLoop()",500);
	clkTimeoutID = setTimeout("LCDclock2()",10000);
}


function clearRow(row) {
        //clearTimeout(putLineID);
        r = row;
        if (r <= rMax) {
                for (c = 0; c < cLen ; c++) {
                        target = "lcd" + r + "c" + c;
                        source = imgprefix + "32.src";
                        document[target].src = eval(source);
                }
        }
        cIndex = 0;
        rIndex = r;
}

//*****************
// scrollR 
			//   once the front of the string reaches the left, then
			//   we start "trimming" the string down as it moves off
			//   the left edge

// should try moving the Window range and Row on the fly...

var WindowLeft	= 0;			 	// *** from          0         to  (WindowRight - 1)
var WindowRight	= 11; //server_time.length - 3;	// *** from  (WindowLeft + 1)  to      (cLen)

var scrollRtime =  110;
//var scrollRtime = Math.round(  300  /  ((WindowRight - WindowLeft) / 1.7)  )  ;

var scrollRptr	= WindowRight;	// goes from WindowRight (or cLen)  to 0

var scrollinRow	= 0		// which row are we in?

//var scrollStringCopy = scrollString = "the truth is out there";
var scrollStringCopy = scrollString = eval("ScrollText1");

var timesThru	= 0;


function scrollR () {

	printing = true;
	//stopDisplay();
	clearTimeout(testLCDid);

	timesThru++;

	var i,c,r;

	var target, source;

	r = scrollinRow;

	if (scrollRptr > WindowLeft) {

		scrollRptr--;	// move column ptr to the left

		i = 0;		// which letter in string?
		c = scrollRptr;	// which column?


		while (c < WindowRight && i < scrollString.length) {
			
			target = "lcd" + r + "c" + c;
			source = fileprefix + scrollString.charCodeAt(i) + filesuffix;
			document[target].src = source;

			c++;
			i++;
		}
		if (  (c < WindowRight)   &&  (i == scrollString.length)  ) {
			
			// we've at least one free space off the right end
			// print a space there to clear the old char from scrollString

			target = "lcd" + r + "c" + c;
			source = fileprefix + 32 + filesuffix;
			document[target].src = source;

		}

		testLCDid = setTimeout("scrollR()", scrollRtime);

	} else if (scrollString.length > 0) {	

		c = WindowLeft;
		i = 0;
		scrollString = scrollString.slice(1);

		while (c < WindowRight && i < scrollString.length) {

			target = "lcd" + r + "c" + c;
			source = fileprefix + scrollString.charCodeAt(i) + filesuffix;
			document[target].src = source;

			c++;
			i++;
		}
		if (  (c < WindowRight)   &&  (i == scrollString.length)  ) {

			// we've at least one free space off the right end
			// print a space there to clear the old char from scrollString

			target = "lcd" + r + "c" + c;
			source = fileprefix + 32 + filesuffix;
			document[target].src = source;

		}

		testLCDid = setTimeout("scrollR()", scrollRtime);

	} else {

		// ****  msg_counter, num_msgs and loop_at are set in calling file **** 
		//
		if (++msg_counter > num_msgs) {
			msg_counter = loop_at;	// (set in caller) cuts off some first msgs from looping
		}
		scrollString = scrollStringCopy = eval("ScrollText" + msg_counter);
		scrollRptr = WindowRight;
		timesThru = 0;
		if (scrollString == "xPAUSEx") {
			if (++msg_counter > num_msgs) {
				msg_counter = loop_at;	// (set in caller) cuts off some first msgs from looping
			}
			scrollString = scrollStringCopy = eval("ScrollText" + msg_counter);
			testLCDid = setTimeout("scrollR()", (msg_delay * 31));
			clockBlink = false;
			printing = false;
		} else {
			testLCDid = setTimeout("scrollR()", msg_delay);
		}
		return;
	}
}

function drawTable() {

   var BGimg = cellbackground;
   var FGimg = fileprefix + "32" + filesuffix;

 /*
   var LCDtable = "<TABLE BGCOLOR=\"#001955\" WIDTH=\"" + ((cellWidth * cLen) + 4) + 
		  "\" CELLPADDING=\"2\" CELLSPACING=\"0\" BORDER=\"0\">\n";
   LCDtable += "<TR><TD>\n";
   LCDtable += "<TABLE BACKGROUND=\"" + BGimg + "\" WIDTH=" + (cellWidth * cLen) + 
 */
   var LCDtable = "<TABLE WIDTH=\"" + ((cellWidth * cLen) + 4) + 
		  "\" CELLPADDING=\"2\" CELLSPACING=\"0\" BORDER=\"0\">\n";
   LCDtable += "<TR><TD>\n";
   LCDtable += "<TABLE WIDTH=" + (cellWidth * cLen) + 
		" CELLPADDING=0 CELLSPACING=0 BORDER=0>\n";

   for (r = 0; r < rMax ; r++) {
	LCDtable += "<TR><TD NOWRAP>";
	for (c = 0; c < cLen ; c++) {
		LCDtable += "<IMG \nNAME=\"lcd" + r + "c" + c + "\" SRC=\"" + FGimg + "\"";
		LCDtable += "WIDTH=\"" + cellWidth + "\" HEIGHT=\"" + cellHeight + "\">";
	}
	LCDtable += "</TD></TR>\n";
   }

   LCDtable += "</TABLE>\n";

   LCDtable += "</TD><TD><IMG SRC=\"/images/corner_50x38.gif\"></TD></TR>\n";
   LCDtable += "</TABLE>\n";

   document.write(LCDtable);
   // this isn't technically polite...
   document.close();
}


