﻿//Begin Google Map Module

var addGOverviewMapControl = true;
var addGMapTypeControl = true;
var map;
var myPano;
var baseIcon;
baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
var resultList = new Object();
var latitude = '';
var longitude = '';

function LoadGMap(mapId)
{
    if (GBrowserIsCompatible())
    {
        var mapObject = document.getElementById(mapId);
        map = new GMap2(mapObject);

        map.addControl(new GSmallZoomControl());
        if (addGOverviewMapControl == true)
            map.addControl(new GOverviewMapControl());

        map.enableDoubleClickZoom();
        //map.enableScrollWheelZoom();
        if (addGMapTypeControl == true)
            map.addControl(new GMapTypeControl());
        return map;
    }
    else
    {
        document.getElementById(mapId).style.display = 'none';
        document.getElementById('map_icons').style.display = 'none';
        return null;
    }
}

function AddMarker(mapObj, lat, lng, textToShow, addListener)
{
    if (lat != 0 && lng != 0)
    {
        var bounds = new GLatLngBounds;
        var point = new GLatLng(lat, lng);
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        if (addListener == true)
            GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(textToShow); });

        map.addOverlay(marker);

        //bounds.extend(marker);

        //mapObj.setZoom(mapObj.getBoundsZoomLevel(bounds));                        
        //mapObj.setCenter(bounds.getCenter());  

        return true;
    }
    return false;
}

function LoadMap(mapId, latString, lngString, textToShow, addListener)
{
    map = LoadGMap(mapId);
    lat = parseFloat(latString.toString().replace(",", "."));
    lng = parseFloat(lngString.toString().replace(",", "."));
    if (AddMarker(map, lat, lng, textToShow, addListener) != true)
    {
        document.getElementById(mapId).style.display = 'none';
        document.getElementById('map_icons').style.display = 'none';
    }
}

function loadGoogleMap(latString, lngString, textToShow)
{
    LoadMap('map', latString, lngString, textToShow, false);
}

function LoadStreetView(lat, lng)
{
    myPano = new GStreetviewPanorama(document.getElementById('map'));
    var PanoLoc = new GLatLng(lat, lng);
    var myPOV = { yaw: 0, pitch: 0 };
    myPano.setLocationAndPOV(PanoLoc, myPOV);

    panoClient = new GStreetviewClient();
    panoClient.getNearestPanorama(PanoLoc, showPanoData);
}

function showPanoData(panoData)
{
    if (panoData.code != 200)
    {
        LoadMap('map', panoData.query.split(",")[0], panoData.query.split(",")[1], '', false);
        return;
    }
    myPano.setLocationAndPOV(panoData.location.latlng);
}

function SetLocalSearch(latString, lngString)
{
    latitude = latString;
    longitude = lngString;
}

function doLocalSearch(query, icon)
{
    if (resultList[query] == null)
    {
        var localSearch = new GlocalSearch();

        localSearch.setCenterPoint(new GLatLng(latitude, longitude));
        localSearch.setAddressLookupMode(GlocalSearch.ADDRESS_LOOKUP_DISABLED);
        localSearch.setSearchCompleteCallback(this, show_results, new Array(localSearch, query, icon));
        localSearch.setResultSetSize(GSearch.LARGE_RESULTSET);

        localSearch.execute(query);
    } else
    {
        //if they are clicking for the 2nd time, remove the overlays for this query
        for (var i = 0; i < resultList[query].length; i++)
        {
            map.removeOverlay(resultList[query][i]);
        }
        resultList[query] = null;
    }
}

function show_results(searcher, query, icon)
{
    resultList[query] = new Array();

    for (var i = 0; i < searcher.results.length; i++)
    {
        var mark = makeQueryMarker(
                  new GLatLng(searcher.results[i].lat, searcher.results[i].lng),
                  icon,
                  searcher.results[i].html.innerHTML);
        resultList[query][resultList[query].length] = mark;

        map.addOverlay(mark);
    }
}

function makeQueryMarker(point, iconFile, wincontent)
{
    var icon = new GIcon(baseIcon);
    icon.image = '/images/' + iconFile;

    var prop = new GMarker(point, icon);
    GEvent.addListener(prop, "click", function()
    {
        prop.openInfoWindowHtml(wincontent);
    });
    return prop;

}

