// create a TOC of H2's
function createToc() {
	h2s = document.getElementsByTagName('h2');
	if (h2s.length > 1) {
		tocList = document.createElement('ol');
		tocList.style.paddingLeft = tocList.style.paddingLeft = '30px';
		tocList.style.marginTop = '10px';
		if (location.href.indexOf('factsheets')!=-1) tocList.className = 'greylinks';
		
		for(toc=0;toc<h2s.length;toc++) {
			el = h2s[toc];
			elTitle = trim(el.innerHTML.replace('<br>','').replace('<BR>','').replace("\n",'').replace("\t",'').replace("<",'<!--').replace(">",'-->'));
			if (toc && location.href.indexOf('print=1')==-1) {
				topA = document.createElement('a');
				topA.innerHTML = '^ top';
				topA.className = 'topLink';
				topA.href = '#top';
				el.parentNode.insertBefore(topA, el);
			}
			tocA = document.createElement('a');
			tocA.id = 'toc'+(toc+1);
			tocA.name = 'toc'+(toc+1);
			el.parentNode.insertBefore(tocA, el);
			
			listItemEl = document.createElement('li');
			listItemEl.style.marginTop = '3px';
			listItemEl.style.marginBottom = '3px';
			listItemA = document.createElement('a');
			listItemA.href = '#toc'+(toc+1);
			listItemA.innerHTML = elTitle;
			
			listItemEl.appendChild(listItemA);
			tocList.appendChild(listItemEl);			
		}

		if (location.href.indexOf('print=1')==-1) {
			topA = document.createElement('a');
			topA.className = 'topLink';
			// tocA.style.textAlign = 'right';
			topA.innerHTML = '^ top';
			topA.href = '#top';
			topA.style.marginBottom = '0px';
			
			if (shareTop = document.getElementById('campaignInfo')) {
				shareTop.parentNode.insertBefore(topA, shareTop);
			} else if (shareTop = document.getElementById('sharethis_container')) {
				shareTop.parentNode.insertBefore(topA, shareTop);
			} else {
				document.getElementById('contentCol').appendChild(topA);
			}
		}
		
		tocListContainer = document.createElement('div');
		if (location.href.indexOf('factsheets')!=-1) tocListContainer.className = 'sml_bodytext';
		tocListContainer.appendChild(tocList);
		
		tocTitle = document.createElement('h2');
		if (location.href.indexOf('factsheets')!=-1) tocTitle.innerHTML = 'In this factsheet...';
		else if (location.href.indexOf('features')!=-1) tocTitle.innerHTML = 'In this article...';
		else tocTitle.innerHTML = 'On this page...';
		tocTitle.style.marginTop = '20px';
		tocList.appendChild(tocTitle);
		
		hrEl = document.createElement('hr');
		hrEl.style.marginBottom = '20px';
		if (location.href.indexOf('features')!=-1) hrEl.style.marginTop = '30px';
		
		if (location.href.indexOf('factsheets')!=-1) {
			// find the page title
			pageTitle = document.getElementsByTagName('h1')[0]
			insertAfter(pageTitle,tocTitle);
		} else if (1 || location.href.indexOf('features')!=-1 || location.href.indexOf('issues')!=-1) {
			// add above 1st h2
			pageTitle = document.getElementById('toc1');
			pageTitle.parentNode.insertBefore(tocTitle, pageTitle);
		}
		insertAfter(tocTitle,tocListContainer);		
		insertAfter(tocList,hrEl);
		matchColumns();		
	}
}

function trim(str) {
	if (!str) return str;
	while(str.charAt(0)==' '|| str.charAt(0)=="\n" || str.charAt(0)=="\t") str = str.substr(1);
	while(str.charAt(str.length-1) ==' '|| str.charAt(str.length-1)=="\n" || str.charAt(str.length-1)=="\t") str = str.substr(0,str.length-1);
	return str;
}

function insertAfter(referenceNode, newElement) {
	referenceNode.parentNode.insertBefore(newElement, referenceNode.nextSibling);
}

oldOnload = onload;

onload = function() {
	oldOnload();
	createToc();
	matchColumns();
}