/** 
 *    Author: Arne Eckhoff <eckhoff@kdo.de>
 *      Date: 14.11.2007 16:50:00
 *   Project: KIM - Gis Bürgerclient
 * Copyright: 2007 Kommunale Datenverarbeitung Oldenburg (KDO)
 *      Link: http://www.kdo.de/
*/
// global variables

// Includes all markers, which were selected from the list
var MMgrBranchen = new Array();
// Includes all markers, which matches to the search term
var MMgrBranchenSuche = new Array(); 
// Have the list of branches already loaded
var isBranchenAuswahlLoaded = false;
// Holds the currently selected company ID of the search results
var selectedCompanySearch = 0;
// holds the currently selected company ID of the list with the selected branches
var selectedBrancheEintrag = 0;
	
// functions

// show the list, that contains the branches to select
// The list will be loaded from the database only the first time
function listBranchen()	{
	// If search result is still visible => reset and hide it
	if(document.getElementById('query_branchen_results').style.display!="none") {
		$("#query_branchen_results").hideSlow();
		map.clearOverlays(); // remove Search-Markers from map
		// determine if there already are markers from previous selections
		if(MMgrBranchen.length>0) {
			// show markers from previous selections
			for (var i in MMgrBranchen) {
				// get marker-Array from branch-Array
				var MMgrBranchenUnter = MMgrBranchen[i];
				for (var j in MMgrBranchenUnter) {
					map.addOverlay(MMgrBranchenUnter[j]); // add marker to map
				}
			}
			// Display the list of the companies entries
			showbranchenAdressen("true");
		}
	}
	
	// Determine if the list is already visible
	if(document.getElementById('branchenListe').style.display=="none") {
		if(!isBranchenAuswahlLoaded) {
			// first time loading, get data from database
			var kdnr = document.getElementById("kdnr").value;
			$("#branchenListe").html("<center>Lade Branchen...<br><br><img src='img/ajax_loader.gif'><br><br></center>").load("branchen_liste.php?kdnr=" + kdnr).showSlow();
			document.getElementById('branchenListe').style.visibility="visible";
			isBranchenAuswahlLoaded = true; // loaded
		} else {
			// not the first time, only show the list
			$("#branchenListe").showSlow();
			document.getElementById('branchenListe').style.display="block";
			document.getElementById('branchenListe').style.visibility="visible";
		}
		document.getElementById('branchenListe').style.display="block";
		document.getElementById('branchen_auswaehlen_up').style.visibility="visible";
		
	} else {
		// only hide the list
		$("#branchenListe").hideSlow();
		document.getElementById('branchen_auswaehlen_up').style.visibility="hidden";
	}
}


// Search within the companies entries
function queryBranche(f) {
	$("#branchenListe").hideSlow();
	showbranchenAdressen("false"); // hide selected companies
	wasShownBrancheSearch = true; // set global variable, if returning to this tab
	var suche_st = f.branche.value;
	var kdnr = document.getElementById("kdnr").value;
	// load the search result
	$("#query_branchen_results").html("Bitte warten...").load(f.action + "?kdnr=" + kdnr + "&" + $.param({close: 1, suche: suche_st})).showSlow();
}

	
// show or hide the companies entries
function showbranchenAdressen(value)	{
	if(value=="true") {
		$("#branchenAdressen").showSlow();
		document.getElementById('branchenAdressen').style.display="block";
		document.getElementById('branchenAdressen').style.visibility="visible";
		// set global variable, if returning to this tab
		wasShownBrancheList = true;
	} else {
		$("#branchenAdressen").hideSlow();
		// set global variable, if returning to this tab
		wasShownBrancheList = false;
	}
}


