/*		
*		scripts by house of laudanum, 2006
*		house@laudanum.net http://laudanum.net
*
*		Creative Commons Attribution-Noncommercial-Share Alike 2.5  License
*		http://creativecommons.org/licenses/by-nc-sa/2.5/
*
*		Creative Commons Developing Nations license
*		http://creativecommons.org/licenses/devnations/2.0/
*/


		var target_node, recurse, menu_level, target_level, use_header, menu_header, ignore_files, automating
		recurse = 0
		menu_level = 0
		target_level = 0
		use_header = 1
		menu_header = 'Select...'
		ignore_files = 1
		automating = 0
		
		/* Create a new XMLHttpRequest object to talk to the Web server */
		var xmlHttp = false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				xmlHttp = false;
			}
		}
		@end @*/
		
		if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
			xmlHttp = new XMLHttpRequest();
		}
		
		
		function initMenu(node) {
			target_node = node
			callServer('/')			
		}		
		

		function callServer(directory) {
			setDiv('info',"TX")
			
//			alert(directory)

	//	firefox cross domain protection
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			} catch (e) {
//				alert("Permission UniversalBrowserRead denied.");
			}
   
			// Build the URL to connect to
//			var url = "http://localhost/cgi-bin/path.pl?directory=" + escape(directory) + "&recurse=" + recurse
			var url = "/cgi-bin/path.pl?directory=" + escape(directory) + "&recurse=" + recurse

