﻿var geocoder = null;
var map = null;
var theMarker = null;

var _geoFenceMain = new GPolyline([
                                    new GLatLng(44.1427978, -123.228836),
                                    new GLatLng(44.1427978, -123.048188),
                                    new GLatLng(43.9789811, -123.048188),
                                    new GLatLng(43.9789811, -123.228836),
                                    new GLatLng(44.1427978, -123.228836)], "#ff0000", 2);
var _geoFenceSub1 = new GPolyline([
                                    new GLatLng(43.9789811, -123.048188),
                                    new GLatLng(44.0359811, -123.048188),
                                    new GLatLng(44.0359811, -123.015836),
                                    new GLatLng(43.9789811, -123.015836),
                                    new GLatLng(43.9789811, -123.048188),], "#ff0000", 2);


function loadMap(zoomLevel)
{
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapCanvas"));
    centerMapOnEugene(zoomLevel);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
        
    map.enableScrollWheelZoom();    
        
    //map.addOverlay(_geoFenceMain);    
    //map.addOverlay(_geoFenceSub1);  
       
    geocoder = new GClientGeocoder();
  }
}

function clearAndCenterMap()
{    
    map.closeInfoWindow();
    map.clearOverlays(); 
    centerMapOnEugene();
}
function addBikeLanes()
{
    var geoXml = new GGeoXml("http://140.211.82.141/BikeLanes.kml");
    map.addOverlay(geoXml);
}
function centerMapOnEugene(zoomLevel)
{
    if (zoomLevel == null)
        zoomLevel = 12;
                
    map.setCenter(new GLatLng(44.052, -123.085), zoomLevel);
    map.setMapType(G_NORMAL_MAP); 
}

function checkGeoFence(point)
{
    // Check to make sure that they are within the city limits
    var result = _geoFenceMain.getBounds().containsLatLng(point);
    
    if (result)
        return result;
    else
        return _geoFenceSub1.getBounds().containsLatLng(point);
}

function modifyAddress(geocodeText) {     
    if (geocodeText.match('eugene, or'))
        return geocodeText;
    
    if (geocodeText.match('eugene or'))
        return geocodeText.replace('eugene or', 'eugene, or');
        
    if (geocodeText.match('eugene'))
        return geocodeText.replace('eugene', 'eugene, or');
        
    return geocodeText + ' eugene, or';    
}