﻿function GoogleMap()
{
    this._map = null;
    this._ge = null;
    this._lat = null;
    this._lng = null;
    this._heading = null;
    this._range = null;
    this._tilt = null;
    this._desc = null;
    this._link = null;
    this._frameendEL = false;
    var point;
    var currentMapType = null;
    this._screenOverlay = null;    
}

GoogleMap.prototype = {

    createMap: function () {
        Canvas3D = document.createElement('div');
        Canvas3D.setAttribute('id', "map");
        Canvas3D.style.position = "absolute";
        Canvas3D.style.left = "5000px";
        Canvas3D.style.top = "0px";

        document.body.appendChild(Canvas3D);

        this._map = new GMap2(document.getElementById("map"));
        this._map.setCenter(new GLatLng(0, 0), 0);

        this._map.addMapType(G_SATELLITE_3D_MAP);
        this._map.setMapType(G_SATELLITE_3D_MAP);

        this._map.getEarthInstance(function (ge) {
            if (ge == null)
                return;

            GMap._ge = ge;

            ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, false);
            ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false);
            ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, false);
            ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, false);
            ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS_LOW_RESOLUTION, false);

            GMap._screenOverlay = ge.createScreenOverlay('');

            GMap.addPub(rootPath + "Models/Billboard/pubindex.kml");
            pubmarkers();

            setTimeout('google.earth.addEventListener(GMap._ge, "frameend", GMap._rangeValidator)', 1000);

            var isLoadDone = false;

            setInterval(function () {
                var streamingPercent = ge.getStreamingPercent();

                if (streamingPercent == 100 && isLoadDone == false) {
                    GMap.loadingOverlay("Done");
                    isLoadDone = true;
                }

                if (streamingPercent != 100) {
                    GMap.loadingOverlay("Loading");
                    isLoadDone = false;
                }
            }, 2000);

            GMap._GlobeLoaded();

        });
    },

    _rangeValidator: function () {
        var camera = GMap._ge.getView().copyAsCamera(GMap._ge.ALTITUDE_RELATIVE_TO_GROUND);
        var Alt = camera.getAltitude();

        if (Alt < 1000 && haspub == false) {
            haspub = true;
            drawPubMarkers();
        }
        if (Alt > 1001 && haspub == true) {

            for (var i = 0; i < pubmarkersArray.length; i++) {
                GMap._ge.getFeatures().removeChild(pubmarkersArray[i]);
            }
            haspub = false;
        }
    },

    _GlobeLoaded: function () {

        setTimeout(function () {
            GMap.flyAnimatedTo(entity.Latitude, entity.Longitude, entity.laHeading, entity.laRange, entity.laTilt);
            if (entity.Kml != '') {
                GMap.addModel(entity.Kml);
            }

            if (entity.Latitude != '39.69') {
                addMarkers();
            }
            $("#accordion").accordion('activate', 0)
            document.getElementById("Button3").disabled = false;
            document.getElementById("btn_Map").disabled = false;
            document.getElementById("btn_Hyb").disabled = false;
            document.getElementById("btn_Sat").disabled = false;
        }, 500);
    },

    loadingOverlay: function (type) {
        var x = $("#gContainer").width() - 22;
        var y = $("#gContainer").height() - 43;

        this._screenOverlay.getOverlayXY().setXUnits(this._ge.UNITS_PIXELS);
        this._screenOverlay.getOverlayXY().setYUnits(this._ge.UNITS_PIXELS);
        this._screenOverlay.getOverlayXY().setX(x);
        this._screenOverlay.getOverlayXY().setY(y);
        this._ge.getFeatures().removeChild(this._screenOverlay);

        if (type == "Done") {
            var icon = this._ge.createIcon('');
            icon.setHref(rootPath + 'map/Images/Done.png');
            this._screenOverlay.setIcon(icon);

            this._ge.getFeatures().appendChild(this._screenOverlay);
        }

        else {
            var icon = this._ge.createIcon('');
            icon.setHref(rootPath + 'map/Images/Loading.png');
            this._screenOverlay.setIcon(icon);

            this._ge.getFeatures().appendChild(this._screenOverlay);
        }
    },

    flyTo: function (lat, lng, zoom) {
        this._lat = lat;
        this._lng = lng;
        point = new GLatLng(this._lat, this._lng);
        this._map.setCenter(point, zoom);
    },

    flyAnimatedTo: function (lat, lng, head, range, tilt) {
        this._lat = lat;
        this._lng = lng;
        this._heading = head;
        this._range = range;
        this._tilt = tilt;

        point = new GLatLng(this._lat, this._lng);

        if (this._getMapType() == G_SATELLITE_3D_MAP) {
            if (this._frameendEL) {
                this._stateFrameend();
            }

            var lookAt = GMap._ge.getView().copyAsLookAt(this._ge.ALTITUDE_RELATIVE_TO_GROUND);
            lookAt.setLatitude(point.lat());
            lookAt.setLongitude(point.lng());
            lookAt.setHeading(parseFloat(this._heading));
            lookAt.setRange(parseFloat(this._range));
            lookAt.setTilt(parseFloat(this._tilt));
            this._ge.getView().setAbstractView(lookAt);

        }
        else {
            this._map.setCenter(point, range);
        }
    },

    addMarker: function (lat, lng, desc, link) {
        this._lat = lat;
        this._lng = lng;
        this._desc = desc;
        this._link = link;

        var url = "http://maps.google.com/mapfiles/kml/shapes/info.png";

        var icon = new GIcon();
        icon.image = url;
        icon.iconSize = new GSize(30, 30);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
        entityMarker = new GMarker(new GLatLng(lat, lng), { icon: icon });

        GEvent.addListener(entityMarker, "click", function () {
            //            entityMarker.openInfoWindowHtml('<a href="#" onclick="parent.location=' + link +
            //            '">Página no portal</a> <div style="width: 500px;padding-top: 10px; padding-right: 10px">' + desc + '</div>' +
            //            '<div style="width: 100px;padding-top: 10px; padding-right: 10px; background-color:Gray"' +
            //            '<input id="Button1" type="button" value="Birds Eye" name="getinfo" onclick="BirdEye()"/>' +
            //            '<input id="Button2" type="button" value="Visita Virtual" name="getinfo" onclick="VirtualTour()"/>');

            entityMarker.openInfoWindowHtml('<div style="width: 500px;padding-top: 10px; padding-right: 10px">' + desc + '</div>');
        });

        this._map.addOverlay(entityMarker);
    },

    removeMarker: function () {
        this._map.removeOverlay(entityMarker);
    },

    addModel: function (kmlLink) {
        networkLink = this._ge.createNetworkLink("");
        var link = this._ge.createLink("");
        link.setHref(kmlLink);
        networkLink.setLink(link);
        networkLink.setVisibility(true);
        this._ge.getFeatures().appendChild(networkLink);
    },

    addPub: function (kmlLink) {
        pubLink = this._ge.createNetworkLink("");
        var link = this._ge.createLink("");
        link.setHref(kmlLink);
        pubLink.setLink(link);
        pubLink.setVisibility(true);
        this._ge.getFeatures().appendChild(pubLink);
    },

    removeModel: function () {
        GMap._ge.getFeatures().removeChild(networkLink);
    },

    _getMapType: function () {
        var Mtype = this._map.getCurrentMapType();
        return (Mtype);
    },

    getView: function () {
        var view = {};
        view.center = this._map.getCenter();
        view.zoom = this._map.getZoom();
        return (view);
    },

    _startRotator: function () {
        this._la = this._ge.getView().copyAsLookAt(this._ge.ALTITUDE_RELATIVE_TO_GROUND);

        this._lat = this._la.getLatitude();
        this._lng = this._la.getLongitude();
        this._range = this._la.getRange();
        this._heading = this._la.getHeading();
        this._tilt = this._la.getTilt();

        this._heading += 15/*this._rotationSpeed*/;

        this._la.setHeading(this._heading);
        this._ge.getView().setAbstractView(this._la);
    },

    _stopRotator: function () {
        if (this._frameendEL) {
            this._stateFrameend();
        }
    },

    rotateOnPoint: function ()  //Point Lat, Point Lng, Camera Range, Camera Heading
    {
        this.callback_stopRotator = GEvent.callback(this, this._stopRotator);
        google.earth.addEventListener(this._ge.getWindow(), "mousedown", this.callback_stopRotator);

        this._stateFrameend();
    },

    _stateFrameend: function () {
        if (this._frameendEL == false) {
            this.callback_startRotator = GEvent.callback(this, this._startRotator);
            google.earth.addEventListener(this._ge, "frameend", this.callback_startRotator);
            this._frameendEL = true;
            isRotate = true;
        }
        else {
            google.earth.removeEventListener(this._ge, "frameend", this.callback_startRotator);
            this._frameendEL = false;
            isRotate = false;
        }
    }
};