// add / remove entries of a branch from / to the map
function showBrancheOnMap(id, value) {
	var kdnr = document.getElementById("kdnr").value;
	if(value) {
		// true, load the script to add the markers and display the list of companies of the selected branch
		$("#branchenJS").html("").load("branchen_adressen.php?kdnr=" + kdnr + "&add="+id);	
		
	} else {
		// false, remove markers from the map
		var arrBranche = new Array();
		// get the selected branch to remove
		arrBranche = MMgrBranchen[id];
		for (i in arrBranche) {
			// remove the markers from map
			arrBranche[i].remove();
		}
		
		var branchen = "";
		// save the new MarkerManager for the branches
		var MMgrBranchenNeu = new Array();
		for (i in MMgrBranchen) {
			// if not the branch to remove...
			if(i!=id) {
				// ...add it to the new Marker Manager
				MMgrBranchenNeu[i] = MMgrBranchen[i];
				// ...add it to display in the company list
				branchen = branchen + i + ",";
			}
		}
		// updates the global MarkerManager for branches
		MMgrBranchen = MMgrBranchenNeu;
		
		// reload the list with the company entries
		$("#branchenAdressenListe").html("Lade Daten, bitte warten...").load("branchen_adressen.php?kdnr=" + kdnr + "&branchen="+branchen);	
	}
}


// filter the current search result
function filterBranche(f, suche) {
	var kdnr = document.getElementById("kdnr").value;
	// get filter keyword
	var filter_st = document.frmFilterBranche.branchefilter.value;
	// load filtered search result
	$("#query_branchen_results").load(f.action + "?kdnr=" + kdnr + "&suche=" + suche + "&" + $.param({close: 1, filter: filter_st}));
}


// close the search bar on the right and clear all search result markers
function closeClearBrancheSearch() {
	// clear markers
	map.clearOverlays();
	// reset global Marker Manager for branches
	MMgrBranchenSuche=new Array();
	// set global variable, if returning to this tab
	wasShownBrancheSearch=false;
	// hide results
	$('#query_branchen_results').hideSlow();
	// if there are selected branches...
	if(MMgrBranchen.length>0) {
		// ...show their markers
		for (var i in MMgrBranchen) {
			var MMgrBranchenUnter = MMgrBranchen[i];
			for (var j in MMgrBranchenUnter) {
				map.addOverlay(MMgrBranchenUnter[j]);
			}
		}
		// display the list with the companies of the selected branches
		showbranchenAdressen("true");
	}
}


// display the Marker and the InfoWindow of the given company
// set the backgroundcolor of the selected company in the list of search results
// called from iframe in branchen_suche.php
function showCompany(id,url) {
	// set new and reset old background color
	document.getElementById('firma'+id).style.backgroundColor = "#e5ebf1";
	if(selectedCompanySearch>0 && selectedCompanySearch!=id) {
		try {
			document.getElementById('firma'+selectedCompanySearch).style.backgroundColor = "#ffffff";
		} catch(ex) {
			selectedCompanySearch=0;
		}
	}
	selectedCompanySearch = id;
	// set the iframe with the given url 
	url = '<iframe frameborder="0" width="400" height="300" src="' + url + '"></iframe>';
	// open the marker Info window
	setMapCenter(MMgrBranchenSuche[id].getPoint());
	MMgrBranchenSuche[id].openInfoWindowHtml(url);
}


// display the Marker and the InfoWindow of the given company
// set the backgroundcolor of the selected company in the list
// called from iframe in branchen_adressen.php
function showBrancheEintrag(id,brancheid,url) {
	// set new and reset old background color
	document.getElementById('brancheEintrag'+id).style.backgroundColor = "#e5ebf1";
	if(selectedBrancheEintrag>0 && selectedBrancheEintrag!=id) {
		try {
			document.getElementById('brancheEintrag'+selectedBrancheEintrag).style.backgroundColor = "#ffffff";
		} catch(ex) {
			selectedBrancheEintrag=0;
		}
	}
	selectedBrancheEintrag = id;
	// hide the list with the branch selection to get more space, maybe its visible
	$("#branchenListe").hideSlow();
	parent.document.getElementById('branchen_auswaehlen_up').style.visibility="hidden";
	// set the iframe with the given url 
	url = '<iframe frameborder="0" width="400" height="300" src="' + url + '"></iframe>';
	// open the marker Info window, getthe marker from the marker manager
	setMapCenter((parent.MMgrBranchen[brancheid])[id].getPoint());
	(MMgrBranchen[brancheid])[id].openInfoWindowHtml(url);
}
