﻿
/*
 /google mappage
 /
 /
 */
 
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var geocoder;
var map;

function initializeMap(zoomsize){
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(45.951950, -122.657482);
    var myOptions = {
        zoom: zoomsize,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
   }
  
  
  function initializeDirectionsDisplay(){ 
   directionsDisplay = new google.maps.DirectionsRenderer();
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("directionsPanel"));
  }
  
  function calcRoute( start, end) { 
  var selectedMode = document.getElementById("mode").value;
  
   var request = {
    origin:start, 
    destination:end,
     travelMode: google.maps.DirectionsTravelMode[selectedMode] 
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    }
  });
}


function codeAddress(address, MarkerType, name) {
    var icon = 'http://labs.google.com/ridefinder/images/mm_20_orange.png'
    if (MarkerType == '21') {
        icon = '/_img/icons/google-map/red-flag/image.png'//'http://labs.google.com/ridefinder/images/mm_20_red.png'
    }
    if (MarkerType == 'ALLAGES') {
        icon = '/_img/icons/google-map/green-flag/image.png'
    }
    if (MarkerType == 'DEAD') {
        icon = 'http://labs.google.com/ridefinder/images/mm_20_black.png'
    }

    var address = address;
    if (geocoder) {
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location); 
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: name,
                    icon: icon,
        shadow:'/_img/icons/google-map/green-flag/shadow.png'
                });
                var infowindow = new google.maps.InfoWindow({
                    content: '<strong>' + name + '</strong><br />' + address
                });
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow.open(map, marker);
                });

            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}

function MarkAddresses(latatude, longitude, address, MarkerType, name) {
    var myLatlng = new google.maps.LatLng(latatude, longitude) 
    var centerPoint = new google.maps.LatLng(45.553185,-122.652610) ;
    var icon = 'http://www.google.com/mapfiles/arrow.png'
//    if (MarkerType == '3') {
//          icon = 'http://www.google.com/mapfiles/dd-end.png'
//            }
//    if (MarkerType == '2' || MarkerType == '1') {
//        icon = 'http://www.google.com/mapfiles/dd-start.png'
//    }
//    if (MarkerType == 'DEAD') {
//        icon = 'http://labs.google.com/ridefinder/images/mm_20_black.png'
//    }
 
    map.setCenter(myLatlng);
    var marker = new google.maps.Marker({
        map: map,
        position: myLatlng,
        title: name,
        icon: icon,
        shadow:'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
    });
    var infowindow = new google.maps.InfoWindow({
        content: '<strong>' + name + '</strong><br />' + address + '',
        maxWidth: 100

    });
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });


}











////////////////////////----------------------internal -page methods.
 $(document).ready(function() { 
 
 $("#mode").change(function () {
    if ($("#txtDirectionsFrom").hasClass("hint-text") == false) {
        var TravelMode = document.getElementById("mode").value;
        switch (TravelMode) {
        case "BUS":
            LoadTriMetMap();
            break;
        default:
            LoadDirectionByType();
            break;
        }
    }
    return false;
});


function LoadTriMetMap() {
    $("#MappingInformation").removeClass("Error").empty().append("<p>TriMet maps will load in a new window.</p>");
    $("#txtDirectionsFrom").removeClass("Highlight").focus();

    var endAddress = document.getElementById("VenueAddress").value;
    var startAddress = document.getElementById("txtDirectionsFrom").value;
     pageTracker._trackEvent('GoogleMaps', 'Directions', document.getElementById("mode").value,5);
    window.open("http://maps.google.com/maps?f=d&source=s_d&saddr=" + startAddress + "&daddr=" + endAddress + "&hl=en&geocode=%3BCTJzOAY7VZ-2FUARtwIdw2Ww-CkR5rlWPKeVVDH2stEDy02jVw&mra=ls&dirflg=r&ttype=dep&date=03%2F28%2F10&time=11:16am&noexp=0&noal=0&sort=&sll=45.551936,-122.657341&sspn=0.010804,0.018389&ie=UTF8&z=14&start=0");
};

function LoadDirectionByType() {
    $("#MappingInformation").empty();
    if ($("#txtDirectionsFrom").hasClass("hint-text") == false) {
        $("#txtDirectionsFrom").removeClass("Highlight").focus();
        $("#MappingInformation").removeClass("Error").empty().append("<p>TriMet maps will load in a new window.</p>");
         pageTracker._trackEvent('GoogleMaps', 'Directions', document.getElementById("mode").value,5);
        calcRoute(document.getElementById("txtDirectionsFrom").value, document.getElementById("VenueAddress").value)
       
    }
    else {
        $("#MappingInformation").addClass("Error").empty().append("Please enter your address");
        $("#txtDirectionsFrom").addClass("Highlight").focus();
    }
};

$("#MapIt").click(function () {
    $("#MappingInformation").empty();
    if ($("#txtDirectionsFrom").hasClass("hint-text") == false) {
           var TravelMode = document.getElementById("mode").value;
        switch (TravelMode) {
        case "BUS":
            LoadTriMetMap();
            break;
        default:
            LoadDirectionByType();
            break;
        }
    }
    else {
        $("#MappingInformation").addClass("Error").empty().append("Please enter your address");
        $("#txtDirectionsFrom").addClass("Highlight").focus();
    }
    return false;
});
});