
var map, i, gmarkers, htmls, spsmarkerOrt, spsmarkerRegion, spsmarkerinvis, /*istRegion, */kartentyp /*KTYP_X*/,
	startBounds, clickZoom, spalten, labelCssClass, zoomOrt, 
	pxProLabelDiv, landGPoly, landMinimapGPoly, gmUeberschriftTyp1, aktiverMarker;
var minimap, minimap_xhair, map_moving = 0, minimap_moving = 0;
var INDEXASP = "index_gmaps"+sprache+".asp",
	PX_SIDE_BAR = 600,
	KTYP_WELT = 1, KTYP_LAND = 2, KTYP_REGION = 3;
var pts = new Array();
pts.push(new GLatLng(36.491973470593685, 15.0732421875));
pts.push(new GLatLng(38.89103282648846, 8.61328125));
pts.push(new GLatLng(43.866218006556394, 7.6904296875));
pts.push(new GLatLng(45.706179285330855, 6.943359375));
pts.push(new GLatLng(46.830133640447386, 11.953125));
pts.push(new GLatLng(44.49650533109348, 15.29296875));
pts.push(new GLatLng(45.259422036351694, 12.4365234375));
pts.push(new GLatLng(44.29240108529005, 12.41455078125));
pts.push(new GLatLng(40.17887331434696, 18.43505859375));
pts.push(new GLatLng(36.58024660149866, 15.1611328125));	

