function legendObj(id)
{
	this.id = id;
	this.layers = new Array();
	this.groups = new Array();
	this.load = load;
	this.createNode = createNode;	
	this.createLegend = createLegend;	
	this.findLayer = findLayer;
	this.findGroup = findGroup;
	this.setSublayers = true;
	this.setGroups = false;
	this.showVAI = showVAI;
	this.VAI = false;
	this.expand = expand;
	this.getURL = getURL;
	var xmlDoc;
		
	function load()
	{
		var elem = document.getElementById(this.id);
		while (elem.firstChild)
			elem.removeChild(elem.firstChild);
		if (!xmlDoc)
		{
			if (document.implementation && document.implementation.createDocument)
			{
				xmlDoc = document.implementation.createDocument("", "", null);
				xmlDoc.onload = function() {
					legend.createLegend();
				}
			}
			else if (window.ActiveXObject)
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.onreadystatechange = function() {
					if (xmlDoc.readyState == 4) {
						legend.createLegend();
					}
				}
			}
			xmlDoc.async = true;  
		}
		var url = mapServerURL + "?User=&VName=" + map.viewName;
		url += "&Cmd=Legend";
		xmlDoc.load(url);
	}

	function createNode(parent, ul, node) {
		var li, span, img;
		if (node.nodeName == "LayGroup") {
		 	li = document.createElement("li");
			var group = new legendLayer(parent, node, li);			
			this.groups[this.groups.length] = group;

			hasChildren = false;
			for (var i = 0; (i < node.childNodes.length) && !hasChildren; i++) {
				var childNode = node.childNodes[i];
				if ((childNode.nodeName == "LayGroup" || childNode.nodeName == "Layer") &&
		 		    childNode.getAttribute("Hidden") == null) {
				    hasChildren = true;
          }
			}		 		

		  li.id = "g" + this.groups.length;

		 	img = document.createElement("img");

	 		if (hasChildren) {
	  		img.src = node.getAttribute("Expanded") != "0" ? "images/node_open.gif" : "images/node_closed.gif";
			  img.onclick = function() { onClickNode(this); }
				img.width = 15;
				img.height = 12;
				img.align = "top";
				img.style.marginTop = "3px";
			}	
			else {
			 	img.src = "images/node_item.gif"
			 	img.style.marginLeft = "5px";
			 	img.style.marginTop = "9px";
			 	img.align = "top";
			 	img.width = 9;
			 	img.height = 1;
			}
			li.appendChild(img);

		 	img = document.createElement("img");
	 		img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'l'); }
		 	img.align = "top";
		 	img.style.marginLeft = "2px";
		 	img.style.marginTop = "5px";
		 	img.width = 10; 
      img.height = 8;
	 		li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'v'); }
		 	img.align = "top";
		 	img.style.marginTop = "2px";
		 	img.style.marginLeft = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
		 	li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'a'); }
		 	img.align = "top";
		 	img.style.marginTop = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
		 	li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'i'); }
		 	img.align = "top";
		 	img.style.marginTop = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
		 	li.appendChild(img);

		 	span = document.createElement("span");
		 	span.className = "caption";
		 	li.appendChild(span);
		 	
		 	txt = document.createTextNode(group.caption);
		 	if (hasChildren) {
			 	var strong = document.createElement("strong");
			 	span.appendChild(strong);
			 	strong.appendChild(txt);
			}
			else {
				span.appendChild(txt);
			}
		 	group.updateView();
	  	var lastItem = null;
	  	if (node.childNodes.length > 0) {
			  var ul1 = document.createElement("ul");
			 	if (hasChildren) {
					if (node.getAttribute("Expanded") == "0") { 
			 			ul1.style.display = "none";
				 	}
			 	}
			 	li.appendChild(ul1);
				for (var i = 0; i < node.childNodes.length; i++) {  
				  var item = this.createNode(group, ul1, node.childNodes[i]);
				  if (item) lastItem = item;
			 	}
		 	}
	 		if (lastItem)  
	 			lastItem.elem.className = "last";
		 	if (group.hidden) 
				return null;
			ul.appendChild(li);
			return group;
		}
		if (node.nodeName == "Layer") {
		 	li = document.createElement("li");
			var layer = new legendLayer(parent, node, li);					
			this.layers[this.layers.length] = layer;
			if (layer.hidden) 
			  return null;
		 	li.id = "l" + this.layers.length;
		 	ul.appendChild(li);
		 	img = document.createElement("img");
		 	img.src = "images/node_item.gif"
		 	img.style.marginLeft = "5px";
		 	img.style.marginTop = "9px";
		 	img.align = "top";
		 	img.width = 9;
		 	img.height = 1;
			li.appendChild(img);
		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'l'); }
		 	img.align = "top";
		 	img.style.marginLeft = "2px";
		 	img.style.marginTop = "5px";
		 	img.width = 10;
		 	img.height = 8;
			li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'v'); }
		 	img.align = "top";
		 	img.style.marginLeft = "2px";
		 	img.style.marginTop = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
			li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'a'); }
		 	img.align = "top";
		 	img.style.marginTop = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
			li.appendChild(img);

		 	img = document.createElement("img");
		 	img.onclick = function() { onClickLayer(legend, this.parentNode.id, 'i'); }
		 	img.align = "top";
		 	img.style.marginTop = "2px";
		 	img.style.display = "none";
		 	img.width = 9;
		 	img.height = 12;
			li.appendChild(img);

		 	span = document.createElement("span");
			span.className = "caption";
			li.appendChild(span);
		 	txt = document.createTextNode(layer.caption);
		 	span.appendChild(txt);
		 	layer.updateView();
		 	return layer;
	  }
	  return null;
	}
	
	function createLegend() {
		var elem = document.getElementById(this.id);
		var root = xmlDoc.getElementsByTagName("Legend")[0];

		while (elem.firstChild)
			elem.removeChild(elem.firstChild);
		
		var p = document.createElement("p");
		elem.appendChild(p);
	 	txt = document.createTextNode(root.getAttribute("Name"));
	 	p.appendChild(txt);

		this.groups.length = 0;
		this.layers.length = 0;
		var lastItem = null;
	  
		var ul = document.createElement("ul");
		elem.appendChild(ul);

	  for (var i = 0; i < root.childNodes.length; i++) { 
			var item = this.createNode(null, ul, root.childNodes[i]);
			if (item) lastItem = item;
		}
		if (lastItem) {
			lastItem.elem.className = "last";
			if (!lastItem.group) lastItem.elem.childNodes[0].className = "last";
		}
		this.showVAI(this.VAI);
	}

	function showVAI(show)
	{
		for (var i = 0; i < this.groups.length; i++)
			this.groups[i].showVAI(show);
		for (i = 0; i < this.layers.length; i++)
			this.layers[i].showVAI(show);
		this.VAI = show;
	}

	function expand(v)
	{
		var legend = document.getElementById(id);
		var e = legend.getElementsByTagName("ul");
		for (var i = 1; i < e.length; i++) {
			var p = e[i].parentNode;
			while (p.tagName != "LI") p = p.parentNode;
			p.childNodes[0].src = v ? "images/node_open.gif" : "images/node_closed.gif";
 			e[i].style.display = v ? "" : "none";
 		}
	}
		
	function getURL()
	{
		if (this.groups.length == 0 && this.layers.length == 0)
			return null;
		var result = "";
		for (var i = 0; i < this.groups.length; i++)
		{
			if (this.groups[i].hidden)
			  continue;		
			if (result != "")
				result += ";";
			result += this.groups[i].id + ";" + this.groups[i].getState();
		}						
		for (i = 0; i < this.layers.length; i++)
		{
			if (this.layers[i].hidden)
			  continue;		
			if (result != "")
				result += ";";
			result += this.layers[i].id + ";" + this.layers[i].getState();
		}
		result = "Layers=" + result;
		return result;
	}
	
	function onClickNode(elem)
	{
	  node = elem.parentNode;
		node = node.getElementsByTagName("ul")[0];
	 	if (elem.src.indexOf("open") >= 0)
	 	{
	 		node.style.display = "none";
	 		elem.src = "images/node_closed.gif";
		}
		else
		{
	 		node.style.display = "";
	 		elem.src = "images/node_open.gif";
		}
	}
	
	function findGroup(num)
	{
		for (var i = 0; i < this.groups.length; i++)
		  if (this.groups[i].num == num)
		    return this.groups[i];
		return null;
	}
	
	function findLayer(num)
	{
		for (var i = 0; i < this.layers.length; i++)
		  if (this.layers[i].num == num)
		    return this.layers[i];
		return null;
	}

	function setSublayers(legend, group, type) {
		var layer;
		for (var i = 0; i < legend.layers.length; i++)
		{
			layer = legend.layers[i];
			if (legend.setGroups || legend.setSublayers && layer.parent == group)
			{
				switch (type)
				{
					case 'l':
						layer.visible = group.visible;
						break; 
					case 'v':
						layer.fillVisible = group.fillVisible;
						break; 
					case 'a':
						layer.attrVisible = group.attrVisible;
						break; 
					case 'i':
						layer.iconVisible = group.iconVisible;
						break; 
				}
				layer.updateView();
			}
		}
		if (!legend.SetGroups) {
			for (var i = 0; i < legend.groups.length; i++)
			{
				layer = legend.groups[i];
				if (layer.parent == group)
				{
					switch (type)
					{
						case 'l':
							layer.visible = group.visible;
							break; 
						case 'v':
							layer.fillVisible = group.fillVisible;
							break; 
						case 'a':
							layer.attrVisible = group.attrVisible;
							break; 
						case 'i':
							layer.iconVisible = group.iconVisible;
							break; 
					}
					layer.updateView();
					setSublayers(legend, layer, type);
				}
			}
		}
	}
	
	function onClickLayer(legend, id, type)
	{
		var elem = document.getElementById(id);
		var isGroup = id.charAt(0) == 'g';
		var layer = isGroup ? legend.groups[id.substring(1) - 1] :
												  legend.layers[id.substring(1) - 1];
		var parent, group;
		switch (type)
		{
			case 'l':
				layer.visible = !layer.visible;
				if (layer.visible) {
					parent = layer.parent;
					while (parent) {
						parent.visible = true;
						parent.updateView();
						parent = parent.parent;
					}
				}
				break; 
			case 'v':
				layer.fillVisible = !layer.fillVisible;
				if (layer.fillVisible) {
					parent = layer.parent;
					while (parent) {
						parent.fillVisible = true;
						parent.updateView();
						parent = parent.parent;
					}
				}
				break; 
			case 'a':
				layer.attrVisible = !layer.attrVisible;
				if (layer.attrVisible) {
					parent = layer.parent;
					while (parent) {
						parent.attrVisible = true;
						parent.updateView();
						parent = parent.parent;
					}
				}
				break; 
			case 'i':
				layer.iconVisible = !layer.iconVisible;
				if (layer.iconVisible) {
					parent = layer.parent;
					while (parent) {
						parent.iconVisible = true;
						parent.updateView();
						parent = parent.parent;
					}
				}
				break; 
		}
		layer.updateView();
		if (isGroup && legend.setSublayers) {
			setSublayers(legend, layer, type);
		}
		if (isGroup && legend.setGroups)
		{
			group = layer;
			for (var i = 0; i < legend.groups.length; i++)
			{
				var layer = legend.groups[i];
				if (layer != group)
				{
					switch (type)
					{
						case 'l':
							layer.visible = group.visible;
							break; 
						case 'v':
							layer.fillVisible = group.fillVisible;
							break; 
						case 'a':
							layer.attrVisible = group.attrVisible;
							break; 
						case 'i':
							layer.iconVisible = group.iconVisible;
							break; 
					}
					layer.updateView();
				}
			}
		}
		map.flashRepaintIcon(true);
	}
}
	