//			debug(url)
	
			if ( url == "/cgi-bin/path.pl?directory=/&recurse=0" && document.all )
				xmlHttp.open("GET", "/cgi-bin/path.pl?directory=/", true);
			else	
				xmlHttp.open("GET", url, true);
		
			// Setup a function for the server to run when it's done
			xmlHttp.onreadystatechange = updatePage;
		
			// Send the request
			xmlHttp.send(null);
		}		
		
		
		function updatePage () {
			window.status = xmlHttp.readyState
//			debug(xmlHttp.readyState)
			
			if (xmlHttp.readyState == 1) {
			//	loading		
				setDiv('info',"RX")
				document.getElementById('progress').src = 'http://www.firstdraftgallery.com/_images/spacer.gif'
			}
			if (xmlHttp.readyState == 2) {
			//	loaded
				setDiv('info',"OK")
			}
			if (xmlHttp.readyState == 3) {
			//	interactive	
				setDiv('info',"INIT")
			}
			if (xmlHttp.readyState == 4) {
			//	complete
				var response = xmlHttp.responseText;
				var responseXML = xmlHttp.responseXML;

//				debug(responseXML.hasChildNodes())
//				debug(response)

				setDiv('info',responseXML)
				buildMenu(target_node, responseXML)				
				document.getElementById('progress').src = 'http://www.firstdraftgallery.com/_images/spacer.gif'
				subdirs = location.pathname.split("/")

/*
				for ( i = 1; i < subdirs.length - 2; i++ ) {
				
					that = document.getElementById('menu_'+ (menu_level-1))
					for ( j = 0; j < that.options.length; j++ ) {
						if ( that.options[j].value.indexOf(escape(subdirs[i])) > -1 ) {
							that.options[j].selected = true
							update(that, (menu_level-1))
							break
						}
					}
				}
*/
			}
		}
		
		
		function buildMenu(node, xml) {

//			debug(xml.childNodes[0])
			
//	explorer ??
			if ( document.all )
				x = xml.childNodes[1]
			else
				x = xml.firstChild
	
			
			if ( !x.childNodes.length ) {
				window.location.href = x.getAttribute('path')
				return 0
			}

			id = 'menu_'+target_level
//			debug('id '+ document.getElementById(id))

			if ( document.getElementById(id)) {
				window.status = ('already created ' + id)
				m = document.getElementById(id).owner
				m.reset()
			} else {
//				debug("none by that id :: " + id)
//				debug(x)
//				debug(x.getAttribute('path'))
				m = new Menu(node, x.getAttribute('path'))
			}
						
			for ( i = 0; i < x.childNodes.length; i++ ) {
				e = x.childNodes[i]
//	this catches problems in the url, for example, apostrophes
				url = e.firstChild.parentNode.getAttribute('path')
//	debug(url)
				if ( !url ) {
					return
				} 
//	ignore the top level index page
				else if ( url.toLowerCase().indexOf('/index') == 0 )
					continue
				else if ( url.toLowerCase().indexOf('index') >= 0 ) {
//	if this contains an index page, jump away
					window.location.href = url
					return
				}				
				m.addItem(e.firstChild)
			}
	
//	moved this here to see if it stopped unnecessary display of third menu, it didn't
			node.appendChild(m.list)
			m.list.style.display = 'inline'
			m.list.style.visibility = 'visible'			
			
			junk = location.pathname.split("/")
			if ( m.level < junk.length - 3 )
				update(m.list, m.level)
			
		}
		
		
		function Menu(node, root) {
		
			this.level = menu_level++
			this.path = root
			
			this.list = document.createElement('select')
			this.list.level = this.level
			this.list.id = 'menu_'+this.list.level
			this.list.owner = this
			this.list.style.display = 'none'
			this.list.style.visibility = 'hidden'
			
//			node.appendChild(this.list)
			this.list.setAttribute('onChange', 'update(this, '+this.level+')')
//	explorer
			if ( document.all) {
				this.list.onchange = function () {
					eval("update(this, " + this.level + ")")
				}
			}
			
			if ( use_header )
				this.addOption(menu_header, '')
		}
		
		
		function addOption(string, url) {			
			if ( document.all ) {
				this.list.options[this.list.options.length] = new Option(string, url);
			}
    		else {
				item = document.createElement('option')
			
				value = document.createTextNode(string)
				item.value = url
				item.appendChild(value)
				this.list.appendChild(item)
			}
		}
		
		
		function addItem(node) {
			type = node.parentNode.getAttribute('type')
			url = node.parentNode.getAttribute('path')
			string = node.nodeValue
						
//	ignore root level index page
			if ( url.toLowerCase().indexOf('/index') == 0 )			
				return
				
//	if this contains an index page, jump away
			if ( node.nodeValue.toLowerCase().indexOf('index') == 0 ) {
				window.location.href = url
				return
			}
				
			if ( type == 'file' && ignore_files )
				return

			if ( type == 'directory' )
				url += "/"

			if ( document.all ) {
				selected = location.pathname.indexOf(url) + 1
				this.list.options[this.list.options.length] = new Option(string, url);
				this.list.options[this.list.options.length-1].selected = selected
			}
    		else {
				item = document.createElement('option')
				value = document.createTextNode(node.nodeValue)
				item.value = url
				item.appendChild(value)

//	test the current location to see if it is described by this option
//	safari is escaped, firefox is not
				if (( escape(location.pathname).indexOf(url) > -1 ) || ( location.pathname.indexOf(url) > -1 )) {
					item.selected = true
				}
				this.list.appendChild(item)
			}
		}
		
		
		function reset() {
			while ( menu_level > this.level + 1 ) {
				id = 'menu_'+(menu_level-1)
				node_to_remove = document.getElementById(id)
				node_to_remove.parentNode.removeChild(node_to_remove)
				menu_level--
			}

			while(this.list.firstChild)
				this.list.removeChild(this.list.firstChild);
			
			if ( use_header )
				this.addOption(menu_header, '')
		}
				
		
		Menu.prototype.addItem = addItem
		Menu.prototype.reset = reset
		Menu.prototype.addOption = addOption


		function update(that, level) {
		
			if ( automating ) {
				automating = false
				return
			}
		
			if ( use_header && that.selectedIndex == 0)
				return	
			target_level = level+1

			if ( document.all ) {
				callServer(that[that.selectedIndex].value)
				return
			}
			
//	what does this do because it fucks explorer
//	looks like it tries to detect a trailing slash
//	but our folders don't have trailing slashes

			if ( that[that.selectedIndex].value.lastIndexOf('/')+1 != that[that.selectedIndex].value.length )
				location.replace(that[that.selectedIndex].value)
			else
				callServer(that[that.selectedIndex].value)
		}
		
		
		function setDiv (id, content) {
				document.getElementById(id).innerHTML = content
		}


		function hide (id) {
			document.getElementById(id).style.display = 'none'
		}


		function generateTitle() {
			title = location.href
			setDiv('location', title)
			junk = title.split("/")
			title = junk[junk.length-2]
			setDiv('heading', title)
		}

		
		function debug(msg) {
			if (document.all)
				alert(msg)
		}
