﻿// v1.0
// copyright information: 
// - VE API's developed and copyrighted by Microsoft Virtual Earth
// - customization and development of this application copyrighted 2008, 2009 by DENALImultimedia LLC, Colorado, USA.
//   Do not use without prior permission / Benutzung ohne vorherige Genehmigung nicht gestattet.
// changes:
//          2008-07-08 / fs:    - removed shape layer functionality for the sake of simplicity
//                              - added JSON notation for xml generated input data
//          2008-07-14 / fs:    - map view is zoomed according to zoom-level IF only one location exists otherwise zoom-level is depending on all locations (region)
//                              - default MapStyle changed from Hybrid to ROAD.
//          2008-07-21 / fs:    - added conversion to degree in order to output the coordinates
//          2008-08-08 / fs:    - added map style selection; xml data support new attribute mapstyle and passes it on to JS; xslt modified, too.
//          2009-08-03 / fs:    - added numbering of locations. Same numbering applied to XSLT file for GPS coord. display

var map = null;
var pinID = 1;
var points = new Array();



function init() {
    try {
        GetMap('');
    } catch (err) {
        alert(err.name + ' - ' + err.message + ' - ' + err.description);
    }
}
function GetMap(_data) {

    //map = new VEMap('myMap');
    if (typeof (mapid) != 'undefined' && mapid != "") {
        map = new VEMap(mapid);
    } else {
        map = new VEMap('myMap');
    }
    map.LoadMap();

    var maptype = VEMapStyle.Road;
    if (typeof (mapstyle) != 'undefined' && mapstyle != "") {

        switch (mapstyle.toUpperCase()) {
            case 'AERIAL':
                maptype = VEMapStyle.Aerial;
                break;
            case 'HYBRID':
                maptype = VEMapStyle.Hybrid;
                break;
            default:
                maptype = VEMapStyle.Road;
                break;
        }
    }
    map.SetMapStyle(maptype);
    //map.SetZoomLevel(10);

    var zoomLevel = 10; //default
    if (typeof (zoom) != 'undefined' && zoom != "") {
        //it is filled: parseInt(zoom))
        //map.SetZoomLevel(parseInt(zoom));
        zoomLevel = (parseInt(zoom));
    } else {
        //just in case there are no points
        //map.SetZoomLevel(10);
    }

    AddPins(_data);
    if (points.length > 1)
        map.SetMapView(points);
    else {
        var center = points[0];
        var initView = new VEMapViewSpecification(center, zoomLevel, 1000, -90, 0);
        map.SetMapView(initView);
    }

}

function ConvertDegreeToDecimal(_value) {
    //convert degree and minutes to decimal
    //do the stuff:

    //it is already decimal

    //convert to float
    return (parseFloat(_value));
}

function AddPins(_datalocations) {


    var data = eval("(" + oJSONlocations + ")"); //use as data the JSON result
    var pinCount = data.locations.length - 1;



    /* remove the shape layer for simplicity reasons
    var points1 = new Array(
    new VELatLong(45.16865884030668,-105.05733668804171)
    //,new VELatLong(45.168511,-105.05702)
    );
    for (var i = 1; i <= points1.length; i++)
    {
    //addLayer(i);
    //shape = new VEShape(VEShapeType.Pushpin, points[i]);
    //layer = map.GetShapeLayerByIndex(i);//change to: just base layer
    }
    //map.SetMapView(points1);*/


    for (i = 0; i < pinCount; i++) {

        points.push(new VELatLong(ConvertDegreeToDecimal(data.locations[i].latitude), ConvertDegreeToDecimal(data.locations[i].longitude)));

        var shape = new VEShape(VEShapeType.Pushpin
		                                , new VELatLong(ConvertDegreeToDecimal(data.locations[i].latitude)
					                                        , ConvertDegreeToDecimal(data.locations[i].longitude))
		                                , null);


        layer = map.GetShapeLayerByIndex(0); //just the base layer for the sake of simplicity
        var sUrl = "";
        if (data.locations[i].url != "") {

            sUrl = "<br /><a href='" + data.locations[i].url + "'>More...</a>";
        } else {
        }
        shape.SetDescription(data.locations[i].desc + sUrl);
        shape.SetTitle(data.locations[i].title);
        //shape.SetCustomIcon(icon_url);
        var n = i + 1;
        shape.SetCustomIcon('<div class="VENumberIcons" style="">' + n + '</div>');
        layer.AddShape(shape);
        //map.AddShape(shape);//alternatively to base layer mode

    } //endfor

}
function docWriteDMS(_valueLat, _valueLon) {

    var sResult = xDecimalToDMS(_valueLat, _valueLon);

    document.writeln(sResult);

}

function xDecimalToDMS(_valueLat, _valueLon) {
    var decLat = _valueLat;
    var decLon = _valueLon;
    var iNorthSouth = (decLat < 0) ? -1 : 1;
    var iEastWest = (decLon < 0) ? -1 : 1;
    var dblFull = Math.abs(Math.round(decLat * 1000000.00)); //precision adjustment
    var dblFloor = Math.floor(dblFull / 1000000);
    var iDirection = iNorthSouth;
    var resultDMSLat = concatDMS(dblFull, dblFloor, iDirection);
    dblFull = Math.abs(Math.round(decLon * 1000000.00));
    dblFloor = Math.floor(dblFull / 1000000);
    iDirection = iEastWest;
    var resultDMSLon = concatDMS(dblFull, dblFloor, iDirection);
    return ("Lat:" + resultDMSLat + " / " + "Lon:" + resultDMSLon);
}
function concatDMS(_dblFull, _dblFloor, _iDirection) {
    var dblFullNormal = _dblFull / 1000000;
    var resultDMS = (_dblFloor * _iDirection
             + '&deg; '
             + Math.floor((dblFullNormal - _dblFloor) * 60)
             + '\' '
             + Math.floor((((dblFullNormal - _dblFloor) * 60) - Math.floor((dblFullNormal - _dblFloor) * 60)) * 100000) * 60 / 100000
             + '&quot;');
    return resultDMS;
}

function xDMStoDD(_value) {
    // Format 1: "latitude":  "N 040° 41' 45.42"      //s==minus
    //"longitude": "W 073° 59' 52.6755"    //w== minus

    ////Decimal Lat: 45.632414 / Lon: -122.671509
    //Format 2: Degree Lat:45° 37' 56.6898" / Lon:-122° 40' 17.4324" 

    var components = _value.split(" ");
    var sign = "";
    var NSWE = "" + components[0];
    var TheSeconds = "" + components[3];
    var LLseconds = TheSeconds.length;
    // todo: find out if Format 1 or 2 is provided
    //todo: find out how many digits the degree is

    if (NSWE == "W" || NSWE == "S") {
        sign = "-";
    }

    // drop the degree symbol
    var degrees = new Number(components[1].substring(0, 3));
    var minutes = new Number(components[2].substring(0, 2));
    var seconds = new Number(components[3].substring(0, LLseconds)); alert(seconds);
    var minutesDecimal = minutes / 60;
    var secondsDecimal = seconds / 60 / 60;
    var result = sign + (degrees + minutesDecimal + secondsDecimal);

    return result;
}

