﻿function gicon(_miimage, _mishadow, _imagemap) {
    this.miimage = _miimage;
    this.mishadow = _mishadow;
    this.imagemap = _imagemap;
}

function addMarker(map, markers, bounds, title, desc, address, lat, lng, gi) {   
    //Create new LatLng object to hold result
    
    var latlng = null

    //If the user has supplied the latitude and longitude then use it
    //Then display the marker and extend the bounds of the map
    //Else use the address as a second resort
    
    if (lat != '' && lng != '') {
        displayMarker(map, markers, new google.maps.LatLng(lat, lng), title, desc, gi);
        latlng = new google.maps.LatLng(lat, lng);
        
        bounds.extend(latlng);
        map.fitBounds(bounds)
    } else {
        //Get the google geocoder, and if there is an address, search for it
        
        var geocoder = new google.maps.Geocoder();
        if (geocoder && address != '') {
            geocoder.geocode({ 'address': address }, function(results, status) {
            
            //If the status is OK then display the marker and
            //Extend the bounds of the map
            //Also if the zoom is more than the defined then reduce to that
                
                if (status == google.maps.GeocoderStatus.OK) {
                    displayMarker(map, markers, results[0].geometry.location, title, desc, gi);
                    latlng = results[0].geometry.location;
                    bounds.extend(latlng);
                    map.fitBounds(bounds)

                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    }
}

function displayMarker(map, markers, position, title, desc, gi) {
    //Centre the map on this position
    map.setCenter(position);
    
    //Create a new marker
    if (gi != null) {
        var marker = new google.maps.Marker({
            map: map,
            position: position,
            title: title,
            icon: gi.miimage,
            shadow: gi.mishadow,
            shape: gi.imagemap
        });
    } else {
        var marker = new google.maps.Marker({
            map: map,
            position: position,
            title: title
        });
    }

    //Attach a bubble to this marker
    attachInfowindow(marker, title, desc);

    markers[markers.length] = marker;
}

function clearMarkers(markers) {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
    }
}

function attachInfowindow(marker, title, desc) {

    //create a new infowindow bubble if there is a title or description
    if (title != '' || desc != '') {
        var contentStr = "";
        if (title != "") { contentStr += "<strong>" + title + "</strong>" + "<br />"; }
        if (desc != "") { contentStr += desc; }

        var infowin = new google.maps.InfoWindow({
            content: contentStr
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowin.open(marker.getMap(), marker);
        });
    } else {
        google.maps.event.addListener(marker, 'click', function() {
            marker.getMap().panTo(marker.getPosition());
        });
    }        
}

function showDirections(markers, ds, dd, from) {
          
    //Create request with from, to, and travelmode
    var request = {
        origin: from,
        destination: markers[0].getPosition(),
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL,
        provideRouteAlternatives: true
    };
  
    clearMarkers(markers);
    
    //display the response on the panel defined earlier
    ds.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            dd.setDirections(response);
        }
    });

    return false;
}
