﻿var directionsDisplay;
var map;
var marker;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var lat = $('#ctl00_PageContentArea_Lat').val();
    var lng = $('#ctl00_PageContentArea_Lng').val();

    var end = new google.maps.LatLng(lat, lng);
    var myOptions = {
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: end
    }
    var image = 'Site/Images/gmarkers/blueMarker.png';
    map = new google.maps.Map(document.getElementById("GoogleMap"), myOptions);
    marker = new google.maps.Marker({
        map: map,
        position: end,
        icon: image
    });

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));

    $('#ctl00_PageContentArea_MapListingAddressHeader').hide();
    $('#ctl00_PageContentArea_MapInfoContainerHeader').text('Mapped Location');
    $('#ctl00_PageContentArea_MapStartingAddressContainer').hide();
}

function initialize_Flyer() {

    directionsDisplay = new google.maps.DirectionsRenderer();
    var lat = $('#Lat').val();
    var lng = $('#Lng').val();

    var end = new google.maps.LatLng(lat, lng);
    var myOptions = {
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: end
    }
    var image = 'Site/Images/gmarkers/blueMarker.png';
    map = new google.maps.Map(document.getElementById("GoogleMap"), myOptions);
    marker = new google.maps.Marker({
        map: map,
        position: end,
        icon: image
    });

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));

    if ($("#StartingAddress").val() != '') {
        calcRoute_Flyer()
    } else {
        $('MapListingAddressHeader').hide();
        $('#MapInfoContainerHeader').text('Mapped Location');
        $('#MapStartingAddressContainer').hide();
    }

    
}



function calcRoute() {
    //clear markers
    marker.setMap(null);
    var directionsService = new google.maps.DirectionsService();
    var lat = $('#ctl00_PageContentArea_Lat').val();
    var lng = $('#ctl00_PageContentArea_Lng').val();
    var start = $("#ctl00_PageContentArea_StartingAddress").val();
    var end = new google.maps.LatLng(lat, lng);
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': start }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0].types[0] == 'street_address') {
                $('#ctl00_PageContentArea_MapStartingAddressWarning').hide();
            } else if (results[0].types[0] == 'locality') {
                $('#ctl00_PageContentArea_MapStartingAddressWarning').show();
                $('#ctl00_PageContentArea_MapStartingAddressWarning').html('<img src="/Site/Images/ico-warning.gif" />The exact starting address could not be found. The following directions are for the city provided.');
            } else if (results[0].types[0] == 'postal_code') {
                $('#ctl00_PageContentArea_MapStartingAddressWarning').show();
                $('#ctl00_PageContentArea_MapStartingAddressWarning').html('<img src="/Site/Images/ico-warning.gif" />The exact starting address could not be found. The following directions are for the zip code provided.');
            }

            var request = {
                origin: start,
                destination: end,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };

            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setMap(map);
                    directionsDisplay.setDirections(response);
                    var route = response.routes[0];
                    $('#ctl00_PageContentArea_MapStartingAddressContainer').show();
                    $('#ctl00_PageContentArea_MapListingAddressHeader').show();
                    $('#ctl00_PageContentArea_MapInfoContainerHeader').text('Mapping Information');
                    $('#ctl00_PageContentArea_MapStartingAddress1').text(route.legs[0].start_address);
                } else if (status == google.maps.DirectionsStatus.NOT_FOUND) {
                    $('#ctl00_PageContentArea_MapStartingAddressContainer').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').text('Directions were not successful because the starting address was not found.').css('color', 'red'); ;
                } else if (status == google.maps.DirectionsStatus.NO_RESULTS) {
                    $('#ctl00_PageContentArea_MapStartingAddressContainer').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').text('Directions were not successful because no route was found between starting and ending locations.').css('color', 'red'); ;
                } else {
                    $('#ctl00_PageContentArea_MapStartingAddressContainer').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').show();
                    $('#ctl00_PageContentArea_MapStartingAddressWarning').text('Directions were not successful because of the following error: ' + status).css('color', 'red'); ;
                }
            });


        }

    });
    

}



function calcRoute_Flyer() {
    //clear markers
    marker.setMap(null);
    var directionsService = new google.maps.DirectionsService();
    var lat = $('#Lat').val();
    var lng = $('#Lng').val();
    var start = $("#StartingAddress").val();
    var end = new google.maps.LatLng(lat, lng);
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ 'address': start }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0].types[0] == 'street_address') {
                $('#MapStartingAddressWarning').hide();
            } else if (results[0].types[0] == 'locality') {
                $('#MapStartingAddressWarning').show();
                $('#MapStartingAddressWarning').html('<img src="/Site/Images/ico-warning.gif" />The exact starting address could not be found. The following directions are for the city provided.');
            } else if (results[0].types[0] == 'postal_code') {
                $('#MapStartingAddressWarning').show();
                $('#MapStartingAddressWarning').html('<img src="/Site/Images/ico-warning.gif" />The exact starting address could not be found. The following directions are for the zip code provided.');
            }
            var request = {
                origin: start,
                destination: end,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };

            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setMap(map);
                    directionsDisplay.setDirections(response);
                    var route = response.routes[0];
                    $('#MapStartingAddressWarning').hide();
                    $('#MapStartingAddressContainer').show();
                    $('#MapListingAddressHeader').show();
                    $('#MapInfoContainerHeader').text('Mapping Information');
                    $('#MapStartingAddress1').text(route.legs[0].start_address);
                } else if (status == google.maps.DirectionsStatus.NOT_FOUND) {
                    $('#MapStartingAddressContainer').show();
                    $('#MapStartingAddressWarning').show();
                    $('#MapStartingAddressWarning').text('Directions were not successful because the starting address was not found.').css('color', 'red'); ;
                } else if (status == google.maps.DirectionsStatus.NO_RESULTS) {
                    $('#MapStartingAddressContainer').show();
                    $('#MapStartingAddressWarning').show();
                    $('#MapStartingAddressWarning').text('Directions were not successful because no route was found between starting and ending locations.').css('color', 'red'); ;
                } else {
                    $('#MapStartingAddressContainer').show();
                    $('#MapStartingAddressWarning').show();
                    $('#MapStartingAddressWarning').text('Directions were not successful because of the following error: ' + status).css('color', 'red'); ;
                }
            });
        }
    });
}
