function LayerListLib()
{
	//this.GroupNameArray = new Array();
	
	this.divArray = new Array();
	this.Container = null;
	this.DivLegend = document.getElementById("DIV_LayerList");
	this.Height = 450;
	this.Width = 275;
	
	this.Init = function()
	{
        var imgs = new Array();

        for (var i = 0; i < Map.Layers.length; i++)
        {
            imgs[i] = new Image();
            imgs[i].src = Map.Layers[i].urlImage;
        }
	}
	
	
	this.Resize = function()
	{
	    this.DivLegend.style.height = this.Height-50+"px";
	}
	
	this.ClearAllLayers = function()
	{
		for (i=0;i<Map.Layers.length;i++)
		{
			Map.Layers[i].Enabled = false;
		}
		this.Draw();
	}
	
		/*
	  Function to Actually draw the legend
	  Everything is inserted into the legendframe div
	  By Default the Groups are expanded but the Images for each layer are not
	*/
	this.Draw = function()
	{
		//var myGroups = this.GetGroups();	//Populate the GroupNameArray with the Groups
		var HTMLOutput = "<form id=\"LegendForm\" name=\"LegendForm\">";
        var CompletedGroups = new Array();
	    var GroupNameChecked = "";
		
		
		//Loop to make sure that the various layers belonging to a group get listed in the a appropriate order in the appropriate group
		for (j=0;j<Map.Layers.length;j++)
		{
			var thisLayer = Map.Layers[j];
			
			//Determine the Layer Group & if appropriate build the checkbox
			if ((thisLayer.GroupName != "") && (thisLayer.GroupName != null))
			{
				var GroupExists = false;
				
				//Check to see if this is a new group
				if (CompletedGroups.length>0)
				{
    				for (k=0;k<CompletedGroups.length;k++)
    				{
    				    if (thisLayer.GroupName == CompletedGroups[k])
    				    {
    				        GroupExists = true;
    				        break;
    				    }
    				}
    			}
    			
    			if (!GroupExists)
    			{
    				GroupNameChecked = "";
    				//Check if this group should be checked
 				    for (k=j;k<Map.Layers.length;k++)
				    {
				        if (Map.Layers[k].GroupName == thisLayer.GroupName)
				        {
					        if (Map.Layers[k].Enabled) 
					        {
						        GroupNameChecked = " checked ";
						        break;
					        }
					    }
                    }
                    
    				//Need to add all of the group information
    				HTMLOutput += 
				            "<div id=\"div" + thisLayer.GroupName + "Group\" style=\"DISPLAY: block; POSITION: relative;\">" + 
			                "<img src=\"Images/btnMinus.gif\" border=\"0\" onclick=\"if(document.getElementById('div" + thisLayer.GroupName + "Layers').style.display=='none'){this.src='Images/btnMinus.gif';document.getElementById('div" + thisLayer.GroupName + "Layers').style.display='block';}else{this.src='Images/btnPlus.gif';document.getElementById('div" + thisLayer.GroupName + "Layers').style.display='none';}\">" + 
				            "<input id=\"Layers" + thisLayer.GroupName + "\" type=\"checkbox\" onclick=\"LayerList.LayersOnOff(\'" + thisLayer.GroupName + "'\, this.checked);\"" + GroupNameChecked + ">" + 
				            "<b>" + thisLayer.GroupName + "</b>" +
				            "<div id=\"div" + thisLayer.GroupName + "Layers\" style=\"DISPLAY: block; POSITION: relative;\">";
				            

//////    				HTMLOutput += 
//////				            "<div id=\"div" + thisLayer.GroupName + "Group\" style=\"DISPLAY: block; POSITION: relative;\">" + 
//////			                "<a href=\"JavaScript:LayerList.ToggleDivLegend(\'div" + thisLayer.GroupName + "Group\');\"><img src=\"Images/btnMinus.gif\" border=\"0\" onclick=\"if((this.src).substr((this.src).length - 11)==\'btnPlus.gif\'){this.src=\'Images/btnMinus.gif\'}else{this.src=\'Images/btnPlus.gif\'}\"></a>" + 
//////				            "<input id=\"Layers" + thisLayer.GroupName + "\" type=\"checkbox\" onclick=\"LayerList.LayersOnOff(\'" + thisLayer.GroupName + "'\, this.checked);\"" + GroupNameChecked + ">" + 
//////				            "<b>" + thisLayer.GroupName + "</b>" +
//////				            "<div id=\"div" + thisLayer.GroupName + "Layers\" style=\"DISPLAY: block; POSITION: relative;\">";
//////				            
				    for (k=j;k<Map.Layers.length;k++)
				    {
				        if (Map.Layers[k].GroupName == thisLayer.GroupName)
				        {
			            var CheckedStr = (Map.Layers[k].Enabled)?"checked":"";
				            var LayerVisible = (Map.Layers[k].Visible)? " " : " disabled ";
                    	
					        HTMLOutput +=
						        "<div id=\"Layer" + Map.Layers[k].ArcIMSLayerID + thisLayer.GroupName + "\" style=\"DISPLAY: block; POSITION: relative; VERTICAL-ALIGN:Middle; white-space: nowrap\">" + 
					            "<img src=\"Images/btnNone.gif\" border=\"0\">" +
					            "<img src=\"Images/btnNone.gif\" border=\"0\">" +
					            "<img src=\"Images/btnNone.gif\" border=\"0\">" +
						        "<input id=\"Layer" + Map.Layers[k].ArcIMSLayerID + "\" type=\"checkbox\" " + LayerVisible + CheckedStr + " onclick=\"Map.Layers[" + k + "].Enabled=this.checked;\"> ";
						        
						    if ((Map.Layers[k].urlImage != "") && (Map.Layers[k].urlImage != null))
						    {
						        HTMLOutput +=
						            "<img src=\"Images/" + Map.Layers[k].urlImage + "\" border=0 />&nbsp;";
						    }
						    
					        HTMLOutput +=
						        Map.Layers[k].LayerName + 
						        "</div>";
                    		
   				        }				             
				    }
				            
				    HTMLOutput += "</div></div>";
    			
    				CompletedGroups.push(thisLayer.GroupName);
    			}
			}
			else
			{
				var CheckedStr = (thisLayer.Enabled)?"checked":"";
 				var LayerVisible = (thisLayer.Visible)? " " : " disabled ";
                
				HTMLOutput +=
					"<div id=\"Layer" + thisLayer.ArcIMSLayerID + thisLayer.GroupName + "\" style=\"DISPLAY: block; POSITION: relative; VERTICAL-ALIGN:Middle; white-space: nowrap\">" + 
					"<img src=\"Images/btnNone.gif\" border=\"0\">" +
					"<input id=\"Layer" + thisLayer.ArcIMSLayerID + "\" type=\"checkbox\" " + LayerVisible + CheckedStr + " onclick=\"Map.Layers[" + j + "].Enabled=this.checked;\"> ";
						
					if ((thisLayer.urlImage != "") && (thisLayer.urlImage != null))
					{
						HTMLOutput +=
						    "<img src=\"Images/" + thisLayer.urlImage + "\" border=0 />&nbsp;";
					}
					
					HTMLOutput +=
						thisLayer.LayerName + 
						"</div>";

			}
		}
		
		//Close the form & Update the HTML
		HTMLOutput = HTMLOutput + "</form>";
		this.DivLegend.innerHTML = HTMLOutput;
	}
	
	/*
		function to determine whether or not a group name should or shouldn't be checked based on the layers within the group
	*/
	this.LayersOnOff = function (GroupName, IsChecked)
	{
		//Loop through the layers in the map
		for (var i=0;i<Map.Layers.length;i++)
		{
			//if the layer is in the group, determine wther or not it is checked
			//if any layer has a check next to it, the group should be checked
			if (GroupName == Map.Layers[i].GroupName)
			{
				Map.Layers[i].Enabled = IsChecked;
				for (var j=0;j<document.LegendForm.elements.length;j++)
				{
					if (document.LegendForm.elements[j].id == "Layer" + Map.Layers[i].ArcIMSLayerID) 
					{
						document.LegendForm.elements[j].checked = IsChecked;
					}
			
				}
			}
		}
	}
	
	/*
	  function to show/hide the layers within each group
	*/
	this.ToggleDivLegend = function (DivID)
	{
		//Find the Group that should be opened/closed
		var divFound = 0;
		for (var i=0;i<this.divArray.length;i++)
		{
			if (this.divArray[i] == DivID) 
			{
				divFound = i;
			}	
		}
		//if open hide it, if closed display it		
		if (document.getElementById(this.divArray[divFound+1]).style.display == "block") 
		{
			document.getElementById(this.divArray[divFound+1]).style.display = "none";
		}
		else 
		{
			document.getElementById(this.divArray[divFound+1]).style.display = "block";
		}
	}
	
	/*
	  function to refresh the map & update the layers on/off
	*/
	this.RefreshMap = function ()
	{
		Map.LoadImage = true;
		Map.Refresh();
	}
	
	//this.Resize();
}