var pos=new Array();

pos["Rodewitz"]=new GLatLng(51.104444, 14.445277);
pos["Kirschau"]=new GLatLng(51.091016751587055, 14.422903060913086);
pos["Sonnenberg"]=new GLatLng(51.102740467064095, 14.434618949890137);
pos["Bederwitz"]=new GLatLng(51.102228458815986, 14.452214241027832);
pos["Kleinpostwitz"]=new GLatLng(51.099937826167356, 14.4215726852417);
pos["Crostau"]=new GLatLng(51.08290273932798, 14.453673362731934);
pos["Eulowitz"]=new GLatLng(51.11209035704021, 14.455389976501465);
pos["Weigsdorf-Köblitz"]=new GLatLng(51.09732367142133, 14.471139907836914);
pos["Carlsberg"]=new GLatLng(51.0886716517688, 14.466032981872559);
pos["Großpostwitz"]=new GLatLng(51.11855603327498, 14.4405841827392588);
pos["Wilthen"]=new GLatLng(51.098617292244, 14.396467208862305);
pos["Schirgiswalde"]=new GLatLng(51.07662081555961, 14.428868293762207);
pos["Cunewalde"]=new GLatLng(51.09786268449607, 14.50718879699707);
pos["Oppach"]=new GLatLng(51.058336373791725, 14.500751495361328);
pos["Sohland a.d. Spree"]=new GLatLng(51.051970183096245, 14.431400299072266);
pos["Neugersdorf"]=new GLatLng(50.97658847089682, 14.607610702514648);
/*
pos["1"]=new GLatLng(51.09488455870863, 14.432913064956665); /*Körse
pos["6"]=new GLatLng(51.10356909997617, 14.445626735687256); /*Feuerwehr
pos["7"]=new GLatLng(51.1058326068575, 14.445444345474243);  /*ROTEC
pos["9"]=new GLatLng(51.10249120059904, 14.44701075553894);  /*Lehmann
pos["10"]=new GLatLng(51.10635131157365, 14.445358514785767);/*Roscher
pos["11"]=new GLatLng(51.10418214403434, 14.445497989654541);/*Vörös
pos["12"]=new GLatLng(51.102248669775385, 14.449907541275024);/*Winkler
pos["13"]=new GLatLng(51.103420890302175, 14.443888664245605);/*Petasch
pos["14"]=new GLatLng(51.102120666883245, 14.44477915763855);/*Küchler
pos["15"]=new GLatLng(51.096252364279835, 14.4285249710083);/*Grellert
pos["16"]=new GLatLng(51.10208698185273, 14.442161321640015);/*Katzer
pos["17"]=new GLatLng(51.115107785206476, 14.416208267211914);/*Mönchser
pos["18"]=new GLatLng(51.11346438894182, 14.403548240661621);/*Jägerhaus
*/
var map;

    function load(where) {
  
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
	map.addMapType(G_PHYSICAL_MAP);
	    map.addControl(new GSmallMapControl());
	    map.addControl(
	     new GMapTypeControl(),
	     new GControlPosition(
	      G_ANCHOR_BOTTOM_LEFT, 
	      new GSize(10,10)
	     )
	    );
	    map.addControl(new myControl());
        map.setCenter( where , 14);
	    GEvent.addListener(map, "moveend", function() {
         var center = map.getCenter();
         document.getElementById("message").innerHTML = center.toString();
        }); 
      }
      if(Number(where)>0){setMarker(where);}
    }



function panTo(where){
 if(pos[where]){  map.panTo(pos[where]); }
}

var m; var point;
var AjaxLabel;
var AjaxPos;
var Ajax;

function showMarker(what,id){
 var bounds = map.getBounds();
 var southWest = bounds.getSouthWest();
 var northEast = bounds.getNorthEast();
 var lngSpan = northEast.lng() - southWest.lng();
 var latSpan = northEast.lat() - southWest.lat();
 Ajax=createAjax();
 Ajax.onreadystatechange = function() {
  if(Ajax.readyState == 4){
   if(Ajax.status==200){  
    //alert(Ajax.responseText);
    try{
     var result=json_decode(Ajax.responseText);
    }catch(err){
     throw(err+'\nshowMarker('+what+','+id+'):json_decode-Fehler');
    }
    var point=new GLatLng(result.lat, result.lng);
    map.panTo(point);  
    var Marker=new GMarker(point, createIcon());
    map.addOverlay(Marker);
    Marker.openInfoWindowHtml(result.html);
   }else{
    alert('Error:\n'+Ajax.responseText);
   }
  }
 }
 Ajax.open('POST','http://'+window.location.hostname+'/dyn.php',true);
 Ajax.setRequestHeader('If-Modified-Since','Sat, 1 Jan 2000 00:00:00 GMT');
 var s=  json_encode(
   {'query':what, 'id':id }
  ) ;
 //alert('sende '+s);
 Ajax.send( s );
}

