/*==============================
Google Maps
================================*/
var map = null;
var geocoder = null;
var Xmarker = null;
var txtLocation = null;

// Reads the array of elements to DisplayMap (DisplayMapElements)
function DisplayAllMaps() {

    // Only go if the array exists
    if (typeof (DisplayMapElements) != 'undefined') {
        for (entry in DisplayMapElements) {
            DisplayMap(DisplayMapElements[entry].DivId, DisplayMapElements[entry].PanControls, DisplayMapElements[entry].MapType, DisplayMapElements[entry].MapLat, DisplayMapElements[entry].MapLong, DisplayMapElements[entry].MapZoom, DisplayMapElements[entry].InitialDisplay)
        }
    }
}

// Displays a Google Map
function DisplayMap(DivID, PanControls, MapType, MapLat, MapLong, MapZoom, InitialDisplay) {
    if (GBrowserIsCompatible()) {

        // Look for the Map 
        var map = new GMap2(document.getElementById(DivID));

        // Add Pan Controls
        if (PanControls == 1) {
            map.addControl(new GSmallMapControl());
        }
        else if (PanControls == 2) {
            map.addControl(new GLargeMapControl());
        }
        else if (PanControls == 3) {
            map.addControl(new GSmallZoomControl());
        }

        // Add MapType Controls
        if (MapType == 1) {
            map.addControl(new GMapTypeControl());
        }

        // Set the Map
        if (InitialDisplay == 1) {
            map.setCenter(new GLatLng(MapLat, MapLong), MapZoom, G_NORMAL_MAP)
        }
        else if (InitialDisplay == 2) {
            map.setCenter(new GLatLng(MapLat, MapLong), MapZoom, G_SATELLITE_MAP)
        }
        else if (InitialDisplay == 3) {
            map.setCenter(new GLatLng(MapLat, MapLong), MapZoom, G_HYBRID_MAP)
        }

        // Add Markers
        if (typeof (MapMarkerElements) != 'undefined') {
            for (entry in MapMarkerElements) {
                if (MapMarkerElements[entry].DivId == DivID) {
                    map.addOverlay(createMarker(new GLatLng(MapMarkerElements[entry].MarkerLat, MapMarkerElements[entry].MarkerLong), MapMarkerElements[entry].MarkerInfo, MapMarkerElements[entry].MarkerTooltip));
                }
            }
        }

    }
}

// Creates a Marker
var ismouseOver = false;
var isClicked = false;
function createMarker(point, info, tooltip) {

    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function () {
        //if tooltip window is already open, close it,
        if (ismouseOver) {
            ismouseOver = false;
            marker.closeInfoWindow();
        }

        isClicked = true;
        marker.openInfoWindowHtml(info);
    });

    if (tooltip != "") {
        GEvent.addListener(marker, "mouseover", function () {
            if (!isClicked) {
                ismouseOver = true;
                marker.openInfoWindowHtml(tooltip);
            }
        });

        GEvent.addListener(marker, "mouseout", function () {
            if (!isClicked) {
                marker.closeInfoWindow();
            }

            ismouseOver = false;
        });

        GEvent.addListener(marker, "infowindowclose", function () {
            isClicked = false;
        });

    }

    return marker;
}

// Displays a Google Map with a search input box using Google AJAX
var app;

function MapCentreDisplay(lat, lng, zoom, txtLoc, txtZoom, iControl, InitialDisplay) {
    if (GBrowserIsCompatible()) {
        app = new App(lat, lng, zoom, txtLoc, txtZoom, iControl, InitialDisplay);
    }
}

function App(lat, lng, zoom, txtLoc, txtZoom, iControl, InitialDisplay) {

    this.myMap = null;
    this.markerList = new Array();
    txtLocation = txtLoc;

    // create a map
    if (txtZoom != '') {
        this.myMap = new GMap2(document.getElementById("mapCenter"));
    }
    else {
        this.myMap = new GMap2(document.getElementById("mapMarker"));
    }
    this.myMap.addControl(new GLargeMapControl());
    this.myMap.addControl(new GMapTypeControl());
    map = this.myMap

    if (InitialDisplay == 1) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_NORMAL_MAP)
    }
    else if (InitialDisplay == 2) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_SATELLITE_MAP)
    }
    else if (InitialDisplay == 3) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_HYBRID_MAP)
    }

    if (txtZoom != '') {
        GEvent.addListener(map, "moveend", function () {
            var ncenter = map.getCenter();
            var nzoom = map.getZoom();
            document.getElementById(txtLoc).value = ncenter.toString();
            document.getElementById(txtZoom).value = nzoom.toString();
        });
    }
    else {
        Xmarker = new GMarker(new GLatLng(lat, lng), { draggable: true });
        GEvent.addListener(Xmarker, "dragend", function () {
            var point = Xmarker.getPoint();
            document.getElementById(txtLoc).value = point.toString();
        });
        map.addOverlay(Xmarker);
        document.getElementById(txtLoc).value = Xmarker.getPoint().toString();
    }

    if (InitialDisplay == 1) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_NORMAL_MAP)
    }
    else if (InitialDisplay == 2) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_SATELLITE_MAP)
    }
    else if (InitialDisplay == 3) {
        this.myMap.setCenter(new GLatLng(lat, lng), zoom, G_HYBRID_MAP)
    }

    // Add in a full set of searchers
    var localSearch = new GlocalSearch();
    var options = new GsearcherOptions();
    options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
    searchControl.addSearcher(localSearch, options);

    // Set the Local Search center point
    localSearch.setCenterPoint(this.myMap);

    // tell the searcher to draw itself and tell it where to attach
    searchControl.draw(document.getElementById("searchcontrol" + iControl));

    // tell the search control to call be on start/stop
    searchControl.setSearchCompleteCallback(this, App.prototype.OnSearchComplete);

}