function setGMap() {
	if(this['gs_data'] === undefined){
		window.setTimeout(function(){setGMap();}, 50);
		return;
	}
	//istRegion = (typeof(gs_region) == "undefined") ? false : gs_region;
	//kartentyp = (typeof(gs_region) == "undefined") ? KTYP_REGION : (gs_region ? KTYP_LAND : KTYP_REGION);
	kartentyp = (typeof(gs_kartentyp) == "undefined") ? KTYP_REGION : gs_kartentyp;
	
	if(!GBrowserIsCompatible() || (gs_data == null) || (gs_data.length == 0) || (document.getElementById("map") == null)){
		document.getElementById('googlemaps_map').style.display = 'none';
		return;
	}
	
	gmUeberschriftTyp1 = document.getElementById('gm_ueberschrift').innerHTML;
	
	map = null;
	map = new GMap2(document.getElementById("map"));
	//map.enableContinuousZoom();
	//map.enableDoubleClickZoom();
	//map.addMapType(G_PHYSICAL_MAP);
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
	map.setUIToDefault();
	
	landGPoly = new GPolygon(pts,'#000000',.5,.5,'#FEFADA',.5);
	landMinimapGPoly = new GPolygon(pts,'#000000',.5,.5,'#FEFADA',.5);
	
	GEvent.addListener(map, 'zoomend', function(zoomStufeAlt, zoomStufeNeu) {
			if((gmarkers == null) || (gmarkers.length == 0)){
				return;
			}
			if(!clickZoom && (kartentyp == KTYP_LAND)){
				var typ = (zoomStufeNeu < 8) ? 1 : 2;
				createMarkers(typ, map, null, zoomStufeNeu);
				uebersichtText(typ);
				//uebersichtSpanEinfuegen();
			}
	});
	
	document.getElementById('gm_uebersicht').onclick = function(){
		clickZoom = true;
		var typ = (kartentyp == KTYP_LAND) ? 1 : 2;
		createMarkers(typ, map, null, null);
		uebersichtText(typ);
		clickZoom = false;	
	}

	spsmarkerOrt = new GIcon();
	spsmarkerOrt.image = "../../images/gmaps_marker.png";
	//spsmarkerOrt.shadow = "fingershadow.png";
	spsmarkerOrt.iconSize = new GSize(20, 28);
	spsmarkerOrt.shadowSize = new GSize(36, 28);
	spsmarkerOrt.iconAnchor = new GPoint(5, 28);
	spsmarkerOrt.infoWindowAnchor = new GPoint(5, 2);
	spsmarkerOrt.infoShadowAnchor = new GPoint(14, 25);
	
	spsmarkerRegion = new GIcon();
	spsmarkerRegion.image = "../../images/gmaps_marker_region_kreis.png";
	//spsmarkerRegion.shadow = "fingershadow.png";
	//spsmarkerRegion.iconSize = new GSize(20, 28);
	spsmarkerRegion.iconSize = new GSize(30, 30);
	spsmarkerRegion.shadowSize = new GSize(36, 28);
	spsmarkerRegion.iconAnchor = new GPoint(5, 28);
	spsmarkerRegion.infoWindowAnchor = new GPoint(5, 2);
	spsmarkerRegion.infoShadowAnchor = new GPoint(14, 25);	

	spsmarkerLand = new GIcon();
	spsmarkerLand.image = "../../images/gmaps_marker_land.png";
	//spsmarkerLand.shadow = "fingershadow.png";
	//spsmarkerLand.iconSize = new GSize(20, 28);
	spsmarkerLand.iconSize = new GSize(50, 50);
	spsmarkerLand.shadowSize = new GSize(36, 28);
	spsmarkerLand.iconAnchor = new GPoint(5, 28);
	spsmarkerLand.infoWindowAnchor = new GPoint(5, 2);
	spsmarkerLand.infoShadowAnchor = new GPoint(14, 25);	
		
	spsmarkerinvis = new GIcon();
	spsmarkerinvis.image = "";
	/*spsmarkerOrt.transparent = "fingertran.png";
	spsmarkerOrt.printImage = "fingerie.gif";
	spsmarkerOrt.mozPrintImage = "fingerff.gif";*/		
	

	clickZoom = true;
	var typ = (kartentyp == KTYP_WELT) ? 3 : ((kartentyp == KTYP_LAND) ? 1 : 2);
	if(kartentyp != KTYP_WELT){
		var standardZoom = (kartentyp == KTYP_WELT) ? 3 : ((kartentyp == KTYP_LAND) ? 6 : 9);
		map.setCenter(new GLatLng(gs_data.markers[0].point[0], gs_data.markers[0].point[1]), standardZoom/*(kartentyp == KTYP_LAND) ? 6 : 9*/);	
		startBounds = createMarkers(/*(kartentyp == KTYP_LAND) ? 1 : 2*/typ, map, null, null);
	}
	else{
		//map.setCenter(new GLatLng(0, 0));
		startBounds = polygoneZeichnen(map);
	}
	//uebersichtSpanEinfuegen();
	clickZoom = false;
	
	if(document.getElementById('minimap') != null){
		minimap();
	}
	
	mapLoad();
}

function mapLoad(){
	if(map.isLoaded()){
		document.getElementById('mapload').style.display = 'none';
		document.getElementById('map').style.visibility = 'visible';
		if(document.getElementById('minimap') != null){
			document.getElementById('minimap').style.visibility = 'visible';
		}
	}
	else{
		window.setTimeout(mapLoad, 50);
	}	
}

function polygoneZeichnen(map){
	var bounds = new GLatLngBounds();
	var pts, p, point;
	var breite = 4;
	for (var i = 0; i < gs_data.markers.length; i++) {
		if(gs_data.markers[i].typ == 3){
			pts = new Array();			
			for(var c = 0; c < gs_data.markers[i].points.length; c++){				
				point = new GLatLng(gs_data.markers[i].points[c][0], gs_data.markers[i].points[c][1]);
				pts.push(point);
				bounds.extend(point);
			}
			p = new GPolygon(pts,'#000000',breite,.5,'#FF0000',.5);
			
			GEvent.addListener(p, 'click', function(){alert('Noch nicht implementiert!');}); 
			
			GEvent.addListener(p, 'mouseover', function(){ 
					if(this instanceof GPolygon){
						this.setFillStyle({color: 'White'});
					}
			}); 

			GEvent.addListener(p, 'mouseout', function(){ 
					if(this instanceof GPolygon){
						this.setFillStyle({color: '#FF0000'});
					}			
			});							
			map.addOverlay(p);	
		}
	}
	map.setCenter(bounds.getCenter());
	map.setZoom(map.getBoundsZoomLevel(bounds));
	
	
	return bounds;
}

