﻿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();
            var PTYP = "%";
            var DISP = "100";
            PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
            PushpinService.GetPushpins(DISP, PTYP, 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();
         this.control = document.createElement("div");                
         this.control.id = "myControl";               
         this.control.style.top ="3px";                
         this.control.style.left = "398px"; 
         this.control.style.height = "27px";                                  
         this.control.style.background= "#265896"; 
         this.control.style.filter = "alpha(opacity:77)"   
         this.control.style.opacity = ".77"       
         this.control.innerHTML = "<span style='font-family: Verdana; font-size:" +
         "8pt; color: white;' >Show: </span>" +
         "<select style='margin:3px 3px 0px 0px;' id='MapPTYP' name='MapPTYP' onchange=$find('TheMap').Dropfind_Corners(); >" +
	     "<option value='%'>All Property Types</option>" +
	     "<option value='BANK'>Only Bank-Owned/Foreclosure</option>" +
	     "<option value='RESI'>Only Residential</option>" +
	     "<option value='COND'>Only Condominiums</option>" +
	     "<option value='VACL'>Only Vacant Land</option>" +
	     "<option value='MANU'>Only Manufactured Homes</option>" +
	     "<option value='MULT'>Only Multi-Family Homes</option>" +
	     "<option value='FARM'>Only Farm and Ranch</option>" +
         "</select>" +
         "<span style='font-family: Verdana; font-size:" +
         "8pt; color: white;' >Display: </span>" +
         "<select style='margin:3px 3px 0px 0px;' id='MapDISPLAY' name='MapDISPLAY' onchange=$find('TheMap').Dropfind_Corners(); >" +
	     "<option value='100'>100 Listings</option>" +
	     "<option value='200'>200 Listings</option>" +
	     "<option value='300'>300 Listings</option>" +
	     "<option value='400'>400 Listings</option>" +
	     "<option value='500'>500 Listings</option>" +
         "</select>"
         ;                 
         this._instance.AddControl(this.control);               
         this.addShim(this.control);
        //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");
    },
         addShim: function(el) {            
         var shim = document.createElement("iframe");            
         shim.id = "myShim";            
         shim.frameBorder = "0";            
         shim.style.position = "absolute";            
         shim.style.zIndex = "1";            
         shim.style.top  = el.offsetTop;            
         shim.style.left = el.offsetLeft;            
         shim.width  = el.offsetWidth;            
         shim.height = el.offsetHeight;            
         el.shimElement = shim;            
         el.parentNode.insertBefore(shim, el); 
    },
        Dropfind_Corners: function() {
        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();
        var PTYP = document.getElementById("MapPTYP").value;
        var DISP = document.getElementById("MapDISPLAY").value;

        PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
        PushpinService.GetPushpins(DISP, PTYP, zoom, centerLat, centerLong, bottom, top, left, right, onGetComplete, onGetFailed);

        function onGetComplete(result) {
            $find("TheMap").set_data(result);
        }

        function onGetFailed() {
            alert("Unable to retrieve listings.");
        }
    },
    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();
        var PTYP = document.getElementById("MapPTYP").value;
        var DISP = document.getElementById("MapDISPLAY").value;
        PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
        PushpinService.GetPushpins(DISP, PTYP, 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);

