function addEvent(o,e,f)
{
	if (o.addEventListener) {
		o.addEventListener(e,f,true);
		return true;
	}
	else if (o.attachEvent) {
		var r=o.attachEvent('on'+e,f);
		return r;
	}
	else return false;
}
function setCookie(n,v,d)
{
	var t=new Date();
	var e=new Date();
	if(d==null||d==0) d=1;
	e.setTime(t.getTime()+86400000*d);
	document.cookie=n+'='+escape(v)+';expires='+e.toGMTString()+';path='+PATH_ABSOLUTE;
}
function getCookie(n)
{
	var c = document.cookie;
	var l = c.length;
	
	if (l > 0) {
		var v = c.indexOf(n + "=");
		if (v != -1) {
			v += n.length + 1;
			var w = c.indexOf(';', v);
			if (w == -1) w = l;
			return unescape(c.substring(v, w));
		}
	}
	
	return "";
}

function convert_ul2select(ul, select, level)
{
	var i,j,li,cnt,opt,pre='';
	
	if (!level) level = 0;
	for (i=0; i<level; i++) pre += '&nbsp;&nbsp;&nbsp;&nbsp;';
	var href = document.location.href;
	var path = document.location.pathname;
	
	for (i=0; i<ul.childNodes.length; i++) if (ul.childNodes[i].nodeName == 'LI') {
		cnt = 0;
		for (j=0; j<ul.childNodes[i].childNodes.length; j++) {
			li = ul.childNodes[i].childNodes[j];
			switch (ul.childNodes[i].childNodes[j].nodeName) {
			case 'A':
				if (cnt) break;
				opt = add_option(select, li.href, pre+li.innerHTML);
				if ((li.href == href) || (li.href == path)) opt.selected = true;
				cnt++;
				break;
			case 'UL':
				convert_ul2select(li, select, level+1);
				cnt++;
				break;
			}
		}
		if (cnt == 0) add_option(select, '', ul.childNodes[i].textContent);;
	}
}

var ulLists = new Array();
function CreateSelectFromUL(ul_id, select_id)
{
	var ul = document.getElementById(ul_id);
	var select = document.getElementById(select_id);
	if (!ul || !select) return;
	
	convert_ul2select(ul, select);
	ulLists.push(ul_id);
	addEvent(document, 'load', function () {
		var d;
		for (var i=0; i<ulLists.length; i++) {
			d = document.getElementById(ulLists[i]);
			if (d) d.parentNode.removeChild(d);
		}
	});
}

var collapseListsID = new Array();
var collapseListsElementUL = new Array();
function CollapseList(listid)
{
	listElement = document.getElementById(listid);
	if (!listElement) return;
	
	if (listElement.nodeName != "UL") {
		tags = listElement.getElementsByTagName("ul");
		if (tags.length > 0) listElement = tags[0];
		else return;
	}
	
	collapseListsID[collapseListsID.length] = listid;
	
	var path = getCookie(listid);
	path = path.split(";");
	
	collapseListsElementUL[listid] = listElement.getElementsByTagName("ul");
	if (collapseListsElementUL[listid]) for (i=0; i<collapseListsElementUL[listid].length; i++) {
		aElement = collapseListsElementUL[listid][i].parentNode.firstChild;
		addEvent(aElement, 'click', CollapseListSwitch);
	}
	
	var pid = 0;
	if (path.length == 0) return;
	
	t = listElement.firstChild;
	while (t) {
		if (t.nodeName == 'LI') {
			if (t.firstChild.getAttribute('href') == path[pid]) {
				t = t.firstChild;
				while (t) {
					if (t.nodeName == 'UL') break;
					t = t.nextSibling;
				}
				if (!t) break;
				CollapseListSet(t, true);
				pid++;
				if (pid >= path.length) break;
				t = t.firstChild;
				continue;
			}
		}
		
		t = t.nextSibling;
	}
}
function CollapseListSet(target, active)
{
	target.style['display'] = active?'block':'none';
	classpassive = target.parentNode.getAttribute('classpassive');
	classactive = target.parentNode.getAttribute('classactive');
	if ((classpassive != null) && (classactive != null)) target.parentNode.className = active?classactive:classpassive;
}
function CollapseListSwitch(e)
{
	var target;
	if (!e) var e = window.event;
	if (e.target) target = e.target;
	else target = e.srcElement;
	
	var ref = target.getAttribute('href');
	
	while (target) {
		if (target.nodeName == 'UL') break;
		target = target.nextSibling;
	}
	
	if (target) {
		ul = target.parentNode;
		while (ul) {
			if (ul.nodeName == 'UL') {
				id = '';
				for (i in collapseListsID) {
					if (typeof(collapseListsID[i]) == 'function') continue;
					if (collapseListsID[i] == ul.id) {
						id = ul.id;
						break;
					}
				}
				if (id.length > 0) break;
			}
			ul = ul.parentNode;
		}
		
		if (ul) {
			var path = new Array();
			
			if (target.style['display'] == 'block') CollapseListSet(target, false);
			else {
				path[path.length] = ref;
				if (collapseListsElementUL[ul.id]) for (i=0; i<collapseListsElementUL[ul.id].length; i++) collapseListsElementUL[ul.id][i].style['display'] = 'none';
				CollapseListSet(target, true);
				
				t = target.parentNode;
				while (t) {
					if (t.nodeName == 'UL') {
						if (t.id == ul.id) break;
						CollapseListSet(t, true);
					}
					t = t.parentNode;
				}
			}
			
			t = target.parentNode.parentNode;
			while (t) {
				if ((t.nodeName == 'UL') && (t.id == ul.id)) break;
				if (t.nodeName == 'LI') path[path.length] = t.firstChild.getAttribute('href');
				t = t.parentNode;
			}
			
			path = path.reverse();
			setCookie(ul.id, path.join(";"), 1);
		}
	}
	
	if (e.preventDefault) {
		e.preventDefault();
		e.stopPropagation();
	}
	else {
		e.cancelBubble=true;
		e.returnValue=false;
	}
	return false;
}
