/*
 * ul2finder
 * written by Christian Heilmann (http://icant.co.uk)
 * turns the nested list with the ID "finder" into a dynamic list
 * uses the CSS classes defined in the variables
 */
function ul2finder()
{
    // Define variables used and classes to be applied/removed
    var i,uls,als,finder;
    var parentClass='parent';
    var showClass='shown';
    var hideClass='hidden';
    var openClass='open';

    // check if our finder list exists, if not, stop all activities
    finder=document.getElementById('finder');
    if(!finder){return;}
    
    if (finder.className == "save") {
	var save_enabled = true;
    } else {
	var save_enabled = false;
    }

    // add the class domenabled to the body
    cssjs('add',document.body,'domenabled');
    
    // loop through all lists inside finder, position and hide them 
    // by applying the class hidden
    uls=document.getElementById('finder').getElementsByTagName('ul');
    for(i=0;i<uls.length;i++)
	{
	    cssjs('add',uls[i],hideClass);
	}	
    
    // loop through all links of inside finder
    lis=document.getElementById('finder').getElementsByTagName('li');
    for(i=0;i<lis.length;i++)
	{
	    if (!lis[i].id) {
		lis[i].id = "finder_list_"+i;
	    }
	    // if the li containing the link has no nested list, skip this one
	    if(!lis[i].getElementsByTagName('ul')[0])
		{
		    continue;
		}
	    if(lis[i].firstChild.nodeName == "A") {
		var lul = lis[i].getElementsByTagName('ul')[0];
		var topli=document.createElement('li');
		var newa=document.createElement('a');
		newa.href = lis[i].firstChild.href;
		newa.appendChild(document.createTextNode(lis[i].firstChild.firstChild.nodeValue));
		topli.appendChild(newa);
		lul.insertBefore(topli, lul.firstChild);
		cssjs('add',lis[i].firstChild,parentClass);
	    } else {
		var newa=document.createElement('a');
		newa.href='#';
		newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
		lis[i].replaceChild(newa,lis[i].firstChild);
		// otherwise apply the parent class
		cssjs('add',newa,parentClass);
	    }
	    
	    // if the user clicks on the link
	    lis[i].getElementsByTagName('a')[0].onclick=function()
		{
		    if (cssjs("check", this, openClass)) {
			cssjs('swap',this,openClass, parentClass);
			var subuls = this.parentNode.getElementsByTagName('ul');
			for (i=0; i<subuls.length; i++) {
			    cssjs("add", subuls[i], hideClass);
			    cssjs("remove", subuls[i], showClass);
			    cssjs("remove", subuls[i].parentNode.getElementsByTagName('a')[0], openClass);
			    cssjs("add", subuls[i].parentNode.getElementsByTagName('a')[0], parentClass);
			}
			if (save_enabled) {
			    if (this.parentNode.parentNode.parentNode.id != "finderparent") {
				setOpen(this.parentNode.parentNode.parentNode.id);
			    } else {
				removeOpen();
			    }
			}
		    } else {
			// loop through all lists inside finder
			for(var i=0;i<uls.length;i++)
			    {
				// avoid the list connected to this link
				var found=false;
				for(j=0;j<uls[i].getElementsByTagName('ul').length;j++)
				    {
					if(uls[i].getElementsByTagName('ul')[j] == 		
					   this.parentNode.getElementsByTagName('ul')[0])
					    {
						found=true;
						break;
					    }
				    }
				// and hide all others
				if(!found)
				    {
					cssjs('add',uls[i],hideClass)
					cssjs('remove',uls[i],showClass)
					cssjs('remove',uls[i].parentNode.getElementsByTagName('a')[0],openClass)
					cssjs('add',uls[i].parentNode.getElementsByTagName('a')[0],parentClass)
				    }
			    }	
			// change the current link from parent to open 	
			cssjs('swap',this,parentClass,openClass)
			// show the current nested list 
			cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass)
			
			// don't follow the real HREF of the link
			if (save_enabled) {
			    setOpen(this.parentNode.id);
			}
		    }
		    return false;
		}
	}	
    if (save_enabled) {
	if (getOpen()) {
	    var onode = document.getElementById(getOpen());
	    var pnode = onode.parentNode.parentNode;
	    var pnode_array = new Array();
	    while (pnode.id != "finderparent") {
		pnode_array.unshift(pnode);
		pnode = pnode.parentNode.parentNode;
	    }
	    for (var i in pnode_array) {
		pnode_array[i].firstChild.onclick();
	    }
	    onode.firstChild.onclick();
	}
    }
    /*
     * cssjs
     * written by Christian Heilmann (http://icant.co.uk)
     * eases the dynamic application of CSS classes via DOM
     * parameters: action a, object o and class names c1 and c2 (c2 optional)
     * actions: swap exchanges c1 and c2 in object o
     *			add adds class c1 to object o
     *			remove removes class c1 from object o
     *			check tests if class c1 is applied to object o
     * example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
     */
    function cssjs(a,o,c1,c2)
    {
	switch (a){
	case 'swap':
	    o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
	case 'add':
	    if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
	    break;
	case 'remove':
	    var rep=o.className.match(' '+c1)?' '+c1:c1;
	    o.className=o.className.replace(rep,'');
	    break;
	case 'check':
	    return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
	}
    }
    
    function setOpen(id) {
	//alert(id);
	var hostdot = window.location.host.split(".");
	if (hostdot.length > 1) {
	    hostdot.shift();
	    var host = hostdot.join(".");
	} else {
	    var host = window.location.host;
	    var host = null;
	}
	setCookie("finderopen", id, null, "/", host);
    }

    function getOpen() {
	return getCookie("finderopen");
    }

    function removeOpen() {
	//alert("remove");
	var hostdot = window.location.host.split(".");
	if (hostdot.length > 1) {
	    hostdot.shift();
	    var host = hostdot.join(".");
	} else {
	    var host = window.location.host;
	    var host = null;
	}
	deleteCookie("finderopen", "/", host);
    }

    function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
	    ((expires) ? "; expires=" + expires.toGMTString() : "") +
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    ((secure) ? "; secure" : "");
	//alert(document.cookie);
    }

    function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	} else {
	    begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
	    end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
    }

    function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
	    document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
    }
}

// Check if the browser supports DOM, and start the script if it does.
/*
  if(document.getElementById && document.createTextNode)
  {
  window.onload=ul2finder;
  }
*/