/************************************
*
* Google Map specific
*
************************************/
var map;
var bounds;
var markers = new Array();
var content = new Array();
var noOverlays = true;

// var clusterer;

/* Load and Display the Map  */
function GMLoadAndDisplayMap() 
{
    if (!GBrowserIsCompatible())
    {
        return;
    }
           
        map = new GMap2(document.getElementById("map")); 
        map.setCenter(new GLatLng(52, 5.5), 11);
        map.addControl(new GMapTypeControl()); 
        map.addControl(new GSmallMapControl()); 
        map.enableDoubleClickZoom();
        // map.enableScrollWheelZoom();
        
        bounds = new GLatLngBounds();
        
        if(window.GMLoadContent)
        {
            GMLoadContent();
        }
        
        //  Generate the Markers
        GMGenerateMarkers(); 
        
        // Reset the Display of the Map
        GMResetMapDisplay(); 
}

/* Create and Add Markers to the Map according the Latitude and Longitude  */
function GMCreateAndAddMarker(key, lat, lng)
{
    if(!markers)
    {
        markers = new Array()
    }
    
    var point = new GLatLng(lat, lng);
    var marker = GMCreateMarker(key, point);
       
    // Add marker to marker collection
    markers[key] = marker;
    
    // Add marker to map
    map.addOverlay(marker);
    
    // clusterer.AddMarker(marker, "test " + key)
    bounds.extend(point);
}

/* Reset the display of the Map */
function GMResetMapDisplay()
{
    if(!bounds)
    {
        map.setCenter(new GLatLng(52, 5.5), 8);
        return;
    } 
    
	map.setZoom(Math.min(map.getBoundsZoomLevel(bounds), 14));
//	map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter()); 
}

/* Create a Marker with an Icon */
function GMCreateMarker(key, point) 
{
    var icon = GMCreateIcon(key);
    
    var marker = new GMarker(point, icon);
    marker.Key = key;
    
    // Assign the event handler
    GEvent.addListener(marker, "click", GMMarkerClick);
    
    // We do have overlays
    noOverlays = false;
    
    // Return the Generated Marker
    return marker;
}

function GMCreateIcon(resultNumber)
{          
    var icon = new GIcon();
    icon.image = "http://www.autoweek.nl/includes/mapicon.php?text=" + resultNumber;
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GLatLng(16, 16);
    icon.infoWindowAnchor = new GLatLng(10, 20);   
    
    return icon;
}

function GMCreateHTML(a)
{
    if(!a[0])
        return "";
        
    var str = "<div class=s13><b>"+a[1]+"</b></div>";
    
    // Tel.
    if(a[2] != "")
        str += "<div class=s11>Tel. "+a[2]+"</div>";
        
    str += "<div class=s11>"+a[3]+" "+a[4]+"</div><div class=s11>"+a[5]+" "+a[6]+"</div>";
    
    // URL
    if(a[7] != "")
        str += "<div class=s11><a href='http://" + a[7] + "'>" + a[7] + "</a></div>";
    
    str += "<div class='controls'><a href='javascript:GMZoomToMarkerByKey("+a[0]+");'>Zoom in</a>";
    if(a[8] != "") str += " - <a href='"+a[8]+"'>Dealerpagina</a></div>";
    
    return str;
}

function GMMarkerClick(e)
{
    if(content[this.Key])
        this.openInfoWindowHtml(content[this.Key]);       
}
function GMZoomToMarkerByKey(key)
{
    var marker = markers[key];
    if(!marker)
    {
         alert('Het adres kan niet op de kaart worden getoond :-(');
         return;
    }
    var point = marker.getPoint();
    map.closeInfoWindow();
    map.setCenter(point, 16);
}

function GMZoomToAndOpenMarkerByKey(key)
{
    GMZoomToMarkerByKey(key);
    var marker = markers[key];
    
    if(!marker || !content[key])
    {
        return;
    }
    
    if(content[key])
    {
        marker.openInfoWindowHtml(content[key]);
    }
}