function legendLayer(parent, node, elem)
{
	this.id = node.getAttribute("ID");
	this.parent = parent;
	this.hidden = node.getAttribute("Hidden") == "1";
	if (!this.hidden) this.elem = elem;
	var state = node.getAttribute("State");
	this.visible = (state & 1) != 0;
	this.fillVisible = (state & 2) != 0;
	this.attrVisible = (state & 4) != 0;
	this.iconVisible = (state & 8) != 0;
	this.caption = node.getAttribute("Name");
	this.group = node.tagName == "LayGroup";
	this.updateView = updateView;
	this.showVAI = showVAI;
	this.getState = getState;
		
	function updateView()
	{
		if (this.elem)
		{
		 	this.elem.childNodes[1].src = this.visible ? "images/lay_on.gif" : "images/lay_off.gif";
		 	this.elem.childNodes[2].src = this.fillVisible ? "images/v_on.gif" : "images/v_off.gif";
		 	this.elem.childNodes[3].src = this.attrVisible ? "images/a_on.gif" : "images/a_off.gif";
		 	this.elem.childNodes[4].src = this.iconVisible ? "images/i_on.gif" : "images/i_off.gif";
		}	
	}
	
	function showVAI(show)
	{
		if (this.elem)
		{
			for (var i = 2; i < 5; i++)
			 	this.elem.childNodes[i].style.display = show ? "" : "none";
		}				
	}
	
	function getState()
	{
		var result = (this.visible) ? 1 : 0;
		if (this.fillVisible) result |= 2;
		if (this.attrVisible) result |= 4;
		if (this.iconVisible) result |= 8;
		return result;		
	}
}
