var g_originaltypetyme=50;
var g_typetyme=g_originaltypetyme;
var g_doneslower=20;

var g_prompt=new Array();

var g_allowall=0;

var g_writing=0;

var g_content=new Array();

var g_popversion=0;

var g_feedbackon=0;

var g_seenalready=new Array();

function allowall() {
 g_allowall=1;
 onload_prep();
}

var g_prep=new Array();

function prep_content(t,n,c) {
 g_prep[0]=t;
 g_prep[1]=n;
 g_prep[2]=c;
}

function onload_prep() {
 if (g_prep[0]>0 && g_prep[1]>0) { populate(g_prep[0],g_prep[1]); }
 else { typepop(g_prep[0],g_prep[2]); }
}

function set_content(t,n,c) {
 g_content[0]=t;
 g_content[1]=n;
 g_content[2]=c;
 debug("set_content: "+t+"_"+n+"_"+c);
 if (!g_seenalready[t+"_"+n+"_"+c]) { g_seenalready[t+"_"+n+"_"+c]=1; }
 else { g_seenalready[t+"_"+n+"_"+c]++; }
}

function populate(t,n) {
 if (t && n && t==g_content[0] && n==g_content[1]) { return; }
 g_popversion++;
 g_stillwriting=0;
 set_content(t,n,"");
 donewithfootnote();
 document.getElementById('gateway').innerHTML='<iframe style="display:none; width:0px; height:0px" id="populate" src="ruby.php?t='+t+'&n='+n+'"></iframe>';
 //g_feedbackon=1;
}

function typepop(t,tt) {
 if (parent==undefined) { parent=0; }
 if (tt==undefined) { tt=0; }
 if (t!='' && t==g_content[0] && g_content[2]=="c") { return; }
 g_popversion++;
 g_stillwriting=0;
 set_content(t,"","c");
 donewithfootnote();
 document.getElementById('gateway').innerHTML='<iframe style="display:none; width:0px; height:0px" id="populate" src="ruby.php?t='+t+'&c=1&tt='+tt+'"></iframe>';
 g_feedbackon=0;
}

function allow_footnotes(allow) {
 if (allow==undefined) { allow=0; }
 g_feedbackon=allow;
 //debug('allow footnotes: '+g_feedbackon);
}

