/*

@name			dynamicUL
@description	transforms a UL list into a folding menu
@version		3.0
@date			2007-05-20
@author			Olav Roth <xor@redlink.de>

*/

function dynamicUL(id,closeAuto,saveCookie){

	if(!document.getElementById)return false;
	if(typeof(id)=="object")this.id=id.id;
	if(typeof(id)=="string")this.id=id;

	this.closeAuto=closeAuto;
	this.saveCookie=saveCookie;

	var t;

	this.init();

	if(document.cookie&&this.saveCookie){
		t=(new RegExp("\\b"+this.id+"=([^;]+)")).exec(document.cookie);
		if(t&&t[1]){
			var u=document.getElementById(this.id).getElementsByTagName('ul');
			if(t[1].length==u.length)for(var i=0;i<u.length;i++)u[i].style.display=(t[1].substr(i,1)!="O"?"none":"");
		}
	}
	else this.closeAll();

	// find active a, that is the a that has a href that equals the window.location.href
	t=document.getElementById(this.id).getElementsByTagName('A');

	for(i=0;i<t.length;i++){
		if (t[i].getAttribute('href')==window.location.href.substr(window.location.href.length-t[i].getAttribute('href').length,t[i].getAttribute('href').length)) {
			if(t[i].className)t[i].className=t[i].className.replace(/ ?\bselected\b/,'')+' selected';
			else t[i].className='selected';
			this.open(t[i]);
		}
	}
	return true;
}

dynamicUL.prototype.init = function() {

	var a=document.getElementById(this.id).getElementsByTagName('A'),f=this;
	for(var i=0;i<a.length;i++){
		if(a[i].addEventListener)a[i].addEventListener("click",function(e){f.click(e)},true);
		else a[i].attachEvent("onclick",function(e){f.click(e)});
	}
	return true;
};

dynamicUL.prototype.click = function(e) {

	var l=(e.target?e.target:e.srcElement).parentNode;
	while(l&&l.nodeName!='LI')l=l.parentNode;
	var p,u,s=l.parentNode.getElementsByTagName('LI');
	for(var i=0;i<s.length;i++)if(s[i].parentNode==l.parentNode){
		u=s[i].getElementsByTagName('UL');
		for(var j=0;j<u.length;j++){
			p=u[j].parentNode;
			while(p&&p.nodeName!='LI')p=p.parentNode;
			if(p==s[i]){
				if(this.closeAuto)u[j].style.display=(s[i]!=l?"none":"");
				else if(s[i]==l){
					u[j].style.display="";
					break;
				}
			}
		}
	}
	if(this.saveCookie)this.save();
	return true;
}

dynamicUL.prototype.open = function(l) {

	if(typeof(l)=="string")l=document.getElementById(l);
	while(l&&l.nodeName!='LI')l=l.parentNode;
	var p,u,s=l.parentNode.getElementsByTagName('LI');
	for(var i=0;i<s.length;i++)if(s[i].parentNode==l.parentNode){
		u=s[i].getElementsByTagName('UL');
		for(var j=0;j<u.length;j++){
			p=u[j].parentNode;
			while(p&&p.nodeName!='LI')p=p.parentNode;
			if(p==s[i]){
				if(this.closeAuto)u[j].style.display=(s[i]!=l?"none":"");
				else if(s[i]==l)u[j].style.display="";
				var n=u[j];
				while (n&&n.id!=this.id){
					n=n.parentNode;
					if(n.nodeName=='UL')n.style.display="";
				}
				break;
			}
		}
	}
	if(this.saveCookie)this.save();
	return true;
};

dynamicUL.prototype.close = function(l) {

	if(typeof(l)=="string")l=document.getElementById(l);
	while(l&&l.nodeName!='LI')l=l.parentNode;
	var p,u=l.getElementsByTagName('UL');
	for(var j=0;j<u.length;j++){
		p=u[j].parentNode;
		while(p&&p.nodeName!='LI')p=p.parentNode;
		if(p==l)u[j].style.display="none";
	}
	if(this.saveCookie)this.save();
	return true;
};

dynamicUL.prototype.closeAll = function(l) {

	if(typeof(l)=="string")l=document.getElementById(l);
	if(l==null)l=document.getElementById(this.id);
	var u=l.getElementsByTagName('UL');
	for(var i=0;i<u.length;i++)u[i].style.display="none";
	if(this.saveCookie)this.save();
	return true;
};

dynamicUL.prototype.save = function() {

	var cookie=this.id+"=";
	var subuls=document.getElementById(this.id).getElementsByTagName('ul');
	for(var i=0;i<subuls.length;i++)cookie+=(subuls[i].style.display=="none"?"C":"O");
	var exp=new Date();
	exp.setTime(exp.getTime()+1000*60*60*24*99);
	document.cookie=cookie+";expires="+exp.toGMTString();
	return true;
};

