/*
	outline.js
	
	v0.06, 2008-09-23, Christian Augustin

*/
/*
	JavaScript 1.3+, W3C DOM Level 1
	
	MSIE 5 (excl. Mac???), Netscape 7, Mozilla 1.0, Firefox 1.0, Safari 1.1, Opera 7.0
	
	ToDo
	- Everything ... this is only a quick hack for VW CD-Net 2006!
	
	Changes
	2008-09-23 cma: Link normalization had problems with MSIE 6
	                (perhaps because it is doubled in linkstyler.js).
	2007-05-02 cma: Automatic link selection only with CSS class "autoSelect".
	2007-05-01 cma: Extended interactive behavior.
	2007-04-28 cma: Toggle switch added if "interactive".
	2007-02-07 cma: Optionaly using window.onDomReady() for initialization.
	2006-01-21 cma: Initial setup, borrowing from other JavaScript modules ...
	
	This is a total rewrite of outliner.js -- hopefully simplifying and only
	for W3C DOM compatible browsers!
	
*/


/* ===========================
   Constructor function ...
=========================== */

function Outline(obj) {
	if (!obj || !obj.getElementsByTagName) return;
	

	/* Normalize links and register them in outline and parent li ... */
	
	obj._links = new Array();
	var lnks = obj.getElementsByTagName('a');
	if (lnks) {
		for (var i=0; i<lnks.length; i++) {
			var lnk = lnks[i];
			if (lnk.href && !(lnk.className && lnk.className.match('branchToggle'))) {
				//lnk.href = Outline.normalize(lnk.href);
				obj._links[obj._links.length] = lnk;
				var pnode = lnk.parentNode;
				while (pnode) {
					if (pnode.tagName.match(/li/i)) {
						if (!pnode._link) pnode._link = lnk;
						lnk._li = pnode;
						break;
					};
					pnode = pnode.parentNode;
				};
			};
		};
	};
		

	/* Interactive ... */
	
	var interactive = obj.className.match(/(^|\s)interactive(\s|$)/);
	var lis = obj.getElementsByTagName('li');
	if (lis) {
		for (var l=0; l<lis.length; l++) {
			var li = lis[l];
			if (li.getElementsByTagName('ul').length) {
				if (!li.className || !li.className.match(/(^|\s)(branchClosed|branchOpen)(\s|$)/)) {
					li.className = (li.className) ? li.className + " branchClosed" : "branchClosed";
				};
				if (interactive) {
					var s = document.createElement('a');
					s.className = 'branchToggle';
					s.onclick = Outline.toggle;
					li.insertBefore(s, li.firstChild);
				};
			};
		};
	};

	
	/* Attach prototype methods ... */
	
	for (var m in Outline.prototype) obj[m] = Outline.prototype[m];

	if (obj.className.match(/(^|\s)autoSelect(\s|$)/)) obj.selectLink();
	
}



/* =======================
   Instance methods ...
======================= */

Outline.prototype.selectLink = function() {

	for (var i=this._links.length-1; i>=0; i--) {
		var lnk = this._links[i];
		if (lnk.href == Outline.docUrl) {
			if (!lnk.className)
				lnk.className = "selected"
			else if (!lnk.className.match(/(^|\s)selected(\s|$)/)) {
				lnk.className += " selected";
			};
			if (lnk._li) {
				if (!lnk._li.className)
					lnk._li.className = "selected"
				else if (!lnk._li.className.match(/(^|\s)selected(\s|$)/)) {
					lnk._li.className += " selected";
				};
			};
			var pnode = lnk._li;
			while(pnode && (!pnode.className || !pnode.className.match(/(^|\s)outline(\s|$)/))) {
				if (pnode.tagName.match(/li/i)) Outline.openBranch(pnode);
				pnode = pnode.parentNode;
			};
			break;
		};
	};
	
};



/* ==========================
   Class properties ...
========================== */

//Outline.outlines = new Array();

Outline.href = document.location.href;
if (navigator.userAgent.match(/AppleWebKit\//))
	Outline.href = Outline.href.replace(/file:\/([^\/])/, "file:///$1");
Outline.docUrl = Outline.href.replace(/^([^#]*).*/, "$1");
Outline.docPath = Outline.docUrl.replace(/^(.*\/)[^\/]*$/, "$1");



/* ====================
   Class methods ...
==================== */

Outline.initElements = function(obj) {
	if (!document.getElementsByTagName) return;
	var divs = (obj||document).getElementsByTagName('div');
	var outlines = new Array();
	if (divs) {
		for (var i=0; i<divs.length; i++) {
			var n = divs[i];
			if (n.className && n.className.match(/(^|\s)outline(\s|$)/)) outlines[outlines.length] = n;
		}
	};
	if (outlines.length) {
		for (var j=0; j<outlines.length; j++) new Outline(outlines[j]);
	};

};

Outline.normalize = function(url) {
	/* Very simple form, may corrupt the search part of an url! */
	if (!url.match(/^[^:]+:/)) url = Outline.docPath + url;
	url = url.replace(/\/\.\//g, "/");
	while (url.match(/\/\.\.\//))
		url = url.replace(/\/[^\/]+\/\.\.\//, "/");
	return url;
};

Outline.stripHash = function(url) {
	return url.replace(/^([^#]*).*/, "$1");
};

Outline.openBranch = function(li) {
	if (!li.className || !li.className.match(/branch(Closed|Open)/)) return;
	li.className = li.className.replace('branchClosed', 'branchOpen');
	if (li._link) li._link.className = li._link.className.replace('branchClosed', 'branchOpen');
};

Outline.closeBranch = function(li) {
	if (!li.className || !li.className.match(/branch(Closed|Open)/)) return;
	li.className = li.className.replace('branchOpen', 'branchClosed');
	if (li._link) li._link.className = li._link.className.replace('branchOpen', 'branchClosed');
};

Outline.toggle = function() {
	var p = this.parentNode;
	if (!p.className) return;
	if (p.className.match('branchClosed'))
		Outline.openBranch(p)
	else
		Outline.closeBranch(p);
	return;
};




/* =================
   Initialize ...
================= */

if (document.getElementsByTagName 
	&& document.createElement
	&& (!window.opera || document.createDocumentFragment)) {
	

	Outline.initComplete = false;
	
	Outline.init = function() {
		if (Outline.initComplete) return true;
		
		Outline.initElements();
		
		Outline.initComplete = true;
		return true;
	}
	
	if (window.onDomReady) {
		window.onDomReady(Outline.init);
	} else if (typeof pageComplete == 'function') {
		Outline.oldPageComplete = pageComplete || function(){};
		pageComplete = function() {
			Outline.oldPageComplete();
			Outline.init();
		};
	} else {
		Outline.oldOnload = window.onload || function(){};
		window.onload = function() {
			Outline.oldOnload();
			Outline.init();
		};
	};

};

/* End of outline.js */
