// Support functions and classes for toc

// Functions for manipulating the tocID string
// Get ID first child by adding ".1" to tocID
function getTOCIDFirstChild(tocID) {
	return tocID += ".1";
}

// Get ID for next sibling by incrementing number behind rightmost dot
function getNextTOCID(tocID) {
	var dot = tocID.lastIndexOf(".");
	var nextNumber = Number(tocID.substring(dot + 1)) + 1;
	tocID = tocID.substring(0, dot + 1) + nextNumber.toString();
	return tocID;
}

// Get ID next parent by stripping the rightmost number from tocID
// and incrementing this parent ID
function getNextParentTOCID(tocID, numberOfLevelsDown) {
	for (var i = 0; i < numberOfLevelsDown; i++)
		tocID = tocID.substring(0, tocID.lastIndexOf("."));
	return getNextTOCID(tocID);
}

// TOCItems class
// Add a new TOCItem to tocItems
function TOCItems_addTOCItem(tocID, title, url, target) {
	var tocItem = new  TOCItem(tocID, title, url, target);
	this[this.length] = tocItem;
	var level = tocItem.getLevel();
	if (level + 2 > this.numberOfCols)
		this.numberOfCols = level + 2;
}

function TOCItems_getItemByID(id) {
	if (id) {
		for (var i = 0; i < this.length; i++) {
			var item = 	this[i];
			if (item.id == id)
				return item;
		}
	}
	return null;
}

function TOCItems_getItemIDByURL(url) {
    var id = '0';
    for (var i = 0; i < this.length; i++) {
        if (this[i].url == url) {
            id = this[i].id;
            break;
        }
    }
    return id;
}

var widthImgCol = 16;
var heightImgCol = 16;

function _GetLeft(Col) {
	return (Col * widthImgCol) + 8;
}

function _GetTop(Row) {
	return (Row * heightImgCol) + 8;
}

function TOCItems_getRow(tocItemIndex, rowNumber) {
	var tocItem = tocItems[tocItemIndex];
	var tocLevel = tocItem.getLevel();
	var row = "";

	// Add leading space and vertical line
	for (var i = 1; i < tocLevel; i++) {
		var testID = tocItem.id.substring(0, 2 * i - 1);
		testID = getNextTOCID(testID);
		if (tocItems.getItemByID(testID) != null) {
			row += '<img class="toc-image" style="position: absolute; left: ' + _GetLeft(i -1)
		    	+ 'px; top: ' + _GetTop(rowNumber) + 'px;" src="images/vertical.gif" alt=""/>';
		}
	}
	
	// Select image type leaf, minus or plus
	var sign, image;
	if ((tocItemIndex == tocItems.length - 1)
	|| (tocLevel >= tocItems[tocItemIndex + 1].getLevel())) {
		sign = "leaf"; image = "page"; 
	} else if (tocItem.expanded) {
		sign = "minus"; image = "book_open";
	} else {
	   sign="plus"; image = "book_closed";
	}
	
	var signDirection = '';
	if (tocItemIndex > 0)
		signDirection = '_up';
	if (tocItem.hasNextSibling()) {
		signDirection += '_down';
	}
		
	// Add images
	if (sign != "leaf") {
		row += tocItem.getAnchorStartTag(false, false, true);
	}
	row += '<img class="toc-image" style= "position: absolute; left: ' + _GetLeft(tocLevel - 1)
		+ 'px; top: ' + _GetTop(rowNumber) + 'px;" src="images/' + sign + signDirection + '.gif" alt=""/>'
	row += '<img class="toc-image" style= "position: absolute; left: ' + _GetLeft(tocLevel)
		+ 'px; top: ' + _GetTop(rowNumber) + 'px;" src="images/' + image + '.gif" alt=""/>';
	if (sign != "leaf") {
		row += '</a>';
	}
	
			 
	// Add description
    row += '<div class="toc-description" style= "position: absolute; left: ' + _GetLeft(tocLevel + 1)
         + 'px; top: ' + _GetTop(rowNumber) + 'px;">'
         + tocItem.getAnchorStartTag(true, tocItem == currentTOCItem, false);
    if (showNumbers)
       	row += tocItem.id + " ";
    row += tocItem.title + '</a></div>';
    
    return row;
}

var tocItems = new Array();
tocItems.getItemByID = TOCItems_getItemByID;
tocItems.getItemIDByURL = TOCItems_getItemIDByURL;
tocItems.getItemIDByURL = TOCItems_getItemIDByURL;
tocItems.getRow = TOCItems_getRow;
tocItems.addTOCItem = TOCItems_addTOCItem;
tocItems.numberOfCols = 1;

// TOCItem class
function TOCItem_expand() {
	this.expanded = true;
	if (this.parentItem)
		this.parentItem.expand();
}

function TOCItem_getAnchorStartTag(changeContent, isCurrent, mayCollapse) {
	var s = "<a href=\"javaScript:parent.showItem('"
			+ this.id + "'," + changeContent + "," + changeContent + "," +mayCollapse + ');"';
	if (isCurrent)
    	s += ' class="Current"';
	return s + ">";
}

function TOCItem_getLevel() {
 	return this.id.split(".").length;
}

function TOCItem_getParentID() {
	return this.id.substring(0, this.id.lastIndexOf("."));
}

function TOCItem_getParentItem() {
	return this.parentItem;
}

function TOCItem_getVisible() {
	if (this.parentItem)
		return (this.parentItem.expanded &&	this.parentItem.getVisible());
	else return true;
}

function TOCItem_hasNextSibling() {
	var idNextSibling = getNextTOCID(this.id);
	var nextSibling = tocItems.getItemByID(idNextSibling);
	return nextSibling != null;
}

function TOCItem(id, title, url, target) {
	this.id = id;
	this.title = title;
	this.url = url;
	this.target = target;
	this.expanded = false;
	this.expand = TOCItem_expand;
	this.getAnchorStartTag = TOCItem_getAnchorStartTag;
	this.getLevel = TOCItem_getLevel;
	this.getParentID = TOCItem_getParentID;
	this.getParentItem = TOCItem_getParentItem;
	this.getVisible = TOCItem_getVisible;
	this.hasNextSibling = TOCItem_hasNextSibling;
	this.parentItem = tocItems.getItemByID(this.getParentID())
}


