// JavaScript Document

var xmlDoc;


function importXML() {
	if ( document.implementation && document.implementation.createDocument ) {
		xmlDoc = document.implementation.createDocument("", "", null);
  		xmlDoc.onload = buildNav;
  		xmlDoc.load("/naturallyspeaking/includes/xml/userGuide.xml");
	} else if ( window.ActiveXObject ) {
		xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
		xmlDoc.onreadystatechange = function () {
			if ( xmlDoc.readyState == 4 ) {
				buildNav();
			}
		}
		
		xmlDoc.load("/naturallyspeaking/includes/xml/userGuide.xml");
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
}
	
function buildNav() {
	//alert("BN");
	var chapterPosition = null;
	
	var url = window.location;
	
	var position = url.toString().lastIndexOf( "/" );
	var thisPage = url.toString().slice( position + 1 );
	var thisChapter = url.toString().slice( 0, position );
	thisChapter = thisChapter.slice( thisChapter.lastIndexOf( "/" ) + 1 );
	
	//alert("thisChapter: " + thisChapter);
	var chapters = xmlDoc.documentElement.getElementsByTagName( 'chapter' );
	//alert("chapters.length: " + chapters.length);
	
	chapterPosition = findPosition( chapters, "dir", thisChapter )
	
	var pages = chapterPosition.xmlNode.getElementsByTagName( 'page' );
	//alert("page.length: " + pages.length);
	
	pagePosition = findPosition( pages, "link", thisPage );
			
	chapterName = chapterPosition.xmlNode.attributes.getNamedItem( "title" ).nodeValue;
	addCont = "<div style=\"float:left; width:74%\"><p><strong><a href=\"../../userGuide.asp\">TABLE OF CONTENTS</a></strong> | <strong>" + chapterName + "</strong></p></div><div style=\"float:right; width:24%\">";
	
	//alert("addCont: " + addCont);
	
		
		
	lastPage = getPage( chapters, chapterPosition, pages, pagePosition, -1 );
	nextPage = getPage( chapters, chapterPosition, pages, pagePosition, 1 );
	
	//alert("lastPage: " + lastPage);
	//alert("nextPage: " + nextPage);
		
	if ( lastPage ) {
		addCont += "<p><a href = \"" + lastPage + "\"><< Last</a> | ";
	} else {
		addCont += "<p><< Last | ";
	}
		
	if ( nextPage ) {
		addCont += "<a href = \"" + nextPage + "\">Next >></a></p></div>";
	} else {
		addCont += "Next >></p></div>";
	}
	
	document.title = "Nuance - " + xmlDoc.documentElement.attributes.getNamedItem( "title" ).nodeValue +
	" - " + pagePosition.xmlNode.attributes.getNamedItem( "title" ).nodeValue;
	
	
	document.getElementById("thisTopNav").innerHTML = addCont;
		
	if ( document.getElementById("thisContent").offsetHeight > 650 ) {
			document.getElementById("thisBottomNav").innerHTML = addCont;
	}
}
	
function findPosition( xmlNode, att, val ) {
	
	var tempObj = new Object();
	for ( var i = 0; i < xmlNode.length; i++ ){
		tempObj.xmlNode = xmlNode[ i ];
		tempObj.i = i;
		if ( tempObj.xmlNode.attributes.getNamedItem( att ).nodeValue == val ) {
			return tempObj;
		}
	} 
}
	
function getPage( c, cp, p, pp, dir ) {
	
	//alert( "GP" );
	//alert( "c.length: " + c.length );
	//alert( "cp.i: " + cp.i );
	//alert( "p.length: " + p.length );
	//alert( "pp.i: " + pp.i );
	//alert( "dir: " + dir );
	
	if( (pp.i + dir) < p.length && (pp.i + dir) > -1 ){
		//alert( "in if" );
		//alert( "pp.i + dir = " + (pp.i + dir) );
		//alert( "p.length = " + p.length );
		thisNode = p[ pp.i + dir ];
		var chapName = cp.xmlNode.attributes.getNamedItem( "dir" ).nodeValue;
		var pageName = thisNode.attributes.getNamedItem( "link" ).nodeValue;
		var tempURL = "../" + chapName + "/" + pageName;
		//alert("if tempURL: " + tempURL );
		return tempURL;
	}else if( (cp.i + dir) < c.length && (cp.i + dir) > -1 ){
		//alert( "in else" );
		var i = 0;
		if ( dir == -1 ) {
			//alert( "dir = -1" );
			i = c[ (cp.i + dir) ].getElementsByTagName( 'page' ).length - 1;
			//alert( "i: " + i );
		}
		var chapName = c[ (cp.i + dir) ].attributes.getNamedItem( "dir" ).nodeValue;
		//alert( "chapName: " + chapName );
		//alert( "new pages.length: " + c[ (cp.i + dir) ].getElementsByTagName( 'page' ).length );
		var pageName = c[ (cp.i + dir) ].getElementsByTagName( 'page' )[ i ].attributes.getNamedItem( "link" ).nodeValue;
		//alert( "pageName: " + pageName );
		var tempURL = "../" + chapName + "/" + pageName;
		
		//alert("else if tempURL: " + tempURL );
		return tempURL;
	}
	
	//alert("null");
	return null;
}


//Old
/*function importXML() {
	if ( document.implementation && document.implementation.createDocument ) {
		xmlDoc = document.implementation.createDocument( "", "", null );
		xmlDoc.onload = createTable;
	} else if ( window.ActiveXObject ) {
		xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
		xmlDoc.onreadystatechange = function () {
			if ( xmlDoc.readyState == 4 ) {
				buildNav();
			}
		}
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load("/naturallyspeaking/includes/xml/userGuide.xml");
}
	
function buildNav() {
	var chapterPosition = null;
	var url = window.location;
	var position = url.toString().lastIndexOf( "/" );
	var thisPage = url.toString().slice( position + 1 );
	var thisChapter = url.toString().slice( 0, position );
	thisChapter = thisChapter.slice( thisChapter.lastIndexOf( "/" ) + 1 );
		
	chapterPosition = findPosition( xmlDoc.documentElement, "dir", thisChapter )
	pagePosition = findPosition( xmlDoc.documentElement.childNodes[ chapterPosition ], "link", thisPage );
			
	chapterName = xmlDoc.documentElement.childNodes[ chapterPosition ].attributes.getNamedItem( "title" ).nodeValue;
	addCont = "<div style=\"float:left;\"><p><strong><a href=\"/naturallyspeaking/resources/userguide.asp\">TABLE OF CONTENTS</a></strong> | <strong>" + chapterName + "</strong></p></div><div class='dynNav'>&nbsp;|&nbsp; ";
		
		
	lastPage = getPage( chapterPosition, pagePosition, -1 );
	nextPage = getPage( chapterPosition, pagePosition, 1 );
		
	if ( lastPage ) {
		addCont += "<< <a href = \"" + lastPage + "\">Last</a> | ";
	} else {
		addCont += "<< Last  | ";
	}
		
	if ( nextPage ) {
		addCont += "&nbsp;<a href = \"" + nextPage + "\">Next</a> >> </div>";
	} else {
		addCont += " | Next >></div>";
	}
	
	document.title = "Nuance - " + xmlDoc.documentElement.attributes.getNamedItem( "title" ).nodeValue +
	" - " + xmlDoc.documentElement.childNodes[ chapterPosition ].childNodes[ pagePosition ].attributes.getNamedItem( "title" ).nodeValue;
	
	document.getElementById("thisTopNav").innerHTML = addCont;
		
	if ( document.getElementById("thisContent").offsetHeight > 650 ) {
			document.getElementById("thisBottomNav").innerHTML = addCont;
	}
}
	
function findPosition( xmlNode, att, val ) {
		
	for ( var i = 0; i < xmlNode.childNodes.length; i++ ){
		var thisNode = xmlNode.childNodes[ i ];
		if ( thisNode.attributes.getNamedItem( att ).nodeValue == val ) {
			return i;
		}
	} 
}
	
function getPage( c, p, dir ) {
		
	if ( xmlDoc.documentElement.childNodes[ c ].childNodes[ p + dir ] ) {
		var chapName = xmlDoc.documentElement.childNodes[ c ].attributes.getNamedItem( "dir" ).nodeValue;
		var pageName = xmlDoc.documentElement.childNodes[ c ].childNodes[ p + dir ].attributes.getNamedItem( "link" ).nodeValue;
		var tempURL = "../" + chapName + "/" + pageName;
		return tempURL;
	} else if ( xmlDoc.documentElement.childNodes[ c + dir ] ) {
		var i = 0;
		if ( dir == -1 ) {
			i = xmlDoc.documentElement.childNodes[ c + dir ].childNodes.length - 1;
		}
		var chapName = xmlDoc.documentElement.childNodes[ c + dir ].attributes.getNamedItem( "dir" ).nodeValue;
		var pageName = xmlDoc.documentElement.childNodes[ c + dir ].childNodes[ i ].attributes.getNamedItem( "link" ).nodeValue;
		var tempURL = "../" + chapName + "/" + pageName;
		return tempURL;
	}
	return null;
}*/