﻿var allCollapsedSections = [];
function toggleAllSections(oWhichWay) {
	for( var i = 0; i < allCollapsedSections.length; i++ ) {
		if( ( allCollapsedSections[i].parentNode.sectionwrapper.style.display == 'none' ) == oWhichWay ) {
			allCollapsedSections[i].onclick('oDontMess');
		}
	}
	return ( ( location.hash != '' ) && ( location.hash != '#' ) );
}
function collapseSections() {
	var i, j;
	var allheaders, oneheader, sectionlink;
	var loadhash, loadhashheader, currentid = '';
	if( !document.getElementById || !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return; }
	//collapse all sections and add links into the header
	for( i = 1; i < 7; i++ ) {
		allheaders = document.getElementsByTagName('h'+i);
		for( j = 0; j < allheaders.length; j++ ) {
			oneheader = allheaders[j];
			if( oneheader.id && ( oneheader.sectionwrapper = document.getElementById(oneheader.id+'_collapsed') ) ) {
				sectionlink = document.createElement('a');
				sectionlink.href = '#' + oneheader.id;
				sectionlink.onclick = function (oDontMess) {
					//toggle the section
					var pageoff = 0
					this.parentNode.className = ( this.parentNode.sectionwrapper.style.display == 'none' ) ? 'nowexpanded' : 'canexpand';
					this.parentNode.sectionwrapper.style.display = ( this.parentNode.sectionwrapper.style.display == 'none' ) ? this.parentNode.sectionwrapper.originaldisplay : 'none';
					if( this.parentNode.sectionwrapper.style.display == 'none' ) {
						if( typeof( window.pageYOffset ) != 'undefined' ) {
							pageoff = window.pageYOffset;
						} else if( document.documentElement.scrollTop ) {
							pageoff = document.documentElement.scrollTop;
						} else if( document.body.scrollTop ) {
							pageoff = document.body.scrollTop;
						}
						if( oDontMess != 'oDontMess' && !( navigator.product == 'Gecko' && ( window.Controllers || window.controllers ) ) ) {
							//reset the hash so it doesn't expand if they reload
							//don't do this in real Gecko (extra checks are to try not to accidentally get the other browsers that also pretend to be Gecko),
							//because it reloads the page instead of setting the hash (why?) - can't detect this bug, have to detect browser, grrr
							location.hash = '';
						}
						//reset scrolling so there are no ugly jumps
						window.scrollTo(0,pageoff);
						setTimeout(function () { window.scrollTo(0,pageoff); }, 10 ); //Safari scrolls at end of thread, not when hash is set
						return false;
					} else {
						return true;
					}
				};
				while( oneheader.childNodes[0] ) {
					sectionlink.appendChild(oneheader.childNodes[0]);
				}
				allCollapsedSections[allCollapsedSections.length] = sectionlink;
				oneheader.className = 'canexpand';
				oneheader.appendChild(sectionlink);
				//used to assign it to oneheader.sectionwrapper.style.originaldisplay
				//but then Gecko sometimes returns undefined instead of ''
				oneheader.sectionwrapper.originaldisplay = oneheader.sectionwrapper.style.display ? oneheader.sectionwrapper.style.display : '';
				oneheader.sectionwrapper.style.display = 'none';
			}
		}
	}
	//get the hash from the address, and expand relevant sections
	if( loadhash = location.hash.replace(/^#/,'') ) {
		loadhash = loadhash.split(/_/);
		for( i = 0; i < loadhash.length; i++ ) {
			if( !loadhash[i] ) { continue; }
			currentid += ( currentid ? '_' : '' ) + loadhash[i];
			loadhashheader = document.getElementById(currentid);
			if( loadhashheader.tagName.match(/^h[1-6]/i) && loadhashheader.firstChild && loadhashheader.firstChild.onclick && document.getElementById(currentid+'_collapsed') ) {
				loadhashheader.firstChild.onclick();
			}
		}
	}
	var newStyle = document.createElement('link');
	newStyle.setAttribute('rel','stylesheet');
	newStyle.setAttribute('type','text\/css');
	newStyle.setAttribute('href','\/css\/add\/qa.css');
	document.getElementsByTagName('head')[0].appendChild(newStyle);
	var excollinks = document.getElementById('expcolall');
	if( excollinks ) { excollinks.style.display = ''; }
}
//call immediately before the body end, or onload (slower)
collapseSections();




