/**
 * toggle.js - implements dynamic segments
 *
 * by Michael Guitton (saramaca/at/tiscali/dot/fr)
 *
 * 07-Sep-2005 :: minor code tweak in swap method - mg
 *
 * 15-Jan-2003 :: better class attribute lookup method - mg 
 * 
 * 16-Dec-2003 :: added an optional parameter to prevent the link from being
 *                actually followed when it is activated - mg
 *
 * 02-Jun-2003 :: refined minitoc parsing algorithm - mg
 *
 * 26-Mar-2003 :: statement 'document.all[hash] || document.getElementById(hash)' 
 *                didn't work in Mozilla for obvious reasons! ;) - mg
 *
 * 25-Mar-2003 :: visibility of segments is no longer being set via CSS; use DOM - mg
 *
 */

var $_ = new Object();
$_.swap = new Function("return true");

if (!window.opera && document.getElementById) {

	var $_ = {
		 config: { 
			 myID: 'content'
		 	,className: ['segment', 'minitoc']
		 	,tags: ['div', 'p'] // preferably block elts
		 	,hash: 's0'
		 	,regex: new RegExp('^#s[0-9]+$')
		 	,follow: false
		 	,delay: 0
		}
		,toggle: function(which, on) {
			which.style.display = on ? 'block' : 'none';
		}
		,swap: function(a, b) {
			this.config.hash = b;
			if (document.all) {
				this.toggle(document.all[a], false);
				this.toggle(document.all[b], true);
			}
			else if (document.getElementById) {
				this.toggle(document.getElementById(a),false);
				this.toggle(document.getElementById(b),true);
			}
			return this.config.follow;
		}
		,init: function() {
			var toc = document.getElementById(this.config.myID);
			if (toc) {
				var id = this.config.hash;
				if (this.config.regex.test(location.hash)) {
					var hash = location.hash.substr(1);
					if (document.getElementById(hash))
						id = hash;
				}
			// 15-Jan-2003 :: class attribute lookup array
				var re = [];
				for (var i = 0; i < this.config.className.length; i++) {
					re[i] = '(^|\\s)'+this.config.className[i]+'(\\s|$)';
				}
				var collection = toc.getElementsByTagName(this.config.tags[0]);
				for (var i = 0; i < collection.length; i++) {
					var segment = collection[i];
					if (-1 != segment.className.search(re[0] /* auto typecast? */)) {
					// segment class attribute contains this.config.className[0]
						this.toggle(segment, (segment.id == id) ? true : false);
					// 02-Jun-2003 :: better find minitoc method - mg
						var elt, j = 0;
						while (elt = segment.getElementsByTagName(this.config.tags[1])[j++]) {
							if (-1 != elt.className.search(re[1])) {
							// elt class attribute contains this.config.className[1]
								this.toggle(elt, true);
							}
						}
					}
				}
			}
		}
	}
	if (!document.all) {
		$_.init();
	}
	else {
		setTimeout('$_.init()', $_.config.delay); // prevents a rendering glitch on IE5/Mac
	}
}