function writetoscreech(b,t,nowipe) {
 g_popversion++;
 g_typetyme=g_originaltypetyme;
 g_writing=1;
 if (!nowipe) {
	g_internallink="";
	superwipe('screech');
 }
 hidefootnote();
 b=b.replace(/"/g,'&quot;');
 b=b.replace(/\'/g,"\'");
 /*debug("checking if seen: "+g_content[0]+"_"+g_content[1]+"_"+g_content[2]);
 if (g_seenalready[g_content[0]+"_"+g_content[1]+"_"+g_content[2]]>1) {
 }*/
 typeout(b,'screech',g_popversion,t);
}

function superwipe(id) {
 document.getElementById(id).innerHTML="";
}

function forcerefresh(id) {
 if (id==undefined) { id='wholecontentarea'; }
 document.getElementById(id).style.display='none';
 document.getElementById(id).style.display='block';
}

var g_typelinking=0;
var g_internallink=0;
var g_stillwriting=0;

var g_fr=0;

function typeout(b,id,vers,type) {
 if (vers!=g_popversion || !document.getElementById(id)) { g_stillwriting=0; return; }
 g_stillwriting=1;
 if (id==undefined || id=="") { id='screech'; }
 if (b) {
	g_fr++;
	if (g_fr%24==23) { forcerefresh(); }
	var n="";
	if (b.match(/^``(\d+)``(\d+|C|X)~~/)) {	// LINK TO CONTENT
		var du=b.split("~~");
		var links=du[0].split("``");
		links.shift();
		g_internallink++;
		b=b.replace(/^``(\d+)``(\d+|C|X)~~/,"");
		var nl='<div>'+g_internallink+'. <a href="javascript:';
		if (links[1]=="C") {
			nl+='typepop(\''+links[0]+'\')"';
		}
		else if (links[1]=="X") {
			nl+='typepop(\''+links[0]+'\',\'1\')"';
		}
		else { nl+='populate(\''+links[0]+'\',\''+links[1]+'\')"'; }
		nl+=' id="link'+g_internallink+'"></a></div>';
		document.getElementById(id).innerHTML+=nl;
		var g=b.split("_");
		typeout(g[0],"link"+g_internallink,vers);
		g.shift();
		if (g.length) { b=g.join("_"); }
		else { b="_"; }
		return typeout(b,id,vers,type);
	}
	else if (b.match(/^_`~`~/)) {	// LINK TO FOOTNOTE
		b=b.replace(/^_/,"");
		var g=b.split("_");
		g_internallink++;
		document.getElementById(id).innerHTML+='<div class="footnote" id="link'+g_internallink+'"></div>';
		g[0]=g[0].replace(/^`~`~/,"");
		debug("footnote");
		typeout(g[0],"link"+g_internallink,vers);
		g.shift();
		if (g.length) { b=" "+g.join("_"); }
		else { b="_"; }
		return typeout(b,id,vers,type);
	}
	else if (b.match(/^http:\/\//)) {	// LINK TO URL
		b=b.replace(/_/g," \n");
		var g=b.split(" ");
		g_internallink++;
		document.getElementById(id).innerHTML+='<a href="'+g[0]+'" id="link'+g_internallink+'" target="_blank"></a>';
		g[0]=g[0].replace(/^http:\/\//,"");
		debug("relink "+g[0]);
		typeout(g[0],"link"+g_internallink,vers);
		g.shift();
		if (g.length) { b=" "+g.join(" "); }
		else { b="_"; }
		b=b.replace(/\n/g,"_");
		return typeout(b,id,vers,type);
	}
	else if (b.match(/^&(#|)(\w|\d)+\;/)) {
		var g=b.split(";");
		n=g[0];
		g.shift();
		b=g.join(";");
	   b=" "+b;
	}
	else { n+=b.substr(0,1); }
	var t=g_typetyme;
	if (n=="_") {
		if (g_typelinking) { n+="!</a>"; }
		n="<BR>"; t*=2;
	}
	document.getElementById(id).innerHTML += n;
	var b2=b;
	setTimeout('typeout("'+b2.substr(1,b2.length-1).replace("'","\'")+'","'+id+'","'+vers+'","'+type+'")',t);
 }
 else {
	if (g_typelinking) { document.getElementById(id).innerHTML+="</a>"; }
	g_stillwriting=0;
	liketoclearwriting();
	startblinkdone();
	//alert(document.getElementById(id).innerHTML);
 }
}

function liketoclearwriting() {
 forcerefresh();
 setTimeout('if (!g_stillwriting) { g_writing=0; donewritingwhatnext(); }',g_typetyme*5);
 //debug("waiting to clear...");
}

function donewritingwhatnext() {
 if (g_feedbackon) { addfootnote(); }
 forcerefresh();
}

var g_blinkversion=0;

function stopblinking() {
 g_blinkversion++;
}

function startblinkdone() {
 g_blinkversion++;
 blinkdone(g_blinkversion);
}

function blinkdone(vers) {
 if (vers!=g_blinkversion) { return; }
 id="doneblock";
 if (!document.getElementById(id)) { return; }
 else {
  if (document.getElementById(id).style.visibility=="visible") {
	document.getElementById(id).style.visibility="hidden";
  }
  else { document.getElementById(id).style.visibility="visible"; }
  setTimeout('blinkdone('+vers+')',g_typetyme*g_doneslower);
 }
}


var g_curx;
var g_cury;

function track_mouse_move() {
 /* handles keeping track of mouse motions: */
 if (!document.all) { document.captureEvents(Event.MOUSEMOVE); } //non-ie way
 document.onmousemove = getMouseXY; //ie way
}

function getMouseXY(e) {
 if (document.all) { g_curx = event.clientX + document.body.scrollLeft; g_cury = event.clientY + document.body.scrollTop; }
 else if (e.pageX) { g_curx = e.pageX; g_cury = e.pageY; }
}

track_mouse_move();

function movecursor() {
 moveid('curse');
 tryfootnotecatch();
}

function moveid(id) {
 if (g_allowall<1) { return; }
 track_mouse_move();
 showid(id,1);
 var xof=-30;
 var yof=-30;
 document.getElementById(id).style.left=g_curx + xof;
 document.getElementById(id).style.top=g_cury + yof;
}

function showcursor(sh) {
 return showid('curse',sh);
}

function showid(id,sh) {
 if (!document.getElementById(id)) { alert("Bad Showid: "+id); }
 var ash=new Array("none","block");
 if (id=="doneblock" || id=="footnoteblock" || id=="footnotetext" || id=="footsignclear") { ash[1]="inline"; }
 document.getElementById(id).style.display=ash[sh];
}

function footnoteprompt(remove) {
 if (!g_writing) {
	if (g_feedbackon) {
	 	if (remove==undefined) {remove=1;}
		if (remove) { moveid('footp'); }
		else { showid('footp',1); }
	}
	else {
		if (remove) { moveid('nofootp'); }
		showid('nofootp',1);
	}
 }
}

function revertprompt() {
 if (!g_writing) {
	if (g_feedbackon) { showid('footp',0); }
	else { showid('nofootp',0); }
 }
 //else if (g_writing==2) { showid('foots',0); }
}

function movefootnoteprompt() {
}

function addfootnote() {
 if (!g_feedbackon) { return; }
 g_writing=2;
 document.getElementById('footnotecatch').focus();
 showid('footnoteblock',1);
 showid('footnotehead',1);
 showid('footnotetext',1);
 showid('footp',0);
 document.getElementById('footnoteblock').style.color="#FFFFFF";
 document.getElementById('footnotehead').innerHTML='enter a footnote';
 document.getElementById('doneblock').style.color="#FFFFFF";
 //showid('foots',0);
 typefootnote();
 forcerefresh();
}

var g_typefootforce=0;

function typefootnote(id) {
 if (g_writing==2) {
	if (!id) { id='footnotecatch'; }
	pre = cleanusertext(document.getElementById(id).value,255);
	document.getElementById('footnotetext').innerHTML=pre;
	if (pre.length) { showid('foots',1); }
	else { showid('foots',0); }
	g_typefootforce++;
	setTimeout('if (g_typefootforce=='+g_typefootforce+') { forcerefresh(); }',500);
 }
}

function tryfootnotecatch() {
 if (g_writing==2) { document.getElementById('footnotecatch').focus(); }
 else if (g_writing==3) { document.getElementById('yoursig').focus(); }
}

var g_user="";

function get_usersign() { return g_user; }
function set_usersign(u) {
 u=cleanusertext(u,50);
 g_user=u;
 document.getElementById('usersign').value=u;
 if (u) {
  showid('footsignclear',1);
  document.getElementById('footsign').innerHTML='post as '+u;
 }
 else {
  showid('footsignclear',0);
  document.getElementById('footsign').innerHTML='sign footnote';
 }
}

function savefootnote() {
 if (!get_usersign()) {
  try_to_sign(1);
 }
 else {
  writefootnotetodb();
  donewithfootnote(1);
  //blinkdone();
 }
}

var g_signandsubmit=0;

function try_to_sign(andsubmit) {
 if (andsubmit==undefined || !andsubmit) { andsubmit=0; }
 else { andsumbit=1; }
 var adone=new Array('change signature','sign & post');
 document.getElementById('donesigning').innerHTML=adone[andsubmit];
 g_signandsubmit=andsubmit;
 stopblinking();
 g_writing=3;
 showid('footsigninput',1);
 showid('foots1',0);
 document.getElementById('yoursig').focus();
}

function donesigning() {
 var u = document.getElementById('yoursig').value;
 document.getElementById('yoursig').value='';
 if (u) { set_usersign(u);
  if (g_signandsubmit && get_usersign()) { savefootnote(); }
 }
 else { set_usersign('');
 }
 closeoutsign();
}

function closeoutsign() {
 g_writing=2;
 showid('footsigninput',0);
 showid('foots1',1);
 startblinkdone();
}

function cleanusertext(t,maxlen) {
 t=t.replace(/[^a-z0-9 _\.\,\?\!\;\"\'\#\@\:\[\]\&\*\%\^\-\+\(\)\$]/gi,"");
 if (maxlen!=undefined && maxlen>0) { t=t.substr(0,maxlen); }
 return t;
}

function cleanthisinput(id) {
 if (document.getElementById(id) && document.getElementById(id).value) {
  document.getElementById(id).value=cleanusertext(document.getElementById(id).value);
 }
}

function donewithfootnote(dontwipe) {
 showid('foots',0);
 g_writing=0;
 document.getElementById('footnotehead').innerHTML='';
 document.getElementById('footnoteblock').style.color="#999999";
 document.getElementById('doneblock').style.color="#999999";
 if (!dontwipe) { clearfootnotetext(); }
 showid('footnotetext',0);
}

function clearfootnotetext() {
 document.getElementById('footnotecatch').value='';
 document.getElementById('footnotetext').innerHTML='';
 closeoutsign();
 showid('foots',0);
 forcerefresh();
}

function concatfootnote(b) {
 document.getElementById('footnotecatch').value="";
 document.getElementById('footnotetext').innerHTML="";
 writetoscreech(b,g_content[0],1);
}

function hidefootnote() {
 showid('footnoteblock',0);
 showid('footnotehead',0);
 document.getElementById('doneblock').style.color="#008800";
}



function zindexpush(id,sh) {
 document.getElementById(id).style.zIndex=sh;
}

function screenscroll(dir) {
 if (dir%2==0) { document.getElementById('screech').style.top--; }
 else { document.getElementById('screech').style.top++; }
}

function hepme(id) {
 alert(document.getElementById(id).innerHTML);
}

var g_debugcount=0;
function debug(msg) {
 if (document.getElementById('debug')) {
	g_debugcount++;
   document.getElementById('debug').innerHTML=g_debugcount+" "+msg+'<br/>'+document.getElementById('debug').innerHTML;
   if (document.getElementById('debug').innerHTML.length>300) {
		document.getElementById('debug').innerHTML=document.getElementById('debug').innerHTML.substr(0,500);
	}
 }
}

//////////////

function writefootnotetodb() {
 document.getElementById('gateway').innerHTML='<iframe style="display:none; width:0px; height:0px" id="populate" src="ruby.php?t='+g_content[0]+'&n='+g_content[1]+'&footnote=1"></iframe>';
}



//////////////////	for caching..


