





var loadURL = '';

var nextnode,prevnode,fcnode,panode;

var bodylevelclass = '';



YUI({combine: true, timeout: 10000}).use("io",

	function(Y) {



		var geturls = new Array();
		var uls;
		var lis;
		var d;

		

		/*
		 * function ul2finder
		 * inspired by Christian Heilmann (http://icant.co.uk)
		 * extended by Roman Abt (http://taywa.ch)
		 * 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,finder;
			var parentClass='parent';
			var showClass='shown';
			var hideClass='hidden';
			var openClass='open';

			d = document.getElementById('cont');

			// check if our finder list exists, if not, stop all activities
			finder=document.getElementById('finder');
			if(!finder){return;}




			var initdomel = document.getElementById('initpl');

			try{
				nextnode = initdomel.parentNode.nextSibling.firstChild;
			}catch(e) {
				nextnode = false;
			}

			try{
				prevnode = initdomel.parentNode.previousSibling.firstChild;
			}catch(e){
				prevnode = false;
			}

			try{
				fcnode = initdomel.nextSibling.firstChild.firstChild;
			}catch(e){
				fcnode = false;
			}

			try{
				panode = initdomel.parentNode.parentNode.previousSibling;
			}catch(e){
				panode = 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++)
			{
				//alert('class='+ uls[i].parentNode.className );
				if(uls[i].parentNode.className == openClass || uls[i].parentNode.className == 'firstli '+openClass || uls[i].parentNode.className == 'lastli '+openClass){
					cssjs('add',uls[i],showClass);
				}else{
					cssjs('add',uls[i],hideClass);
				}
			}	

			// loop through all links of inside finder
			lis=document.getElementById('finder').getElementsByTagName('li');
			//alert('lis:'+ lis.length);
	
			for(i=0;i<lis.length;i++)
			{

		
		//		alert('lis[i].firstChild.nodeValue): '+ lis[i].firstChild.firstChild.nodeValue );

				if(lis[i].getElementsByTagName('a').length == 0){
					continue;
				}

				var temp_pid = lis[i].getElementsByTagName('a')[0].href.split('=')[1];
				lis[i].getElementsByTagName('a')[0].href='#' + temp_pid;
	
				var newa = lis[i].getElementsByTagName('a')[0];
		

		




			}




			Y.on("click", handleClick, "#finder a");

			function handleClick(e){
					var oTarget = e.currentTarget; 
					var sID = oTarget.get("id");
					//alert('click called: '+ sID );
					//var tid = e.currentTarget.get("id");
					var domEL = document.getElementById(sID);
					//alert('node:'+ domEL);


					domEL.blur();
					var pid = parseInt( domEL.href.split('#')[1] );
					getcont( pid );

					// 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(var j=0;j<uls[i].getElementsByTagName('ul').length;j++)
						{
					
							if(	uls[i].getElementsByTagName('ul')[j] == domEL.parentNode.getElementsByTagName('ul')[0]  || 
								uls[i].getElementsByTagName('ul')[j] == domEL.parentNode.parentNode ||
								uls[i] == domEL.parentNode.parentNode 
							)
							{
								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',domEL,parentClass,openClass);
			
					

					if( domEL.parentNode.getElementsByTagName('ul')[0].getElementsByTagName('li').length > 0 ){
						// show the current nested list
						bodylevelclass = domEL.parentNode.getElementsByTagName('ul')[0].attributes['class'].nodeValue.split(' ')[0];
						cssjs('add',domEL.parentNode.getElementsByTagName('ul')[0],showClass);
					}else {
						bodylevelclass = domEL.parentNode.parentNode.attributes['class'].nodeValue.split(' ')[0];
						cssjs('add',domEL,openClass);
					}
					document.body.attributes['class'].nodeValue = bodylevelclass +' domenabled';

					
					try{
						nextnode = domEL.parentNode.nextSibling.firstChild;
					}catch(e) {
						nextnode = false;
					}

					
					try{
						prevnode = domEL.parentNode.previousSibling.firstChild;
					}catch(e){
						prevnode = false;
					}
					//alert('prevnode: '+ prevnode);

					
					try{
						fcnode = domEL.nextSibling.firstChild.firstChild;
					}catch(e){
						fcnode = false;
					}
					//alert('fcnode: '+ fcnode);

					
					try{
						panode = domEL.parentNode.parentNode.previousSibling;
					}catch(e){
						panode = false;
					}
					//alert('panode: '+ panode);




					// don't follow the real HREF of the link					try { e.preventDefault();  }catch(e){ }finally{}
					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;
				}
			}

			function getcont(pid){
				//alert('pid: '+ pid);
				loadURL = 'index.php?id='+ pid;
				Y.io( loadURL +'&type=96');
			}
	
		}

/*
		function simulateClick(cb) {
		  var evt = document.createEvent("MouseEvents");
		  evt.initMouseEvent("click", true, true, window,
		    0, 0, 0, 0, 0, false, false, false, false, 0, null);
		  //var cb = document.getElementById(domel); 
		  var canceled = !cb.dispatchEvent(evt);
		  if(canceled) {
		    // A handler called preventDefault
		    //alert("canceled");
		  } else {
		    // None of the handlers called preventDefault
		    //alert("not canceled");
		  }
		}

*/

		function simulateClick(obj){
			var evt = 'click';
			var fireOnThis = obj;
			if( document.createEvent ) {
				var evObj = document.createEvent('MouseEvents');
				evObj.initEvent( evt, true, false );
				fireOnThis.dispatchEvent(evObj);
			} else if( document.createEventObject ) {
				fireOnThis.fireEvent('on'+evt);
			}
		}



		var handle = Y.on('key', function(e) { 
	       // Y.log(e.type + ": " + e.keyCode + ' -- ' + arg1); 
			//alert('e.keyCode:'+ e.keyCode);
			try { e.preventDefault();  }catch(e){ }finally{}

			switch (e.keyCode) {
			    case 37:
				//alert('left');
				if(panode){
					simulateClick(panode);
				}
				break;    
			    case 38:
				//alert('up'); 
				if(prevnode){
					simulateClick(prevnode);
				}
				break;    
			    case 39:
				//alert('right');  
				if(fcnode){
					simulateClick(fcnode);
				}
				break;    
			    case 40:
				//alert('down');
				
				if(nextnode){
					//alert('nextnode:'+ nextnode); 
					//Y.Event.simulate(nextnode, "click");
					simulateClick(nextnode);
				}
				break;    
			 }
			
			d.setAttribute('style', 'overflow:hidden;');
			window.scrollTo(0,0);


	    }, document, 'up:37,38,39,40' );
		

		// ### global listener object 
		var gH = {
			write: function(str, args) {
					 d.innerHTML += "ID: " + str;
					 if (args) {
					 	d.innerHTML += " " + "The arguments are: " + args;
					 }
					 d.innerHTML += "<br>";
				   },
			start: function(id, args) {
					 d.innerHTML = '<img src="/fileadmin/template/loading9.gif" alt="Loading Contents" />';
				   },
			complete: function(id, o, args) {
						this.write(id + ": Global Event Complete.  The status code is: " + o.status + ".", args);
				   },
			success: function(id, o, args) {
					//alert('TEST: '+ o.responseText );
					d.innerHTML = o.responseText;
					window.scrollTo(0,0);
					d.setAttribute('style', 'overflow:visible;');

					if (typeof pageTracker != "undefined"){
						pageTracker._trackPageview( loadURL );
					} 
				},
			failure: function(id, o, args) {
					   this.write(o + ": Global Event Failure.  The status text is: " + o.statusText + ".", args);
					 },
			end: function(id, args) {
					// this.write(id + ": Global Event End.", args);
			}
		}
		// ### end global listener object 

 

		// ###  attach global listeners 
		Y.on('io:start', gH.start, gH, 'global foo');
		Y.on('io:complete', gH.complete, gH, 'global bar');
		Y.on('io:success', gH.success, gH, 'global baz');
		Y.on('io:failure', gH.failure, gH);
		Y.on('io:end', gH.end, gH, 'global boo');
		// ###  end global listener binding 



/*

// #### not needed ###  


		// ### transaction event object 
		var tH = {
			write: function(str, args) {
					 d.innerHTML += "ID: " + str;
					 if (args) {
					 	d.innerHTML += " " + "The arguments are: " + args;
					 }
					 d.innerHTML += "<br>";
				   },
			start: function(id, args) {
					 this.write(id + ": Transaction Event Start.", args.start);
				   },
			complete: function(id, o, args) {
						this.write(id + ": Transaction Event Complete.  The status code is: " + o.status + ".", args.complete);
				   },
			success: function(id, o, args) {
					   this.write(id + ": Transaction Event Success.  The response is: " + o.responseText + ".", args.success);
					 },
			failure: function(id, o, args) {
					   this.write(id + ": Transaction Event Failure.  The status text is: " + o.statusText + ".", args.failure);
					 },
			end: function(id, args) {
					 this.write(id + ": Transaction Event End.", args.end);
			}
		}
		// ### end transaction event object


		// ###  configuration object for transactions 
		var cfg = {
			on: {
				start: tH.start,
				complete: tH.complete,
				success: tH.success,
				failure: tH.failure,
				end: tH.end
			},
			context: tH,
			headers: { 'X-Transaction': 'GET Example'},
			arguments: {
					   start: 'foo',
					   complete: 'bar',
					   success: 'baz',
					   failure: 'Oh no!',
					   end: 'boo'
					   }
		};
		// ###  end configuration object 

		function call(e, b) {
			if (b) {
				Y.io('assets/get.php?user=YDN&allListeners=1', cfg);
			}
			else {
				Y.io('assets/get.php?user=YDN&globalListeners=1');
			}
		}

		Y.on('click', call, "#get1", this, false);
		Y.on('click', call, "#get2", this, true);




*/

Y.on("contentready", ul2finder, '#all');

	});



/*
// Check if the browser supports DOM, and start the script if it does.
if(document.getElementById && document.createTextNode)
{
	window.onload=ul2finder;
}

*/

