

var map = new GMap2(document.getElementById("gmap"));
map.setMapType(G_HYBRID_MAP);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());	
map.setCenter(new GLatLng(0,0),1);

var bounds = new GLatLngBounds();

								
function showMarker(lat, lng, mHtml, mTitle, mAction, mIcon, mForm){
	if (mTitle != '')
		var marker = new GMarker(new GLatLng(lat,lng), {title:mTitle, icon:mIcon});
	else
		var marker = new GMarker(new GLatLng(lat,lng), {icon:mIcon});
	if (typeof(mForm)=="undefined"){
		mForm = '';
	}	
	map.addOverlay(marker);	
	bounds.extend(marker.getLatLng());		
	if (mAction != ''){
		if (mForm != ''){
	        GEvent.addListener(marker, "click", function() {
	      		$(mForm).action = mAction;
       			$(mForm).submit();
	        });
       }else{
	        GEvent.addListener(marker, "click", function() {
	        	document.location.href=mAction;
	        });
       }
        if (mHtml != ''){
	        if (mTitle != ''){
  				    GEvent.addListener(marker,"mouseover", function() {
		    		marker.openInfoWindowHtml(mHtml);
		    	}); 			
		    }
    	}								  								        										
    }else{
        if (mHtml != ''){
	        GEvent.addListener(marker, "click", function() {
	        	marker.openInfoWindowHtml(mHtml);
	        });
	        if (mTitle != ''){
  				    GEvent.addListener(marker,"mouseover", function() {
		    		marker.openInfoWindowHtml(mHtml);
		    	}); 			
		    }
    	}								    
    }		
}
function checkMap(id, page, element, type, dir, procMap){	
	if (typeof(procMap)=="undefined"){
		procMap = true;
	}
	if (procMap){
		$('gmapOLNoResults').hide();
		$('gmapOL').show();
	}
	
	bounds = null;
	bounds = new GLatLngBounds();
	
	aMethod = 'get';
	aParams = '';	  
	
	if(type == 'search'){
		aMethod = 'post';
		aParams = $('comm_search_form').serialize(true);	
	}

	new Ajax.Request('index.php?id=' + id + '&cmd=com_image_list_browse&type=' + type + '&dir=' + dir + '&p=' + page, {
	  method:aMethod,
	  parameters: aParams,
	  onSuccess: function(transport){
	    //alert(transport.responseText);
	    var resParts = transport.responseText.split("|1|");
	    $(element).update(resParts[0]);
		if (procMap){
			map.clearOverlays();
		    if(type == 'search'){
		    	$('searchResultInfo').show();
		    	if (resParts[2] > 0){
			    	$('imgCount').update(resParts[2]);
			    	$('imgCurPage').update(resParts[3]);
			    	$('imgTotalPages').update(resParts[4]);
			    	
			    	if (resParts[3] == 1){
			    		$('navLeft2I').show();
			    		$('navLeft2').hide();
			    		$('navLeft1I').show();
			    		$('navLeft1').hide();		    		
			    	}else{
			    		$('navLeft2I').hide();
			    		$('navLeft2').show();		    	
			    		$('navLeft1I').hide();
			    		$('navLeft1').show();		    		
			    	}
			    	
			    	if (resParts[3] == resParts[4]){
			    		$('navRight2I').show();
			    		$('navRight2').hide();
			    		$('navRight1I').show();
			    		$('navRight1').hide();		    				    	
			    	}else{
			    		$('navRight2I').hide();
			    		$('navRight2').show();		    	
			    		$('navRight1I').hide();
			    		$('navRight1').show();		    		
			    	}
			    	$('navSortOptions').show();
			    	$('navPageInfo').show();
			    	
		    	}else{
		    		 $(element).update("Es wurden keine &ouml;ffentlichen Bilder gefunden.");
		    		 $('gmapOL').hide();
		    	}
		    }
		    var geoParts = resParts[1].split("|2|");

			if (resParts[1] != ''){
				for(var i = 0; i < geoParts.length; ++i)
				{
					geoSubParts = geoParts[i].split("|;|");
					
					var Icon = new GIcon();
					Icon.image = geoSubParts[4];
					Icon.iconSize = new GSize(63, 68);
					Icon.shadow = geoSubParts[5];
					Icon.shadowSize = new GSize(69, 75);
					Icon.iconAnchor = new GPoint(31, 68);
					Icon.infoWindowAnchor = new GPoint(31, 2);
					//Icon.transparent = geoSubParts[3];
					Icon.imageMap=[0,0,62,0,62,67,0,67];					
					
					if(type == 'search')
						showMarker(geoSubParts[0], geoSubParts[1], '', geoSubParts[3], geoSubParts[6], Icon, 'comm_search_form')
					else	
						showMarker(geoSubParts[0], geoSubParts[1], '', geoSubParts[3], geoSubParts[6], Icon)
				}
				var zoom = map.getBoundsZoomLevel(bounds)-1;
				if (zoom >15)
					zoom = zoom -3;
				else if (zoom >5)
					zoom = zoom -2;
					
				map.setCenter(bounds.getCenter(),zoom);
				setTimeout("$('gmapOL').hide();", 1000);
			}else{
				map.setCenter(new GLatLng(0,0),1);
				$('gmapOL').hide();
				$('gmapOLNoResults').setOpacity(0.8);
				$('gmapOLNoResults').show();
				
				
			}
		}
	  }
	});
}
function getNearby(id, page, element, type, dir, procMap){	
	if (typeof(procMap)=="undefined"){
		procMap = true;
	}
	if (procMap){
		$('gmapOLNoResults').hide();
		$('gmapOL').show();
	}
	
	aMethod = 'post';
	aParams = $('nearby_search_form').serialize(true);	

	new Ajax.Request('index.php?id=' + id + '&cmd=com_image_list_browse&type=' + type + '&dir=' + dir + '&p=' + page, {
	  method:aMethod,
	  parameters: aParams,
	  onSuccess: function(transport){
	    var resParts = transport.responseText.split("|1|");
	    $(element).update(resParts[0]);
    	if (resParts[2] > 0){		    	
	    	if (resParts[3] == 1){
	    		$('navLeftAdd1I').show();
	    		$('navLeftAdd1').hide();		    		
	    	}else{
	    		$('navLeftAdd1I').hide();
	    		$('navLeftAdd1').show();		    		
	    	}
	    	
	    	if (resParts[3] == resParts[4]){
	    		$('navRightAdd1I').show();
	    		$('navRightAdd1').hide();		    				    	
	    	}else{
	    		$('navRightAdd1I').hide();
	    		$('navRightAdd1').show();		    		
	    	}
    	}else{
    		 $(element).update("Es wurden keine &ouml;ffentlichen Bilder in der N&auml;he gefunden.");
			map.setCenter(new GLatLng(0,0),1);
			$('gmapOL').hide();
			$('gmapOLNoResults').setOpacity(0.8);
			$('gmapOLNoResults').show();    		 
    	}

		if (procMap){
			map.clearOverlays();
		    var geoParts = resParts[1].split("|2|");
			var maxImg;
			if (resParts[1] != ''){
				if (geoParts.length > 200)
				{
					maxImg = 200;
				}
				else
				{
					maxImg = geoParts.length;
				}				
				for(var i = 0; i < maxImg; ++i)
				{
					geoSubParts = geoParts[i].split("|;|");
					
					var Icon = new GIcon();
					Icon.image = geoSubParts[4];
					Icon.iconSize = new GSize(63, 68);
					Icon.shadow = geoSubParts[5];
					Icon.shadowSize = new GSize(69, 75);
					Icon.iconAnchor = new GPoint(31, 68);
					Icon.infoWindowAnchor = new GPoint(31, 2);
					//Icon.transparent = geoSubParts[3];
					Icon.imageMap=[0,0,62,0,62,67,0,67];					
					showMarker(geoSubParts[0], geoSubParts[1], '', geoSubParts[3], geoSubParts[6], Icon, 'nearby_search_form')	
					
				}
				
				zoom = 6;
				map.setCenter(new GLatLng($('lat_add_val').value,$('lng_add_val').value),zoom);
				setTimeout("$('gmapOL').hide();", 1000);
			}else{
				map.setCenter(new GLatLng(0,0),1);
				$('gmapOL').hide();
				$('gmapOLNoResults').setOpacity(0.8);
				$('gmapOLNoResults').show();
				
				
			}
		}
	  }
	});
}
function getUser(id, page, element, type, dir){	

	aMethod = 'post';
	aParams = $('user_search_form').serialize(true);	

	new Ajax.Request('index.php?id=' + id + '&cmd=com_image_list_browse&type=' + type + '&dir=' + dir + '&p=' + page, {
	  method:aMethod,
	  parameters: aParams,
	  onSuccess: function(transport){
	    var resParts = transport.responseText.split("|1|");
	    $(element).update(resParts[0]);
    	if (resParts[2] > 0){		    	
	    	if (resParts[3] == 1){
	    		$('navLeftAddUser1I').show();
	    		$('navLeftAddUser1').hide();		    		
	    	}else{
	    		$('navLeftAddUser1I').hide();
	    		$('navLeftAddUser1').show();		    		
	    	}
	    	
	    	if (resParts[3] == resParts[4]){
	    		$('navRightAddUser1I').show();
	    		$('navRightAddUser1').hide();		    				    	
	    	}else{
	    		$('navRightAddUser1I').hide();
	    		$('navRightAddUser1').show();		    		
	    	}
    	}else{
    		 $(element).update("Es wurden keine &ouml;ffentlichen Bilder des Benutzers gefunden.");	 
    	}
	  }
	});
}
function getImages(id, page, element, type, dir){
	
	aMethod = 'get';
	aParams = '';	  
	
	if(type == 'isearch'){
		aMethod = 'post';
		aParams = $('comm_search_form').serialize(true);	
	}
	
	new Ajax.Request('index.php?id=' + id + '&cmd=com_image_list_browse&type=' + type + '&dir=' + dir + '&p=' + page, {
	  method:aMethod,
	  parameters: aParams,
	  onSuccess: function(transport){
	    //alert(transport.responseText);
	    var resParts = transport.responseText.split("|1|");
	    $(element).update(resParts[0]);
	    if(type == 'isearch'){
	    	$('searchResultInfo').show();
	    	if (resParts[2] > 0){
		    	$('imgCount').update(resParts[2]);
		    	$('imgCurPage').update(resParts[3]);
		    	
		    	if (resParts[3] == 1){
		    		$('navLeft1I').show();
		    		$('navLeft1').hide();		    		
		    	}else{
		    		$('navLeft1I').hide();
		    		$('navLeft1').show();		    		
		    	}
		    	
		    	if (resParts[3] == resParts[4]){
		    		$('navRight1I').show();
		    		$('navRight1').hide();		    				    	
		    	}else{
		    		$('navRight1I').hide();
		    		$('navRight1').show();		    		
		    	}
		    	
		    	if (resParts[5] != 0){
		    		$('imgPrev').update(resParts[5]);
		    		$('imgLeft1I').hide();
		    		$('imgLeft1').show();
		    	}else{
		    		$('imgPrev').update = 0;
		    		$('imgLeft1I').show();
		    		$('imgLeft1').hide();		    		
		    	}
		    	if (resParts[6] != 0){
		    		$('imgNext').update(resParts[6]);
		    		$('imgRight1I').hide();
		    		$('imgRight1').show();		    		
		    	}else{
		    		$('imgNext').update = 0;
		    		$('imgRight1I').show();
		    		$('imgRight1').hide();			    		
		    	}		    		
		    	
	    	}else{
	    		 $(element).update("Es wurden keine &ouml;ffentlichen Bilder gefunden.");
	    	}
	  }}
	});
}
function searchAddress(){
	 $('autocomplete_geo').hide();
	 if($('ort').value != ''){
		 if($('umkreis').value == ''){
		 	alert('Geben Sie zuerst den Suchradius (Umkreis) in km ein.'); 
		 	return;
		 }
	 	if($('lat').value == '' && $('lng').value == ''){
			 $('indicator2').show();
			 var geo = new GClientGeocoder(); 
							
			 // ====== Array for decoding the failure codes ======
			 var reasons=[];
			 reasons[G_GEO_SUCCESS]            = "Success";
			 reasons[G_GEO_MISSING_ADDRESS]    = "Fehlende Adresse: Die Adresse fehlt oder hat keinen Wert.";
			 reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unbekannte Adresse:  Es konnte keine geopgraphische Entsprechung zu der eingegebenen Adresse gefunden werden.";
			 reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Addressdaten nicht verf&uuml;gbar aus rechtlichen Gr&uuml;nden.";
			 reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
			 reasons[G_GEO_TOO_MANY_QUERIES]   = "TMQ";
			 reasons[G_GEO_SERVER_ERROR]       = "Server Fehler: Der Geocoding Prozess konnte nicht erfolgreich abgeschlossen werden.";
			
			var search = document.getElementById("ort").value;
							// ====== Perform the Geocoding ======        
			geo.getLocations(search, 
				function (result){ 
					$('indicator2').hide();
					// If that was successful
					if (result.Status.code == G_GEO_SUCCESS) {
						$('autocomplete_geo').show();
					  // How many resuts were found
					  // Loop through the results, placing markers
					  $("autocomplete_geo").innerHTML = "Gefundene Orte:";
					  for (var i=0; i<result.Placemark.length; i++) {
						var p = result.Placemark[i].Point.coordinates;
						$("autocomplete_geo").innerHTML += "<br />"+(i+1)+": <a href=\'javascript:void(0);\' onclick=\'javascript:$(\"lng\").value = \"" + p[0] + "\";$(\"lat\").value = \"" + p[1] + "\";$(\"ort\").value =\"" + result.Placemark[i].address + "\";$(\"autocomplete_geo\").hide();document.comm_search_form.submit();return false;\'>"+ result.Placemark[i].address + "</a>";														
					  }
					}else {
					  var reason="Code "+result.Status.code;
					  if (reasons[result.Status.code]) {
						reason = reasons[result.Status.code]
					  } 
					  alert( search + " konnte nicht gefunden werden."  + reason);
					}
				}
			);
		}else
			document.comm_search_form.submit();return false;
	} else
		document.comm_search_form.submit();return false;
}	   


