// *************************************************************
function ______HIGHLIGHTING______ () {}

// Initially set to false by InitializeHighlighting, then set to true if the hl() function is called; if it's false when we leave, no need to update highlighting info in UpdateHighlighting
var hlChanged;

// Highlighting color; make this user selectable?
var hlColor = "#FFCC66";	// "#CCCCFF";		

// The following array holds an array with a value for each section
// that has highlighted spans; if it isn't null, the array element is another array 
// with an element for each span in the page
var hlSpans = new Array();

// Called when a page is first loaded. If something on the page was highlighted before,
//    re-highlight the appropriate spans
function InitializeHighlighting () {
	// set hlChanged to false, so we won't re-store the data when we leave the page
	hlChanged = false;
	
	// If hlSpans[curSection] is null, there's no highlighting
	if (hlSpans[curSection] == null) {
		return;
	}
	
	// Go through all the spans for this page
	var a = hlSpans[curSection];
	var i = 0;
	var s;
	// KC: The line while ((s = main.document.getElementById("sh" + i)) != null) assumes that span ids start at 0 -- but what if they don't? (They don't in reflectrelate1e.)
	// We need to find the minimum span id on the page.  Let's assume some large but not too large number
	var min = 0;
	var k;
	for (k = 0; k < 1000; k++) {
		if (main.document.getElementById("sh" + k) == null) {
			min = k + 1;
		} else {
			break;
		}
	}
	i = min;		
	while ((s = main.document.getElementById("sh" + i)) != null) {
		if (a[i]) {
			s.style.backgroundColor = hlColor;
		}
		++i;
	}
}

// hl: highlight or de-highlight a sentence.  
// Called by ondblclick handlers of span elements in sections
var hlWarningIssued = false;
function hl(index) {
	// If the user is establishing a StickyNote, call the appropriate function
	// (see ebookNotesAPI)
	if (stickyNoteBeingEstablished == true) {
		stickyNoteBeingEstablished = false;
		PlaceStickyNote(index);
		return;
	}
	
	// If the user won't be able to save notes, issue a warning
	if (username == 'guest' && !hlWarningIssued) {
		alert ('You may highlight the text while you\'re using the eBook in Preview mode, but the highlighting information will not be recorded permanently -- as soon as you close the Textbook Window, your highlighting (and notes and bookmarks) will be lost.');
		hlWarningIssued = true;
	}

	// We're going to change something, so make sure we save the changes when we leave the page
	hlChanged = true;
	
	// If the curSection doesn't have a hlSpans element defined yet, do so
	if (hlSpans[curSection] == null) {
		hlSpans[curSection] = new Array();
	}
	
	var s = main.document.getElementById("sh" + index).style; 
	
	// if the indexed span is transparent, change it to the highlight color, or vice-versa
	if (s.backgroundColor == "transparent" || s.backgroundColor == '' || s.backgroundColor == null) {
		s.backgroundColor = hlColor;
		hlSpans[curSection][index] = true;
	} else {
		s.backgroundColor = "transparent";
		hlSpans[curSection][index] = null;
	}
	
	return false;		// by returning false, we cancel further processing of the double click, thereby preventing the clicked word from being selected (at least in Mac IE5)
}

var startCode = "@".charCodeAt(0);
function IndexToHLCode(i) {
	var hi = Math.floor(i / 27);
	var lo = i % 27;
	return String.fromCharCode(startCode + hi, startCode + lo);
}

function HLCodeToIndex(code) {
	var hi = code.charCodeAt(0) - startCode;
	var lo = code.charCodeAt(1) - startCode;
	return hi * 27 + lo;
}

// Build the hlCodeString for the section, recorded in hlSection.
// We'll send the info to the server the next time InitializeSection or Logout are called.
var hlCodeString, hlSection;
function RecordHighlighting() {
	// Return if nothing was changed
	if (!hlChanged) {
		hlSection = 0;
		return;
	}

	// Pack the hlSpans array for this page into chars
	hlSection = curSection;
	var a = hlSpans[hlSection];
	hlCodeString = "";	
	
	// Go through each element in the array
	for (var i = 0; i < a.length; ++i) {
		// If the span is set
		if (a[i]) {
			// Add to the hlCodeString
			hlCodeString += IndexToHLCode(i);
		}
	}
}

function ImportHighlighting(section, code) {
	// Create a new array for the section
	hlSpans[section] = new Array();
	var a = hlSpans[section];
	
	// Go through each pair of letters in the code
	for (var i = 0; i < code.length; i += 2) {
		// And set the corresponding element of the array
		a[HLCodeToIndex(code.substr(i, 2))] = true;
	}
}

// Clear all highlighting from the current page
function ClearHighlighting() {
	// Close the pop-in window used to call this function
	ClosePopInWindow_ebook();
	
	// Get a reference to the hlSpans array for this page
	var a = hlSpans[curSection];
	
	// If there is no array, there's no highlighting to clear, so return
	if (a == null) {
		return;
	}
	
	// Go through the array, de-highlighting everything
	for (var i = 0; i < a.length; ++i) {
		// But only do anything for non-empty array elements
		if (a[i] != null) {
			a[i] = null;
			main.document.getElementById("sh" + i).style.backgroundColor = "transparent";
		}		
	}
	
	// Set hlChanged to true, so that when we leave the page we'll record the changes
	hlChanged = true;	
}


