
function triggerSelfEvent() {
	this.EventManager.triggerEvent(this.eventTrigger, this.mapTitle);
}

	function createIcon(EventManager, src,eventTrigger,mapTitle) {
		var newIcon = document.createElement('img');
		newIcon.setAttribute('src', src);
		newIcon.style.verticalAlign = 'middle';
		newIcon.EventManager = EventManager;
		newIcon.eventTrigger= eventTrigger;
		newIcon.mapTitle = mapTitle;
		newIcon.onclick = triggerSelfEvent;
		return newIcon;
	}
	

var VISIBLE_LAYERS_INSTANCE = 0;

function VisibleLayers(EventManager) {
	var ListParent;
	var LayersList = document.createElement('ul');
	var LayersListId = 'LayersList'+VISIBLE_LAYERS_INSTANCE+'-';
	VISIBLE_LAYERS_INSTANCE++;
	LayersList.className = 'VisibleLayers';

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


	this.clearActiveLayerSelection = function() {
		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.addLayer = function(mapXMLObj) {
		var title = mapXMLObj.getAttribute('title');

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

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

		LayersList.appendChild(ListItem);	

		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;
	
		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(ActivateLayer);
		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'));

/*		var upButton = createIcon(EventManager, UP_ICON,'movelayerup', title);
		var downButton = createIcon(EventManager, DOWN_ICON,'movelayerdown',title);
		var fadeButton = createIcon(EventManager, FADE_ICON,'fadelayer',title);
		var unfadeButton = createIcon(EventManager, UNFADE_ICON,'unfadelayer',title);
		var removeButton = createIcon(EventManager, REMOVE_ICON,'removelayer',title);
		var refreshButton = createIcon(EventManager, RELOAD_ICON,'refreshlayer', title);
*/
		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);
		}

	}

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

	this.disableLayers = function() {
		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('LayersList-'+mapTitle);
		if(LayerIcons[mapTitle]) {
			delete LayerIcons[mapTitle];
		}
		if(ListItem) {
			LayersList.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('LayersList-'+title);
		if(listItem && listItem.previousSibling) {
			LayersList.insertBefore(listItem, listItem.previousSibling);
		}
	}

	this.moveLayerDown = function(title) {
		var listItem = document.getElementById('LayersList-'+title);
		if(listItem && listItem.nextSibling) {
			LayersList.insertBefore(listItem.nextSibling, listItem);
		}
	}

	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];
		}
	}



	
	this.setParent = function(parent) {
		ListParent = parent;
		document.getElementById(ListParent).appendChild(LayersList);
	}
}
