////////////////////////////////////////////////////////////////
// Vars

var toggle = 0;
var alias = 'katholische-kirche/katholische-kirche-deutschland/aufbau-ktah-kirche/bistuemer/';
var temp_address = 0;
var mouseover = [];



//////////////////////////////////////////////////////////////////////////////
// Hooks

onloadHooks.push(function(){
	//alias = $('alias').value;
	tx_igmaps_onload();
	tooltip();
	suchfeld();
});

//////////////////////////////////////////////////////////////////////////////
// Maps Funktionen


	/********************************
	* Marker setzen
	********************************/

	function setMarkerAtPoint(uid,lat,lng,zoom,icon,infos) {
		var point = new GPoint(lng, lat);
		map.addOverlay(createMarker(point,uid,lat,lng,zoom,icon,infos));
	}	
	function setMarkerAtPointEU(uid,lat,lng,zoom,icon,infos) {
		var point = new GPoint(lng, lat);
		map.addOverlay(createMarkerEU(point,uid,lat,lng,zoom,icon,infos));
	}	
		
	/********************************
	* Marker erstellen
	********************************/
		
	function createMarkerEU(point,uid,lat,lng,zoom,icon,infos) {
		var marker = new GMarker(point,icon);
		GEvent.addListener(marker, "click", function() {
			window.open(infos['web'],infos['name'],"toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=600,left=100,top=100",  toolbar="no", scrollbars="yes");
    });
		/*GEvent.addListener(marker, "mouseover", function() {
			
			if(!$('tooltipdiv')){
				var tooltipdiv = new Element('div', {
	    		'id': 'tooltipdiv',
	   			'html': infos['name'],
	    	});
	    	tooltipdiv.inject($('map_wrap'));
			}else{
				$('tooltipdiv').innerHTML = infos['name'];
			}
			
						
		});	
		GEvent.addListener(marker, "mouseout", function() {
				$('tooltipdiv').dispose();
		});	
		*/
		markerPos[j] = point;
		gmarkers[j] = marker;
		j++;
		return marker;
	}
	
		
	/********************************
	* Marker erstellen
	********************************/
		
	function createMarker(point,uid,lat,lng,zoom,icon,infos) {
		var marker = new GMarker(point,icon);
		var iftel = ""; var iffax = ""; var ifmail = ""; var ifweb = "";
		var web = ""; var ifboth = ""; 
		var iferz = ""; var ifbisch = ""; var ifweih = "";
		var erz = ""; var bisch = ""; var weih = "";
		
		if(infos['art'] == "typ0") { var bname = ("Bistum "+infos['name']).toUpperCase(); }
		else{ var bname = ("Erzbistum "+infos['name']).toUpperCase(); }
		
		if(infos['tel'] != "") iftel = "Tel. "+infos['tel']+"<br>";
		if(infos['fax'] != "") iffax = "Fax. "+infos['fax']+"<br>";
		if(infos['mail'] != "") ifmail = "<a href='mailto:"+infos['mail']+"'>E-Mail</a>";
		if(infos['mail'] != "" && infos['web'] != "") ifboth = ', ';
		
		if(infos['web'] != "") {
			ifweb = infos['web'].split(';');
			for(i = 0; i < ifweb.length; i++)	{ web += "<a target='_blank' href='"+("http://"+ifweb[i]).split('http://http://').join('http://')+"'>Homepage</a>, "; }
			web = web.substr(0,(web.length-2));
		}
		if(infos['erzbischof'] != "") {
			iferz = infos['erzbischof'].split(';');
			for(e = 0; e < iferz.length; e++)	{ erz += iferz[e].replace(/^\s+/, '').replace(/\s+$/, '')+", "; }
			erz = "<b>Erzbischof:</b> "+erz.substr(0,(erz.length-2))+"<br />";
		}
		if(infos['bischof'] != "") {
			ifbisch = infos['bischof'].split(';');
			for(b = 0; b < ifbisch.length; b++)	{ bisch += ifbisch[b].replace(/^\s+/, '').replace(/\s+$/, '')+", "; }
			bisch = "<b>Bischof:</b> "+bisch.substr(0,(bisch.length-2))+"<br />";
		}
		if(infos['weihbischof'] != "") {
			ifweih = infos['weihbischof'].split(';');
			for(w = 0; w < ifweih.length; w++)	{ weih += ifweih[w].replace(/^\s+/, '').replace(/\s+$/, '')+", "; }
			weih = "<b>Weihbischof:</b> "+weih.substr(0,(weih.length-2))+"<br />";
		}

		
		GEvent.addListener(marker, "mouseover", function() {
			if(testDiv) testDiv.destroy();	
			var testDiv = document.createElement('div');
			testDiv.id='testdiv';
			testDiv.innerHTML = '<div id="tiptitle" class="tip-title">'+bname+'</div>';		
			var markerPosition = map.fromLatLngToDivPixel(marker.getPoint());
			map.getPane(G_MAP_FLOAT_PANE).appendChild(testDiv);
			$('testdiv').setStyle('position','absolute');
			$('testdiv').setStyle('left',(markerPosition.x+20)+'px');
			$('testdiv').setStyle('top',(markerPosition.y-30)+'px');
			$('testdiv').addClass('tip');	
		});
		
		GEvent.addListener(marker, "mouseout", function() {
			//map.getPane(G_MAP_FLOAT_PANE).innerHTML = '';
			$('testdiv').dispose();
		});
		
		GEvent.addListener(marker, "click", function() {
		
			marker.openExtInfoWindow(
      	map, 
        "custom_info_window_red",
        "<div class='title'>"+bname+"</div>"+
        "<div class='image'><img src='"+infos['logo']+"' alt='"+bname+"' title='"+bname+"'></div>"+
        "<div class='text'>"+
        	"<p><b>"+infos['titel']+"</b><br>"+infos['strasse']+"<br>"+infos['plz']+" "+infos['ort']+"</p>"+
        	"<p>"+iftel+iffax+ifmail+ifboth+web+"</p>"+
        "</div>"+
        "<div class='clear'></div>"+
        "<div class='text2'>"+
       	 "<p>"+erz+bisch+weih+"</p>"+   
        "</div>"+
        "</div>",
        {beakOffset: 3}
      );
    });
    		
		
		markerPos[j] = point;
		gmarkers[j] = marker;
		j++;
		return marker;
	}
	
	
	/********************************
	* Menü ein udn ausfahren
	********************************/
	
	function togglemenu()	{
		if(toggle == 0) { 
			$('map_menu').tween('width',[39,288]); 
			$('bistumskarte').tween('opacity',[0.4,1]); 
			$('bistumstoggler').addClass('nobg'); 
			toggle = 1; 
		}else { 
			$('map_menu').tween('width',[288,39]); 
			$('bistumskarte').tween('opacity',[1,0.4]); 
			$('bistumstoggler').removeClass('nobg'); 
			toggle = 0; 
		}
	}
	
	
	/********************************
	* Coordinaten auslesen
	********************************/
	
	function getpos() {
		alert(map.getCenter()+", "+map.getZoom());
	}
	
	
	/********************************
	* suchbegriff korrigieren
	********************************/
	
	function finalword(word) {
		word = word.split('Erzbistum ').join('');
		word = word.split('Bistum ').join('');
		word = word.split('erzbistum ').join('');
		word = word.split('bistum ').join('');
		return word.substr(0,1).toUpperCase() + word.substr(1,word.length);
	}
	
	
	/********************************
	* Bistümer anzoomen
	********************************/
	
	function target(bistum) {
		map.closeExtInfoWindow();
		if(bistum == 'Aachen') { map.setCenter(new GLatLng(50.77815527465925, 6.11663818359375), 12); }
		else if(bistum == 'Augsburg') { map.setCenter(new GLatLng(48.38019777837494, 10.923843383789062), 12); }
		else if(bistum == 'Bamberg') { map.setCenter(new GLatLng(49.89861523555844, 10.948562622070312), 11); }
		else if(bistum == 'Berlin') { map.setCenter(new GLatLng(52.52457705042609, 13.465118408203125), 11); }
		else if(bistum == 'Dresden-Meißen') { map.setCenter(new GLatLng(51.043552519760375, 13.809814453125), 11); }
		else if(bistum == 'Eichstätt') { map.setCenter(new GLatLng(48.89417966006481, 11.202449798583984), 13); }
		else if(bistum == 'Erfurt') { map.setCenter(new GLatLng(50.980479350114486, 11.06048583984375), 12); }
		else if(bistum == 'Essen') { map.setCenter(new GLatLng(51.46427482966439, 7.07244873046875), 11); }
		else if(bistum == 'Freiburg') { map.setCenter(new GLatLng(48.00140902055227, 7.882347106933594), 12); }
		else if(bistum == 'Fulda') { map.setCenter(new GLatLng(50.562086348378486, 9.708137512207031), 12); }
		else if(bistum == 'Görlitz') { map.setCenter(new GLatLng(51.14704810491207, 14.99582290649414), 13); }
		else if(bistum == 'Hamburg') { map.setCenter(new GLatLng(53.56029682571355, 10.075149536132812), 11); }
		else if(bistum == 'Hildesheim') { map.setCenter(new GLatLng(52.15371445951162, 10.009918212890625), 11); }
		else if(bistum == 'Köln') { map.setCenter(new GLatLng(50.95539907504092, 6.998291015625), 11); }
		else if(bistum == 'Limburg') { map.setCenter(new GLatLng(50.391338395315024, 8.080101013183594), 13); }
		else if(bistum == 'Magdeburg') { map.setCenter(new GLatLng(52.140652614711804, 11.69769287109375), 11); }
		else if(bistum == 'Mainz') { map.setCenter(new GLatLng(50.01347552251671, 8.329010009765625), 11); }
		else if(bistum == 'München und Freising') { map.setCenter(new GLatLng(48.14134883691423, 11.707305908203125), 10); }
		else if(bistum == 'Münster') { map.setCenter(new GLatLng(51.96880766656288, 7.6815032958984375), 11); }
		else if(bistum == 'Münster (Offizialatsbezirk Oldenburg)') { map.setCenter(new GLatLng(52.7362916559109, 8.344802856445312), 11); }
		else if(bistum == 'Osnabrück') { map.setCenter(new GLatLng(52.28475233153269, 8.075637817382812), 12); }
		else if(bistum == 'Paderborn') { map.setCenter(new GLatLng(51.72617752055949, 8.787002563476562), 12); }
		else if(bistum == 'Passau') { map.setCenter(new GLatLng(48.57842428752037, 13.481769561767578), 13); }
		else if(bistum == 'Regensburg') { map.setCenter(new GLatLng(49.03134052498211, 12.12890625), 12); }
		else if(bistum == 'Rottenburg-Stuttgart') { map.setCenter(new GLatLng(48.474742152125636, 8.953685760498047), 13); }
		else if(bistum == 'Speyer') { map.setCenter(new GLatLng(49.322212946155325, 8.4539794921875), 13); }
		else if(bistum == 'Trier') { map.setCenter(new GLatLng(49.761308205624985, 6.6790008544921875), 12); }
		else if(bistum == 'Würzburg') { map.setCenter(new GLatLng(49.79943890415109, 9.961509704589844), 12); }
		else if(bistum == 'Hannover') { map.setCenter(new GLatLng(52.15624211595189, 9.980735778808594), 12); }
		 
		else if(bistum == 'Baden-Württemberg') { map.setCenter(new GLatLng(48.88639177703194, 9.656982421875), 7); }
		else if(bistum == 'Bayern') { map.setCenter(new GLatLng(48.951366470947725, 12.271728515625), 7); }
		else if(bistum == 'Berlin') { map.setCenter(new GLatLng(52.51622086393074, 13.542022705078125), 10); }
		else if(bistum == 'Brandenburg') { map.setCenter(new GLatLng(52.36218321674425, 13.985595703125), 7); }
		else if(bistum == 'Bremen') { map.setCenter(new GLatLng(53.10474338066629, 8.9483642578125), 10); }
		else if(bistum == 'Hamburg') { map.setCenter(new GLatLng(53.61857936489517, 10.5853271484375), 8); }
		else if(bistum == 'Hessen') { map.setCenter(new GLatLng(50.88917404890332, 9.68994140625), 8); }
		else if(bistum == 'Mecklenburg-Vorpommern') { map.setCenter(new GLatLng(53.66417110963306, 12.9254150390625), 8); }
		else if(bistum == 'Niedersachsen') { map.setCenter(new GLatLng(52.529589999433014, 9.942626953125), 7); }
		else if(bistum == 'Nordrhein-Westfalen') { map.setCenter(new GLatLng(51.515579783755925, 8.580322265625), 7); }
		else if(bistum == 'Rheinland-Pfalz') { map.setCenter(new GLatLng(50.07124366044474, 8.074951171875), 8); }
		else if(bistum == 'Saarland') { map.setCenter(new GLatLng(49.42169406084924, 7.17681884765625), 9); }
		else if(bistum == 'Sachsen') { map.setCenter(new GLatLng(51.089722918116344, 13.9581298828125), 8); }
		else if(bistum == 'Sachsen-Anhalt') { map.setCenter(new GLatLng(51.984880139916626, 12.557373046875), 7); }
		else if(bistum == 'Schleswig-Holstein') { map.setCenter(new GLatLng(54.17529672404642, 10.4095458984375), 8); }
		else if(bistum == 'Thueringen') { map.setCenter(new GLatLng(50.8822431111397, 11.5576171875), 8); }
		 
		
		else if(bistum == 'Belgien') { map.setCenter(new GLatLng(51.29799348237416, 4.39178466796875), 9); }
		else if(bistum == 'Bosnien und Herzegowina') { map.setCenter(new GLatLng(44.04811573082351, 17.589111328125), 7); }
		else if(bistum == 'Bundesrepublik Deutschland') { map.setCenter(new GLatLng(50.73210923577103, 7.105064392089844), 12); }
		else if(bistum == 'England und Wales') { map.setCenter(new GLatLng(51.553166729621275, -0.076904296875), 8); }
		else if(bistum == 'Estland') { map.setCenter(new GLatLng(58.57970885637573, 24.98291015625), 7); }
		else if(bistum == 'Frankreich') { map.setCenter(new GLatLng(48.85929404028653, 2.34283447265625), 11); }
		else if(bistum == 'Griechenland') { map.setCenter(new GLatLng(38.94232097947902, 23.00537109375), 6); }
		else if(bistum == 'Irland') { map.setCenter(new GLatLng(53.30462107510271, -7.5146484375), 6); }
		else if(bistum == 'Italien') { map.setCenter(new GLatLng(42.00848901572399, 12.601318359375), 7); }
		else if(bistum == 'Kroatien') { map.setCenter(new GLatLng(45.805828539928356, 15.97412109375), 9); }
		else if(bistum == 'Lettland') { map.setCenter(new GLatLng(56.8609857506449, 24.554443359375), 7); }
		else if(bistum == 'Litauen') { map.setCenter(new GLatLng(55.247815044675555, 23.280029296875), 7); }
		else if(bistum == 'Luxemburg') { map.setCenter(new GLatLng(49.72092792670335, 6.15234375), 8); }
		else if(bistum == 'Malta') { map.setCenter(new GLatLng(35.934652585994556, 14.434661865234375), 10); }
		else if(bistum == 'Niederlande') { map.setCenter(new GLatLng(52.12337138625387, 5.06195068359375), 9); }
		else if(bistum == 'Österreich') { map.setCenter(new GLatLng(48.12576783370166, 16.116943359375), 9); }
		else if(bistum == 'Polen') { map.setCenter(new GLatLng(52.01193653675363, 19.2041015625), 6); }
		else if(bistum == 'Portugal') { map.setCenter(new GLatLng(38.98503278695906, -8.8330078125), 7); }
		else if(bistum == 'Rumänien') { map.setCenter(new GLatLng(45.78284835197676, 25.02685546875), 6); }
		else if(bistum == 'Russland') { map.setCenter(new GLatLng(55.50374985927514, 42.71484375), 5); }
		else if(bistum == 'Schottland') { map.setCenter(new GLatLng(56.92099675839106, -3.8232421875), 6); }
		else if(bistum == 'Schweiz') { map.setCenter(new GLatLng(46.76244305208004, 7.9541015625), 7); }
		else if(bistum == 'Skandinavien') { map.setCenter(new GLatLng(58.97266715450153, 11.8212890625), 5); }
		else if(bistum == 'Slowenien') { map.setCenter(new GLatLng(46.126556302418514, 14.7216796875), 8); }
		else if(bistum == 'Slowakische Republik') { map.setCenter(new GLatLng(48.021161285657804, 17.369384765625), 9); }
		else if(bistum == 'Spanien') { map.setCenter(new GLatLng(40.245991504199026, -3.8232421875), 6); }
		else if(bistum == 'Tschechische Republik') { map.setCenter(new GLatLng(49.61070993807422, 15.040283203125), 7); }
		else if(bistum == 'Ukraine') { map.setCenter(new GLatLng(49.403824657885124, 24.312744140625), 7); }
		else if(bistum == 'Ungarn') { map.setCenter(new GLatLng(47.07760411715964, 19.5556640625), 7); }
		else if(bistum == 'Weißrussland') { map.setCenter(new GLatLng(53.61857936489517, 27.70751953125), 6); }

	
	}
	
	
	/********************************
	* Tooltips in der Imagemap
	********************************/
	
	function tooltip() {
		
		var as = [];
		$$('area').each(function(a){
			if (a.getAttribute('title')) as.push(a); //console.log(a);
		});
		var myTips = 	new Tips(as);
	}
	
	
	/********************************
	* Ajax Suchfeld
	********************************/
	
	function suchfeld() {
		
		if($('psuchwort')){
			$('suchebtn').removeEvents('click');
			$('psuchwort').removeEvents('keyup');
			$('suchebtn').addEvent('click', function() {
				if($('psuchwort').value != "") { 
					map.closeExtInfoWindow();
					target(finalword($('psuchwort').value));
					if($('suchhilfe')) $('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
				}else if($('psuchplz') && $('psuchplz').value != ""){
					map.closeExtInfoWindow();
					ajaxRequest('plzsuche','suche',$('psuchplz').value); 
				}else if($('psuchbundesland').value != ""){
					map.closeExtInfoWindow();
					target(finalword($('psuchbundesland').value));
				}
			});
			$('psuchwort').addEvent('keyup', function(event) {
				if(event.key == "enter") {
					if($('psuchwort').value != "") {
						map.closeExtInfoWindow();
						target(finalword($('psuchwort').value));
						if($('suchhilfe')) $('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
					}
				}else{
					if($('psuchplz'))$('psuchplz').set('value','');
					$('psuchbundesland').set('value','');
					ajaxRequest('suchhilfe','suche',$('psuchwort').value);
				}
			});
			
			if($('psuchplz')){
			$('psuchplz').addEvent('keyup', function(event) {
				if(event.key == "enter") {
					if($('psuchplz').value != "") {
						map.closeExtInfoWindow();
						$('psuchwort').set('value','');
						$('psuchbundesland').set('value','');
						ajaxRequest('plzsuche','suche',$('psuchplz').value); 
						if($('suchhilfe')) $('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
					}
				}else{
					$('psuchwort').set('value','');
					$('psuchbundesland').set('value','');
					if($('suchhilfe')) $('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
				}
			});
			}
			
			$('psuchbundesland').addEvent('change', function(event) {
				$('psuchwort').set('value','');
				if($('psuchplz')) $('psuchplz').set('value','');
				if($('suchhilfe')) $('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
			});	
			
		}
	}
	
	
	/********************************
	* HTML Requests 
	********************************/

	function ajaxRequest(action,name,value)
	{
		if(action == "plzsuche")	{
				req = new Request.HTML({url:alias+'?&ajax=true&action='+action+'&name='+name+'&value='+value, 
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML == 'not found') {
						findLocation($('psuchplz').value);
					}else{
						var latlng = responseHTML.split(',');
						mapSetCenter(latlng[0],latlng[1]);
					}					
				},
				onFailure: function() {
					alert('Suchvorgang fehlgeschalgen, bitte suchen Sie erneut.');
				}
			});
			req.send();
		}else if(action == "plzimport")	{
				req = new Request.HTML({url:alias+'?&ajax=true&action='+action+'&name='+name+'&value='+value, 
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {

				},
				onFailure: function() {
					alert('Suchvorgang fehlgeschalgen, bitte suchen Sie erneut.');
				}
			});
			req.send();
		}else if(action == "suchhilfe")	{
			if(value != "" && $('suchhilfe').getStyle('display') == 'none') { $('suchhilfe').setStyle('display','block'); }
			else if(value == "" && $('suchhilfe').getStyle('display') == 'block') { $('suchhilfe').setStyle('display','none'); }
			var tagetframe = $('snav_mid');
		
			req = new Request.HTML({url:alias+'?&ajax=true&action='+action+'&name='+name+'&value='+value, 
				onSuccess: function(html) {
					tagetframe.set('text', '');
					tagetframe.adopt(html);
					ajaxfollowedfunctions();
				},
				onFailure: function() {
					tagetframe.set('text', 'Ladevorgang fehlgeschalgen, bitte laden Sie erneut.');
				}
			});
			req.send();
		}
	}
	
	
	/********************************
	* Suchhilfe verwenden
	********************************/
	
	function ajaxfollowedfunctions() {
		if($('suchhilfe')){
			$$('#snav_mid a').addEvent('click',function(){
				$('psuchwort').set('value',this.get('text'));
				if($('psuchplz')) $('psuchplz').set('value','');
				$('psuchbundesland').set('value','');
				$('snav_mid').set('text', ''); $('suchhilfe').setStyle('display','none');
			});
		}
	}
	
	function findLocation(address) {
		temp_address = address;
    geocoder.getLocations(address+" ,germany", addAddressToMap);
  }
  
	function addAddressToMap(response) {
    
    if ((!response || response.Status.code != 200) && temp_address != 0) {
      alert("Die eingegebene Postleitzahl wurde nicht gefunden.");
    } else {
     place = response.Placemark[0];
     mapSetCenter(place.Point.coordinates[1],place.Point.coordinates[0]);
   	 ajaxRequest("plzimport",temp_address,place.Point.coordinates[1]+","+place.Point.coordinates[0])
    }
  }

 

	function mapSetCenter(lat,lng) {
		map.setCenter(new GLatLng(lat,lng), 12);
	}
