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 LayersToIgnore = {};

	var MyInstance = LOADING_BAR_INSTANCE;
	LOADING_BAR_INSTANCE++;
	var LoadingBarId = 'LoadingBar-'+MyInstance;

	var MySelf = this;

	this.addLayer = function (mapObjXML) {
		var title = mapObjXML.getAttribute('title');
		var reference = new String(mapObjXML.getAttribute('reference')).toLowerCase();
		if(reference != 'only') {
			LayersInQueue[title] = 0;
		} else {
			LayersToIgnore[title] = true;
		}
	}


	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';
		}
		if(document.getElementById('LoadingBarDisableNav'+MyInstance)) {
			document.getElementById('LoadingBarDisableNav'+MyInstance).style.visibility = 'hidden';
		}
	}

	function showLoadingPanel () {
		positionLoadingPanel();
		if(document.getElementById(LoadingBarId)) {
			document.getElementById(LoadingBarId).style.visibility = 'visible';
		}

		if(document.getElementById('LoadingBarDisableNav'+MyInstance)) {
			document.getElementById('LoadingBarDisableNav'+MyInstance).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);
		if(!loadingBar) return false; 

		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 disableNav = document.getElementById('LoadingBarDisableNav'+MyInstance);
		disableNav.style.top = '0px';
		disableNav.style.left = '0px';
		disableNav.style.width = mapWidth + 'px';
		disableNav.style.height = mapHeight + 'px';
		disableNav.className = 'LoadingBarDisableNav';

	}
	
	var ReloadIteration = 0;

	/*
	 * I had to add a "relative" check to each of these functions
	 * because IE seems to be incapable of dealing with it.
	 */
	this.reloadAll = function () {
		for(var l in LayersInQueue) {
			if(!LoadingMap.isRelativeLayer(l) && LoadingMap.isValidLayer(l)) {
				if(!LayersToIgnore[l]) {
					LayersInQueue[l] = 1;
				}
			}
		}
		showLoadingPanel();
		if(allLoaded()) {
			hideLoadingPanel();
		}
		ReloadIteration++;
		setTimeout("document.getElementById('"+LoadingBarId+"').timeout("+ReloadIteration+")",LOADING_TIMEOUT);
	}

	this.reloadLayer = function (title) {	
		if(!LoadingMap.isRelativeLayer(title) && LoadingMap.isValidLayer(title)) {
			if(!LayersToIgnore[title]) {
				LayersInQueue[title] = 1;
			}
		}
		showLoadingPanel();

		if(allLoaded()) {
			hideLoadingPanel();
		}
	}

	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);

		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);

		var disableNav = document.createElement('div');
		parent.appendChild(disableNav);
		disableNav.style.top = '0px';
		disableNav.style.left = '0px';
		disableNav.style.width = parent.offsetWidth + 'px';
		disableNav.style.height = parent.offsetHeight + 'px';
		disableNav.className = 'LoadingBarDisableNav';
		disableNav.setAttribute('id', 'LoadingBarDisableNav'+MyInstance);

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

	this.initializeEvents = function() {
		EventManager.addEventListener('imagelayer', LoadingBarId, MySelf.loadedLayer);
		EventManager.addEventListener('addlayer', LoadingBarId, MySelf.addLayer);
		EventManager.addEventListener('removelayer', LoadingBarId, MySelf.removeLayer);
		EventManager.addEventListener('refreshlayer', LoadingBarId, MySelf.reloadLayer);
		EventManager.addEventListener('navigate', LoadingBarId, MySelf.reloadAll);
	}

}
