﻿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;
    this.qs2 = new Querystring();
    this.LO = this.qs2.get("LO");
    this.REGID = this.qs2.get("REGID");
    this.CLR = this.qs2.get("CLR");
    this.LAG = this.qs2.get("LAG");
    this.FEA = this.qs2.get("FEA");
    this.PTYP = this.qs2.get("PTYP");
    this.CPT = this.qs2.get("CPT");
    this.COU = this.qs2.get("COU");
    this.PGSess = this.qs2.get("PGSess");
    this.control = null;
    if (this.LAG == null) {
        this.LAG = "0";
    }
    if (this.CPT == null) {
        this.CPT = "N";
    }
    if (this.COU == null) {
        this.COU = "0";
    }
    if (this.PGSess == null) {
        this.PGSess = "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 + 820), (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(360, 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 = "%";
            if (this.PTYP != null && this.PTYP != "") {
            PTYP = this.PTYP;
            }
            var LPMIN = "0";
            var LPMAX = "100000000000"
            var DISP = "100";
            PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
            PushpinService.GetPushpins(DISP, PTYP, LPMIN, LPMAX, zoom, centerLat, centerLong, this.LO, this.REGID, this.CLR, this.LAG, this.FEA, this.CPT, this.COU, this.PGSess, 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 = "DisplayProp";               
         this.control.style.top ="3px";                
         this.control.style.left = "398px"; 
         this.control.style.height = "27px"; 
         this.control.style.width = "455px";                                 
         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;' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
         "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Show: </span><select title='Change to show only a certain type.'" +
         " style='font-family:Arial;font-size:" +
         "9pt;margin:3px 3px 0px 0px;' id='MapPTYP' onchange=$find('TheMap').Dropfind_Corners(); >" +
	     "<option ID='%' value='%'>All Property Types</option>" +
	     "<option ID='BANK' value='BANK'>Only Bank-Owned/Foreclosures</option>" +
	     "<option ID='SHORT' value='SHORT'>Only Short Sale</option>" +
	     "<option ID='RESI' value='RESI'>Only Residential</option>" +
	     "<option ID='COND' value='COND'>Only Condominiums</option>" +
	     "<option ID='VACL' value='VACL'>Only Vacant Land</option>" +
	     "<option ID='MANU' value='MANU'>Only Manufactured Homes</option>" +
	     "<option ID='MULT' value='MULT'>Only Multi-Family Homes</option>" +
	     "<option ID='FARM' value='FARM'>Only Farm and Ranch</option>" +
         "</select>" +
         "<span style='font-family: Verdana; font-size:" +
         "8pt; color: white;' >Display: </span>" +
         "<select title='Change to display more listings on the map.' style='font-family: Arial; font-size:" +
         "9pt;margin:3px 3px 0px 0px;' id='MapDISPLAY' onchange=$find('TheMap').Dropfind_Corners(); >" +
	     "<option value='100'>100</option>" +
	     "<option value='200'>200</option>" +
	     "<option value='300'>300</option>" +
         "</select>"
         ;                 
         this._instance.AddControl(this.control);               
         this.addShim(this.control, '1'); 
         var curPTYP = document.getElementById(this.PTYP);
         if (curPTYP != null){
         curPTYP.selected = true;
         }else{
         curPTYP = "%";
        }
         this.control2 = document.createElement("div");                
         this.control2.id = "Price";               
         this.control2.style.top ="30px";                
         this.control2.style.left = "613px"; 
         this.control2.style.height = "25px"; 
         this.control2.style.width = "240px";
         this.control2.style.overflow = "hidden";                                 
         this.control2.style.background= "#265896"; 
         this.control2.style.filter = "alpha(opacity:77)"   
         this.control2.style.opacity = ".77"       
         this.control2.innerHTML = "<span style='font-family: Verdana; font-size:" +
         "8pt; color: white;' >Price: </span>" +
         "<select title='Should be the lowest amount you are searching for.' style='font-family:Arial;font-size:9pt;margin:3px 3px 0px 0px;' id='PriceMin'" +
         " onchange=$find('TheMap').Dropfind_Corners(); >" +
	     "<option ID='0' value='0'>Min</option>" +
	     "<option ID='50000' value='50000'>$50,000</option>" +
	     "<option ID='75000' value='75000'>$75,000</option>" +
	     "<option ID='100000' value='100000'>$100,000</option>" +
	     "<option ID='150000' value='150000'>$150,000</option>" +
	     "<option ID='200000' value='200000'>$200,000</option>" +
	     "<option ID='250000' value='250000'>$250,000</option>" +
	     "<option ID='300000' value='300000'>$300,000</option>" +
	     "<option ID='350000' value='350000'>$350,000</option>" +
	     "<option ID='400000' value='400000'>$400,000</option>" +
	     "<option ID='450000' value='450000'>$450,000</option>" +
	     "<option ID='500000' value='500000'>$500,000</option>" +
	     "<option ID='600000' value='600000'>$600,000</option>" +
	     "<option ID='700000' value='700000'>$700,000</option>" +
	     "<option ID='800000' value='800000'>$800,000</option>" +
	     "<option ID='900000' value='900000'>$900,000</option>" +
	     "<option ID='1000000' value='1000000'>$1,000,000</option>" +
	     "<option ID='1500000' value='1500000'>$1,500,000</option>" +
	     "<option ID='2000000' value='2000000'>$2,000,000</option>" +
	     "<option ID='2500000' value='2500000'>$2,500,000</option>" +
	     "<option ID='3000000' value='3000000'>$3,000,000</option>" +
	     "<option ID='3500000' value='3500000'>$3,500,000</option>" +
         "</select>" +
         "<span style='font-family: Verdana; font-size:" +
         "8pt; color: white;' >to </span>" +
         "<select title='Should be the highest amount you are searching for.' style='font-family:Arial;font-size:" +
         "9pt;margin:3px 3px 0px 0px;' id='PriceMax' onchange=$find('TheMap').Dropfind_Corners();>" +
	     "<option ID='100000000000' value='100000000000'>Max</option>" +
	     "<option ID='50000' value='50000'>$50,000</option>" +
	     "<option ID='75000' value='75000'>$75,000</option>" +
	     "<option ID='100000' value='100000'>$100,000</option>" +
	     "<option ID='150000' value='150000'>$150,000</option>" +
	     "<option ID='200000' value='200000'>$200,000</option>" +
	     "<option ID='250000' value='250000'>$250,000</option>" +
	     "<option ID='300000' value='300000'>$300,000</option>" +
	     "<option ID='350000' value='350000'>$350,000</option>" +
	     "<option ID='400000' value='400000'>$400,000</option>" +
	     "<option ID='450000' value='450000'>$450,000</option>" +
	     "<option ID='500000' value='500000'>$500,000</option>" +
	     "<option ID='600000' value='600000'>$600,000</option>" +
	     "<option ID='700000' value='700000'>$700,000</option>" +
	     "<option ID='800000' value='800000'>$800,000</option>" +
	     "<option ID='900000' value='900000'>$900,000</option>" +
	     "<option ID='1000000' value='1000000'>$1,000,000</option>" +
	     "<option ID='1500000' value='1500000'>$1,500,000</option>" +
	     "<option ID='2000000' value='2000000'>$2,000,000</option>" +
	     "<option ID='2500000' value='2500000'>$2,500,000</option>" +
	     "<option ID='3000000' value='3000000'>$3,000,000</option>" +
	     "<option ID='3500000' value='3500000'>$3,500,000</option>" +
	     "<option ID='5000000' value='5000000'>$5,000,000</option>" +
         "</select>" 
         ;                
         this._instance.AddControl(this.control2);               
         this.addShim(this.control2, '2'); 
         
         this.control3 = document.createElement("div");                
         this.control3.id = "Corner";               
         this.control3.style.top ="30px";                
         this.control3.style.left = "588px"; 
         this.control3.style.height = "25px";                                         
         this.control3.innerHTML = "<img id='Corner' height='25' src='images/corner.png' />"
         this._instance.AddControl(this.control3);               
         this.addShim(this.control3, '3'); 
        //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,x) {            
         var shim = document.createElement("iframe");            
         shim.id = "myShim" + x;            
         shim.frameBorder = "0";            
         shim.style.position = "absolute"; 
         shim.style.padding = "0px 0px 0px 0px"; 
         shim.style.margin = "0px 0px 0px 0px";                      
         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 + 820), (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(360, 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;
        var LPMIN = document.getElementById("PriceMin").value;
        var LPMAX = document.getElementById("PriceMax").value;
        this.qs2 = new Querystring();
        this.LO = this.qs2.get("LO");
        this.REGID = this.qs2.get("REGID");
        this.CLR = this.qs2.get("CLR");
        this.LAG = this.qs2.get("LAG");
        this.FEA = this.qs2.get("FEA");
        this.PTYP = this.qs2.get("PTYP");
        this.CPT = this.qs2.get("CPT");
        this.COU = this.qs2.get("COU");
        this.PGSess = this.qs2.get("PGSess");

        if (this.LAG == null) {
            this.LAG = "0";
            }
        if (this.CPT == null) {
        this.CPT = "N";
            }
        if (this.COU == null) {
        this.COU = "0";
    }
        if (this.PGSess == null) {
        this.PGSess = "0";
    }

        PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
        PushpinService.GetPushpins(DISP, PTYP, LPMIN, LPMAX, zoom, centerLat, centerLong, this.LO, this.REGID, this.CLR, this.LAG, this.FEA, this.CPT, this.COU, this.PGSess, 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 + 820), (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(360, 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;
        var LPMIN = document.getElementById("PriceMin").value;
        var LPMAX = document.getElementById("PriceMax").value;
        this.qs2 = new Querystring();
        this.LO = this.qs2.get("LO");
        this.REGID = this.qs2.get("REGID");
        this.CLR = this.qs2.get("CLR");
        this.LAG = this.qs2.get("LAG");
        this.FEA = this.qs2.get("FEA");
        this.PTYP = this.qs2.get("PTYP");
        this.CPT = this.qs2.get("CPT");
        this.COU = this.qs2.get("COU");
        this.PGSess = this.qs2.get("PGSess");
        if (this.LAG == null) {
            this.LAG = "0";
        }
        if (this.CPT == null) {
        this.CPT = "N";
        
        }
            if (this.COU == null) {
        this.COU = "0";
    }
        if (this.PGSess == null) {
        this.PGSess = "0";
    }

        PushpinService.LoadingGetPushpins(centerLat, centerLong, onGetComplete, onGetFailed);
        PushpinService.GetPushpins(DISP, PTYP, LPMIN, LPMAX, zoom, centerLat, centerLong, this.LO, this.REGID, this.CLR, this.LAG, this.FEA, this.CPT, this.COU, this.PGSess, 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);

