
function triggerSelfEvent() {
	if(this.eventTrigger == null) { return false; } /* Don't anger the event manager will null values */
	if(typeof this.eventTrigger == "object") {
		for(var i = 0; i < this.eventTrigger.length; i++) {
			this.EventManager.triggerEvent(this.eventTrigger[i], this.mapTitle);
		}
	} else {
		this.EventManager.triggerEvent(this.eventTrigger, this.mapTitle);
	}
}



var VISIBLE_LAYERS_INSTANCE = 0;

function VisibleLayers(EventManager,VisibleMap) {
	var ListParent;
	var LayersListId = 'LayersList'+VISIBLE_LAYERS_INSTANCE+'-';
	VISIBLE_LAYERS_INSTANCE++;

	var LayerIcons = new Array();
//	var LayerIconObjs = new Array();
	var MySelf = this;

	this.clearActiveLayerSelection = function() {
		var layersList = document.getElementById(LayersListId);
		var radios = layersList.getElementsByTagName('input');
		for(var i = 0; i < radios.length; i++) {
			radios[i].checked = false;
		/*	radios[i].parentNode.style.border = 'none';*/
			if(radios[i].parentNode.className == 'SelectedLayer') {
				radios[i].parentNode.className = 'ActiveLayer';
			}
		}
	}

	function activateLayer() {
		var title = this.getAttribute('LayerName');
		EventManager.triggerEvent('changetool', title);
		EventManager.triggerEvent('activatelayer', title);
		/** Old Selection is Cleared by the Event Manager **/
		if(!this.disabled) {
			this.checked = true;
			this.parentNode.className = 'SelectedLayer';
		}
	}

	this.sortListByZIndex = function() {
		var ordered = VisibleMap.getLayersByZIndex();
		var p = document.getElementById(LayersListId);
		for(var i = 0; i < ordered.length; i++) {
			var obj = document.getElementById(LayersListId+ordered[i]);
			if(obj) {
				p.appendChild(obj);
			}
		}
	}

	this.addLayer = function(mapXMLObj) {
		var title = mapXMLObj.getAttribute('title');

		var ListItem = document.createElement('li');
		ListItem.id = LayersListId+title;

		var alreadyHere = document.getElementById(ListItem.id);
		if(alreadyHere) return false;

		layersList = document.getElementById(LayersListId);
		layersList.appendChild(ListItem);	
		if(MAP_POPUPS_ENABLED) {
			var ActivateLayer = document.createElement('input');
			ActivateLayer.type = 'radio';
			ActivateLayer.name = 'activateLayer';
			ActivateLayer.disabled = true;
			ActivateLayer.id = 'DataLayer-'+title;
			ActivateLayer.setAttribute('LayerName', title);
			ActivateLayer.onclick = activateLayer;
			ListItem.appendChild(ActivateLayer);
		}

		var LegendImg = document.createElement('img');
		LegendImg.id = 'LayerLegend-'+title;
		LegendImg.style.visibility = 'hidden';

		if(mapXMLObj.getAttribute('icon')) {
			LayerIcons[title] = mapXMLObj.getAttribute('icon');
		}
		
		var titleLi = document.createElement('li');
		titleLi.id = LayersListId+'Icon-'+title;
		titleLi.className = 'VisibleLayersMap'
		if(LayerIcons[title]) {
			titleLi.style.listStyleImage = 'url('+LayerIcons[title]+')';
		}

		ListItem.appendChild(titleLi);
		titleLi.appendChild(document.createTextNode(title));

		if(mapXMLObj.getElementsByTagName('help')[0] && mapXMLObj.getElementsByTagName('help')[0].firstChild.nodeValue) {
			var helpIcon = document.createElement('img');
			helpIcon.src = HELP_DEFAULT_ICON;
			helpIcon.onclick = function () {
				window.open(mapXMLObj.getElementsByTagName('help')[0].firstChild.nodeValue);
			}
			ListItem.appendChild(helpIcon);
		}

		ListItem.appendChild(document.createElement('br'));

		ListItem.appendChild(createIcon(EventManager, UP_ICON,'movelayerup', title));
		ListItem.appendChild(createIcon(EventManager, DOWN_ICON,'movelayerdown',title));
		ListItem.appendChild(createIcon(EventManager, FADE_ICON,'fadelayer',title));
		ListItem.appendChild(createIcon(EventManager, UNFADE_ICON,'unfadelayer',title));
		ListItem.appendChild(createIcon(EventManager, RELOAD_ICON,'refreshlayer', title));
		if(!mapXMLObj.getAttribute('locked') || !mapXMLObj.getAttribute('locked').match(/true/i)) {
			ListItem.appendChild(createIcon(EventManager, REMOVE_ICON,'removelayer',title));
		}
		if(mapXMLObj.getElementsByTagName('meta')[0] && mapXMLObj.getElementsByTagName('meta')[0].firstChild.nodeValue) {
			var metaIcon = document.createElement('img');
			metaIcon.src = META_DEFAULT_ICON;
			metaIcon.style.verticalAlign = 'middle';
			metaIcon.onclick = function () {
				window.open(mapXMLObj.getElementsByTagName('meta')[0].firstChild.nodeValue);
			}
			ListItem.appendChild(metaIcon);
		}

		ListItem.appendChild(document.createElement('br'));
		ListItem.appendChild(LegendImg);

		if(parseInt(mapXMLObj.getAttribute('zindex')) == -1 || !layersList.firstChild) {
			layersList.appendChild(ListItem);
		} else {
			layersList.insertBefore(ListItem, layersList.firstChild);
		}
		MySelf.sortListByZIndex();

	}

	this.activateLayer = function(mapTitle) {
		var opt = document.getElementById('DataLayer-'+mapTitle);
		if(opt) {
			opt.disabled = false;
			opt.parentNode.className = 'ActiveLayer';
		}
	}

	this.disableLayers = function() {
		var layersList = document.getElementById(LayersListId);
		if(layersList) {
			var opts = layersList.getElementsByTagName('input');
			for(var i = 0; i < opts.length; i++) {
				if(opts[i].type == 'radio') {
					opts[i].disabled = true;
					opts[i].checked = false;
					opts[i].parentNode.className = '';
				}
			}
		}

	}

	this.removeLayer = function(mapTitle) {
		var ListItem = document.getElementById(LayersListId+mapTitle);
		if(LayerIcons[mapTitle]) {
			delete LayerIcons[mapTitle];
		}
		if(ListItem) {
			var p = ListItem.parentNode;
			p.removeChild(ListItem);
//			document.getElementById(LayersListId).removeChild(ListItem);
		}
	}

	this.showLegend = function(xmlObj) {
		var title = xmlObj.id;
		var src = xmlObj.legend;
		

		// This is hack for IE, it does not properly resize the image upon
		// loading.
		var tempImage = new Image();
		tempImage.onload = function () {
			var LegendImg = document.getElementById('LayerLegend-'+title);
			if(!LegendImg) {
				return false;
			}
			LegendImg.style.visibility = 'visible';
			LegendImg.style.width = 'auto';
			LegendImg.style.height = 'auto';
			LegendImg.src = src;
			LegendImg.style.width = this.width+'px';
			LegendImg.style.height = this.height+'px';
		}
		tempImage.src=  src;
	}
	
	this.moveLayerUp = function(title) {
		var listItem = document.getElementById(LayersListId+title);
		var layersList = document.getElementById(LayersListId);
		if(listItem && listItem.previousSibling) {
			layersList.insertBefore(listItem, listItem.previousSibling);
		}
		MySelf.sortListByZIndex();
	}

	this.moveLayerDown = function(title) {
		var listItem = document.getElementById(LayersListId+title);
		var layersList = document.getElementById(LayersListId);
		if(listItem && listItem.nextSibling) {
			layersList.insertBefore(listItem.nextSibling, listItem);
		}
		MySelf.sortListByZIndex();
	}

	function setIconClass(title, iconClass) {
		var icon = document.getElementById(LayersListId+'Icon-'+ title);
		if(!icon) { return false; } /* Don't attempt to set the className of an object that doesn't exist */
		if(!iconClass) {
			icon.className = 'VisibleLayersMap';
		} else {
			icon.className = iconClass;
		}

	}

	function setIcon(title, iconSrc) {
		var icon = document.getElementById(LayersListID+'Icon-'+ title);
		if(!iconSrc) {
			icon.style.listStyleImage = '';	
		} else {
			icon.style.listStyleImage = 'url('+iconSrc+')';
		}
	}

	this.setAllRefreshIcon = function () {
		for(var title in LayerIcons) {
			setIconClass(title, 'VisibleLayersMapLoading');
		}
	}
	
	this.setRefreshIcon = function(title) {
		setIconClass(title, 'VisibleLayersMapLoading');
	}
	
	this.setLoadedIcon = function(title) {
		var icon = document.getElementById(LayersListId+'Icon-'+title);
		if(icon) {
			icon.src = LayerIcons[title];
		}
	}



	function enableOpacityControls() {
		if(confirm(OPACITY_CONTROLS_ENABLE_WARNING)) {
			OPACITY_CONTROLS = 'full';
			this.onclick = null;
			this.style.color = 'black';
			this.innerHTML = 'Opacity Controls Enabled.';
			setTimeout("document.getElementById('"+this.id+"').style.visibility = 'hidden';", 2000);
		}
	}
	
	this.setParent = function(parent) {
		ListParent = parent;
		var layersList = document.createElement('ul');
		layersList.className = 'VisibleLayers';
		layersList.setAttribute('id',LayersListId);
		document.getElementById(ListParent).appendChild(layersList);
		if(OPACITY_CONTROLS == 'prompt' || (OPACITY_CONTROLS == 'auto' && getBrowser().match(/microsoft/i) && getBrowserVersion() < MAP_IE_SCALE_STYLE_SWITCH)) {
			var a = document.createElement('a');
			document.getElementById(ListParent).appendChild(a);
			a.href = 'javascript:function nil() { }';
			a.appendChild(document.createTextNode('Enable Opacity Controls'));
			a.onclick = enableOpacityControls;
			a.setAttribute('id', LayersListId+'OpacityControl');
			with(a.style) {
				display = 'block';
				position = 'absolute';
				bottom = '20px';
				padding = '5px';
				textDecoration = 'none';
				width = '90%';
				color = 'red';
				textAlign = 'center';
				fontWeight = 'bold';
				
			}
			a = null;
		}	
	}

	this.initializeEvents = function() {
		EventManager.addEventListener('addlayer', LayersListId, MySelf.addLayer);
		EventManager.addEventListener('datalayer', LayersListId, MySelf.activateLayer);
		EventManager.addEventListener('navigate', LayersListId, [MySelf.disableLayers, MySelf.setAllRefreshIcon]);

		EventManager.addEventListener('legendlayer', LayersListId, MySelf.showLegend);
		EventManager.addEventListener('removelayer', LayersListId, MySelf.removeLayer);
		EventManager.addEventListener('changetool', LayersListId, MySelf.clearActiveLayerSelection);
		EventManager.addEventListener('movelayerup', LayersListId, MySelf.moveLayerUp);
		EventManager.addEventListener('movelayerdown', LayersListId, MySelf.moveLayerDown);
		EventManager.addEventListener('refreshlayer', LayersListId, MySelf.setRefreshIcon);
		EventManager.addEventListener('imagelayer', LayersListId, MySelf.setLoadedIcon);
	}
}
