/***************************************************************
 * content.js
 * version 1.4 2007-01-11
 **************************************************************/ 
var xmlDoc = new Object();

if (document.getElementById) {
	window.onload = function() {
		var oContent = new Ajax.Request('content.xml', { method: 'get', asynchronous: true, onComplete: initialize }); 
	}
}

/***************************************************************
 * store loaded content, load client related content
 * @param xmlContent is content loaded from file
 **************************************************************/ 
function initialize(xmlContent) {

	xmlDoc = xmlContent.responseXML;	
	var oClient = new Ajax.Request('../../client.xml', { method: 'get', asynchronous: true, onComplete: pageCreate }); 
}

/***************************************************************
 * store loaded content, begin content parsing and display
 * @param xmlContent is content loaded from file
 **************************************************************/ 
function pageCreate(xmlContent) {

	//prepare config
	var doc = xmlContent.responseXML;
	var xmlClient = doc.documentElement;
	removeEmptyTextNodes(xmlClient);	
	
	//extract sections
	var oHeader = xmlClient.getElementsByTagName("header").item(0);
	//var oNav = xmlClient.getElementsByTagName("nav").item(0);

	//create content section
	var hContent = document.createElement('div');
	hContent.id = "content";

	//create page sections
	headCreate(oHeader, true);	
	titleCreate(oHeader, hContent);		
	stylesLoad();
	resourceNavCreate(hContent);		
	sectionsCreate(hContent);
	
	document.getElementsByTagName("body").item(0).appendChild(hContent);	
	
	//set initial nav state 
	resourceNavInit();	
	navHighlightSection(0);
	navHighlightGroup(0,0);				
	
}

/***************************************************************
* create the resource pager, if designated
* @param hParent is the html parent element to attach new content to
 **************************************************************/ 
function pagerCreate(hParent) {

	var oPager = xmlDoc.getElementsByTagName("pager")[0];	
	var sPager = oPager.firstChild.nodeValue;
	if (sPager == "true") {
	
		//pager always starts at first group of first section
		document.bPager = true;
		document.nSection = 0;
		document.nGroup = 0;		
	
		var hPager = document.createElement('div');
		hPager.id = 'pager';
		
		var hPrevious = document.createElement('a');
		hPrevious.setAttribute('href', 'javascript:pagerPrevious();');		
		hPrevious.appendChild(document.createTextNode('previous'));		
		hPager.appendChild(hPrevious);
		
		var hDivider = document.createElement('span');
		hDivider.appendChild(document.createTextNode(' | '));
		hPager.appendChild(hDivider);
		
		var hNext = document.createElement('a');
		hNext.setAttribute('href', 'javascript:pagerNext();');		
		hNext.appendChild(document.createTextNode('next'));				
		hPager.appendChild(hNext);	
	
		hParent.appendChild(hPager);
	
	}
	
}

/***************************************************************
* if a local style sheet is specified, load it
 **************************************************************/ 
function stylesLoad() {

	if (xmlDoc.getElementsByTagName("css")[0] != undefined) {
	
		var oStyles = xmlDoc.getElementsByTagName("css")[0];	
		if (oStyles.firstChild) {
			var sFile = oStyles.firstChild.nodeValue;
			if (sFile != "" && sFile != undefined) {				
				var hHead = document.getElementsByTagName('head').item(0);
				
				//remove default style
				hHead.removeChild(document.getElementsByTagName('style').item(0));
				
				var hCSS = document.createElement('link');
				hCSS.setAttribute('rel', 'stylesheet');
				hCSS.setAttribute('type', 'text/css');
				hCSS.setAttribute('href', "../../styles/" + sFile);
				hHead.appendChild(hCSS);				
			}
		}
	}	
}

/***************************************************************
* handle pager next button click, advance display to the next group
 **************************************************************/ 
function pagerNext() {
	
	var hSection = $('sectionList' + document.nSection);	
	var aGroups = hSection.getElementsByTagName('a');
	var nGroupsLen = aGroups.length - 2;
	var aSections = document.getElementsByClassName('sectionList', 'navResources');
	var nSectionLen = aSections.length -1;
	
	if (document.nGroup < nGroupsLen) {
		document.nGroup++;				
		getGroup(document.nSection, document.nGroup);
	} else if (document.nGroup == nGroupsLen) {	
		document.nSection = (nSectionLen == document.nSection) ? 0 : parseInt(document.nSection+1);
		document.nGroup = 0;	
		getGroup(document.nSection, document.nGroup);		
	}
	
	//make sure menu is open
	pagerNavUpdate();

}

/***************************************************************
* handle pager previous button click, advance display to previous group
 **************************************************************/ 
function pagerPrevious() {

	var aSections = document.getElementsByClassName('sectionList', 'navResources');
	var nSectionLen = aSections.length -1;
	
	if (document.nGroup > 0) {
		document.nGroup--;				
		getGroup(document.nSection, document.nGroup);
	} else if (document.nGroup == 0) {	
		document.nSection = (document.nSection == 0) ? nSectionLen : parseInt(document.nSection-1);	
		var hSection = $('sectionList' + document.nSection);	
		var aGroups = hSection.getElementsByTagName('a');
		var nGroupsLen = aGroups.length - 2;	
		document.nGroup = nGroupsLen;
		getGroup(document.nSection, document.nGroup);		
	}
	
	//make sure menu is open
	pagerNavUpdate();		
}

/***************************************************************
* make sure menu is open
 **************************************************************/ 
function pagerNavUpdate() {

	var hSectionCheck = $('sectionList' + document.nSection);
	if (Element.hasClassName(hSectionCheck, "sectionClosed")) {
		toggleSection(document.nSection, "false");
	}
}

/***************************************************************
* create resource navigation area
* @param hParent is the html parent element to attach new content to
 **************************************************************/ 
function resourceNavCreate(hParent) {

	//html container for navigation
	var hNav = document.createElement('div');	
	hNav.id = "navResources";	
	hParent.appendChild(hNav);					
	
	//add the pager
	pagerCreate(hNav);	
	
	//create the nav
	if (xmlDoc.documentElement) {
		var oNav = new aarfNav(xmlDoc, hNav); 
	}	
	
	return hNav;
}

/***************************************************************
* turn section nodes to initial open or closed state
 **************************************************************/ 
function resourceNavInit() {

	var oSections = xmlDoc.getElementsByTagName("section");			
	for (var s=0; s < oSections.length; s++) {
	
		var hSection = document.getElementById("sectionList" + s);
		if (s == 0) {
			Element.addClassName(hSection, "sectionOpen");		
		} else {
			Element.hide(hSection.childNodes[1]);				
			Element.addClassName(hSection, "sectionClosed");				
		}
		
		/*
		if (oSections[s].getAttribute("open") == "false") {
			Element.hide(hSection.childNodes[1]);				
			Element.addClassName(hSection, "sectionClosed");					
		} else {
			Element.addClassName(hSection, "sectionOpen");
		}
		*/
	}
}

/***************************************************************
* create sections for each section node in loaded content
* @param hParent is the html parent element to attach new content to
 **************************************************************/ 
function sectionsCreate(hParent) {

	if (xmlDoc.documentElement) {
		var oSections = xmlDoc.getElementsByTagName("section");	
		var oSection = new aarfSection(oSections[0], hParent);	
	}
}

		
		
			