function createMarkers(typ, map, orteIds /*Optional, nur gültig wenn typ = 2 (Orte)*/, zoom /*Optional*/) {
	aktiverMarker = null;
	if(typ == 2){
		//document.getElementById('gm_hinweis').innerHTML = hinweisTxt;
		document.getElementById('gm_hinweis').style.display = 'block';
	}
	else{
		//document.getElementById('gm_hinweis').innerHTML = '';
		document.getElementById('gm_hinweis').style.display = 'none';
	}
	loescheAlleUnterelemente(document.getElementById("gm_side_bar"));
	
	var anz;
	if(typ == 3){
		anz = gs_anz_laender;
	}
	else if(typ == 1){
		anz = gs_anz_regionen;
	}
	else if(typ == 2){
		anz = ((orteIds != null) ? orteIds.length : gs_anz_orte);
	}
	if(anz < 6){
		spalten = 2;
		labelCssClass = 'gmaps_label3';
	}
	else if(anz <= 30){
		spalten = 3;
		labelCssClass = 'gmaps_label2';
	}
	else{
		spalten = 4;
		labelCssClass = 'gmaps_label1';
	}
	var zeilen = Math.floor(anz / spalten);

	zeilen += ((anz % spalten) > 0) ? 1 : 0;
	
	var box = document.getElementById("gm_side_bar");
	var div;
	pxProLabelDiv = Math.floor(PX_SIDE_BAR / spalten);
	for(var z = 0; z < spalten; z++){
		div = document.createElement('div');
		div.id = 'gm_side_bar_div_' + (z + 1);
		div.style.cssFloat = 'left';
		div.style.styleFloat = 'left'; // IE
		div.style.width = pxProLabelDiv + 'px';
		box.appendChild(div);
	}
	
	i = 1;
	map.clearOverlays();
	gmarkers = [];
	htmls = [];
	var anzMarker = 0,
		bounds = new GLatLngBounds(),
		aktZeile = 0, 
		aktSpalte = 1,
		marker, onclickStr, boundsTmp, html;
	for (var c = 0; c < gs_data.markers.length; c++) {			
		if(((typ == 2) && (gs_data.markers[c].typ == 2) && ((orteIds == null) || ((orteIds.length > 0) && (existertElemInArr(gs_data.markers[c].idOrt, orteIds, 2))))) || ((typ == 3) && (gs_data.markers[c].typ == 3))){
			if(aktZeile == zeilen){
				aktSpalte++;
				aktZeile = 0;
			}		
			if(kartentyp == KTYP_REGION){
				onclickStr = 'onclick="self.location.href=\'#' + gs_data.markers[c].label + '\'"';
			}
			else{
				onclickStr = 'onclick="window.location.href=\'/regionen/' + gs_data.markers[c].regionVerzeichnis + '/' + INDEXASP + '#' + gs_data.markers[c].label + '\'"';
			}
			html = '<span ' + onclickStr + ' style="color:Black;cursor:pointer;text-decoration:none;">' + gs_data.markers[c].html + '<span>';
			marker = createMarker(new GLatLng(gs_data.markers[c].point[0], gs_data.markers[c].point[1]), 
								  gs_data.markers[c].label, html, true, gs_data.markers[c].zoomLvl, 
								  gs_data.markers[c].markerText, gs_data.markers[c].typ, null, 
								  gs_data.markers[c].regionVerzeichnis, aktSpalte);
			map.addOverlay(marker);
			bounds.extend(marker.getPoint());
			anzMarker++;
			aktZeile++;
		}
		else if((typ == 1) && (gs_data.markers[c].typ == 1)){
			if(aktZeile == zeilen){
				aktSpalte++;
				aktZeile = 0;
			}	
		
			boundsTmp = new GLatLngBounds();
			for(var g = 0; g < gs_data.markers[c].latLngOrte.length; g++){				
				if((gs_data.markers[c].latLngOrte[g].length == 3) && (gs_data.markers[c].latLngOrte[g][0] > 0) && (gs_data.markers[c].latLngOrte[g][1] > 0)){
					boundsTmp.extend(new GLatLng(gs_data.markers[c].latLngOrte[g][0], gs_data.markers[c].latLngOrte[g][1]));
				}
			}
			onclickStr = 'onclick="window.location.href=\'/regionen/' + gs_data.markers[c].regionVerzeichnis + '/' + INDEXASP + '\'"';
			html = '<span ' + onclickStr + ' style="color:Black;cursor:pointer;text-decoration:none;">' + gs_data.markers[c].html + '<span>';
			gs_data.markers[c].point[0] = boundsTmp.getCenter().lat();
			gs_data.markers[c].point[1] = boundsTmp.getCenter().lng();
			marker = createMarker(boundsTmp.getCenter(), gs_data.markers[c].label, html, true, 
								 gs_data.markers[c].zoomLvl, gs_data.markers[c].markerText, gs_data.markers[c].typ,
								 gs_data.markers[c].latLngOrte, gs_data.markers[c].regionVerzeichnis, aktSpalte);
			map.addOverlay(marker);								 
			bounds.extend(marker.getPoint());
			anzMarker++;
			aktZeile++;
		}
	}
	
	if(map == this.map){
		if(zoomOrt > 0) {
			map.setZoom(zoomOrt);
		}
		else if(anzMarker < 2){
			map.setZoom((typ == 1) ? 7 : 8);
		}
		else{
			if(zoom == null){
				map.setZoom(map.getBoundsZoomLevel(bounds));		
			}
			else{
				map.setZoom(zoom);		
			}
		}
		if(zoom == null){
			map.setCenter(bounds.getCenter());
		}
	}
	
	//pts.push(new GLatLng(36.491973470593685, 15.0732421875);
	//pts.push(new GLatLng(36.491973470593685, 15.0732421875);
	//landGPoly = new GPolygon(pts,'#000000',2,.5,'#008800',.5);//new GPolyline(pts,'#008800',3);
	//map.addOverlay(landGPoly);	
	
	return bounds;
}

