
function followLink() {
	try {
		if (window != top) {
			top.followLink(this.href);
			return false;
		}
	} catch (e) {
		for (i in e)
		    document.write (i + ': ' + e[i] + '<br>');
		document.write ('Fehler: ' + e + '<br>');
		return false;
	}
	
	return true;
}

function showHide() {
	var li = this.parentNode;
	
	var className = li.className;
	if ('open_' == className.substring(0,5)) {
		className = className.substring(5);
	} else {
		className = 'open_' + className;
	}
	li.className = className;
	
	var subList = $(this).nextAll ();
	subList.toggle();
	
	return false;
}

function pageName(url) {
	var pos = url.lastIndexOf('/');
	if (pos >= 0) {
		url = url.substring(pos+1);
	}
	return url;
}

$(document).ready(function(){
	var closeBook = $('#clickToCloseBook').text ();
	if (!closeBook) closeBook = "Click to close this book";
	var closeChapter = $('#clickToCloseChapter').text ();
	if (!closeChapter) closeChapter = "Click to close this chapter";
	var visitScene = $('#clickToVisitScene').text ();
	if (!visitScene) visitScene = "Click to go to this part of the book";
	
   	$('a.book').each(function(index) {
   		this.title = closeBook;
   		$(this).click(showHide);
   	});
   	$('a.chapter').each(function(index) {
   		this.title = closeChapter;
   		$(this).click(showHide);
   	});
   	$('a.section').each(function(index) {
   		this.title = visitScene;
   		$(this).click(followLink);
   	});
   	
   	if (window != window.parent) {
	   	var thisPage = pageName(window.parent.location.href);
	   	var escaped = thisPage.replace(/(['])/g, "\\$1");
	   	var selector = "a[href*='"+escaped+"']:first";
	   	$(selector).each(function(index) {
   			$(this).toggleClass('current');
   			var top = $(this).offset().top;
   			$('html,body').animate({scrollTop: top}, 100);
	   	});
   	}
});

