/*
 * Initial Version of the Tree Viewer
 */

function TreeView(parentXML) {

	function makeInvisible(elm) {
		elm.style.height = '1px';
	}

	function createBranch(xmlObj) { // Assumes the Element Has Children
		var root = document.createElement('ul');
		for(var i = 0; i < xmlObj.childNodes.length; i++) {
			if(xmlObj.childNodes[i].tagName == 'group') {
				var rootLeaf = getLeaf(xmlObj.childNodes[i], 0);
				var underList = createBranch(xmlObj.childNodes[i]);
				underList.style.height = '1px';
				underList.style.overflow = 'hidden';
				root.appendChild(rootLeaf);
				root.appendChild(underList);
				rootLeaf.childTree = underList;
				rootLeaf.onclick = function() {
					var vis = this.childTree.style.visibility;
					if(this.childTree.style.height != '1px') {
						height = '1px';
						var allUl = this.childTree.getElementsByTagName('ul');
						for(var u = 0; u < allUl.length; u++) {
							makeInvisible(allUl[u]);
						}
					} else {
						height = 'auto';
					}
					this.childTree.style.height = height;
					return false;
				}
			} else if(xmlObj.childNodes[i].tagName != null && xmlObj.childNodes[i].getAttribute('title') != null) {
				var newItem = getLeaf(xmlObj.childNodes[i], 0);	
				root.appendChild(newItem);
			}
		}
		return root;
	}


	function getLeaf(obj, depth) {
		var newLi = document.createElement('span');
		var imgIcon = document.createElement('img');
		newLi.onmousedown = clearSelection;
		newLi.style.cursor = 'pointer';
		if(obj.tagName == 'group') {
			newLi.onclick = function () {
				if(this.parentNode.className == 'Selected') {
					alert('Add All Children of '+this.parentNode.id);
				}
			}
			imgIcon.src = FOLDER_ICON;
		} else {
			var layerCheckBox = document.createElement('input');
			layerCheckBox.setAttribute('type','checkbox');
			var enabled = true;
			var checked = false;
				var objTitle = obj.getAttribute('title');

			if(layerObjs[obj.getAttribute('title')]) {
				checked = true;
				layerCheckBox.defaultChecked = true;
				layerCheckBox.setAttribute('checked', 'true');
				if(obj.getAttribute('locked') && obj.getAttribute('locked').match(/true/i)) {
					layerCheckBox.disabled = true;
				}

			}

			if(obj.tagName.match(/map/i)) {
				layerCheckBox.onclick = function() {
					if(!layerObjs[objTitle]) {
						addMapLayer(parentXML, objTitle, objTitle, true);
					} else {
						if(confirm('Are you sure you want to remove "'+objTitle+'"?')) {
							layerObjs[objTitle].remove();
							delete layerObjs[objTitle];
						}
					}

				}
			} else if(obj.tagName.match(/service/i)) {
				layerCheckBox.onclick = function() {
					if(!layerObjs[objTitle]) {
						addService(objTitle,objTitle);
					} else {
						if(confirm('Are you sure you want to remove "'+objTitle+'"?')) {
							layerObjs[objTitle].remove();
							delete layerObjs[objTitle];
						}
					}

				}
			}




			//function addService(serviceName, serviceTitle) {
			//layerCheckBox = createCheckBox(checked,enabled,onAction,doNadda);
			newLi.appendChild(layerCheckBox);
			var iconUrl = UNKNOWN_DEFAULT_ICON;
			/* If no custom icon is specified - then use a default one */
			if(obj.getAttribute('icon')) {
				iconUrl = obj.getAttribute('icon');
			} else if(obj.tagName.match(/map/)) {
				iconUrl = MAP_DEFAULT_ICON;
			} else if(obj.tagName.match(/service/)) {
				iconUrl = SERVICE_DEFAULT_ICON;
			} else if(obj.tagName.match(/view/)) {
				iconUrl = VIEW_DEFAULT_ICON;
			}
			imgIcon.src = iconUrl;
			/*newLi.onclick = function(event) {
				if(window.event) {
					event = window.event;
				}
			}*/
		}
		newLi.appendChild(imgIcon);
		var titleText = obj.getAttribute('title');
		newLi.appendChild(document.createTextNode(titleText));
		//newLi.innerHTML = newLi.innerHTML + titleText;
		newLi.appendChild(document.createElement('br'));
		return newLi;
	}

	this.getTree = function() {
		return createBranch(parentXML);
	}
}