function createMarker(point, name, html, showMarker, zoomLvl, markerText, typ, unterelemente, verzeichnis, aktSpalte) {
	var nameLoc = name;
	var unterelementeLoc = unterelemente;
	var markerTextLoc = markerText;
	
	var marker;
	if(markerText != null){
		var icon, labelOffset;
		if(typ == 1){
			icon = spsmarkerRegion;
			labelOffset = new GSize(((String(markerText).length > 1) ? 2 : 7), -21);
		}
		else if(typ == 2){
			icon = spsmarkerOrt;
			labelOffset = new GSize(((String(markerText).length > 1) ? -2 : 2), -17);
		}
		else if(typ == 3){
			icon = spsmarkerLand;
			labelOffset = new GSize(((String(markerText).length > 1) ? 2 : 7), -21);
		}
		opts = { 
			"icon": icon,
			"clickable": true,
			"labelText": String(markerText),
			"labelOffset": labelOffset,
			"labelClass" : "gmapsMarkerLabel"
		};
		marker = new LabeledMarker(point, opts);	
	}
	else{
		marker = new GMarker(point, spsmarkerOrt);
	}
	marker.idi = i;
	marker.typi = parseInt(typ);
	marker.unterelementeAnz = (unterelemente == null) ? 0 : unterelemente.length;
	marker.verzeichnis = verzeichnis;
	GEvent.addListener(marker, 'click', function() {
		if(kartentyp == KTYP_REGION){
			self.location.href = '#' + nameLoc;
		}
		else{
			if(markerTextLoc > 0){
				if(this.typi == 1){
					clickZoom = true;
					createMarkers(2, map, unterelementeLoc, null); 
					uebersichtText(2);
					//uebersichtSpanEinfuegen();
					clickZoom = false;			
				}
				else{
					window.location.href = '/regionen/' + this.verzeichnis + '/' + INDEXASP;
				}
			}
		}
	});
	GEvent.addListener(marker, 'mouseover', function() {
		//var height = ((this.unterelementeAnz + 2/*Überschrift & br*/) * 19);
		//if(height > 130){
		//	height = 130;
		//}
		//this.openInfoWindowHtml('<div style="overflow:auto;height: '+height+'px; width: 265px;">' + htmls[this.idi] + '</div>');	
		infoZeigen(this);
	});	
	
	gmarkers[i] = marker;
	htmls[i] = html;
	
	var spanElem = document.createElement('span');	
	spanElem.innerHTML = '<img src="/images/forward.gif" alt="' + name + '" border="0"><b class="' + labelCssClass + '">&nbsp;' + name + '</b>';
	spanElem.title = name;
	spanElem.setAttribute("class", "gmaps_labelSpanElem");
	spanElem.setAttribute("className", "gmaps_labelSpanElem"); // IE
	spanElem.idi = i;
	spanElem.zoomLvl = zoomLvl;
	spanElem.id = 'span' + (typ > 0) ? i : typ; 
	/*if((typ > 0) || !istRegion){
		spanElem.onclick = function(){
			clickZoom = true;
			zoomIntoMarkerLocation(this.idi, this.zoomLvl);
			clickZoom = false;
		}
	}
	else{
		spanElem.onclick = function(){
			clickZoom = true;
			createMarkers(1);
			uebersichtSpanEinfuegen();	
			clickZoom = false;
		}
	}*/
	if(kartentyp == KTYP_LAND){
		spanElem.onclick = function(){window.location.href='/regionen/' + gmarkers[this.idi].verzeichnis + '/' + INDEXASP;}
	}
	else{
		spanElem.onclick = function(){
			clickZoom = true;
			zoomIntoMarkerLocation(this.idi, this.zoomLvl);
			clickZoom = false;		
			if(aktiverMarker != null){
				aktiverMarker.closeInfoWindow();				
			}
			infoZeigen(gmarkers[this.idi]);
		}
	}
	spanElem.onmouseover = function(){
		var bgc = '#FFFF99';
		var stc = 'Black';
		if (document.URL.search(/fewolino/) > 0) {			
			bgc = '#5A73E7';
			stc = 'White';
		}		
		this.parentNode.style.backgroundColor=bgc;
		this.style.color=stc;	

	}									
	spanElem.onmouseout = function(){
		var bgc = '#5A73E7';
		var stc = '#FFFF99';
		if (document.URL.search(/fewolino/) > 0) {			
			bgc = '#F7BE5C';
			stc = '5A73E7';		
		}					
		this.parentNode.style.backgroundColor=bgc;
		this.style.color=stc;	
	}							
	
	if(showMarker){
		var div = document.createElement('div');
		div.setAttribute("class", "gmaps_labelDivElem");
		div.setAttribute("className", "gmaps_labelDivElem"); // IE	
		div.style.width = pxProLabelDiv + 'px';
		div.appendChild(spanElem);
		
		var box = document.getElementById('gm_side_bar_div_' + aktSpalte);
		box.appendChild(div);
	}
	i++;		
	return marker;
}
/*
function uebersichtSpanEinfuegen(){
	// Zurück zur Übersicht
	i = 0;
	if(startBounds != null) {
		createMarker(startBounds.getCenter() , 'Zurück zur Übersicht', '', false,  
					 map.getBoundsZoomLevel(startBounds), null, -1, null, null, spalten);
	}		
	// **
}*/

