function FundeOverview() {
	this.speciesContainer = null;
	this.speciesImageContainer = null;	
	this.speciesImage = null;
	this.speciesImageDescription = null;
	var lthis = this;
	this.speciesSelector = new SpeciesSelector(
					function(taxon) { lthis.taxonActivated(taxon); },
					function(list) { });
	this.speciesSelector.autoResize = false;
	this.speciesSelector.openGroupsOnMiddleBar = false;
	this.speciesSelector.taxonsRightIcon = '/imgs/icon_karte.png';
	
	this.mapContainer = null;
	this.mapHeaderTaxonName = null;

	this.aktionenKarte = new AktionenKarte();
	this.aktionenKarte.aktionenListeSichtbar = false;
	this.aktionenKarte.aktionenSuchresultate = [];
	this.aktionenKarte.aktionenSuchresultateIDs = {};
	
	this.ortSuchen = null;
	
	this.currentYear = document.location.pathname.split('/')[2];
	if (this.currentYear == '' || this.currentYear == null) this.currentYear = 'alle';
	
	this.registerCallbacks();
}
FundeOverview.prototype = {
	taxonActivated: function(taxon) {
		this.loadAktionenForTaxon(taxon);
		this.setMapHeader(taxon);
		this.speciesContainer.style.display = 'none';
		this.mapContainer.style.visibility = 'visible';

		this.aktionenKarte.aktionenListeSichtbar = true;
		this.aktionenKarte.setLoading();
		document.getElementById('aktionenStatistik').style.display = 'none';
		this.aktionenKarte.aktionenListe.style.display = 'block';
		this.aktionenKarte.aktionenListe.style.top = (190 + 110) + 'px';
		this.aktionenKarte.setAktionenListeHeader("Aktionen");
	},
	
	goNewSearch: function() {
		this.speciesContainer.style.display = 'block';
		this.mapContainer.style.visibility = 'hidden';
		
		this.aktionenKarte.aktionenListeSichtbar = false;
		this.aktionenKarte.aktionenListe.style.display = 'none';
		document.getElementById('aktionenStatistik').style.display = 'block';
		this.aktionenKarte.setAktionenListeHeader("Statistik");
	},

	setMapHeader: function(taxon) {
		clearNode(this.mapHeaderTaxonName);
		var syn = '';
		if (taxon.synonyms.length > 0) {
			syn = ' (' + taxon.synonyms.join(', ') + ')';
		}
		this.mapHeaderTaxonName.appendChild(document.createTextNode(taxon.name + syn + ', ' + taxon.numFound));
	},
	
	loadAktionen: function() {
		var lthis = this;
		
		makeHTTPRequest('/ajax/aktionen/' + this.currentYear, '',
    				function(request, success) {
    					var aktionen = request.responseXML.getElementsByTagName('aktion');
    					lthis.aktionenKarte.doLoadAktionen(aktionen); });
	},
	
	loadAktionenForTaxon: function(taxon) {
		var lthis = this;
		makeHTTPRequest('/ajax/funde/' + taxon.id + '/' + this.currentYear, '',
    				function(request, success) {
    					var list = request.responseXML.getElementsByTagName('list')[0];
						var image = list.getAttribute('speciesImage');
						if (taxon.findable && image != null) {
							lthis.speciesImage.src = image;
				            clearNode(lthis.speciesImageDescription);
				            lthis.speciesImageDescription.appendChild(document.createTextNode(list.getAttribute('speciesName')));
							lthis.speciesImageContainer.style.visibility = 'visible';
						} else {
							lthis.speciesImageContainer.style.visibility = 'hidden';
						}
    					lthis.aktionenKarte.zeigeAktionenSucheResultate(request, success);
    					lthis.aktionenKarte.zoomToAktionenSuchResultate();
    				});
    },
	
	registerCallbacks: function() {
		var lthis = this;
		var el = document.getElementById('aktionenListe');
		if (el == null) {
			window.setTimeout(function() { lthis.registerCallbacks(); }, 50);
			return;
		}

		this.aktionenKarte.registerCallbacks();
		this.findElements();
                this.aktionenKarte.setAktionenListeHeight(358);
		this.loadAktionen();
		
		this.speciesSelector.showAllKingdoms();
	},
	
	findElements: function() {
		var lthis = this;

		this.speciesContainer = document.getElementById('speciesContainer');
		this.mapContainer = document.getElementById('mapContainer');
		this.mapHeaderTaxonName = document.getElementById('mapHeaderTaxonName');
		document.getElementById('goNewSearch').onclick = function() { lthis.goNewSearch(); };
		document.getElementById('showAllKingdoms').onclick = function() { lthis.speciesSelector.showAllKingdoms(); };
		
		this.speciesImageContainer = document.getElementById('speciesImageContainer');
		this.speciesImage = document.getElementById('speciesImage');
		this.speciesImageDescription = document.getElementById('speciesImageDescription');
		this.speciesSelector.year = this.currentYear;
		this.speciesSelector.registerCallbacks();
		
		var linkList = document.getElementById('yearChangeLinkList');
		var links = linkList.getElementsByTagName('a');
		for (var i = 0; i < links.length; i ++) {
			links[i].onclick = function() {
				lthis.savePositionInCookie();
				document.location.href = this.href;
			}
		}
		
		this.ortSuchen = document.getElementById('ortSuchen');
	    this.ortSuchen.onkeypress = function(e) {
	    	var ev = e || window.event;
	    	if (ev.keyCode == 13) 
	    		lthis.aktionenKarte.geocoder.getLocations(lthis.ortSuchen.value,
	    					function(result) {lthis.locationFound(result); });
	    };
	},
	
	initZoomButtons: function(container) {
		var lthis = this;
		var select = document.createElement('select');
		for (var id in this.zoomPoints) {
			var button = document.createElement('option');
			button.appendChild(document.createTextNode(id));
			select.appendChild(button);
		}
		container.appendChild(select);
		select.onchange = function() { lthis.aktionenKarte.zoomTo(this.value || this.options[this.options.selectedIndex].text); return false; };
	},
	
	locationFound: function(result) {
		if (result.Status.code != 200 || result.Placemark.length == 0) {
			alert("Ort nicht gefunden.");
			return;
		}
        var coords = result.Placemark[0].Point.coordinates;
		this.aktionenKarte.map.setCenter(new google.maps.LatLng(coords[1], coords[0]), 10);
	}
}

var fundeOverview = new FundeOverview();

function fundeOverviewZoom(where) {
    fundeOverview.aktionenKarte.zoomTo(where);
    return false;
}

