﻿var _towerIcon = null;
var _towerMarkerAray = new Array();
var _selectedGridRow = null;
var _towerBounds;
var _rowClick = false;

function loadMap(mapId) {

    if (mapId == null)
        mapId = "towerMap";

    map = new GMap2(document.getElementById(mapId));
    centerMapOnEugene();
    
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    GEvent.addListener(map, "infowindowclose", function() {
        deselectGridRow();
    });

    map.enableScrollWheelZoom();

    _towerBounds = new GLatLngBounds();

    createTowerIcon();
}
function centerMapOnEugene(zoomLevel) {
    if (zoomLevel == null)
        zoomLevel = 10;

    map.setCenter(new GLatLng(44.052, -123.085), zoomLevel);
    map.setMapType(G_NORMAL_MAP);
}
function mapTowers() {
    $.get("/Towers/TowerListingsForMap", {},
        function(towers) {
            $.each(towers, function(i, tower) {
                addTowerToMap(tower);
            });

        map.setZoom(map.getBoundsZoomLevel(_towerBounds)); 
        
        }, "json"
    );
}
function addTowerToMap(tower) {
    map.addOverlay(getMarkerForMap(tower));
}
function addSimpleTowerToMap(lat, lon) {
    map.addOverlay(getSimpleMarkerForMap(lat, lon));
    map.setCenter(new GLatLng(lat, lon), 10);
}
function getMarkerForMap(tower) {
    var towerPoint = new GLatLng(tower.Latitude, tower.Longitude);
    var marker = new GMarker(towerPoint, _towerIcon);

    _towerBounds.extend(towerPoint);

    GEvent.addListener(marker, "click", function() {
        var markerHtml = '<table border="0" cellpadding="2" cellspacing="2" class="towerListTableInfoWindow"><tr><td colspan="4" class="towerListItemHeading" style="border-bottom: solid 2px #FF8D00;">'
        markerHtml += tower.SiteName;
        markerHtml += '</td></tr><tr><td>Elevation:</td><td>';
        markerHtml += tower.Elevation;
        markerHtml += ' m.</td><td>Tower:</td><td>';
        markerHtml += tower.Height;
        markerHtml += ' ft.</td></tr><tr><td>Latitude:</td><td>';
        markerHtml += tower.LatAsString;
        markerHtml += '</td><td>Longitude:</td><td>';
        markerHtml += tower.LonAsString;
        markerHtml += '</td></tr></table>';

        map.closeInfoWindow();
        marker.openInfoWindowHtml(markerHtml);
        selectRowFromInfoWindow(tower.SiteID);
    });

    _towerMarkerAray.push(new markerHolder(tower.SiteID, marker));
    return marker;
}
function getSimpleMarkerForMap(lat, lon) {
    var towerPoint = new GLatLng(lat, lon);
    return new GMarker(towerPoint, _towerIcon);
}
function createTowerIcon() {
    _towerIcon = new GIcon();
    _towerIcon.image = "/Content/Images/tower_icon.png";
    _towerIcon.shadow = "/Content/Images/tower_icon_shadow.png";
    _towerIcon.iconSize = new GSize(38.0, 45.0);
    _towerIcon.shadowSize = new GSize(61.0, 45.0);
    _towerIcon.iconAnchor = new GPoint(38.0, 40.0);
    _towerIcon.infoWindowAnchor = new GPoint(19.0, 22.0);

    //map.addOverlay(new GMarker(point, icon));
}
function centerMapOnTower(siteId) {
    var marker = getMarkerForSiteId(siteId);

    if (marker != null) {
        map.closeInfoWindow();
        _rowClick = true;       
        GEvent.trigger(marker, "click");
        map.setZoom(13);
        map.setCenter(marker.getLatLng());
        //selectGridRow(siteId);
    }
}
function getMarkerForSiteId(siteId) {
    for (var i = 0; i <= _towerMarkerAray.length; i++) {
        if (_towerMarkerAray[i].TowerID() == siteId)
            return _towerMarkerAray[i].TheMarker();
    }
}
function markerHolder(towerId, theMarker)
{
    var _towerId;
    var _marker;
    
    _towerId = towerId;
    _marker = theMarker;

    this.TowerID = TowerID;
    this.TheMarker = TheMarker;
    
    function TowerID()
    {
        return _towerId;
    }
    
    function TheMarker()
    {
        return _marker;
    }
}
function selectRowFromInfoWindow(siteId) {
    selectGridRow(siteId);
    if (!_rowClick)
        document.getElementById('towerGridRow_' + siteId).scrollIntoView(false);
    _rowClick = false;
}
function getGridRowId(id) {
    return '#towerGridRow_' + id;
}
function selectGridRow(id) {
    if (_selectedGridRow != null)
        deselectGridRow();

    _selectedGridRow = getGridRowId(id);
    $(_selectedGridRow).toggleClass('towerListItem').toggleClass('towerListItemSelected');
}
function deselectGridRow() {
    $(_selectedGridRow).toggleClass('towerListItem').toggleClass('towerListItemSelected');
    _selectedGridRow = null;
}
function clearMarkers() {
    map.clearOverlays();
}