﻿/// <reference path="jquery-1.4.2.min.js" />

var map = null;

$(document).ready(function() {
    /*map = new GMap2(document.getElementById("mappa"));
    map.setCenter(new GLatLng(44.7708, 10.7917), 10);
    map.setUIToDefault();

    var polyOptions = { geodesic: true };
    var polyline = new GPolyline([
    new GLatLng(40.65642, -73.7883),
    new GLatLng(61.1699849, -149.944496)
    ], "#ff0000", 4, 1, polyOptions);
    map.addOverlay(polyline);*/

    $('input:text').focusin(function() {
        $(this).addClass('textBorder');
        }
     );
    $('input:text').focusout(function() {
        $(this).removeClass('textBorder');
        }
     );

});


function Unload() {
    GUnload();
}

function PointToMap() {
    //$('div#mappa').show(1000);
    var wwloc = $('#txtLocator').val();
    var parameters = "{'locator':'" + wwloc + "'}"

    $.ajax({
        type: "POST",
        url: "LocatorService.asmx/CalculateLatLongDecimal",
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "JSON",
        success: PointLocator,
        error: function(e) {
            $('div#resultLat').css('display', 'block');
            //$('div#resultLat').html('Error: ' + e);
            //alert('Error: ' + data);
        }
    })

}


function PointLocator(resp) {
        
    var tmp = $.evalJSON(resp).d;

    if (tmp != null) {
        $('div#resultLat').html("Latitude: " + tmp[0].toString()).css('display', 'block');
        $('div#resultLon').html("Longitude: " + tmp[1].toString()).css('display', 'block');
        map = new GMap2(document.getElementById("mappa"));
        map.setCenter(new GLatLng(tmp[0], tmp[1]), 8);
        map.setUIToDefault();

        // Enable the Earth map type
        map.addMapType(G_SATELLITE_3D_MAP);

        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
        //map.setMapType(G_SATELLITE_3D_MAP);
        
        var point = new GLatLng(tmp[0], tmp[1]);
        map.addOverlay(new GMarker(point));
    }
    else {
        WriteErrorText();
        //$('div#resultLat').html("Error: incorrect locator").css('display', 'block');
        //$('div#resultLon').html("").css('display', 'none');
    }
}

function WriteErrorText() {
    $('div#resultLat').html("Error: incorrect locator").css('display', 'block');
    $('div#resultLon').html("").css('display', 'none');
}


function TraceLocator() {
    map = new GMap2(document.getElementById("mappa"));
    map.setCenter(new GLatLng(44.7708, 10.7917), 10);
    map.setUIToDefault();

    var polyOptions = { geodesic: true };
    var polyline = new GPolyline([
          new GLatLng(44.7708, 10.7917),
          new GLatLng(61.1699849, -149.944496)
          ], "#ff0000", 5, 1, polyOptions);
    map.addOverlay(polyline);
}