App.prototype.OnSearchComplete = function (sc, searcher) {

    var mCenter = 1;
    var resDiv = document.getElementById("searchcontrol1Results")
    if (resDiv == null) {
        mCenter = 2;
        resDiv = document.getElementById("searchcontrol2Results")
    }
    if (resDiv != null) {
        resDiv.innerHTML = '';
    }

    // if we have local search results, put them on the map
    if (searcher.results && searcher.results.length > 0) {
        for (var i = 0; i < searcher.results.length; i++) {
            var result = searcher.results[i];

            // if this is a local search result, then proceed...
            if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS) {

                var markerObject = new Object();
                markerObject.result = result;
                markerObject.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));

                // create item to be added to the list
                resDiv.innerHTML = resDiv.innerHTML + '<div class="ResultItems">' + markerObject.result.title + '  <div class="ResultItemClick" OnClick="GotoMap(' + parseFloat(result.lat) + ',' + parseFloat(result.lng) + ',' + mCenter + ');">view on map</div></div>'

                if (searcher.results.length > 1) {
                    resDiv.style.display = 'block';
                    map = this.myMap;
                }
                else {
                    resDiv.style.display = 'none';
                    if (Xmarker) this.myMap.removeOverlay(Xmarker);
                    Xmarker = new GMarker(markerObject.latLng, { draggable: true });
                    GEvent.addListener(Xmarker, "dragend", function () {
                        var point = Xmarker.getPoint();
                        document.getElementById(txtLocation).value = point.toString();
                    });
                    this.myMap.addOverlay(Xmarker);
                    document.getElementById(txtLocation).value = Xmarker.getPoint().toString();
                    this.myMap.setCenter(markerObject.latLng);
                }

            }
        }
    }
    else {
        resDiv.innerHTML = '<div class="NormalRed">No matching addresses found.</div>';
        resDiv.style.display = 'block';
    }
}

// function called when an item in the results list is clicked
function GotoMap(mLat, mLng, mC) {
    map.removeOverlay(Xmarker)
    if (mC == 1) {
        Xmarker = new GMarker(new GLatLng(mLat, mLng), { draggable: false });
    }
    else {
        Xmarker = new GMarker(new GLatLng(mLat, mLng), { draggable: true });
        GEvent.addListener(Xmarker, "dragend", function () {
            var point = Xmarker.getPoint();
            document.getElementById(txtLocation).value = point.toString();
        });
    }
    map.addOverlay(Xmarker);
    document.getElementById(txtLocation).value = Xmarker.getPoint().toString();
    map.setCenter(new GLatLng(mLat, mLng));
}

function method_closure(object, method, opt_argArray) {
    return function () {
        return method.apply(object, opt_argArray);
    }
}

// function called when the "Go!" button is clicked
function showAddress(thisMap) {
    var address = document.getElementById(thisMap).value;
    searchControl.execute(address);
}

function onNoTooltipClick(chk) {
    var dvMarker = document.getElementById("dvMarkerTooltip");
    var spnTooltip = document.getElementById("spnTooltipSameAsInfo");
    if (dvMarker == null || spnTooltip == null) return;

    if (chk.checked) {
        dvMarker.style.display = "none";
        spnTooltip.style.display = "none";
    }
    else {
        dvMarker.style.display = "block";
        spnTooltip.style.display = "inline";
    }
}

function onSameAsMarkerInfoClick(chk) {
    var dvMarker = document.getElementById("dvMarkerTooltip");   
    if (dvMarker == null) return;
    if (chk.checked) {
       dvMarker.style.display = "none";
    }
    else {
        dvMarker.style.display = "block";
    }
}
