var LOADING_BAR_INSTANCE = 0;
/*
Events.addEventListener('imagelayer', 'Loading', LoadingBar.loadedLayer);
Events.addEventListener('addlayer', 'Loading', LoadingBar.addLayer);
Events.addEventListener('removelayer', 'Loading', LoadingBar.removeLayer);
Events.addEventListener('refreshlayer', 'Loading', LoadingBar.reloadLayer);
Events.addEventListener('navigate', 'Loading', LoadingBar.reloadAll);
*/

function GLoadingBar(EventManager, LoadingMap) {
	var LayersInQueue = new Array();
	var LoadingBarId;
	var MyInstance = LOADING_BAR_INSTANCE;
	LOADING_BAR_INSTANCE++;

	this.addLayer = function (mapObjXML) {
		var title = mapObjXML.getAttribute('title');
		LayersInQueue[title] = 0;
	}


	this.removeLayer = function (title) {
		var deleted = 'no';
		if(LayersInQueue[title] || LayersInQueue[title] == 0) {
			delete LayersInQueue[title];
			deleted = 'yes';
		}
		if(allLoaded()) {
			hideLoadingPanel();
		}
	}

	function allLoaded() {
		for(var l in LayersInQueue) {
			if(LayersInQueue[l] > 0) {
				return false;
			}
		}
		return true;
	}


	function hideLoadingPanel () {
		if(document.getElementById(LoadingBarId)) {
			document.getElementById(LoadingBarId).style.visibility = 'hidden';
		}
	}

	function showLoadingPanel () {
		positionLoadingPanel();
		if(document.getElementById(LoadingBarId)) {
			document.getElementById(LoadingBarId).style.visibility = 'visible';
		}
	}
	
	this.loadedLayer = function (title) {
		LayersInQueue[title] = 0;
		if(allLoaded()) {
			hideLoadingPanel();
		}
	}

	this.getUnloadedList = function () {
		var list = new String();
		for(var l in LayersInQueue) {
			if(LayersInQueue[l] > 0) {
				list = list+l+'\n';
			}
		}
		return list;
	}
	function positionLoadingPanel() {
		if(!LoadingBarId || !LoadingMap) { return false; } /* Can't align an object if it doesn't exist yet! */
		var loadingBar = document.getElementById(LoadingBarId);
		var barWidth = loadingBar.offsetWidth;
		var mapWidth = LoadingMap.getMapSize().x;
		var mapHeight = LoadingMap.getMapSize().y;
		var mapLeft = 0;
		var barHeight = loadingBar.offsetHeight;
		var barLeft = mapLeft + (mapWidth - barWidth ) / 2;
		var barTop = (mapHeight - barHeight) / 2;
		loadingBar.style.left = barLeft + 'px';
		loadingBar.style.top = barTop + 'px';
	}
	
	var ReloadIteration = 0;

	this.reloadAll = function () {
		for(var l in LayersInQueue) {
			LayersInQueue[l] = 1;
		}
		showLoadingPanel();
		ReloadIteration++;
		setTimeout("document.getElementById('"+LoadingBarId+"').timeout("+ReloadIteration+")",LOADING_TIMEOUT);
	}

	this.reloadLayer = function (title) {	
		LayersInQueue[title] = 1;
		showLoadingPanel();
	}

	MySelf = this;

	function timeoutCheck (iteration) {
		if(iteration == ReloadIteration && !allLoaded()) {
			var timeoutInSeconds = LOADING_TIMEOUT / 1000;
			var unloadedList = MySelf.getUnloadedList();
			if(confirm('Timeout! The following layers took longer than '+timeoutInSeconds+' seconds to load:\n'+unloadedList+'Would you like me to remove it?')) {
				var arr = unloadedList.split('\n');
				for(var i = 0; i < arr.length; i++) {
					EventManager.onRemoveLayer(arr[i]);
				}
			}
			
			hideLoadingPanel();
		}
			
	}
	this.setParent = function (p) {
		var parent = document.getElementById(p);
		var loadingBar = document.createElement('div');
		loadingBar.className = 'LoadingBar';
		parent.appendChild(loadingBar);

		LoadingBarId = 'LoadingBar'+MyInstance;
		loadingBar.setAttribute('id', LoadingBarId);
		loadingBar.appendChild(document.createTextNode(LOADING_MESSAGE));
		loadingBar.appendChild(document.createElement('br'));
		loadingBar.timeout = timeoutCheck;

		var loadingImage = document.createElement('img');
		loadingImage.src = LOADING_IMAGE;
		loadingBar.appendChild(loadingImage);

		loadingImage = null;
		loadingBar=null;
		positionLoadingPanel();
		
	}
}
