﻿Type.registerNamespace("RoREN");

RoREN.VirtualEarth = function(element) {
    // Call the constructor of the base class.
    RoREN.VirtualEarth.initializeBase(this, [element]);

    // private fields
    this._instance = null;
    this._startPoint = new VELatLong(44.435488, 26.102786);
    this._singlePoiLayer = new VEShapeLayer();

    // public fields
    this._enablePlaceMode = false;
    this._enableExploreMode = false;
    this._poiApiID = null;
    this._city = null;
    this._country = null;
    this._width = null;
    this._height = null;
    this._latitude = null
    this._longitude = null;
    this._zoomLevel = null;

    // associated controls
    this._latitudeTextboxID = null;
    this._longitudeTextboxID = null;
    this._latitudeLabelID = null;
    this._longitudeLabelID = null;
    
    // private fields for place mode
    this._newPoiShape = null;
    this._startPan = false;
    this._endPan = false;
    this._zooming = false;
    this._moving = false;

    // private fields for explore mode
    this._explorePoiLayer = null;

    // constants
    this.GEORSS_URL_BASE = "/poi/georss.xml?id=";
}

RoREN.VirtualEarth.prototype = {

    initialize: function() {
        // Call the base implementation.
        RoREN.VirtualEarth.callBaseMethod(this, "initialize");

        // create and load map
        this._instance = new VEMap(this.get_element().id);
        this._instance.SetDashboardSize(VEDashboardSize.Tiny);
        this._instance.LoadMap(this._startPoint, 15, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 0);
        this._instance.AddShapeLayer(this._singlePoiLayer);

        if (this._poiApiID != "") {
            var url = this.GEORSS_URL_BASE + this._poiApiID;
            var layerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, url, this._singlePoiLayer);
            this._instance.ShowMessageBox = false;
            this._instance.ImportShapeLayerData(layerSpec, Function.createDelegate(this, this.onSinglePoiLoad), true);
        }
        else if (this._city != null && this._country != null) {
            this._instance.Find(null,
                                this._city + ',' + this._country,
                                null,
                                null,
                                0,
                                1,
                                null,
                                null,
                                false,
                                true,
                                Function.createDelegate(this, this.onFind));
        }
        if (this._enablePlaceMode == true) {
            this._instance.AttachEvent("onstartpan", Function.createDelegate(this, this.onPlaceStartPan));
            this._instance.AttachEvent("onendpan", Function.createDelegate(this, this.onPlaceEndPan));
            this._instance.AttachEvent("onstartzoom", Function.createDelegate(this, this.onPlaceStartZoom));
            this._instance.AttachEvent("onendzoom", Function.createDelegate(this, this.onPlaceEndZoom));
            this._instance.AttachEvent("onclick", Function.createDelegate(this, this.onPlaceClick));
            this._instance.AttachEvent("onmousedown", Function.createDelegate(this, this.onPlaceMouseDown));
            this._instance.AttachEvent("onmouseup", Function.createDelegate(this, this.onPlaceMouseUp));
            this._instance.AttachEvent("onmousemove", Function.createDelegate(this, this.onPlaceMouseMove));
            this.SetNewVisibility();
        }
    },

    // Perform the disposing of the instance.
    dispose: function() {
        // Nothing to dispose.
        RoREN.VirtualEarth.callBaseMethod(this, "dispose");
    },

    // Event handlers
    onSinglePoiLoad: function(layer) {
        this._instance.ShowMessageBox = true;
        if (this._instance.GetZoomLevel() > 14) { this._instance.SetZoomLevel(14); }
        if (this._enablePlaceMode == true) {
            this._newPoiShape = layer.GetShapeByIndex(0);
            this.SetEditVisibility(this._newPoiShape);
        }
    },

    onFind: function(layer, resultsArray, places, hasMore, veErrorMessage) {
        if (places != null) {
            this._instance.SetCenter(places[0].LatLong);
        }
    },

    onPlaceStartPan: function(e) {
        this._startPan = true;
    },

    onPlaceEndPan: function(e) {
        this._endPan = true;
    },

    onPlaceStartZoom: function(e) {
        this._zooming = true;
    },

    onPlaceEndZoom: function(e) {
        this._zooming = false;
        if (this._newPoiShape != null) {
            if (this._instance.GetZoomLevel() < "17") { this.SetZoomNotice(); } else { this.ClearZoomNotice(); }
        }
    },

    onPlaceClick: function(e) {
        if (this._instance.GetMapStyle() == VEMapStyle.Aerial || this._instance.GetMapStyle() == VEMapStyle.Road ||
            this._instance.GetMapStyle() == VEMapStyle.Shaded || this._instance.GetMapStyle() == VEMapStyle.Hybrid) {
            if (document.getElementById(this._latitudeTextboxID) == null ||
                document.getElementById(this._longitudeTextboxID) == null) {
                alert("Punctul de interes a fost salvat. Dacă vrei să plasezi un alt obiectiv reîncarcă pagina.");
            }
            else if (!this.WasPanning()) {
                var clickedPixel = new VEPixel(e.mapX, e.mapY);
                var clickedLatLon = this._instance.PixelToLatLong(clickedPixel);
                this._newPoiShape = new VEShape(VEShapeType.Pushpin, clickedLatLon);
                this._instance.DeleteAllShapes();
                this._singlePoiLayer.AddShape(this._newPoiShape);
                if (this._instance.GetZoomLevel() < "17") { this.SetZoomNotice(); } else { this.ClearZoomNotice(); }
                this.SetInsertVisibility();
                document.getElementById(this._latitudeTextboxID).value = clickedLatLon.Latitude.toFixed(6);
                document.getElementById(this._longitudeTextboxID).value = clickedLatLon.Longitude.toFixed(6);
                //UpdateRawLatLon(clickedLatLon);
                //this._instance.FindLocations(clickedLatLon, this.onPlaceFindLocations);
            }
        }
    },

    onPlaceMouseDown: function(e) {
        if (this._instance.GetMapStyle() == VEMapStyle.Aerial || this._instance.GetMapStyle() == VEMapStyle.Road ||
            this._instance.GetMapStyle() == VEMapStyle.Shaded || this._instance.GetMapStyle() == VEMapStyle.Hybrid) {
            if (e.elementID) {
                this._moving = true;
                this._instance.vemapcontrol.EnableGeoCommunity(true);
                document.getElementById(this.get_element().id).style.cursor = 'Move';
            }
        }
    },

    onPlaceMouseUp: function(e) {
        if (this._moving) {
            this._moving = false;
            this._instance.vemapcontrol.EnableGeoCommunity(false);
            document.getElementById(this.get_element().id).style.cursor = '';
        }
    },

    onPlaceMouseMove: function(e) {
        var moveTo;
        if (this._moving) {
            moveTo = this._instance.PixelToLatLong(new VEPixel(e.mapX, e.mapY));
            this._instance.HideInfoBox();
            this._newPoiShape.SetPoints(moveTo);
            //UpdateRawLatLon(moveTo);
        }
    },

    // Supporting functions
    WasPanning: function() {
        var wasPanning = this._startPan && this._endPan;
        this._startPan = false;
        this._endPan = false;
        return wasPanning;
    },

    SetZoomNotice: function() {
        this._newPoiShape.SetTitle('Zoom in!');
        this._newPoiShape.SetDescription('Recomandam sa \'zoom in\' pentru a plasa punctul cu mai multa precizie.');
        this._instance.ShowInfoBox(this._newPoiShape);
    },

    ClearZoomNotice: function() {
        this._instance.HideInfoBox();
        this._newPoiShape.SetTitle('');
        this._newPoiShape.SetDescription('');
    },

    SetNewVisibility: function() {
        if (this._latitudeLabelID != null && document.getElementById(this._latitudeLabelID) != null) {
            document.getElementById(this._latitudeLabelID).style.visibility = "hidden";
        }
        if (this._longitudeLabelID != null && document.getElementById(this._longitudeLabelID)) {
            document.getElementById(this._longitudeLabelID).style.visibility = "hidden";
        }
    },

    SetInsertVisibility: function() {
        if (this._latitudeLabelID != null && document.getElementById(this._latitudeLabelID) != null) {
            document.getElementById(this._latitudeLabelID).style.visibility = "visible";
        }
        if (this._longitudeLabelID != null && document.getElementById(this._longitudeLabelID)) {
            document.getElementById(this._longitudeLabelID).style.visibility = "visible";
        }
    },

    SetEditVisibility: function(shape) {
        var shapePoint = shape.GetPoints()[0];
        if (this._latitudeTextboxID != null && document.getElementById(this._latitudeTextboxID) != null) {
            document.getElementById(this._latitudeTextboxID).value = shapePoint.Latitude;
        }
        if (this._longitudeTextboxID != null && document.getElementById(this._longitudeTextboxID)) {
            document.getElementById(this._longitudeTextboxID).value = shapePoint.Longitude;
        }
    },

    // Properties.
    get_poiApiID: function() {
        return this._poiApiID;
    },

    set_poiApiID: function(value) {
        this._poiApiID = value;
    },

    get_enablePlaceMode: function() {
        return this._enablePlaceMode;
    },

    set_enablePlaceMode: function(value) {
        this._enablePlaceMode = value;
    },

    get_enableExploreMode: function() {
        return this._enableExploreMode;
    },

    set_enableExploreMode: function(value) {
        this._enableExploreMode = value;
    },

    get_city: function() {
        return this._city;
    },

    set_city: function(value) {
        this._city = value;
    },

    get_country: function() {
        return this._country;
    },

    set_country: function(value) {
        this._country = value;
    },

    get_width: function() {
        return this._width;
    },

    set_width: function(value) {
        this._width = value;
    },

    get_height: function() {
        return this._height;
    },

    set_height: function(value) {
        this._height = value;
    },

    get_latitude: function() {
        return this._latitude;
    },

    set_latitude: function(value) {
        this._latitude = value;
    },

    get_longitude: function() {
        return this._longitude;
    },

    set_longitude: function(value) {
        this._longitude = value;
    },

    get_zoomLevel: function() {
        this._zoomLevel = this._instance.GetZoomLevel();
        return this._zoomLevel;
    },

    set_zoomLevel: function(value) {
        this._zoomLevel = value;
        if (this._instance && value != 0) {
            this._instance.SetZoomLevel(this._zoomLevel);
        }
    },

    get_latitudeTextboxID: function() {
        return this._latitudeTextboxID;
    },

    set_latitudeTextboxID: function(value) {
        this._latitudeTextboxID = value;
    },

    get_longitudeTextboxID: function() {
        return this._longitudeTextboxID;
    },

    set_longitudeTextboxID: function(value) {
        this._longitudeTextboxID = value;
    },

    get_latitudeLabelID: function() {
        return this._latitudeLabelID;
    },

    set_latitudeLabelID: function(value) {
        this._latitudeLabelID = value;
    },

    get_longitudeLabelID: function() {
        return this._longitudeLabelID;
    },

    set_longitudeLabelID: function(value) {
        this._longitudeLabelID = value;
    }
}

// Register the constructor as a client Control.
try { RoREN.VirtualEarth.registerClass("RoREN.VirtualEarth", Sys.UI.Control); } catch(e) {}
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

