// hilite.js
// Copyright (c) 2007, 2009 by Gary L. Bradshaw.  All rights reserved
//
var taggedSentences = new Array ();
var pageNum;

/* We are using cookies, which are limited to 4K bytes in toto.  To maximize the number of
   codes we are using, we are packing the data into bytes, which is tricky because we can't use
   a bunch of the char codes in a cookie.  But we can get enough for now!
	
	Encoding
	  num += 33; (move to beyond the space char)
	  if (num > 43) num++; (shifts past the comma char)
	  if (num > 58) num++;
	  
	Decoding
	  if (num > 58) num--;
	  if (num > 43) num--;
	  num -= 33;
	  
	  This might provide as many as 125-33 = 92 options!
	  
   */

// 	I tried to use the length of the taggedSentences array, but when we delete items, the length does not decrease

var numTags = 0;
var numPageTags = 0;

function encodeVal(myNum)
  { myNum += 35; // shift beyond quote
	if (myNum > 43) myNum++;
	if (myNum > 58) myNum++;
	return (myNum);
  }
  
function decodeVal(myNum)
  { if (myNum > 58) myNum--;
	 if (myNum > 43) myNum--;
	 myNum -= 35;
	 return(myNum);
  }
  
function gTFC (cookieName) // gTFC is short for "getTagsFromCookies"
  { var allCookies = document.cookie;
	var cookieEndsAt;
	var cookieValue;
  	var i;
	var t1, t2, t3, t4, br;
	var fieldStart, fieldEnd;

	// Here we set the page number so it will be there for the rest of the functions
  	var frameURL = document.URL;
    var slashLocation = frameURL.lastIndexOf('/') + 1;
    var pageName = unescape(frameURL.substring(slashLocation, frameURL.length));
    pageNum = parseInt(pageName);
	
    var cookieStartsAt = allCookies.indexOf(" " + cookieName + "=");
    if (cookieStartsAt == -1)
      { cookieStartsAt = allCookies.indexOf(cookieName + "=");
      }
    if (cookieStartsAt == -1)
      { var cookieValue = null;
      }
    else
     { cookieStartsAt = allCookies.indexOf("=", cookieStartsAt) + 1;
       cookieEndsAt = allCookies.indexOf(";", cookieStartsAt);
       if (cookieEndsAt == -1)
         { cookieEndsAt = allCookies.length;
	     }

       cookieValue = unescape (allCookies.substring(cookieStartsAt, cookieEndsAt));
	   //numTags = 0;
	   while (cookieValue.length > 0) // indexOf(";") > 0)
	     { t1 = parseInt(decodeVal(cookieValue.charCodeAt(0)));
	   	   t2 = parseInt(decodeVal(cookieValue.charCodeAt(1)));
		   t3 = parseInt(decodeVal(cookieValue.charCodeAt(2)));
		   t4 = parseInt(decodeVal(cookieValue.charCodeAt(3)));
		   if (t4 > 127)
		   	  { br = true;
			  	t4 -= 127;
			  }
		   cookieValue = cookieValue.substring(4, cookieValue.length);
		   taggedSentences[numTags++] = new tagMe(t1,t2,t3,t4);
		   if (t1 == top.moduleNumber && t2 == top.modulePart && t3 == pageNum)
		     {	var tid = "t"+t4;
				numPageTags++;

				if (br) document.getElementById(tid).className="brightText";
				else document.getElementById(tid).className="hiliteText";
			 }
		 }
     }
  }

function saveTagsInCookie (cookieName)
  { var t1, t2, t3, t4;
  	// We set the cookie to expire two years from now
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear()+2);
    var cookieExpires = "; expires="+nextYear.toGMTString();

    // Set the path to refer to any page on this server
    var cookiePath = '; path=/';

    var cookieString = cookieName + "=";
	for (i = 0; i < numTags; i++)
	  { if (taggedSentences[i] == null)
	  	  {	continue;
		  }
	  	t1 = encodeVal(taggedSentences[i].module);
	  	t2 = encodeVal(taggedSentences[i].part);
		t3 = encodeVal(taggedSentences[i].page);
		t4 = encodeVal(taggedSentences[i].eid);
		cookieString += String.fromCharCode(t1, t2, t3, t4);
	  }
	cookieString += cookieExpires + cookiePath;
    document.cookie = cookieString;
}    

function tagMe (module, part, page, eid)
  { this.module = module;
  	this.part = part;
	this.page = page;
	this.eid = eid;
  }

function fT (eid) // focus text
  { var tid = "t" + eid;
	if (document.getElementById(tid).className!="hiliteText")
  		document.getElementById(tid).className="focusText";
  }
  
function ufT (eid) // unfocus text
  { var tid = "t" + eid;
	if (document.getElementById(tid).className=="focusText") // Otherwise leave it alone
  	  document.getElementById(tid).className="normalText";
  }

function hT (eid) //highlight text
  { // First see if it is already highlighted.  If so, switch off
    var i, j;
	var tid = "t"+eid;
	// Here we set the page number so it will be there for the rest of the functions
  	var frameURL = document.URL;
    var slashLocation = frameURL.lastIndexOf('/') + 1;
    var pageName = unescape(frameURL.substring(slashLocation, frameURL.length));
    pageNum = parseInt(pageName);
	
	if (document.getElementById(tid).className =="hiliteText")
	  {	document.getElementById(tid).className="normalText";
	    for (i = 0; i < numTags; i++)
	  		{ if (taggedSentences[i].module == top.moduleNumber && taggedSentences[i].part == top.modulePart && 
					taggedSentences[i].page == pageNum && taggedSentences[i].eid == eid) // Unhilight
	  	  		{ j = i + 1;
				  while ( j < numTags)
		  	  		{ taggedSentences[i++] = taggedSentences[j++];
					}
				  taggedSentences[i] = null;
				  numTags--; numPageTags--;
				  saveTagsInCookie(top.worldCookie);
				  return; 
			    }
			}
	  }
	else // Not currently highlighted
	  {	i = 0;
		document.getElementById(tid).className="hiliteText";
		if (top.bottomFrame != null && top.bottomFrame.reportResults != null)
			top.bottomFrame.reportResults("Module: " + top.moduleNumber + " Part: " + top.modulePart + " Page: " + pageNum + " Sentence: " + eid + " tagged");

		while (i < numTags)
	  		{ if (taggedSentences[i].module < top.moduleNumber || taggedSentences[i].part < top.modulePart || 
					taggedSentences[i].page < pageNum || taggedSentences[i].eid < eid) i++;
			  else if (i < numTags - 1)
		  		{ j = numTags-1;
				  taggedSentences[numTags] = new tagMe(taggedSentences[j].module, taggedSentences[j].part,
													  taggedSentences[j].page, taggedSentences[j].eid);
				  while (j > i)
			 		{ taggedSentences[j] = taggedSentences[j-1]; j--;
					}
				  taggedSentences[i] = new tagMe(top.moduleNumber, top.modulePart, pageNum, eid);
				  numTags++;  numPageTags++;
				  saveTagsInCookie(top.worldCookie);
				  return; 
			 	}
			  else
		  	    { 	taggedSentences[i] = new tagMe(top.moduleNumber, top.modulePart, pageNum, eid);
					numTags++;	numPageTags++;
					saveTagsInCookie(top.worldCookie);
					return; 
		  		}
			}
		  taggedSentences[i] = new tagMe(top.moduleNumber, top.modulePart, pageNum, eid);
		  numTags++; numPageTags++;
		  saveTagsInCookie(top.worldCookie);
		  return;
		}
  }
 
