﻿//<![CDATA[

// Global variables
var map; 
var ctlOverview;
var ctlZoom;
var ctlMapType;
var i = 0;
var aMarkers = new Array();

// Object contructor function
function siteMarker(id, lat, lng)
{
    var point = new GLatLng(lat, lng);
    var marker = new GMarker(point);

    oTab1 = document.getElementById('site_' + id)
    oTab2 = document.getElementById('description_' + id)
    
    var infoTabs = [
        new GInfoWindowTab("Site", oTab1),
        new GInfoWindowTab("Description", oTab2)
        ];

    this.id = id;
    this.lat = lat;
    this.lng = lng;
    this.point = point;
    this.marker = marker;
    this.infoTabs = infoTabs;
}

// --- Event handlers     
function onMove()
{
    var center = map.getCenter();
    document.getElementById("message").innerHTML = center.toString();
    //ctlOverview.show();
}

function onMoveEnd()
{
    //ctlOverview.hide();
}

function onMouseOver()
{
    //ctlOverview.hide();
}

function onMouseOut()
{
    //ctlOverview.hide();
}

// --- End of Event handlers


// Places a marker on the map and setup the onClick event handler.
function PlaceMarker(i) {
    
    var marker = aMarkers[i].marker;
    
    // make sure we have a valid marker
    if (marker == null)
        return;
    
    // place the marker on the map
    map.addOverlay(marker);
    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowTabs(aMarkers[i].infoTabs);
    });
}

// Initialize the map, zoom controls and load the marker information.
function load() {
    if (GBrowserIsCompatible()) {
        // get the div element on the page.
        var oMap = document.getElementById("map")
        if (oMap == null)
            return;

        // create the map object.
        map = new GMap2(oMap);
            
        // Initialize the map centered at default location.
        show_all();

        // show controls
        ctlOverview = new GOverviewMapControl(new GSize(200, 150));
        ctlZoom = new GLargeMapControl(); 
        ctlMapType = new GMapTypeControl();   
        
        map.addControl(ctlOverview);
        map.addControl(ctlZoom);
        map.addControl(ctlMapType);
        ctlOverview.hide();
        
        // Assign Listners to show and hide the zoom window
        new GKeyboardHandler(map)
        GEvent.addListener(map, "move", onMove);
        GEvent.addListener(map, "moveend", onMoveEnd);
        GEvent.addListener(map, "mouseout", onMouseOut);
        GEvent.addListener(map, "mouseend", onMoveEnd);
        GEvent.addListener(map, "mousemove", onMouseOver);
       
        // Creat global array of markers for each site. 
        var SiteList = window.document.all('SiteList')
        if (SiteList != null)
        {
            var max = parseInt(SiteList.options.length);
            for (var i=0; i<max; i++) {
                var o = SiteList.options(i);
                
                aMarkers[o.SiteID] = new siteMarker(o.SiteID, o.Latitude, o.Longitude);
                PlaceMarker(o.SiteID);
            }
            
            // Show the default site if parameter was passed to page as querystring
            var site_id = document.getElementById('ctl00_ContentPlaceHolder1_curSiteID').value
            if (site_id != '') 
                select_site(site_id);
        }
        
        // hide the mini-map
        ctlOverview.hide();
        show_all();
    }

    // Display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
function show_all()
{
    var bounds = new GLatLngBounds;
    var MapInfo = window.document.all('ctl00_ContentPlaceHolder1_MapInfo')
    var minPoint = new GLatLng(MapInfo.Min_Latitude, MapInfo.Min_Longitude);
    var maxPoint = new GLatLng(MapInfo.Max_Latitude, MapInfo.Max_Longitude);
    
    bounds.extend(minPoint);
    bounds.extend(maxPoint);
    
    var center = bounds.getCenter()
    var zoom = map.getBoundsZoomLevel(bounds) - 1
    
    zoom_location(center.lat(), center.lng(), zoom)
}

function select_site(id)
{
    var point = aMarkers[id].point;
    var infoTabs = aMarkers[id].infoTabs;

    map.setCenter(point, 13);
    map.openInfoWindowTabs(point, infoTabs);
}

function zoom_location(latitude, longitude, zoom)
{
    point = new GLatLng(latitude, longitude);
    map.setCenter(point, zoom);
    
    map.closeInfoWindow();
}

function show_controls(flag)
{   

    if (flag == 1) {
        ctlOverview.show();
    } 
    else {
        ctlOverview.hide();
    }
}

//]]>
