/*

	== PHP FILE TREE JAVASCRIPT EXTENSION ==
		
		Based on the Expandable Listmenu Script by Daniel Nolan
		http://www.bleedingego.co.uk/webdev.php
		
		Modified by Cory S.N. LaViska
		http://abeautifulsite.net/
		
	== WHAT IT DOES ==
	
		This script makes the nested lists created by PHP File Tree expand and 
		collapse dynamically.
		
	== USAGE ==
	
		Include the script into the <head></head> section of the appropriate 
		page(s) as shown below:
	
			<script src="php_file_tree.js" type="text/javascript"></script>
			
		All file trees generated by PHP File Tree will automatically collapse to 
		the top level (as specified by $directory) and become dynamic.

	== FAQS ==
	
		Q Can I have more than one file tree on one page?
		A Yes.  You can have as many as you want and they will all function as expected.
		
*/

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
  init_php_file_tree();
  
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;


function init_php_file_tree() {
	if (!document.getElementsByTagName) return;
	
	var aMenus = document.getElementsByTagName("LI");
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		if (mclass.indexOf("pft-directory") > -1) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					
					submenu[j].onclick = function() {
						var node = this.nextSibling;
			
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "open" : "closed";
									return false;
								}
								node = node.nextSibling;
							} else {
								return false;
							}
						}
						return false;
					}
					
					submenu[j].className = (mclass.indexOf("open") > -1) ? "open" : "closed";
				}
				
				if (submenu[j].tagName == "UL")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
			if (submenu[j].tagName == "LI")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "block";
			}
		}
	}
	return false;
}
