var aSection = new Array();
var anchorList = new Array();
var sections = document.getElementById('sections');

function doNextPrevious(aSection,tab,anchorList){
    /* initAnchorToggle created the aSection array */
    for (j=0;j<aSection.length;j++) {
 	var fullSectionName = aSection[j].id;
	var sectionIndex = 0;
        /* The substring index is a variable in case we use this for 
	pages other than School Environment ('other') */
        if ((fullSectionName.indexOf('dataSectionotherprivate') > -1)) {
	    sectionIndex = 23;
	}else if ((fullSectionName.indexOf('dataSectionother') > -1)) {
	    sectionIndex = 16;
	}
 	var shortSectionName = aSection[j].id.substring(sectionIndex);

	if(shortSectionName == 'finance'){
	    var divOuter = document.getElementById('outer' + shortSectionName);
	    //this works in firefox, but not ie, probably because the div is in a <tr>
	    divOuter.style.marginTop = '-30px';
	}

	var divBackToTop = document.getElementById('backToTop' + shortSectionName);
	divBackToTop.style.paddingTop = '7px';

	var nextSectionName = '';
	var divnext = document.getElementById('testnext_' + shortSectionName);
	divnext.className = "next";

	var prevSectionName = '';
	var divprevious = document.getElementById('testprevious_' + shortSectionName);
	divprevious.className = "previous";


	if(j<aSection.length - 1){
 	    nextSectionName = aSection[j+1].id.substring(sectionIndex);
	    var nexthref = divnext.firstChild;
	    nexthref.index = j+1;
	    nexthref.onclick = function() {
                        nextprevOnClick(this,aSection,anchorList);
                        window.location.href = document.location.pathname + '#toc';
                        //s.tl is an omniture call.
                        if (s.tl) {
                          s.tl(this,'o',tab + ': ' +this.innerHTML);
                        }
                        return false;
                    }
	    var nextAltTag = getAlt(nextSectionName);
	    nexthref.innerHTML = "<a href='#" + nextSectionName + "'><img src='/images/paging/next.gif' border='0' alt='Go to next page'><img src='/images/paging/" + nextSectionName + ".gif' border='0' alt='" + nextAltTag + "'><img src='/images/paging/nextarrows.gif' border='0' alt = 'Next page'></a>";
	}	
	if(j>0){
 	    prevSectionName = aSection[j-1].id.substring(sectionIndex);
	    var prevhref = divprevious.firstChild;
	    prevhref.index = j-1;
	    prevhref.onclick = function() {
                        nextprevOnClick(this,aSection,anchorList);
                        window.location.href = document.location.pathname + '#toc';
                        //s.tl is an omniture call.
                        if (s.tl) {
                          s.tl(this,'o',tab + ': ' +this.innerHTML);
                        }
                        return false;
                    }
	    var prevAltTag = getAlt(prevSectionName);
	    prevhref.innerHTML = "<a href='#" + prevSectionName + "'><img src='/images/paging/previous.gif' border='0' alt = 'Go to previous page'><img src='/images/paging/" + prevSectionName + ".gif' border='0' alt='" + prevAltTag + "'></a>";
	}	

    }
}

function getAlt(imageName){
    if(imageName == 'ap'){
	return 'AP Exams';
    }else if(imageName == 'students'){
	return 'Students';
    }else if(imageName == 'teachers'){
	return 'Teachers';
    }else if(imageName == 'finance'){
	return 'Spending Per Pupil';
    }
    return imageName;
}

function nextprevOnClick(nphref,aSection,anchorList){
    nphref.focus();
    var index = nphref.index;
    var anchor = anchorList[index];
    toggle(anchor);
    for (j=0;j<aSection.length;j++) {
        if (index==j) {
            aSection[j].style.display = 'block';
        } else {
            aSection[j].style.display = 'none';
        }
    }
}

function initAnchorToggle(tab) {
    if (sections) {
        var children = sections.getElementsByTagName("DIV");
        for (var i = 0; i < children.length; i++) {
            var div = children[i];
            if (div.id && div.id.indexOf('dataSection')==0) {
                div.style.display = 'block';
                aSection.push(div);
            }
        }
    }
    var ul = document.getElementById('sectionAnchorLinks');
    var initialShowAnchor = 0;
    var sectionName = (window.location.hash) ? window.location.hash : 0;
    if (ul) {
        var children = ul.getElementsByTagName("LI");
        if (children.length == aSection.length) {
            for (var i= 0; i < children.length; i++) {
                var li = children[i];
                var anchor = li.firstChild;
                if (anchor) {
                    anchor.index = i;
		    anchorList.push(anchor);
                    if ((sectionName !=0 && anchor.hash == sectionName) || (i==0)) {
                        initialShowAnchor = anchor;
                    }
                    li.firstChild.onclick = function() {
                        toggle(this);
                        window.location.href = document.location.pathname + '#toc';
                        //s.tl is an omniture call.
                        if (s.tl) {
                          s.tl(this,'o',tab + ': ' +this.innerHTML);
                        }
			if(document.location.indexOf('?')){
			  return true;
			}else{
                          return false;
			}
                    }
                }
            }
        }
    }
    if (initialShowAnchor) {
	toggle(initialShowAnchor);
    }
    if(aSection[0].id.indexOf('dataSectionother') > -1){
	doNextPrevious(aSection,tab,anchorList);
    }
}

function toggle(anchor) {
    anchor.focus();
    var index = anchor.index;
    var li = anchor.parentNode;
    for (j=0;j<aSection.length;j++) {
        if (index==j) {
            aSection[j].style.display = 'block';
            li.className = 'active';
        } else {
            aSection[j].style.display = 'none';
        }
    }
    var neighbor = li.nextSibling;
    while (neighbor) {
        neighbor.className = 'inactive';
        neighbor = neighbor.nextSibling;
    }
    neighbor = li.previousSibling;
    while (neighbor) {
        neighbor.className = 'inactive';
        neighbor = neighbor.previousSibling;
    }
}