function infoZeigen(marker){
	var height = ((marker.unterelementeAnz + 2/*Überschrift & br*/) * 19);
	if(height > 130){
		height = 130;
	}
	marker.openInfoWindowHtml('<div style="overflow:auto;height: '+height+'px; width: 265px;">' + htmls[marker.idi] + '</div>');
	aktiverMarker = marker;
}

function minimap(){
	minimap = new GMap2(document.getElementById("minimap"));
	minimap.setCenter(startBounds.getCenter(), 4);
	minimap.disableContinuousZoom();
	minimap.disableDoubleClickZoom();
	minimap.disableScrollWheelZoom();
	//minimap.disableDragging();
	minimap.getContainer().style.overflow = 'hidden'; // Löst das Problem mit der zu großen Copyrightnachricht

	var Icon = new GIcon();
	Icon.image = "../../images/gmaps_xhair.png";
	Icon.iconSize = new GSize(30, 30);
	Icon.shadowSize = new GSize(0,0);
	Icon.iconAnchor = new GPoint(11, 11);
	Icon.infoWindowAnchor = new GPoint(11, 11);
	Icon.infoShadowAnchor = new GPoint(11, 11);	
	
	minimap_xhair = new GMarker(minimap.getCenter(), Icon);
	minimap.addOverlay(minimap_xhair);
	
	GEvent.addListener(map, 'move', minimapMove);
	GEvent.addListener(minimap, 'moveend', minimapMMove);
	
	GEvent.addListener(minimap, 'zoomend', function(zoomStufeAlt, zoomStufeNeu) {
		minimapZoomTo(zoomStufeAlt);
	});	
	
	//minimap.addOverlay(landMinimapGPoly);
	
	//createMarkers(istRegion ? 1 : 2, minimap, null, null);
}

