﻿Type.registerNamespace("Mashup");

Mashup.VirtualEarth = function(element) {
    // Call the constructor of the base class.
    Mashup.VirtualEarth.initializeBase(this, [element]);

    this._instance = null;
    this._initialLongitude = 0.0;
    this._initialLatitude = 0.0;
    this._initialZoomLevel = 0;




}
Mashup.VirtualEarth.prototype = {
    // Perform the initial setup of the instance.
    initialize: function() {
        // Call the base implementation.
        Mashup.VirtualEarth.callBaseMethod(this, "initialize");

        // Create a Virtual Earth map and load it into the associated element.
        this._instance = new VEMap(this.get_element().id);

        this._instance.onLoadMap = Function.createDelegate(this, onLoadComplete);

        function onLoadComplete() {

            this._instance.SetCenterAndZoom(new VELatLong(parseFloat(this._initialLatitude), parseFloat(this._initialLongitude)), parseFloat(this._initialZoomLevel));
            var topleft = this._instance.PixelToLatLong(new VEPixel(1, 1)).toString();
            var bottomright = this._instance.PixelToLatLong(new VEPixel((1 + 920), (1 + 400))).toString();
            var top = topleft.substring(0, topleft.indexOf(','));
            var bottom = bottomright.substring(0, bottomright.indexOf(','));
            var left = topleft.substring(topleft.indexOf(',') + 2, topleft.length);
            var right = bottomright.substring(bottomright.indexOf(',') + 2, bottomright.length);
            var center = this._instance.PixelToLatLong(new VEPixel(440, 180)).toString();
            var centerLat = parseFloat(center.substring(0, center.indexOf(',')));
            var centerLong = parseFloat(center.substring(center.indexOf(',') + 2, center.length - 1));
            var zoom = this._instance.GetZoomLevel();
            
            PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
            PushpinService.GetPushpins(zoom, centerLat, centerLong, bottom, top, left, right, onGetComplete, onGetFailed);

            function onGetComplete(result) {
                $find("TheMap").set_data(result);

            }

            function onGetFailed() {
                alert("Unable to retrieve listings.");
            }
        }

        this._instance.LoadMap();

        //add onpan event
        this._onPanDelegate = Function.createDelegate(this._instance, this.find_Corners);
        this._onZoomDelegate = Function.createDelegate(this._instance, this.find_Corners);
        //attach handlers
        this._instance.AttachEvent("onendpan", this._onPanDelegate);
        this._instance.AttachEvent("onendzoom", this._onZoomDelegate);

    },

    // Perform the disposing of the instance.
    dispose: function() {
        // Nothing to dispose.

        Mashup.VirtualEarth.callBaseMethod(this, "dispose");
    },
    find_Corners: function() {
        var topleft = this.PixelToLatLong(new VEPixel(1, 1)).toString();
        var bottomright = this.PixelToLatLong(new VEPixel((1 + 920), (1 + 400))).toString();
        var top = topleft.substring(0, topleft.indexOf(','));
        var bottom = bottomright.substring(0, bottomright.indexOf(','));
        var left = topleft.substring(topleft.indexOf(',') + 2, topleft.length);
        var right = bottomright.substring(bottomright.indexOf(',') + 2, bottomright.length);
        var center = this.PixelToLatLong(new VEPixel(440, 180)).toString();
        var centerLat = parseFloat(center.substring(0, center.indexOf(',')));
        var centerLong = parseFloat(center.substring(center.indexOf(',') + 2, center.length - 1));
        var zoom = this.GetZoomLevel();

        PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
        PushpinService.GetPushpins(zoom, centerLat, centerLong, bottom, top, left, right, onGetComplete, onGetFailed);

        function onGetComplete(result) {
            $find("TheMap").set_data(result);
        }

        function onGetFailed() {
            alert("Unable to retrieve listings.");
        }
    },
    set_data: function(data) {
        this._render(data);
    },

    // Adds pushpins based on the data passed to the set_data method.
    _render: function(data) {
        if (!this.get_isInitialized()) {
            return;
        }

        this._instance.DeleteAllShapes();

        for (var i = 0; i < data.length; i++) {
            var currPushpin = data[i];

            this._addPushpin(currPushpin);
        }
    },

    // Builds a pushpin from a client object and adds it to the map.
    _addPushpin: function(pushpinData) {
        if (this._instance === null) {
            return;
        }

        var pushpinLocation = new VELatLong(parseFloat(pushpinData.Latitude), parseFloat(pushpinData.Longitude));
        var shape = new VEShape(VEShapeType.Pushpin, pushpinLocation);

        var customIconUrl = pushpinData.ImageUrl;

        if (customIconUrl !== "") {
            shape.SetCustomIcon(pushpinData.ImageUrl);
        }

        shape.SetTitle(pushpinData.Title);
        shape.SetDescription(pushpinData.Description);

        // Add pushpin to the map.
        this._instance.AddShape(shape);
    },

    // Properties.
    get_initialLatitude: function() {
        return this._initialLatitude;
    },

    set_initialLatitude: function(value) {
        this._initialLatitude = value;
    },

    get_initialLongitude: function() {
        return this._initialLongitude;
    },

    set_initialLongitude: function(value) {
        this._initialLongitude = value;
    },

    get_zoomLevel: function() {
        return this._instance.GetZoomLevel();
    },

    set_zoomLevel: function(value) {
        if (this._instance) {
            this._instance.SetZoomLevel(value);
        }
        else {
            this._initialZoomLevel = value;
        }
    }
}
// Register the constructor as a client Control.
Mashup.VirtualEarth.registerClass("Mashup.VirtualEarth", Sys.UI.Control);

