function main()
{
	var hh=hash();
	var h = window.location.hash.substring(1);
	if(h!="" && h!=hh) {
		window.frames["body"].location.replace('chapters/'+h+'.htm');
		syncToc(h);
	}
}
function hash()
{
    var htm = ".htm";
	var h=window.frames["body"].location.pathname;
	var c="/chapters/";
	var n=h.indexOf(c);
	return n>=0?h.substring(h.indexOf(c)+c.length,h.length-htm.length):"";
}
function tocLoad()
{
    var h = window.location.hash;
    if(h) syncToc(h.substring(1));
}
function sync()
{
    var h = hash();
    if (h != "") {
        if (!_redir) { window.location.hash = h; }
        syncToc(h);
		_redir=false;
    }
    return false;
}
function syncToc(h) {

    var f = window.frames["toc"];
	var d=f.document;
	function docY(e){var p=e.offsetParent;return!p?0:docY(p)+e.offsetTop;}
	var v, o, b = d.body, de = d.documentElement;  a = b.getElementsByTagName("A");
	for(var i=0;i<a.length;i++) {
		if(a[i].className=="current")
			o=a[i];
		if(a[i].id=="toc"+h)
			v=a[i];
    }
    if(v && o && v.id == o.id) return;
    if(o)
        o.className = "";
    if(v) {
        v.className = "current";
        var dy = docY(v), dh = v.offsetHeight;
        var st = f.pageYOffset || b.scrollTop || (de ? de.scrollTop : 0);
        var sw = f.innerWidth || (de ? de.clientHeight : 0) || b.clientHeight;
		if(st>dy)st=dy+sw/2;else if(dy+dh>st+sw)st=dy+dh-sw/2;
		f.scrollTo(0,st);
	}
}