/* standard NavJS overrides for Accordion nav */

/* Impl-dependent Configuration */
var SLOW_DURATION = 250;
var HREF = parent.window.location.href;

var EXPAND_COUNT_CONTEXT = 0; // was boolean, but turns out that it must run 2x at instant

// private
// this override accounts for the fact that top-level LIs have the A nested inside a DIV
NavItem.prototype.getChildA = function() {
  	var myKids = this.li.childNodes;

	if(!this.isToplevel) {
		for(var x=0; x<myKids.length; x++) {
			if(myKids[x].tagName=="A") {
				return myKids[x];
			}
		}
	} else {
		// top-level override
		var childDiv = this.li.childNodes[0];
		if(childDiv.tagName!="DIV") alert('Accordion override of getChildA failed to find child DIV of toplevel LI: ' + this.li.id);
		myKids = childDiv.childNodes;

		for(var x=0; x<myKids.length; x++) {
			if(myKids[x].tagName=="A") {
				return myKids[x];
			}
		}

		alert("Couldn't find child A of DIV for: " + this.li.id);
	}
}

// private
// this override accounts for the fact that top-level LIs have the A nested inside a DIV, and the DIV should have the same mouse behavior as the LI and A. Naturally, event bubbling would be best, but I couldn't control it.
NavItem.prototype.getChildDiv = function() {  	
	if(!this.isToplevel) {
		return false; // todo: handle possibility that non-top-level items may use DIV approach
	} else {
		// top-level override
		var childDiv = this.li.childNodes[0]; // todo: this only works if DIV is the first child. But, what if it looked like this: <li><span>small text</span><div>...
		if(childDiv.tagName!="DIV") alert('Accordion override of getChildA failed to find child DIV of toplevel LI: ' + this.li.id);

		return childDiv;
	}
}


function getPageId() {
	var pageID = HREF.toLowerCase();
	var iSep = pageID.lastIndexOf("/");
	var iDot = pageID.lastIndexOf(".");
	pageID = pageID.substring(iSep+1,iDot);

	return pageID;
}

function getPageIndex() {
	return pageMap[getPageId()];
}

function initSelected() { // todo: adding ofSelected class should be part of NavItem oop
	EXPAND_COUNT_CONTEXT = 0;
	var li = findParentLI(PAGE_ID.toLowerCase());
	addClass(li,"ofSelected");
}

function findParentLI(fileNameOfHrefLink) { // todo: what if the path is a servlet; i.e., no filename

	var allLinks = document.getElementsByTagName("A");

	for(x=0;x<allLinks.length; x++) { // todo: there could be multiple A's on page with same link, but only one will have the parent LI
		var a = allLinks[x];
		// SPM: fixed bug for anchor tags without ID attributes
		if (a.getAttribute("id")) { 
		// SPM: Changed this to use the ID attribute since multiple pages under the same
		// TOC heading will need the same TOC element highlighted.  This is why there are
		// addtional A tags within particular LI elements in the TOC.
		
		var id = a.getAttribute("id").toLowerCase();
		
		if(id.indexOf(fileNameOfHrefLink)!=-1) { // todo: could get false positive -- should look just at the end of string
			var li = findAncestor(a, "LI");
			return li;
			}
		}
	}
}

// When accordion is stickyState, we want the accordion to load
// with the particular toplevel nav already open.
// To emulate, set duration of expand to 0
function isExpanding(inProgressTopLevelItemIndex, initIndex) {
	if(inProgressTopLevelItemIndex==initIndex) {
		return true;
	}
	else return false;
}


// obj.el = UL
/*function getSlideDuration(obj) {
	var topLevelItem = findTopLevelItem(obj.el);

	if(isExpanding(topLevelItem.itemIndex, INIT_TOPLEVEL_INDEX) && EXPAND_COUNT_CONTEXT<=1) {
		//alert("will do 0 for: " + topLevelItem.li.id);
		EXPAND_COUNT_CONTEXT++;
		return SLOW_DURATION;
	} else {
		return SLOW_DURATION; // todo: clean up and externalize these two values
	}
}
*/


function getSlideDuration(obj) {
	var topLevelItem = findTopLevelItem(obj.el);

	if(isExpanding(topLevelItem.itemIndex, INIT_TOPLEVEL_INDEX)) {
	    if(EXPAND_COUNT_CONTEXT<=1) {
		    //alert("will do 0 for: " + topLevelItem.li.id);
		    EXPAND_COUNT_CONTEXT++;
		    return 0;
		} else {
		    return SLOW_DURATION;
		}
	} else {
		return SLOW_DURATION; // todo: clean up and externalize these two values
	}
}

