/*
    Hulpjes
*/

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    //return [ posX, posY ];
    return posX;
  } else {
    //return [ oElement.x, oElement.y ];
    return oElement.x;
  }
}

/*
    Fading overlay over kaart
*/

    //define function for control
    function FadeDiv() {
    }

    // To "subclass" the GControl, we set the prototype object to
    // an instance of the GControl object
    FadeDiv.prototype = new GControl();

    // Creates a one DIV for each of the buttons and places them in a container
    // DIV which is returned as our control element. We add the control to
    // to the map container and return the element for the map class to
    // position properly.
    FadeDiv.prototype.initialize = function(oMap) {

      var container = document.createElement("div");
      container.setAttribute('id', 'fadeDiv');
      container.setAttribute('style', 'background: url(../images/fading.png) top left repeat-x; width: 100%; height: 46px;');
      
      oMap.getContainer().appendChild(container);
      
      return container;
      
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    FadeDiv.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_LEFT);
    }

    
/*
    Onload
*/
    
var oMap;
var oGeocoder;
var oMarkerManager;
$(document).ready( function () {

    //plaats overlay met tekst in het midden    
    var oLogo = document.getElementById('logo');
    var offsetLeftLogo = findPosition(oLogo);
    $('#map_overlay_container').css('left', offsetLeftLogo);
    
    if (!GBrowserIsCompatible()) {
        return false;
    }

    //maak map aan
    oMap = new GMap2(document.getElementById("map_canvas"), {backgroundColor: '#f1f1f1'});
    oMap.setMapType(G_NORMAL_MAP);
    oGeocoder = new GClientGeocoder();           
    
    //swis marker
    var oSwisMarkerIcon = new GIcon();
    oSwisMarkerIcon.image = 'images/marker/image.png';
    oSwisMarkerIcon.printImage = 'images/marker/printImage.gif';
    oSwisMarkerIcon.mozPrintImage = 'images/marker/mozPrintImage.gif';
    oSwisMarkerIcon.iconSize = new GSize(61,38);
    oSwisMarkerIcon.shadow = 'images/marker/shadow.png';
    oSwisMarkerIcon.transparent = 'images/marker/transparent.png';
    oSwisMarkerIcon.shadowSize = new GSize(80,38);
    oSwisMarkerIcon.printShadow = 'images/marker/printShadow.gif';
    oSwisMarkerIcon.iconAnchor = new GPoint(31,38);
    oSwisMarkerIcon.infoWindowAnchor = new GPoint(31,0);
    oSwisMarkerIcon.imageMap = [55,1,57,2,58,3,58,4,59,5,59,6,59,7,59,8,59,9,59,10,59,11,59,12,59,13,59,14,59,15,59,16,59,17,59,18,59,19,59,20,59,21,58,22,58,23,57,24,55,25,32,26,31,27,31,28,31,29,31,30,31,31,31,32,31,33,30,34,29,35,28,36,26,36,25,35,24,34,24,33,24,32,24,31,24,30,24,29,23,28,23,27,22,26,5,25,3,24,2,23,1,22,1,21,1,20,1,19,1,18,1,17,1,16,1,15,1,14,1,13,1,12,1,11,1,10,1,9,1,8,1,7,1,6,1,5,2,4,2,3,3,2,5,1];
    
    //swis marker options
    var oSwisMarkerOptions = { title: 'SWIS kantoor', icon:oSwisMarkerIcon, clickable: false }

    //controls
    oMap.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 46)));
    //oMap.addControl(new GHierarchicalMapTypeControl());
    oMap.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 46)));
    //oMap.addControl(new FadeDiv());
        
    //bereken center
    oGeocoder.getLatLng('Centraal Station, Leiden', function(point) {
        if (point != null) {
            oMap.setCenter(point, 15);            
        }
    });
    
    //zet swis marker     
    center = new GLatLng(52.164358, 4.491404);
    var oSwisMarker = new GMarker(center, oSwisMarkerOptions);
    oMap.addOverlay(oSwisMarker);

} );


/*
    Toon alle locaties op de kaart
*/

var locaties;             
function showLocations() {

    oMarkerManager = new MarkerManager(oMap);
    oMarkerManager.clearMarkers();
        
    //laad alle locaties
    $.getJSON("ajax/getLocaties.php", function(json) {
    
        locaties = eval(json);
        addMarkers();
        
    } );
    
} 

function addMarkers() {

    //initialiseer nu de MarkerManager
    //eerder is het niet echt nodig, bovendien geeft het 
    //een foutmelding als de map nog niet volledig geinitialiseerd is
    
    var markers = [];
    for (i=0; i<locaties.length; i++) {
        
        //maak marker icon
        var newMarkerIcon = new GIcon();
        newMarkerIcon.image = locaties[i]['marker'];
        newMarkerIcon.iconSize = new GSize(16, 16);
        newMarkerIcon.iconAnchor = new GPoint(8, 16);
        newMarkerIcon.infoWindowAnchor = new GPoint(8, 0);
        
        //maak marker met custom icon
        var point = new GLatLng(locaties[i]['lat'], locaties[i]['lng']);
        var newMarker = new GMarker(point, {title: locaties[i]['titel'], icon: newMarkerIcon});
        
        //zet alles in een array
        markers.push(newMarker);
        
    }
    
    //plaats alle markers in de manager
    oMarkerManager.addMarkers(markers, 10);
    oMarkerManager.refresh();                    
    
}
