var map;
var drawControls, selectControl, selectedFeature;
var associations;
var vektorlayer;

function load(){
  map = new OpenLayers.Map ("map", {
        controls:[
                  new OpenLayers.Control.Navigation(),
                  new OpenLayers.Control.PanZoomBar(),
                  new OpenLayers.Control.Attribution()],
//                  maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
                  maxResolution: 156543.0399,
                  numZoomLevels: 19,
                  units: 'm',
                  projection: new OpenLayers.Projection("EPSG:900913"),
                  displayProjection: new OpenLayers.Projection("EPSG:4326")
        } );
 
  layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
  map.addLayer(layerMapnik);
 
  var lonLat = new OpenLayers.LonLat(12,48).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
 
  map.setCenter (lonLat, 1);


  var styleMap = new OpenLayers.StyleMap({
                   fillOpacity: 1,
                   pointRadius: 4,
                   fillColor: '${color}'
                 });


  //Initialise the vector layer using OpenLayers.Format.OSM
  vektorlayer = new OpenLayers.Layer.Vector("Punkte", {
                  	  styleMap: styleMap,
                      strategies: [new OpenLayers.Strategy.Fixed()],
                      protocol: new OpenLayers.Protocol.HTTP({
                        url: "membersmap_xml.php?headquarters=1&associations="+associations,   //<-- relative or absolute URL to your .osm file
                        format: new OpenLayers.Format.OSM()
                    }),
                    projection: new OpenLayers.Projection("EPSG:4326")
                });
  vektorlayer.events.on({
    'featureselected': onFeatureSelect,
    'featureunselected': onFeatureUnselect
  });

 

  // Eventhandler (erzeugen für Layer KML, starten)
	var control = new OpenLayers.Control.SelectFeature(vektorlayer);
  map.addControl(control);
  control.activate();


            
  // wird beim Schließen des Popups aufgerufen
  function onPopupClose(evt) {
    control.unselect(this.feature);
  }

  // wird beim anklicken des Objekts in der Karte aufgerufen
  function onFeatureSelect(evt) {
    feature = evt.feature;
    // popup ist ein AnchoredBubble in 120x16px
    popup = new OpenLayers.Popup.FramedCloud("featurePopup",
                feature.geometry.getBounds().getCenterLonLat(),
                new OpenLayers.Size(120,100),
                '<b>' + feature.attributes.name + '</b><br />' + feature.attributes.description,
                null, true, onPopupClose);
    feature.popup = popup;
    popup.feature = feature;
    map.addPopup(popup);
  }
  
  // wird beim abwählen des Objekts aufgerufen
  function onFeatureUnselect(evt) {
    feature = evt.feature;
    if (feature.popup) {
      popup.feature = null;
      map.removePopup(feature.popup);
      feature.popup.destroy();
      feature.popup = null;
    }
  }

  map.addLayers([vektorlayer]);


  selectControl = new OpenLayers.Control.SelectFeature(vektorlayer,
                {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});
            
  map.addControl(selectControl);
}

      