function setMarker(what,id){
// alert('setMarker:'+what+' '+id);
 

 var bounds = map.getBounds();
 var southWest = bounds.getSouthWest();
 var northEast = bounds.getNorthEast();
 var lngSpan = northEast.lng() - southWest.lng();
 var latSpan = northEast.lat() - southWest.lat();
 
 AjaxPos=createAjax();
 AjaxPos.onreadystatechange = function() {
  if(AjaxPos.readyState == 4){  
   //alert('Pos angekommen\n'+AjaxPos.responseText);
   point=eval(AjaxPos.responseText);
   map.panTo(point);  
  }
 }
// alert('Calling:'+'http://rodewitz.net/dyn.php?query='+what+'_pos&id='+id);
 AjaxPos.open('GET','http://'+window.location.hostname+'/dyn.php?query='+what+'_pos&id='+id,true);
 AjaxPos.setRequestHeader('If-Modified-Since','Sat, 1 Jan 2000 00:00:00 GMT');
 AjaxPos.send(null);
 
 AjaxLabel=createAjax();
 AjaxLabel.onreadystatechange = function() {
  if(AjaxLabel.readyState == 4){
  // alert('Label angekommen\n'+AjaxLabel.responseText);
   m=new GMarker(point, createIcon());
   map.addOverlay(m);
   m.openInfoWindowHtml(AjaxLabel.responseText);
  }
 }
 //alert('Calling:'+'http://rodewitz.net/dyn.php?query='+what+'_label&id='+id);
 AjaxLabel.open('GET','http://'+window.location.hostname+'/dyn.php?query='+what+'_label&id='+id,true);
 AjaxLabel.setRequestHeader('If-Modified-Since','Sat, 1 Jan 2000 00:00:00 GMT');
 AjaxLabel.send(null);
// m.openInfoWindowHtml('XX')
}

function createAjax() {
 try {
  ajax = new XMLHttpRequest();
 } catch(w3c) {
  try {
   ajax = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (msie) {
   try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (msie_alt) {
    alert("Ajax kann nicht initialisiert werden.");
    return false;
   }
  }
 }
 return ajax;
}

function json_encode(o,space){
 if(!o){return(false);}
 if(!space){space=1;}
 blank=" ";
// alert(typeof o);
 switch(typeof o){
  case "number": return("\""+o+"\""); break;
  case "string": return("\""+o.replace(/\'/g,"\\\'")+"\"");
  //return("\""+o.replace(/\"/g,'\"').replace(/\'/g,"\\\'")+"\""); break;
  case "boolean": return(o?"true":"false"); break;
  case "object": 
   if(o.getDateSQL){
    return(o.getDateSQL());}
   else{
    s="{\n";
    if(o==null){return "null";}
    for(var ok in o){
     ov=o[ok];
     s+=blank.repeat(space)+"\""+ok+"\" : "+json_encode(ov,space+1)+",\n";
    }
    s=s.substr(0,s.length-2)+"\n"; // , weg
    return(s+blank.repeat(space-1)+"}\n");
   }
  break;
  default: 
   return "null"; 
  break;
 }
}

function json_encode_object(classname,id){
 var obj=objectdef(classname,id);
 return(json_encode_obj(classname,obj));
}

function json_encode_obj(classname,obj){
 var cd=classdef(classname);
 var r=new Array();
 for(var p in cd.properties){
  switch(cd.properties[p].type){
   case "number":   if(!obj[p]){r.push(p+':""');}else{r.push(p+':'+obj[p]+'');} break;
   case "string":   if(!obj[p]){r.push(p+':""');}else{r.push(p+':"'+obj[p]+'"');} break;
   case "date":     if(!obj[p]){r.push(p+':"0000-00-00"');}else{r.push(p+':"'+obj[p].getDateSQL()+'"');} break;
   case "datetime": if(!obj[p]){r.push(p+':"0000-00-00 00:00:00"');}else{r.push(p+':"'+obj[p]+'"');} break;
   case "link":     if(!obj[p]){r.push(p+':0');}else{r.push(p+':'+obj[p]+'');}break;
  }
 }
 return('{\n '+r.join(',\n ')+'\n}');
}

function json_decode(s){
 try{
  return eval("("+s+")");
 }catch(errorInS){ 
  if(s!=''){
   //alert('json_decode\nFehler in:\n'+s+'\n(Länge '+s.length+')');
   throw('json_decode\nFehler in:\n'+s+'\n(Länge '+s.length+')');
  }else{
   //alert('json_decode: Leerer String!');
   throw('json_decode:\tLeerer String!');
  }
 }
}

function createIcon(){
 var icon = new GIcon();
 icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
 icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
 icon.iconSize = new GSize(12, 20);
 icon.shadowSize = new GSize(22, 20);
 icon.iconAnchor = new GPoint(6, 20);
 icon.infoWindowAnchor = new GPoint(5, 1);
 return(icon);
}

// prototypes
String.prototype.repeat=function(cnt){var s='';while(cnt>0){s+=this; cnt--;};return(s);}
String.prototype.toText=function(){
 return(
  this.replace(/\<br.*?\>/g,'\n').replace(/\<.*?\>/,'').replace(/\&nbsp\;/g,' ').replace(/\&gt\;/g,'>').replace(/\&lt\;/g,'<').replace(/\&shy\;/g,'')
 );
}

function show_error(err){
 var sum='';
 for(var i=0; i<err.length; i++){
  sum=sum+err[i]['src']+':\t'+err[i]['text']+'\n';
 }
 alert(sum);
}


function myControl(){}
myControl.prototype = new GControl();
myControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  this.setButtonStyle_(container);
   
  var selector = document.createElement("select");

  
  for(var name in pos){
   var option = document.createElement("option");
   option.value=name;
   option.appendChild(document.createTextNode(name));
   selector.appendChild(option);
  }

  container.appendChild(document.createTextNode('gehe nach '));
  container.appendChild(selector);
  
  GEvent.addDomListener(selector, "click", function() {
    map.panTo(pos[selector.options[selector.selectedIndex].value]);
  });

  map.getContainer().appendChild(container);
  return container;
}

myControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

myControl.prototype.setButtonStyle_ = function(button) {
  //button.style.textDecoration = "underline";
  button.style.color = "#000000";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  //button.style.textAlign = "center";
  //button.style.width = "6em";
  button.style.cursor = "pointer";
}

