addLoadEvent(fiction);

function fiction()
{
if (!document.getElementById('heading'))
	{
	return;
	}
if (!document.createElement || !document.createTextNode)
	{
	return;
	}

//create the select
var jump = document.createElement('select');
jump.setAttribute('id','jump');
jump.onchange = function() { window.location = document.getElementById('jump').value;};

//now find all the bookmarks and run through them
var bmarks = document.getElementsByTagName('link');
var partCounter = 0;
var partCount = 0;
var sectionCounter = 0;
var sectionCount = 0;

//count the parts and find the sections
for (var i = 0; i < bmarks.length; i++)
	{
	var bmark = bmarks[i];
	//only interested in links with rel bookmark
	if (bmark.getAttribute('rel') != 'bookmark')
		{
		continue;
		}

	if (bmark.getAttribute('title').indexOf('Part') == 0)
		{
		partCount++;
		}
	if (bmark.getAttribute('title').indexOf('Section') == 0)
		{
		sectionCount++;
		}
	}

for (var i = 0; i < bmarks.length; i++)
	{
	var bmark = bmarks[i];
	//only interested in bookmarks with section in the title
	if (bmark.getAttribute('rel') != 'bookmark' || bmark.getAttribute('title').indexOf('Section') == -1)
		{
		continue;
		}

	//find the part and the section
	var reg = /p(\d+)-s(\d+)/;
	var matches = reg.exec(bmark.getAttribute('href'));
	var part = matches[1];
	var section = matches[2];

	//create the option
	var option = document.createElement('option');
	var jumpHref = (section == 1 ? '#p-' + part: bmark.getAttribute('href'));
	option.setAttribute('value',jumpHref);
	option.appendChild(document.createTextNode(bmark.getAttribute('title')));
	
	//increase the section counter
	sectionCounter++;
	
	//is it mutipart?
	if (partCount > 0)
		{
		//option groups
		if (partCounter != part)
			{
			if (part > 1)
				{
				jump.appendChild(optgrp);
				}

			var optgrp = document.createElement('optgroup');
			optgrp.setAttribute('label','Part ' + part);
			partCounter = part;
			}

		optgrp.appendChild(option);
		if (sectionCount == sectionCounter)
			{
			jump.appendChild(optgrp);
			}
		}
	else	//no option groups
		{
		jump.appendChild(option);		
		}
	}

if (sectionCount > 1)
	{
	//create the form
	var jumpform = document.createElement('form');
	jumpform.setAttribute('id','jumpform');
	jumpform.setAttribute('action','');
	
	//forms must contain a block element
	var litem = document.createElement('div')
	litem.setAttribute('style','text-align: right;');
	jumpform.appendChild(litem);
	
	//label
	var lab = document.createElement('label');
	lab.setAttribute('for', 'jump');
	var txtj = document.createTextNode('Jump To: ');
	lab.appendChild(txtj);
	litem.appendChild(lab);
	
	//append the select
	litem.appendChild(jump);
	
	//a spacer
	var txts = document.createTextNode(' ');
	litem.appendChild(txts);

	//submit button
	var submt = document.createElement('input');
	submt.setAttribute('type', 'submit');
	submt.setAttribute('value', 'Go');
	litem.appendChild(submt);

	//append the lot to the header section of the page
	document.getElementById('heading').appendChild(jumpform);
	}
}
