/*
 * 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;}

	// 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 the li containing the link has no nested list, skip this one
		if(!lis[i].getElementsByTagName('ul')[0])
		{
				
			continue;
		}
		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);
		//cssjs('add',newa,"mattiaClass");
		
		// if the user clicks on the link
		lis[i].getElementsByTagName('a')[0].onclick=function()
		{
		// 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].getElementsByTagName('ul'),hideClass)
					cssjs('remove',uls[i].getElementsByTagName('ul'),showClass)
				}
			}	
			
			if($(this).next().hasClass(showClass)) {
				$(this).next().find("ul").removeClass(showClass);
//				console.log("NASCONDO!");
				//cssjs('remove',this.parentNode.getElementsByTagName('ul')[0],showClass);
				$(this).parent().eq(0).find("ul").eq(0).removeClass(showClass).css({
					display: "none"
				});
				
			}
			else {
//				console.log("VEDO!");
				
				
				// nascondiamo gli UL dello stesso livello aperti
				
				//$(this).parent().eq(0).css("border","#ff0000 solid 1px");
				//$(this).parent().eq(0).find("ul").each(function() {
					/*
					$(this).removeClass(showClass).css({
						display: "none"
					});
					*/
					//$(this).css("border","#ff00ff solid 1px");
					
				//});
				
				$(this).parentsUntil("ul").parent().eq(0).find("ul").each(function() {
					$(this).removeClass(showClass).css({
						display: "none"
					});
				});
				
				//cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass);
				tps=$(this).position();
				/*
				if(jQuery.support.leadingWhitespace==false) {
					// IE
					mytop=tps.top - ($(this).parent().eq(0).find("ul").eq(0).height()/2) - ($(this).height()/2);
					myleft=(tps.left + $(this).parent().eq(0).find("ul").eq(0).width() - 10);
				}
				else {
				*/
					// TUTTI GLI ALTRI
					//mytop=tps.top - ($(this).parent().eq(0).find("ul").eq(0).outerHeight(true)/2) - ($(this).outerHeight(true)/2) + "px";
					//mytop=tps.top - ($(this).outerHeight(true) + ($(this).next().outerHeight(true)/2)) + "px"; // LA FRECCIA è UN 10PX PIù IN ALTO DELLA METà!
					mytop=tps.top - ($(this).parent().eq(0).find("ul").eq(0).outerHeight(true)/2);
					myleft=(tps.left + $(this).parent().eq(0).find("ul").eq(0).width() + 20);
					
					if($("#finderparent").hasClass("conIE")) {
						// non fare niente, non ho voglia di fare il negato...
						myleft-=30;
					}
					/*
					else {
						// solo Ie, tiriamo indietro di 10...
						
					}
					*/
					
					if($(this).parent().eq(0).find("ul").eq(0).hasClass("primoul")) {
						mytop-=10;
					}
					
				//}

				//$(this).parent().eq(0).find("ul").eq(0).addClass(showClass).css({
				$(this).next().addClass(showClass).css({
					display: "block",
					position: "absolute",
					top: mytop + "px",
					left: myleft + "px"
				});
				
			}
			
			// don't follow the real HREF of the link
			return false;
		}
	}	
	/*
	 * 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;
		}
	}
}

// Check if the browser supports DOM, and start the script if it does.
if(document.getElementById && document.createTextNode)
{
	window.onload=function() {
		ul2finder();
		$("#finder").find("ul").eq(0).addClass("primoul"); //.css("border","#ff0000 solid 1px");
		$("#finder").find("li").eq(0).addClass("primo").find('a').eq(0).addClass("primo");
	}
	
}

