﻿//////////////////////////////////////////////////////////////
// ** CHAINBIZZ ApS - 2010 Javascript Google API Wrapper ** //
// ** Created By  : Lasse Rasch 16-02-2010               ** //
// ** Last Edited : Lasse Rasch 16-02-2010               ** //
// ** Version     : 1.0.1                                ** //
//////////////////////////////////////////////////////////////

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var geocoder;

function showAddress(name, street, zip, city, mapdiv) {

    // Create a Google Map instance on a named DIV and Center the map using address values.
    // This map type supports Information Windows.

    // Create a new GeoCoder Instance.
    geocoder = new GClientGeocoder();

    // Format address
    var address = street + ", " + zip + " " + city;

    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (point) {
                  // Coordinates are returned as string object and can not be send to Google API as is.
                  // Therefore we need to split the string and parsefloat lat and lng.
                  var a = new Array();
                  var b = new Array();
                  var c = new Array();
                  var lat = null;
                  var lng = null;
                  a = point.toString().split(",");
                  b = a.toString().split("(");
                  c = a[1];
                  a = c.toString().split(' ');
                  c = a[1];
                  a = c.toString().split(")");
                  c = b.toString().split(",");
                  lat = c[1];
                  lng = a[0];

                  // Create a new instance of a Gmap2 that supports Information Windows.
                  var map = new GMap2(document.getElementById(mapdiv));
                  map.setCenter(new GLatLng(parseFloat(lat), parseFloat(lng)), 13);

                  /// Create and open a new InformationWindows to display store address
                  map.openInfoWindow(map.getCenter(), name + "<br/>" + street + "<br/>" + zip + " " + city);
              }
          }
        );
    }
}

function calcRoute(mapdiv, directionsdiv, unitstype, start_street, start_zip, start_city, end_street, end_zip, end_city) {
    // Display a GoogleMap that supports Route directions.


    // Clear Div Container for directions. If not clearet, directions would be added to existing div content.
    document.getElementById(directionsdiv).innerHTML = "";
    
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setPanel(document.getElementById(directionsdiv));
    var myOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById(mapdiv), myOptions);
    directionsDisplay.setMap(map);

    var start = start_street + ", " + start_zip + " " + start_city;
    var end = end_street + ", " + end_zip + " " + end_city;
    
    // Create Google Request based on Unittype (KM/Mph)
    var request

    if (unitstype == "mph") {
        request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL,
            region: unitstype,
            language: "da"
        };
    }
    else {
        request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            unitSystem: google.maps.DirectionsUnitSystem.METRIC,
            region: unitstype,
            language: "da"
        };
    }

    // Output directions to Browser
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
        else if (status == "NOT_FOUND")
        {
            alert("Adressen blev ikke fundet.");
            }
        }
    );
}

