// JavaScript Document

var t0;

var init_time = function() {
	t0 = (new Date()).getTime();
//	t0 = Now.getTime();	
}();

function elapsed_time() {
	var n = new Date();
	var s = n.getTime();
	var diff = s - t0;
	return diff;
}

function search_tree(nodeList, container, query) {
	var i = nodeList.childNodes.length;
	while(node = nodeList.childNodes[--i]) {
		if(node.nodeName == container ) {
			var j = node.childNodes.length;
			while(j && (cn = node.childNodes[--j])) {
				for( test in query ) {
					if(cn.nodeName == test && cn.firstChild.nodeValue == query[test]) {
						var data = node.firstChild;
						var retval = new Array();
						do {
							if(data.nodeType == 1) {
								retval[data.nodeName] = data.firstChild.nodeValue;
							}
						} while(data = data.nextSibling);
						return retval;
					}
				}
			}
		}
	}
	return false;
	//higgins 8, atre 16
}

var foo = function() {
	Bootstrap.Loader.Enqueue({name:'interface', verify:function(){ return typeof Bootstrap.Interface == 'undefined' ? false : true; }});
	Bootstrap.Loader.Enqueue({name:'request', verify:function(){ return typeof Bootstrap.Request == 'undefined' ? false : true; }});

	Bootstrap.Loader.Load(
		function() {
			var data = Bootstrap.Ajax.get('data/faculty.xml', null, function(xhr) { 
				data = xhr.responseXML.documentElement; 
			});
			
			var personAttrs = {
				image: {
					container: 'img',	
					src: function(v) { return v; }
				},
				email: {
					container: 'span',
					className: function(v) { 'faculty-email'; },
					innerHTML: function(v) { return '<span class="label">E-mail</span>: <a href="mailto:' + v + '">' + v + '</a>'; }
				},
				phone: {
					container: 'span',
					className: function(v) { 'faculty-phone'; },
					innerHTML: function(v) { return '<span class="label">Phone</span>: ' + v.replace(/ /g,'-'); }
				},
				address: {
					container: 'span',
					className: function(v) { 'faculty-address'; },
					innerHTML: function(v) { return v.replace(/\n/g,'<br />'); }
				},
				website: {
					container: 'span',
					className: function(v) { 'faculty-website'; },
					innerHTML: function(v) { return '<span class="label">Website</span>: <a href="../' + v + '">' + v + '</a>'; }
				}
			}
			
			var people = $$('name');
			Bootstrap.Interface.cancel(people);
			
			people.on('click', function(el){
				var target = $(Bootstrap.Core.getTarget(el));
				var parent = target.elements[0].parentNode;
				
				if( Bootstrap.Core.hasClass(parent, 'faculty-active')) {
					Bootstrap.Interface.removeClass($(parent), 'faculty-active');
					var n = parent.firstChild;
					do {
						if(n.className == 'faculty-extendedinfo') {
							parent.removeChild(n);
							break;
						}
					} while (n = n.nextSibling);
				}
				else {
					if(person = search_tree(data, 'person', {name:target.elements[0].innerHTML})) {
						Bootstrap.Interface.addClass($(parent), 'faculty-active');

						var extended = document.createElement('div');
						extended.className = 'faculty-extendedinfo';
						
						var count = 0;

						for(attr in person) {
							if(a = personAttrs[attr]) {
								var tmp_el = document.createElement(a.container);
								for(el_attr in a) {
									if(el_attr!='container') {
										tmp_el[el_attr] = a[el_attr](person[attr]);
									}
								}
								extended.appendChild(tmp_el);
								++count;
							}
						}
						
						if(!count) {
							extended.innerHTML = '<em>No information available.</em>';
						}	
						
						parent.appendChild(extended);
					}
				}
			});
		}
	);
}
Bootstrap.Core.addEvent(window,'load',foo);

Bootstrap.XML = function() {
	
}();

/*function search_tree(dataSet, container, query) {
	init_time();
	var nodeList = dataSet.getElementsByTagName(container);
	
	var i = nodeList.length;
	while(node = nodeList[--i]) {
		var j = node.childNodes.length;
		while( j && (childNode = node.childNodes[--j])) {
			var flag = false;
			for( test in query ) {
				if( childNode.nodeName == test && childNode.firstChild.nodeValue == query[test]) {
					flag = true;
				} else if( childNode.nodeName == test && childNode.firstChild.nodeValue != query[test]) {
					flag = false;
				}
			}
			
			if(flag) {
				var data = node.firstChild;
				var retval = new Array();
				do {
					if(data.nodeType == 1) {
						retval[data.nodeName] = data.firstChild.nodeValue;
					}
				} while(data = data.nextSibling);
				alert(elapsed_time());
				return retval;
			}
		}
	}
	
	// max: 17ish
	// alert(elapsed_time());
}*/