// *************************************************************
function ______BOOKMARKS______ () {}

var bookmarkWarningIssued = false;
function Bookmark() {
	if (username == 'guest' && !bookmarkWarningIssued) {
		alert('You may record bookmarks while you\'re using the eBook in Preview mode, but the bookmarks will not be recorded permanently -- as soon as you close the Textbook Window, your bookmarks (and highlighting and notes) will be lost.');
		bookmarkWarningIssued = true;
	}
	
	//alert("curSection in BookMark = " + curSection);
	if (sections[curSection].bookmarked) {		// page is currently bookmarked, so un-bookmark it
		sections[curSection].bookmarked = false;
	} else {						// page is currently not bookmarked, so bookmark it
		sections[curSection].bookmarked = true;
	}
	
	// Submit the bookmark to the server
	Servercom("storebookmark&section=" + curSection + "&bookmarked=" + sections[curSection].bookmarked);

	// re-write the bookmark menu
	WriteBookmarkMenu();
	
	// If the tools popin window is open, rewrite it so that the menu there is rewritten
	if (ToolWindowShowing()) {
		ShowEBookTools();
	}
	
	// and show or hide the bookmark icon appropriately
	SetBookmarkIcon();
}

function RegisterBookmark(s) {
	if (sections[s] == null) {
		return;
	}
	sections[s].bookmarked = true;
}

// It's a little tricky to refer to the bookmark menu, since we dynamically write it
// using bookmarkMenuDiv.innerHTML.  So define a function to return a reference to it.
function BookmarkMenuRef(where) {
	if (where == 'banner' && bannerrightF != null) {
		return bannerrightF.document.getElementById('bookmarkMenuDiv').childNodes[0];
	} else {
		return main.document.getElementById('bookmarkMenu');
	}
}

// Set the first item in the bookmark menu to say "remove" or "add" a bookmark, depending on 
// whether or not the current page is bookmarked
var bookmarkMenuWritten = false;		// set to true in the bannerbookmarks frame when the menu is done writing
// This function is called by InitializeSection in ebookNavigation.js
function UpdateBookmarkMenu() {
	//alert("curSection = " + curSection);
	//if (sections[curSection] == null) {
	//	return;
	//}
	// It appears that the bookmarks menu may sometimes still be in the process of being drawn when this function
	// is first called. If this is so, the remove/add item will be correctly set by WriteBookmarkMenu().
	if (bookmarkMenuWritten == true) {
		var theText;
		if (sections[curSection].bookmarked) {
			theText = 'Remove bookmark';
		} else {
			theText = 'Bookmark page';
		}

		var bbm = BookmarkMenuRef('banner');
		if (bbm != null) {
			bbm.options[2].text = theText;
		}
		var mbm = BookmarkMenuRef('main');
		if (mbm != null) {
			mbm.options[2].text = theText;
		}
	}
}

// show the bookmark icon if the current page is bookmarked
// This is called by function Bookmark
// Also by function InitializeSection in ebookNavigation.js
function SetBookmarkIcon() {
	
	//alert("curSection = " + curSection);
	if (sections[curSection].bookmarked) {
		SetDisplay(main.document.getElementById('bmicon'), 'block');
	} else {
		SetDisplay(main.document.getElementById('bmicon'), 'none');
	}
}

var bookmarktitle_wo_sectnum = false; //sm: global var set in WriteBookmarkMenu() used in BookmarkMenu()
// Write the bookmarks menu in the bannerrightF frame.
function WriteBookmarkMenu() {
	// set bookmarkMenuWritten to false so that if UpdateBookmarkMenu gets called during this function's execution,
	// it won't do anything.
	bookmarkMenuWritten = false;
	
	// If we have a bannerrightF (we won't in Angel), write it.
	if (bannerrightF != null) {
		// sm -----------------------------
		switch (bookId)
		{				// in sections.js bt: only space between sect_num and bookmark title, replace ' ' with &nbsp; in toc_functions.pl
			case "listen6e":
				bookmarktitle_wo_sectnum = true;
				break;
				
			default:
				bookmarktitle_wo_sectnum = false;
				break;
		}
		// sm -----------------------------
			
		bannerrightF.document.getElementById('bookmarkMenuDiv').innerHTML = BookmarkMenu('banner');
	}
	
	// Set bookmarkMenuWritten to true so the menu can be updated.
	bookmarkMenuWritten = true;
}

function BookmarkMenu(where) {	
	var ih = '<select '
	
	if (where == 'angelToolbar') {
		ih += 'id="bookmarksControl"';
	} else {
		ih += 'style="font-family:Verdana; font-size:12px" name="bookmarks" id="bookmarkMenu"';
	}
	
	ih += 'onchange="top.JumpToBookmark(\'' + where + '\')">'
		+ '<option value="-1" selected>Bookmarks</option>'
		+ '<option value="-1">-------------</option>';

	// write the "remove/add" option for the open page
	if (curSection != null && sections[curSection] != null && sections[curSection].bookmarked) {
		ih += '<option value="-2">Remove bookmark</option>';
	} else {
		ih += '<option value="-2">Bookmark page</option>';
	}

	ih += '<option value="-1">-------------</option>';

	// go through each section of the text in turn, and write a menu item if the page is bookmarked
	for (var i = 1; i < sections.length; ++i) {
		// only write the bookmark if there is a page and it's bookmarked
		if (sections[i] != null && sections[i].bookmarked) {
			ih = ih + '<option value="' + i + '">';
			if (IsCustomSection(i)) {
				ih += "Custom Section: " + CustomSectionTitle(i).substr(0,15);
			} else if (commentStyle) {
			    // KC: St. Martin's Guide is a comment-style eBook that does not
			    // use the jt array
			    // KC: note: tap4e is quasi-commentStyle, i.e. commentStyle is set to 1 even thought it's not in Comment
			    if (bookId == 'theguide8e' || bookId == 'rules6e' || bookId == 'writersref6e' || bookId == 'bedguide8e' || bookId == 'tap4e' || bookId == 'tap4v1' || bookId == 'tap4v2'|| bookId == 'hunt3v1' || bookId == 'hunt3v2' || bookId == 'tapdocs4e') {
			    	if (sections[i].bt != null) {
			    		ih += sections[i].bt;
			    	}
			    // KC: not really sure why we're not just using bt for all Bedford books
			    } else if (sections[i].jt != null && sections[i].jt.length < 4) {
					ih += "Section " + sections[i].jt;
				} else {
					ih += sections[i].jt;
				}
			// for ASG and probably other BSM books, section titles, not sec numbers (since there
			// are none) must appear in bookmarks
			// let's use a 'bt' (bookmark title) attribute on sections.js
			} else if (sections[i].bt != null) {
				ih += sections[i].bt;
			// KC: actually i == 1 is Title Page, i == 2 is Table of Contents
			// in the default; in BSM books, which often lack one or the other,
			// this may differ
			} else if (i == 2) {
				ih += "Table of Contents";
			} else if (i == 1) {
				ih += "Title Page";
			} else {
				ih = ih + 'Section ' + SectionNumber(i);
			}
			ih += "</option>";
		}
	}
	ih += '</select>';

	return ih;
}

function JumpToBookmark(where) {
	if (where == null) {
		where = 'banner';
	}
	
	var s = BookmarkMenuRef(where);
	var newIndex = s.options[s.selectedIndex].value;
	
	s.options[0].selected = true;
	if (newIndex == -1 || newIndex == curSection) {
		return;
	} else if (newIndex == -2) {
		Bookmark();
	} else {
		LoadSection(newIndex);
	}
}


