/*
 * Initial Version of the Tree Viewer
 */

var TREEVIEW_INSTANCE = 0;
function TreeView(EventManager) {
	var MySelf = this;
	var TreeViewId = 'TreeView'+TREEVIEW_INSTANCE+'-';
	TREEVIEW_INSTANCE++;

	var MapBookData = null;

	var Parent;

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

	this.addObject = function(mapXMLObj) {
		var title = mapXMLObj.getAttribute('title');
		var checkBox = document.getElementById(TreeViewId+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 = '0px';
	}

	var visibilityOverride = false;

	this.setVisibilityOverride = function (v) {
		visibilityOverride = v;
	}

	function onclickLeaf(event) {
		if(window.event) { event = window.event; }
		if(getEventTarget(event) != this) { return true; }
		var childTree = this.getElementsByTagName('ul')[0];
		var vis = childTree.style.visibility;
		if(childTree.style.height != '0px') {
			height = '0px';
			var allUl = childTree.getElementsByTagName('ul');
			for(var u = 0; u < allUl.length; u++) {
				makeInvisible(allUl[u]);
			}
		} else {
			height = 'auto';
		}
		childTree.style.height = height;
		return false;

	}

	var Branches = 0;
	
	function createBranch(xmlObj) { // Assumes the Element Has Children
		var root = document.createElement('ul');
		//root.className = 'TreeView';
		//root.style.marginLeft = '24px';
		root.setAttribute('id',TreeViewId+'Branch-'+Branches);
		Branches++;
		
		if(document.all) {
			root.style.marginLeft = '24px';
		} else {
			root.style.paddingLeft = '24px';
		}
		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]);
					var underList = createBranch(xmlObj.childNodes[i]);
					underList.style.height = '0px';
					underList.style.overflow = 'hidden';
					rootLeaf.appendChild(underList);
					root.appendChild(rootLeaf);
					rootLeaf.onclick = onclickLeaf;
					if(xmlObj.childNodes[i].getAttribute('expand') && xmlObj.childNodes[i].getAttribute('expand').match(/true/)) {
						underList.style.height = 'auto';
					}
				} else if(xmlObj.childNodes[i].tagName != null && xmlObj.childNodes[i].getAttribute('title') != null) {
					/*var newItem = getLeaf(xmlObj.childNodes[i]);	
					root.appendChild(newItem);*/
					root.appendChild(getLeaf(xmlObj.childNodes[i]));
				}
			}
		}
		return root;
	}

	function onclickLayer() {
		if(this.checked) {
			EventManager.triggerEvent('addlayer', this.selfXML);
		} else {
			this.checked=true;
			if(EventManager.onRemoveLayer(this.selfXML.getAttribute('title'))) {
				this.checked=false;
			}
		}
	}

	function onclickService() {
		if(this.checked) {
			EventManager.triggerEvent('addservice', this.selfXML);
		} else {
			this.checked=true;
			if(EventManager.onRemoveService(this.selfXML.getAttribute('title'))) {
				this.checked=false;
			}
		}
	}

	function onclickView() {
		if(this.checked) {
			EventManager.triggerEvent('addview', this.selfXML);
		} else {
			this.checked=true;
			if(EventManager.triggerEvent('removeview',this.selfXML.getAttribute('title'))) {
				this.checked=false;
			}
		}
	}
	
	function getLeaf(obj) {
		var newLi = document.createElement('li');
		var objTitle = obj.getAttribute('title');
		newLi.style.cursor = 'pointer';

		newLi.className = 'Folder';
		newLi.id = TreeViewId+'-List-'+objTitle;

		if(obj.getAttribute('tip')) {
			newLi.title = obj.getAttribute('tip');
		}

		if(obj.getAttribute('icon')) {
			var iconUrl = obj.getAttribute('icon');
			newLi.style.listStyleType = 'none';
			newLi.style.listStyleImage = 'url('+iconUrl+')';
		} else if(obj.tagName.match(/map/)) {
			newLi.className = 'Map';
		} else if(obj.tagName.match(/service/)) {
			newLi.className = 'Service';
		} else if(obj.tagName.match(/view/)) {
			newLi.className = 'View';
		}
		if(obj.tagName != 'group') {
			var layerCheckBox = document.createElement('input');
			layerCheckBox.setAttribute('type','checkbox');
			layerCheckBox.id = TreeViewId+objTitle;
			layerCheckBox.selfXML = obj;
			newLi.appendChild(layerCheckBox);	

			if(obj.tagName.match(/map/i)) {
				layerCheckBox.onclick = onclickLayer; 
			} else if(obj.tagName.match(/service/i)) {
				layerCheckBox.onclick = onclickService;
			} else if(obj.tagName.match(/view/i)) {
				layerCheckBox.onclick = onclickView;
			}

		}

		newLi.appendChild(document.createTextNode(objTitle));
		if(obj.getElementsByTagName('meta')[0] && obj.getElementsByTagName('meta')[0].firstChild.nodeValue) {
			if(obj.getElementsByTagName('meta')[0].parentNode == obj) {
				var metaLink = document.createElement('a');
				newLi.appendChild(metaLink);
				metaLink.href = obj.getElementsByTagName('meta')[0].firstChild.nodeValue;
				var metaImage = document.createElement('img');
				metaImage.src = META_DEFAULT_ICON;
				metaLink.appendChild(metaImage);
				metaImage.title = 'Metadata';
				metaImage.alt = 'Metadata';
				metaLink.className = 'Metadata';
				metaLink.target = '_blank';
				metaImage = null;
				metaLink = null;
			}
		}
		if(TREEVIEW_SHOW_CONTROLS && obj.tagName == 'map') {
			newLi.appendChild(document.createElement('br'));
			newLi.appendChild(createIcon(EventManager, UP_ICON,'movelayerup', objTitle));
			newLi.appendChild(createIcon(EventManager, DOWN_ICON,'movelayerdown',objTitle));
			newLi.appendChild(createIcon(EventManager, FADE_ICON,'fadelayer',objTitle));
			newLi.appendChild(createIcon(EventManager, UNFADE_ICON,'unfadelayer',objTitle));
			newLi.appendChild(createIcon(EventManager, RELOAD_ICON,'refreshlayer', objTitle));
		}

		if(TREEVIEW_SHOW_LEGENDS && obj.tagName == 'map') {
			newLi.appendChild(document.createElement('br'));
			var legendImage = document.createElement('img');
			legendImage.setAttribute('id', TreeViewId+'Legend-'+objTitle);
			newLi.appendChild(legendImage);
			legendImage = null;
		}
		return newLi;
	}

	this.onLoadLegend = function(legendObj) {
		var obj = document.getElementById(TreeViewId+'Legend-'+legendObj.id);
		if(obj) {
			obj.src = legendObj.legend;
		}
	}
	function getLeaf_xx(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 = TreeViewId+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(L, 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) {
		document.getElementById(Parent).removeChild(Tree);
		this.loadMapbook(mbXML);
	}

	this.loadMapbook = function(mbXML) {
		MapBookData = mbXML.getElementsByTagName('mapbook')[0];
		if(Parent) {
			document.getElementById(Parent).appendChild(createBranch(MapBookData));
		}
	}

	this.setParent = function(p) {
		Parent = p;
		if(MapBookData) {
			document.getElementById(Parent).appendChild(createBranch(MapBookData));
		}
	}
}