function minimapZoomTo(lvl){
	minimap.zoomTo(lvl);	
}

function minimapMove(){
	minimap_moving = true;
	if (map_moving == false) {
		minimap.setCenter(map.getCenter());
		minimap_xhair.setPoint(map.getCenter());
		minimap_xhair.redraw(true);
	}
	minimap_moving = false;
}

function minimapMMove(){
	map_moving = true;
	if (minimap_moving == false) {
		map.setCenter(minimap.getCenter());
		minimap_xhair.setPoint(minimap.getCenter());
		minimap_xhair.redraw(true);
	}
	map_moving = false;
}

function zoomIntoMarkerLocation(id, zoomLvl){
	var latLng = gmarkers[id].getLatLng();
	map.setCenter(latLng, zoomLvl);
}

function loescheAlleUnterelemente(elem){
    if((elem != null) && (elem.childNodes != null) && (elem.childNodes.length > 0)){
        for(var i = 0; i < elem.childNodes.length; i++){
            elem.removeChild(elem.childNodes[i]);
            i--;
        }        
    }
}

function existertElemInArr(val, arr, arrPos/*opt*/){
	for(var i = 0; i < arr.length; i++){
		if(arrPos != null){
			if(arr[i][arrPos] === val){
				return true;
			}
		}
		else{
			if(arr[i] === val){
				return true;
			}
		}
	}	
	return false;
}

function uebersichtText(typ){
	if(typ == 2){					
		document.getElementById('gm_ueberschrift').innerHTML = 'Bei diesen Orten haben wir Domizile:';
		if (sprache=="_en") { document.getElementById('gm_ueberschrift').innerHTML = 'At these villages we do have domiciles:'; }
		if (sprache=="_fr") { document.getElementById('gm_ueberschrift').innerHTML = 'Nous avons des domiciles aux endroits suivants:'; }
	}
	else{
		document.getElementById('gm_ueberschrift').innerHTML = gmUeberschriftTyp1;
	}
}
/*
function gs_data_marker_sort(m1, m2){
	var a = new Array(m1.label, m2.label);
	a.sort();
	if(a[0] === m1.label){
		return 1;
	}
	else{
		return 0;
	}
}*/

/*
function showMarkerOnMap(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}*/

/*
Array.prototype.exists = function(val, arr, arrPos){
	for(var i = 0; i < arr.length; i++){
		if(arrPos != null){
			if(arr[i][arrPos] === val){
				return true;
			}
		}
		else{
			if(arr[i] === val){
				return true;
			}
		}
	}	
	return false;
}*/
