﻿// Google maps Implementation for B+W Website
//Coded by Rod Howarth

//center of map (center of bris)
var centerLatitude = -27.464794
var centerLongitude = 153.027878
var startZoom = 8
var map;


//method for displaying project info in infowindow and to the right
function DisplayProjectInfo(projectID, marker) {

    //call the web service for details
    MapsService.GetProjectDetails(projectID, ShowProjectDetails);

    //function to process result
    function ShowProjectDetails(result) {
        marker.openInfoWindowHtml(result);
    }
}

//add a marker method
function AddMarker(latitude, longitude, projectID) {

    //create new marker
    var marker = new GMarker(new GLatLng(latitude, longitude), null);

    //add event handler for click
    GEvent.addListener(marker, 'click', function() { DisplayProjectInfo(projectID, marker); });

    //add to map
    map.addOverlay(marker);
}


// Populates the projects based on a json response
function PopulateJsonProjects(result) {

    for (var i = 0; i < result.length; i++) {
        var project = result[i];
        AddMarker(project.Latitude, project.Longitude, project.ProjectID);
    }

}

//initialize map
function init() {
    //check browser compatibility
    if (GBrowserIsCompatible()) {
        //create new map 
        map = new GMap2(document.getElementById("map"));
        //set up basic settings 
        //  map.disableDragging();  
        map.enableScrollWheelZoom();
        map.addControl(new GMapTypeControl());
        map.addControl(new GSmallZoomControl());
        //set center
        var location = new GLatLng(centerLatitude, centerLongitude);
        map.setCenter(location, startZoom);
        //populate the map with markers from database
        MapsService.GetJsonProjects(PopulateJsonProjects);
    }
}

window.onload = init;
window.onunload = GUnload;