/*
 * Initial Version of the Tree Viewer
 */

function TreeView(EventManager) {
	var MySelf = this;
	var parentXML = '';

	var Tree;
	var Parent;

	this.removeObject = function(id) {
		var checkBox = document.getElementById('TreeView-'+id);
		if(checkBox) {
			checkBox.checked = false;
		}
	}

	this.addObject = function(mapXMLObj) {
		var title = mapXMLObj.getAttribute('title');
		var checkBox = document.getElementById('TreeView-'+title);
		if(checkBox) {
			checkBox.checked = true;
		}
	}

	this.clearAll = function() {
		var inputs = document.getElementsByTagName('input');
		for(var i = 0; i < inputs.length; i++) {
			inputs[i].checked = false;
		}
	}

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

	var visibilityOverride = false;

	this.setVisibilityOverride = function (v) {
		visibilityOverride = v;
	}
	function createBranch(xmlObj) { // Assumes the Element Has Children
		var root = document.createElement('ul');
		root.className = 'TreeView';
		/*root.style.marginLeft = '5px';
		root.style.paddingLeft = '5px';*/
		for(var i = 0; i < xmlObj.childNodes.length; i++) {
			if(xmlObj.childNodes[i].tagName != null && (!xmlObj.childNodes[i].getAttribute('invisible') || !xmlObj.childNodes[i].getAttribute('invisible').match(/true/i) || visibilityOverride)) {
				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';

		var metaIcon, helpIcon;

		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 objTitle = obj.getAttribute('title');

			layerCheckBox.id = 'TreeView-'+objTitle;
	
			/*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(this.checked) {
					//	addMapLayer(parentXML, objTitle, objTitle, true);
						EventManager.triggerEvent('addlayer', obj);
					} else {
						this.checked=true;
						if(EventManager.onRemoveLayer(objTitle)) {
							this.checked=false;
						}
					}

				}
			} else if(obj.tagName.match(/service/i)) {
				layerCheckBox.onclick = function() {
					if(this.checked) {
						//addService(objTitle,objTitle);
						EventManager.triggerEvent('addservice',obj);
					} else {
						this.checked=true;
						if(EventManager.onRemoveService(objTitle)) {
							this.checked=false;
						}
					}

				}
			} else if(obj.tagName.match(/view/i)) {
				layerCheckBox.onclick = function() {
					if(this.checked) {
						EventManager.triggerEvent('addview', obj);
					} else {
						this.checked=true;
						EventManager.triggerEvent('removeview', objTitle);
						this.checked = false;
					}
				}
			}




			//function addService(serviceName, serviceTitle) {
			//layerCheckBox = createCheckBox(checked,enabled,onAction,doNadda);
			newLi.appendChild(layerCheckBox);
			if(obj.getAttribute('locked') && obj.getAttribute('locked').match(/true/i)) {
				layerCheckBox.disabled = true;
			}

			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.style.paddingTop = '1px';
			imgIcon.style.marginTop = '1px';
			imgIcon.src = iconUrl;

			/*newLi.onclick = function(event) {
				if(window.event) {
					event = window.event;
				}
			}*/

			if(obj.getElementsByTagName('meta')[0] && obj.getElementsByTagName('meta')[0].firstChild.nodeValue) {
				metaIcon = document.createElement('img');
				metaIcon.src = META_DEFAULT_ICON;
				metaIcon.style.paddingTop = '1px';
				metaIcon.style.marginTop = '1px';
				metaIcon.onclick = function () {
					window.open(obj.getElementsByTagName('meta')[0].firstChild.nodeValue);
				}
			}

			if(obj.getElementsByTagName('help')[0] && obj.getElementsByTagName('help')[0].firstChild.nodeValue) {
				helpIcon = document.createElement('img');
				helpIcon.src = HELP_DEFAULT_ICON;
				helpIcon.style.paddingTop = '1px';
				helpIcon.style.marginTop = '1px';
				helpIcon.onclick = function () {
					window.open(obj.getElementsByTagName('help')[0].firstChild.nodeValue);
				}
			}

		}
		newLi.appendChild(imgIcon);
		var titleText = obj.getAttribute('title');
		newLi.appendChild(document.createTextNode(titleText));

		if(metaIcon) { newLi.appendChild(metaIcon); }
		if(helpIcon) { newLi.appendChild(helpIcon); }

		//newLi.innerHTML = newLi.innerHTML + titleText;

		newLi.appendChild(document.createElement('br'));
		return newLi;
	}

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

	this.reloadMapbook = function(mbXML) {
		Parent.removeChild(Tree);
		this.loadMapbook(mbXML);
	}

	this.loadMapbook = function(mbXML) {
		parentXML = mbXML.getElementsByTagName('mapbook')[0];
		Tree = MySelf.getTree();
		if(Parent) {
			Parent.appendChild(Tree);
		}
	}

	this.setParent = function(p) {
		Parent = p;
		if(Tree) {
			p.appendChild(Tree);
		}
	}
}
