////////////////////////////////////////////////////////////////
// Vars

var toggle = 0;
var alias = '';
var temp_address = 0;
var htmlcontent = [];
var htmlmarker = [];
var imagelayer = [];
var bilderarray = [];
var htmltitle = [];

//////////////////////////////////////////////////////////////////////////////
// Hooks

onloadHooks.push(function(){
	tx_igmaps_onload();
	tooltip();
	suchfeld();
	$('titletip').setStyle('opacity',0);
	$('titletip').setStyle('display','block');
	
	//listenAnsicht('');
});

//////////////////////////////////////////////////////////////////////////////
// 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));
	}	
		
		
	/********************************
	* Marker erstellen
	********************************/
		
	function createMarker(point,uid,lat,lng,zoom,icon,infos) {
		var marker = new GMarker(point,icon);
		
		
		
		
		htmlmarker[uid] = marker;
    htmlcontent[uid] = "<div id='title_"+uid+"' class='title'>"+infos['title']+"</div><div id='infotext' class='text'>"; 
    if(infos['image'] != ""){
    	bilderarray = infos['image'].split(",");
    	htmlcontent[uid] += "<a href='javascript:openImagelayer("+uid+");' onfocus='this.blur();'><img width='100' src='http://www.unicef.de/uploads/tx_igmaps/"+ bilderarray[0]+"' alt='' /><span>Fotos vergrößern</span></a>";
  		var thumbs = '';
  		for(var i = 1; i < bilderarray.length; i++){
  			if(i%7 == 0) {
  				thumbs += '<div class="imagethumb" id="imagethumb'+i+'" style="float:left; margin:4px 0px 0px 0px;"><img class="imagethumb_img" width="58" src="http://www.unicef.de/uploads/tx_igmaps/'+ bilderarray[i]+'" alt="zoomen" title="zoomen" /></div>';
  			}else{
  				thumbs += '<div class="imagethumb" id="imagethumb'+i+'" style="float:left; margin:4px 4px 0px 0px;"><img class="imagethumb_img" width="58" src="http://www.unicef.de/uploads/tx_igmaps/'+ bilderarray[i]+'" alt="zoomen" title="zoomen" /></div>';
  			}
  		}
  		imagelayer[uid] = '<div id="imagelayer">'+
  		'<div id="imagelarge">'+
  		'<img id="imagelarge_img" width="416" src="http://www.unicef.de/uploads/tx_igmaps/'+ bilderarray[0]+'" alt="schliessen" title="schliessen" /></div>'+
  		thumbs +
  		'</div>';
  		
  		/*<div id="imagethumb1" style="margin-right:4px;"></div>
  		<div id="imagethumb2" style="margin-right:4px;"></div>
  		<div id="imagethumb3" style="margin-right:4px;"></div>
  		<div id="imagethumb4" style="margin-right:4px;"></div>
  		<div id="imagethumb5"></div>
  		
  		';*/
    }
    htmlcontent[uid] += "<p><b>"+(infos['date'])+"</b> - "+infos['note'].split(" href=").join(" target=\"_blank\" href=")+"</p></div>";
    htmltitle[uid] = infos['title'];
   	
   	
   	
   	
	    GEvent.addListener(marker, "click", function() {
			map.closeExtInfoWindow();
			marker.openExtInfoWindow(map,"custom_info_window_red",htmlcontent[uid],{beakOffset: 3});
			if($("title_"+uid).offsetHeight.toInt() > 50) {
				//$("custom_info_window_red_contents").setStyle('height','180px');
				//$("custom_info_window_red_contents").setStyle('border-bottom','1px solid #0099ff');
			}else{
				//$("custom_info_window_red_contents").setStyle('height','160px');
				//$("custom_info_window_red_contents").setStyle('border-bottom','0px none');
			}
			
			var sc2 = new ScrollControl($('infotext'), {'createControls': true});
		});
    
	    GEvent.addListener(marker, "mouseover", function(e) {
	    	$('titletip').innerHTML = htmltitle[uid];
	    	$('titletip').setStyle('left',(583-$('titletip').offsetWidth.toInt())/2);
	    	$('titletip').tween('opacity',[0,1]);
	    	
	    	
	    	//console.log(e);
	    	
		});
	    GEvent.addListener(marker, "mouseout", function() {
	    	$('titletip').tween('opacity',[1,0]);
		});
    		
		markerPos[j] = point;
		gmarkers[j] = marker;
		j++;
		return marker;
	}
	
	function getMousePosition(e) {
		if (e == undefined) e = window.event;
		if (e.pageX || e.pageY) {
			mouse_x_pos = e.pageX;
			mouse_y_pos = e.pageY;
		}else if (e.clientX || e.clientY) {
			mouse_x_pos = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouse_y_pos = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return mouse_x_pos+';'+mouse_y_pos;
		
	}

	
	function openImagelayer(uid)
	{
		var Imageframe = new Element('div', {
    	'id': 'Imageframe',
   		'html': imagelayer[uid],
    });
		Imageframe.inject($('map_wrap'), 'after');
		
		
		$('imagelarge').setStyle('cursor','pointer');
		
		$('imagelarge').addEvent('click', function(event) {
			$('Imageframe').dispose();
		});
		
		$$('#imagelayer .imagethumb_img').addEvent('click', function(event) {
			var large_alt = $('imagelarge_img').src;
			var thumb_alt = this.src;
			$('imagelarge_img').src = thumb_alt;
			//this.src = large_alt;
			if($('tx-igmaps-pi1').offsetHeight < ($('Imageframe').offsetHeight+70)) {
				$('tx-igmaps-pi1').setStyle('height',($('Imageframe').offsetHeight+70));
			}else{
				//$('tx-igmaps-pi1').setStyle('height','auto');
			}
		});
		
		
		if($('tx-igmaps-pi1').offsetHeight < ($('Imageframe').offsetHeight+70)) {
			$('tx-igmaps-pi1').setStyle('height',($('Imageframe').offsetHeight+70));
		}else{
			//$('tx-igmaps-pi1').setStyle('height','auto');
		}
		
		
	}
	
	
	/********************************
	* Menü ein udn ausfahren
	********************************/
	
	function togglemenu()	{
		if(toggle == 0) { 
			$('map_menu').tween('width',[33,290]); 
			$('bundeslaender_img').tween('opacity',[0.4,1]); 
			$('toggler').setStyle('backgroundPosition','bottom center'); 
			toggle = 1; 
		}else { 
			$('map_menu').tween('width',[290,33]); 
			$('bundeslaender_img').tween('opacity',[1,0.4]); 
			$('toggler').setStyle('backgroundPosition','top center');  
			toggle = 0; 
		}
	}
	
	
	/********************************
	* Coordinaten auslesen
	********************************/
	
	function getpos() {
		alert(map.getCenter()+" "+map.getZoom());
	}
		
	
	/********************************
	* Ziele anzoomen
	********************************/
	
	function target(ziel) {
		map.closeExtInfoWindow();
		ziel = ziel.toLowerCase();
		if(ziel == 'aachen') map.setCenter(new GLatLng(50.77815527465925, 6.11663818359375), 12);
		if(ziel == 'augsburg') map.setCenter(new GLatLng(48.38019777837494, 10.923843383789062), 12);
		if(ziel == 'bamberg') map.setCenter(new GLatLng(49.89861523555844, 10.948562622070312), 11);
		if(ziel == 'berlin') map.setCenter(new GLatLng(52.52457705042609, 13.465118408203125), 11);
		if(ziel == 'eichstätt') map.setCenter(new GLatLng(48.89417966006481, 11.202449798583984), 13);
		if(ziel == 'erfurt') map.setCenter(new GLatLng(50.980479350114486, 11.06048583984375), 12);
		if(ziel == 'essen') map.setCenter(new GLatLng(51.46427482966439, 7.07244873046875), 11);
		if(ziel == 'freiburg') map.setCenter(new GLatLng(48.00140902055227, 7.882347106933594), 12);
		if(ziel == 'fulda') map.setCenter(new GLatLng(50.562086348378486, 9.708137512207031), 12);
		if(ziel == 'görlitz') map.setCenter(new GLatLng(51.14704810491207, 14.99582290649414), 13);
		if(ziel == 'hamburg') map.setCenter(new GLatLng(53.56029682571355, 10.075149536132812), 11);
		if(ziel == 'hildesheim') map.setCenter(new GLatLng(52.15371445951162, 10.009918212890625), 11);
		if(ziel == 'köln') map.setCenter(new GLatLng(50.95539907504092, 6.998291015625), 11);
		if(ziel == 'limburg') map.setCenter(new GLatLng(50.391338395315024, 8.080101013183594), 13);
		if(ziel == 'magdeburg') map.setCenter(new GLatLng(52.140652614711804, 11.69769287109375), 11);
		if(ziel == 'mainz') map.setCenter(new GLatLng(50.01347552251671, 8.329010009765625), 11);
		if(ziel == 'münchen und Freising') map.setCenter(new GLatLng(48.14134883691423, 11.707305908203125), 10);
		if(ziel == 'münster') map.setCenter(new GLatLng(51.96880766656288, 7.6815032958984375), 11);
		if(ziel == 'osnabrück') map.setCenter(new GLatLng(52.28475233153269, 8.075637817382812), 12);
		if(ziel == 'paderborn') map.setCenter(new GLatLng(51.72617752055949, 8.787002563476562), 12);
		if(ziel == 'passau') map.setCenter(new GLatLng(48.57842428752037, 13.481769561767578), 13);
		if(ziel == 'regensburg') map.setCenter(new GLatLng(49.03134052498211, 12.12890625), 12);
		if(ziel == 'rottenburg-stuttgart') map.setCenter(new GLatLng(48.474742152125636, 8.953685760498047), 13);
		if(ziel == 'speyer') map.setCenter(new GLatLng(49.322212946155325, 8.4539794921875), 13);
		if(ziel == 'trier') map.setCenter(new GLatLng(49.761308205624985, 6.6790008544921875), 12);
		if(ziel == 'würzburg') map.setCenter(new GLatLng(49.79943890415109, 9.961509704589844), 12);
		if(ziel == 'hannover') map.setCenter(new GLatLng(52.15624211595189, 9.980735778808594), 12);
		
		if(ziel == 1 || ziel == 'baden-württemberg') map.setCenter(new GLatLng(48.88639177703194, 9.656982421875), 7);
		if(ziel == 2 || ziel == 'bayern') map.setCenter(new GLatLng(48.951366470947725, 12.271728515625), 7);
		if(ziel == 3 || ziel == 'berlin') map.setCenter(new GLatLng(52.51622086393074, 13.542022705078125), 10);
		if(ziel == 4 || ziel == 'brandenburg') map.setCenter(new GLatLng(52.36218321674425, 13.985595703125), 7);
		if(ziel == 5 || ziel == 'bremen') map.setCenter(new GLatLng(53.10474338066629, 8.9483642578125), 10);
		if(ziel == 6 || ziel == 'hamburg') map.setCenter(new GLatLng(53.61857936489517, 10.5853271484375), 8);
		if(ziel == 7 || ziel == 'hessen') map.setCenter(new GLatLng(50.88917404890332, 9.68994140625), 8);
		if(ziel == 8 || ziel == 'mecklenburg-vorpommern') map.setCenter(new GLatLng(53.66417110963306, 12.9254150390625), 8);
		if(ziel == 9 || ziel == 'niedersachsen') map.setCenter(new GLatLng(52.529589999433014, 9.942626953125), 7);
		if(ziel == 10 || ziel == 'nordrhein-westfalen') map.setCenter(new GLatLng(51.515579783755925, 8.580322265625), 7);
		if(ziel == 11 || ziel == 'rheinland-pfalz') map.setCenter(new GLatLng(50.07124366044474, 8.074951171875), 8);
		if(ziel == 12 || ziel == 'saarland') map.setCenter(new GLatLng(49.42169406084924, 7.17681884765625), 9);
		if(ziel == 13 || ziel == 'sachsen') map.setCenter(new GLatLng(51.089722918116344, 13.9581298828125), 8);
		if(ziel == 14 || ziel == 'sachsen-anhalt') map.setCenter(new GLatLng(51.984880139916626, 12.557373046875), 7);
		if(ziel == 15 || ziel == 'schleswig-holstein') map.setCenter(new GLatLng(54.17529672404642, 10.4095458984375), 7);
		if(ziel == 16 || ziel == 'thueringen') map.setCenter(new GLatLng(50.8822431111397, 11.5576171875), 8);
		
	}
	
	
	/********************************
	* Tooltips in der Imagemap
	********************************/
	
	function tooltip() {
		
		var as = [];
		$$('#map_menu area').each(function(a){
		if (a.getAttribute('title') != "") as.push(a);
		});
		var myTips = 	new Tips(as);
	}
	
	
	/********************************
	* Ajax Suchfeld
	********************************/
	
	function suchfeld() {
		$('psuchbundesland').addEvent('change', function(event) {
			$('psuchwort').set('value','');
			target($('psuchbundesland').value);
		});
		$('psuchwort').addEvent('keyup', function(event) {
			//if(event.key == "enter") {
				if($('psuchwort').value != "") {
					map.closeExtInfoWindow();
					target($('psuchwort').value);
					listenAnsicht($('psuchwort').value);
					$('psuchbundesland').set('value','');
				}
			//}
		});
		$('suchebtn').addEvent('click', function() {
			target($('psuchwort').value);
			listenAnsicht($('psuchwort').value);
		});
		
	}
	
	
	
	function listenAnsicht(suchbegriff)
	{
		req = new Request.HTML({url:alias+'?&ajax=true&search='+suchbegriff, 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$('alsliste').innerHTML = responseHTML;		
			},
			onFailure: function() {
				alert('Suchvorgang fehlgeschalgen, bitte suchen Sie erneut.');
			}
		});
		req.send();
	}
	
	
	function mapSetCenter(lat,lng) {
		map.closeExtInfoWindow();
		var myFx = new Fx.Scroll(window,{duration:1000}).toElement("map_wrap");
		map.setCenter(new GLatLng(lat,lng), 12);
	}
	
	function mapSetCenterandOpen(lat,lng,uid) {
		map.closeExtInfoWindow();
		var myFx = new Fx.Scroll(window,{duration:1000}).toElement("map_wrap");
		map.setCenter(new GLatLng(lat,lng), 12);
		htmlmarker[uid].openExtInfoWindow(map,"custom_info_window_red",htmlcontent[uid],{beakOffset: 3});
	}
	
	
	
