
function triggerSelfEvent() {
	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.src = BLANK_IMAGE;
		LegendImg.style.visibility = 'hidden';

		var LayerIcon = document.createElement('img');
		if(mapXMLObj.getAttribute('icon')) {
			LayerIcons[title] = mapXMLObj.getAttribute('icon');
		} else {
			LayerIcons[title] = MAP_DEFAULT_ICON;
		}
		LayerIcon.src = LayerIcons[title];
		LayerIcon.id = LayersListId+'Icon-'+title;
		//LayerIconObjs[title] = LayerIcon;

		ListItem.appendChild(LayerIcon);
		ListItem.appendChild(document.createTextNode(title));
		if(mapXMLObj.getElementsByTagName('meta')[0] && mapXMLObj.getElementsByTagName('meta')[0].firstChild.nodeValue) {
			var metaIcon = document.createElement('img');
			metaIcon.src = META_DEFAULT_ICON;
			metaIcon.onclick = function () {
				window.open(mapXMLObj.getElementsByTagName('meta')[0].firstChild.nodeValue);
			}
			ListItem.appendChild(metaIcon);
		}
		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));
		}
		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);
		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) {
			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();
	}

	this.setAllRefreshIcon = function () {
		for(var title in LayerIcons) {
			//LayerIconObjs[title].src = MAP_LOADING_ICON;
			document.getElementById(LayersListId+'Icon-'+title).src = MAP_LOADING_ICON;
		}
	}
	
	this.setRefreshIcon = function(title) {
		var icon = document.getElementById(LayersListId+'Icon-'+title);
		if(icon) {
			icon.src = MAP_LOADING_ICON;
		}
	}
	
	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;
		}	
	}
}
