﻿// JScript File
function AtlasGps() {
    var gps;
    var gpsEnabled = false;
    var pngincapable = false;
    var beforeGpsMapCursor;
    this.gpsDisabledCallback;
    
    this.SetPngIncapable = function() {
        pngincapable = true;
    }
    this.DeterminePngCapable = function(pngincapable) {
        if (pngincapable)
            this.SetPngIncapable();
    }
    this.showgps = function() {
        var e = document.getElementById('gpsposdialog');
        if (e != undefined) {
            e.style.display = 'block';
            e.style.left = '200px';
            e.style.top = '200px';
        }
    }
    this.setGpsDialogCoords = function(coords) {
        var gpsposx = document.getElementById('gpsposx');
        var gpsposy = document.getElementById('gpsposy');
        gpsposx.innerHTML = coords.x;
        gpsposy.innerHTML = coords.y
    }
    this.disableGps = function() {
        if (gpsEnabled == false) return true; //VEMAP2
        if (this.gpsDisabledCallback) {
            this.gpsDisabledCallback();
        }
        var e = document.getElementById('gpsposdialog');
        e.className = 'gpsposdialogHidden';
        gpsEnabled = false;
        if (bVEMap) {
            vemap.DetachEvent("onendpan", this.vemap_calcGPS);
        }
        else {
            Event.stopObserving('map', 'map:moved', this.onDnksMapMoved); //??rikig??
        }
    }
    this.gpsIsEnabled = function() {
        return gpsEnabled;
    }
    this.enableGps = function(disableCallback) {
        if (!gpsEnabled) {

            this.gpsDisabledCallback = disableCallback;
            gpsEnabled = true;
            var e = document.getElementById('gpsposdialog');
            if (pngincapable) e.className = 'gpsposdialog';
            else e.className = 'gpsposdialogpng';

            if (bVEMap) {
                vemap.AttachEvent("onendpan", this.onDnksVEMapMoved.bindAsEventListener(this)); 
                this.vemap_calcGPS();
             }
            else {
                this.updateGpsDialog();
                Event.observe('map', 'map:moved', this.onDnksMapMoved.bindAsEventListener(this));
            }
        }
    }
    this.onDnksVEMapMoved = function(e) {
        this.vemap_calcGPS();
    }

    this.onDnksMapMoved = function(e) {
        this.updateGpsDialog();
    }
    this.onDnksMapZoomed = function (e) {
        this.updateGpsDialog();
    }
    this.getPosition = function () {
        var gpsdialog = document.getElementById('gpsposdialog');
        var winx = parseInt(gpsdialog.style.left) + 72;
        var winy = parseInt(gpsdialog.style.top) + 59;
        var gpsWindowPos = new Coordinate(winx, winy);
        var gpsCoord = map.getPixelLatLon(gpsWindowPos);
        return gpsCoord;
    }
    
    this.updateGpsDialog = function() {
        if (bVEMap) {
            pixel = new VEPixel(300, 400);
            var LL = vemap.PixelToLatLong(pixel);
        }
        else {
            var gpsdialog = document.getElementById('gpsposdialog');
            var winx = parseInt(gpsdialog.style.left) + 72;
            var winy = parseInt(gpsdialog.style.top) + 59;
            var gpsWindowPos = new Coordinate(winx, winy);
            var gpsCoord = map.getPixelLatLon(gpsWindowPos);
            var c = wgs84DecimalToMinutes(gpsCoord);
            this.setGpsDialogCoords(c);
        }
    }
    this.vemap_calcGPS = function() { //VEMAP2
        var gpsdialog = document.getElementById('gpsposdialog');
        var winx = parseInt(gpsdialog.style.left) + 72;
        var winy = parseInt(gpsdialog.style.top) + 59;
        pixel = new VEPixel(winx, winy);
        var LL = vemap.PixelToLatLong(pixel);

        var gpsVECoord = { 'x': LL.Longitude, 'y': LL.Latitude, 'srs': "EPGS:4326" };
        var c = wgs84DecimalToMinutes(gpsVECoord);
        atlasgps.setGpsDialogCoords(c);
    }
}
