﻿

// Define Global variables.
var chMap;
var chMQPoiCollection;
var chMapInit;
var chMarketZoomLevel;
var chOrigZoomLevel;
var chCenter;
var chSiteRoot;
var updateMapFlag = true;
var chSearchParams;
var minLat;
var minLng;
var maxLat;
var maxLng;
var chMaxPoints;
var chDisplayMapType;
var chStateZoomLevel;

var isIE6 = false/*@cc_on || @_jscript_version < 5.7@*/;

// Use ASP.NET Ajax to do the page load and unload.
if (isIE6) 
{
    window.attachEvent("onload", page_load);
}
else 
{
    Sys.Application.add_load(page_load);
}
Sys.Application.add_unload(page_unload);

function page_load(sender, e) {

    // Quit if this function has already been called.
    if (arguments.callee.done) return;

    // Flag this function so we don't
    // execute the page_load twice.
    arguments.callee.done = true;

    // Call set up map.
    setUpMap();

}

// 
function setUpMap() {

    var chCenterLat = $get('chCenterLat').value;
    var chCenterLng = $get('chCenterLng').value;
    chSiteRoot = $get('chSiteRoot').value;
    chSearchParams = $get('chSearchParams').value;

    chMarketZoomLevel = $get('chMarketZoomLevel').value;
    chStateZoomLevel = $get('chStateZoomLevel').value;
    chOrigZoomLevel = $get('chOrigZoomLevel').value;
    chCenter = new MQLatLng(chCenterLat, chCenterLng);

    minLat = parseFloat($get('chMinLat').value);
    maxLat = parseFloat($get('chMaxLat').value);
    minLng = parseFloat($get('chMinLng').value);
    maxLng = parseFloat($get('chMaxLng').value);

    chMaxPoints = parseInt($get('chMaxPoints').value);
    chDisplayMapType = $get('chDisplayMapType').value;

    chMapInit = new MQMapInit();

    //chMap = new MQTileMap($get('mapWindow'), chMarketZoomLevel, chCenter, "map", chMapInit);
    chMap = new MQTileMap($get('mapWindow'), chOrigZoomLevel, chCenter, "map", chMapInit);

    // Create a new Large Zoom control.
    var chLZControl = new MQLargeZoomControl(chMap);
    // Tell the map to display the control we just attached.
    chMap.addControl(chLZControl, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(20, 20)));
    // Check whether we should display the map view control.
    if (chDisplayMapType == "true") {
        // Create a map view type control.
        var chVControl = new MQViewControl(chMap);
        // Display control - Use the corner placements constant to specify which corner,
        // plus an NQSize object to specify X and Y "indent" offset.
        chMap.addControl(chVControl, new MQMapCornerPlacement(MQMapCorner.BOTTOM_RIGHT, new MQSize(20, 20)));
    }






    if (minLat==0 && maxLat==0 && minLng==0 && maxLng==0){

        var topLeftLL = chMap.pixToLL(new MQPoint(0, 0));    
        var mapSize = getMapSize();
        var bottomRightLL = chMap.pixToLL(new MQPoint(mapSize.getWidth(), mapSize.getHeight()));


        //var theseParams = Sys.Serialization.JavaScriptSerializer.deserialize(chSearchParams, true);
        //theseParams.MinLatitude = topLeftLL.getLatitude();
        //theseParams.MaxLatitude = bottomRightLL.getLatitude();
        //theseParams.MinLongitude = topLeftLL.getLongitude();
        //theseParams.MaxLongitude = bottomRightLL.getLongitude();
        //chSearchParams = Sys.Serialization.JavaScriptSerializer.serialize(theseParams);

        minLat = topLeftLL.getLatitude();
        maxLat = bottomRightLL.getLatitude();
        minLng = topLeftLL.getLongitude();
        maxLng = bottomRightLL.getLongitude();
    }








    
    // Call Web Service to get points for map.
    getCommunityPoints(chSearchParams, minLat, minLng, maxLat, maxLng, chMaxPoints);

    // Add Event Listerners.
    MQEventManager.addListener(chMap, "zoomend", zoomEnd);
    MQEventManager.addListener(chMap, "moveend", mapMoveEnd);
    MQEventManager.addListener(chMap.getInfoWindow(), "opened", infoWindowOpen);
    MQEventManager.addListener(chMap.getInfoWindow(), "closed", infoWindowClose);
    MQEventManager.addListener(chMap.getRolloverWindow(), "opened", rolloverWindowOpen);
    MQEventManager.addListener(chMap.getRolloverWindow(), "closed", rolloverWindowClose);
    
}

//*************************************************************************
// Web service calls
//*************************************************************************
function getCommunityPoints(chSearchParams, minLat, minLng, maxLat, maxLng, maxResults) {
    Centex.Web.UI.ServiceProxy.GetCommunityPoints(chSearchParams, minLat, minLng, maxLat, maxLng, maxResults, onCommunityPoiSuccess, onError);
}

function getMarketPoints(minLat, minLng, maxLat, maxLng, maxResults) {
    Centex.Web.UI.ServiceProxy.GetMarketPoints(minLat, minLng, maxLat, maxLng, maxResults, onMarketPoiSuccess, onError);
}

function getStatePoints(minLat, minLng, maxLat, maxLng, maxResults) {
    Centex.Web.UI.ServiceProxy.GetStatePoints(minLat, minLng, maxLat, maxLng, maxResults, onStatePoiSuccess, onError);
}

Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
};

function roundto ( numerator, denominator ) {
    // In JavaScript, dividing integer values yields a floating point result (unlike in Java, C++, C)
    // To find the integer quotient, reduce the numerator by the remainder first, then divide.
    //var remainder = numerator % denominator;
    //var quotient = ( numerator - remainder ) / denominator;

    return (denominator * Math.round(numerator / denominator)); 
    // Another possible solution: Convert quotient to an integer by truncating toward 0.
    // Thanks to Frans Janssens for pointing out that the floor function is not correct for negative quotients.
    /****************************************************
    if ( quotient >= 0 )
        quotient = Math.floor( quotient );
    else  // negative
        quotient = Math.ceil( quotient );
     *****************************************************/

    //return quotient;
}



//function checkLatLng(
function onCommunityPoiSuccess(result, context, methodName) {

    chMQPoiCollection = new MQPoiCollection();

    var centroids = new Array();
    var fuzzycentroids = new Array();
    var fuzzyULCs = new Array();
    var fuzzyURCs = new Array();
    var fuzzyLLCs = new Array();
    var fuzzyLRCs = new Array();
    var fuzzyLatitude = 0.0;
    var fuzzyLongitude = 0.0;
    var fuzzyFactor = 0.0;
    var fuzzyLatLng = "";
    var fuzzyDirection = 1;
    var currentZoom = chMap.getZoomLevel();
    var maxZoomLevel = 16 + 2;
    var marketZoomLevel = 6; //7bm
    var pointDistance = 0.05; //0.0275;  // maximum amount of change to geocoordinates at market level
    var curveRate = 2; // lower number will result in steeper change
    var factor = (Math.pow(Math.log(maxZoomLevel - currentZoom), (maxZoomLevel - currentZoom) / curveRate) / (Math.pow(Math.log(maxZoomLevel - marketZoomLevel), (maxZoomLevel - marketZoomLevel) / curveRate))) * pointDistance;
    
    fuzzyFactor = factor * 100000000;

    // Parse thru all the items in the array of points.
    for (var i = 0; i < result.length; i++) {

        // Create a new object point based on the array item.
        var point = result[i];

        var pointLatLng = point.Latitude.toString() + '|' + point.Longitude.toString();

        while (point.Latitude !== 0 && point.Longitude !== 0 && centroids.contains(pointLatLng)) {
            point.Longitude = point.Longitude + (.75 * factor);
            pointLatLng = point.Latitude.toString() + '|' + point.Longitude.toString();
        }
        if (point.Latitude !== 0 && point.Longitude !== 0)
            centroids.push(pointLatLng);

        fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
        fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
        fuzzyLatLng = fuzzyLatitude.toString() + '|' + fuzzyLongitude.toString();
        
        fuzzyDirection = 1;
        while(fuzzyLatitude !== 0 && fuzzyLongitude !== 0 && fuzzycentroids.contains(fuzzyLatLng)){
            if (fuzzyDirection == 1) {
                point.Longitude = point.Longitude + (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
            }else if (fuzzyDirection == 2) {
                point.Longitude = point.Longitude - (.75 * factor);
                point.Latitude = point.Latitude + (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
                fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
            }else if (fuzzyDirection == 3) {
                point.Longitude = point.Longitude - (.75 * factor);
                point.Latitude = point.Latitude - (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
                fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
            }else if (fuzzyDirection == 4) {
                point.Longitude = point.Longitude + (.75 * factor);
                point.Latitude = point.Latitude - (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
                fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
            }else if (fuzzyDirection == 5) {
                point.Longitude = point.Longitude + (.75 * factor);
                point.Latitude = point.Latitude + (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
                fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
//            }else if ((fuzzyDirection % 2) == 1) {
//                point.Longitude = point.Longitude + factor;
//                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
//            }else{
//                point.Latitude = point.Latitude - factor;
//                fuzzyLatitude = roundto((point.Latitude * 100000000),fuzzyFactor);
//            }
            }else{
                point.Longitude = point.Longitude + (.75 * factor);
                fuzzyLongitude = roundto((point.Longitude * 100000000),fuzzyFactor);
            }
            fuzzyLatLng = fuzzyLatitude.toString() + '|' + fuzzyLongitude.toString();
            fuzzyDirection = fuzzyDirection + 1;
        }













        if (point.Latitude !== 0 && point.Longitude !== 0)
            fuzzycentroids.push(fuzzyLatLng);
            
//        while (point.Latitude !== 0 && point.Longitude !== 0 && centroids.contains(pointLatLng)) {
//            point.Longitude = point.Longitude + factor;
//            pointLatLng = point.Latitude.toString() + '|' + point.Longitude.toString();
//        }
//        if (point.Latitude !== 0 && point.Longitude !== 0)
//            centroids.push(pointLatLng);
        
        // Assign Latitude and Longitude to the MapQuest Point.
        var chPoint = new MQPoi(new MQLatLng(point.Latitude, point.Longitude));
        // Use a string builder to create the HTML for the point.
        var sb = new Sys.StringBuilder();
        
        
        sb.append("<div class=\"modal\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"350px\"><tr><td colspan=\"2\"><span class=\"subtitle_metro\" style=\"line-height:20px;\">");

        if (point.PointID > 0) {
            var pointID = parseInt(point.PointID) > 0 ? point.PointID + '' : '';
            sb.append(pointID);
            sb.append(". ");
            sb.append("<a href=\"");
            sb.append(point.CommunityUrl);
            sb.append("\">");
            //sb.append((point.CommunityName.replace("'","")));
            sb.append(point.CommunityName);
            sb.append("</a></span><br /> {");
            sb.append(point.HomeCount);
            sb.append(" matching homes}<br /><div class=\"floatL\" style=\"margin-top:10px;\">");
            sb.append("<a href=\"");
            sb.append(point.CommunityUrl);
            sb.append("\" style=\"padding-right: 5px;\">");
            sb.append("<img src=\"");
            sb.append(point.CommunityImage);
            sb.append("\" width=\"152\" height=\"104\" style=\"padding-right:10px; border-width: 0px;\"/></a></div><div style=\"float:left; margin-top:5px;\"><span style=\"line-height:20px;\">New homes in ");
            sb.append(point.City);
            sb.append(", ");
            sb.append(point.State);
            sb.append("</span><br/><span style=\"line-height:20px;\">Priced ");
            sb.append(point.PriceRange);
            sb.append("</span><br/><span style=\"line-height:20px;\">");
            sb.append(point.SquareFootage);
            sb.append(point.Bedroom);
            sb.append("</span><br/><span style=\"line-height:12px;\">Community amenities: ");
            sb.append(point.Amenity);
            sb.append("</span><br /><span style=\"line-height:6px;\">&nbsp;</span><br /><span style=\"line-height:16px;\"><a href=\"");
            sb.append(point.ContactUsUrl);
            sb.append("\" class='metrolinks'>Contact Us</a>&nbsp; |&nbsp; ");
            sb.append("<a href=\"");
            sb.append(point.DrivingDirectionsUrl);
            sb.append("\" class='metrolinks'>Driving Directions</a> <br/><a href=\"");
            sb.append(point.ContactUsUrl);
            sb.append("\" class='metrolinks'>Sign Up For Email Updates</a></span><br/><span style=\"line-height:6px;\">&nbsp;</span><br /></div></td></tr/></table></div>");                
        }else{

            if (1==1){
            sb.append(i);
            sb.append(". ");
            sb.append(point.CommunityName);
            sb.append("</span><br /> {");
            sb.append(point.HomeCount);
            sb.append(" matching homes}<br /><div class=\"floatL\" style=\"margin-top:10px;\">");
            sb.append("<img src=\"");
            sb.append(point.CommunityImage);
            sb.append("\" width=\"152\" height=\"104\" style=\"padding-right:10px; border-width: 0px;\"/></div><div style=\"float:left; margin-top:5px;\"><span style=\"line-height:20px;\">New homes in ");
            sb.append(point.City);
            sb.append(", ");
            sb.append(point.State);
            sb.append("</span><br/><span style=\"line-height:20px;\">Priced ");
            sb.append(point.PriceRange);
            sb.append("</span><br/><span style=\"line-height:20px;\">");
            sb.append(point.SquareFootage);
            sb.append(point.Bedroom);
            sb.append("</span><br/><span style=\"line-height:12px;\">Community amenities: ");
            sb.append(point.Amenity);
            sb.append("</span><br /><span style=\"line-height:6px;\">&nbsp;</span><br /><span style=\"line-height:16px;\"><a href=\"");
            sb.append(point.MarketUrl);
            sb.append("\" class='metrolinks'>See communities in the ");
            sb.append(point.MarketName);
            sb.append(" area</a></span><br/><span style=\"line-height:6px;\">&nbsp;</span><br /></div></td></tr/></table></div>");                


            }else{



            sb.append(i);
            sb.append(". ");
            sb.append(point.CommunityName);
            sb.append("</span><br/>");
            sb.append("<span style=\"line-height:14px;\">");
            sb.append("<a href=\"");
            sb.append(point.MarketUrl);
            //sb.append("");
            sb.append("\" class='metrolinks'>See communities in ");
            sb.append(point.MarketName);
            sb.append("</a>");
            sb.append("</span>");
            sb.append("</td></tr></table></div>");                
            
            }
            
            
        }
        // Set the HTML content for the Point from the string builder.
        chPoint.setInfoContentHTML(sb.toString());
        //chPoint.setInfoContentHTML("<div class=\"modal\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td colspan=\"2\"><span class=\"subtitle_metro\" style=\"line-height:20px;\">1. Quail Ridge Estates</span> {15 matching homes}</td></tr><tr><td><div style=\"margin-top:10px;\"><img src=\"http://dev.centexhomes.com/App_Themes/English/Images/modal_img.gif\" width=\"152\" height=\"104\" style=\"padding-right:10px;\"/></div></td><td><div style=\"margin-top:5px; width:195px;\"><span style=\"line-height:20px;\">New homes in city, ST</span><br/><span style=\"line-height:20px;\">Priced from $215's - 267's</span><br/><span style=\"line-height:20px;\">1,370 - 3,882 sq ft., 3-5 br.</span><br/><span style=\"line-height:12px;\">Community amenities: lorem ate ipsum dolor sit ament when i consecutetuer</span><br /><span style=\"line-height:6px;\">&nbsp;</span><br /><span style=\"line-height:12px;\"><a href=\"#\">Contact US</a>&nbsp; |&nbsp; <a href=\"#\">Driving Directions</a> <br/><a href=\"#\">Sign Up For Email Updates</a></span><br/><span style=\"line-height:6px;\">&nbsp;</span><br /></div></td></tr/></table></div>");
        // Set the title for the Point.
        chPoint.setInfoTitleHTML("<span>" + point.CommunityName.replace("'", "") + "</span>");
        // Set the unique identifier for the Point.
        chPoint.setKey(i);
        // Create an Icon object.
        var chIcon = new MQMapIcon();
        // Set the Icon's properties.
        chIcon.setImage(point.ImageUrl, 24, 24, true, false);
        // Associate the Icon to the Point.
        chPoint.setIcon(chIcon);
        // Add the Point to the Point Collection.
        chMQPoiCollection.add(chPoint);
    }    
    
    // Add or replace the Collection to the map object.
    renderPoiCollection(chMQPoiCollection);

    // Set the best fit for the map.
    // chMapInit.setBestFitRect(chMQPoiCollection.getBoundingRect());

}

function onMarketPoiSuccess(result, context, methodName) {

    chMQPoiCollection = new MQPoiCollection();

    var resultLength = result.length;

    for (i = 0; i < resultLength; i++) {

        // Create a new object point based on the array item.
        var point = result[i];
        // Assign Latitude and Longitude to the MapQuest Point.
        var chPoint = new MQPoi(new MQLatLng(point.Latitude, point.Longitude));
        // Use a string builder to create the HTML for the point.
        var sb = new Sys.StringBuilder();
//        sb.append("<div><a href=\"javascript:void(0);\" onclick=\"ZoomToLatLngMkt(");
        sb.append("<div><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><br><a href=\"Metro.aspx?MktID=");
        sb.append(point.MarketID);
//        sb.append("\" onclick=\"ZoomToLatLngMkt(");
        sb.append("\">");
//        sb.append(point.Latitude);
//        sb.append(",");
//        sb.append(point.Longitude);
//        sb.append(",'");
//        sb.append(point.Content);
//        sb.append("',");
//        sb.append(point.MarketID);
//        sb.append(", ");
//        sb.append(chMarketZoomLevel);
//        sb.append(");\">View Communities in ");
        sb.append("View Communities in ");
        sb.append(point.Content);
        sb.append("</a></td></tr></table></div>");
        // Assign the String Builder string to the point.
        chPoint.setInfoContentHTML(sb.toString());
        // Use string builder to create the HTML for the point Info Title.
        var sb2 = new Sys.StringBuilder();
        sb2.append("<strong>");
        sb2.append(point.Title);
        sb2.append(" (");
        sb2.append(point.CommunityCount);
        sb2.append(" communities)</strong>");
        // Assign the String Builder string for the point Info Title HTML.
        chPoint.setInfoTitleHTML(sb2.toString());
        // Set the unique key to the point.
        chPoint.setKey(point.MarketPointID);
        // Create an Icon object.
        var chIcon = new MQMapIcon();
        // Set the Icon's properties.
        chIcon.setImage(point.ImageUrl, 24, 24, true, false);
        // Associate the Icon to the Point.
        chPoint.setIcon(chIcon);
        // Add point to the collection.
        chMQPoiCollection.add(chPoint);
    }

    // Add or replace the Collection to the map object.
    renderPoiCollection(chMQPoiCollection);

    // Set the best fit for the map.
    // chMapInit.setBestFitRect(chMQPoiCollection.getBoundingRect());

}

function onStatePoiSuccess(result, context, methodName) {

    chMQPoiCollection = new MQPoiCollection();

    var resultLength = result.length;

    for (i = 0; i < resultLength; i++) {

        // Create a new object point based on the array item.
        var point = result[i];
        // Assign Latitude and Longitude to the MapQuest Point.
        var chPoint = new MQPoi(new MQLatLng(point.Latitude, point.Longitude));
        // Use a string builder to create the HTML for the point content.
        var sb = new Sys.StringBuilder();
        sb.append("<div><a href=\"javascript:void(0);\" onclick=\"ZoomToLatLng(");
        sb.append(point.Latitude);
        sb.append(",");
        sb.append(point.Longitude);        
        sb.append(", ");
        sb.append(chStateZoomLevel);
        sb.append(");\">View Markets in ");
        sb.append(point.Name);
        sb.append("</a>");
        
        // Assign the String Builder string to the point.
        chPoint.setInfoContentHTML(sb.toString());

        // Use string builder to create the HTML for the point Info Title.
        var sb2 = new Sys.StringBuilder();
        sb2.append("<strong>");
        sb2.append(point.Name);
        sb2.append(" (");
        sb2.append(point.CommunityCount);
        sb2.append(" communities)</strong>");
        // Assign the String Builder string for the point Info Title HTML.
        chPoint.setInfoTitleHTML(sb2.toString());
        // Set the unique key to the point.
        chPoint.setKey(point.StatePointID);
        // Create an Icon object.
        var chIcon = new MQMapIcon();
        // Set the Icon's properties.
        chIcon.setImage(point.ImageUrl, 24, 24, true, false);
        // Associate the Icon to the Point.
        chPoint.setIcon(chIcon);
        // Add point to the collection.
        chMQPoiCollection.add(chPoint);
    }

    // Add or replace the Collection to the map object.
    renderPoiCollection(chMQPoiCollection);

    // Set the best fit for the map.
    // chMapInit.setBestFitRect(chMQPoiCollection.getBoundingRect());

}

function onError(result, context, methodName) {
    var x = 1;
}

function page_unload(sender, e) {
    chMap.removeAllPois();
}

function renderPoiCollection(poiCollection) {
    chMap.replacePois(poiCollection);    
    //hideMapStatus();
}

function ZoomToLatLngMkt(latitude, longitude, marketName, marketID, zoomLevel) {
    // Use built-in ASP.NET AJAX JavaScriptSerializer to update market in search params.
    var result = Sys.Serialization.JavaScriptSerializer.deserialize(chSearchParams, true);
    result.MarketName = marketName;
    result.MarketID = marketID;
    chSearchParams = Sys.Serialization.JavaScriptSerializer.serialize(result);
    // Now zoom to Latitude Longitude now.
    ZoomToLatLng(latitude, longitude, zoomLevel)    
}

function ZoomToLatLng(latitude, longitude, zoomLevel) {    
    chMap.setCenter(new MQLatLng(latitude, longitude));
    chMap.setZoomLevel(zoomLevel);
} 

function zoomEnd(e) {
    if (e.prevZoom != e.zoom) {
        setMapWindowCoordinates();
        updateMapPoints();
    }
}

function mapMoveEnd(e) {
    if (updateMapFlag) {        
        //showMapStatus();
        setMapWindowCoordinates();
        updateMapPoints();
    }
}

function infoWindowOpen(e) {
    updateMapFlag = false;
}

function infoWindowClose(e) {
    updateMapFlag = true;
}

function rolloverWindowOpen(e) {
    updateMapFlag = false;
}
function rolloverWindowClose(e) {
    updateMapFlag = true;
}

function getMapSize() {
    var mapContainer = $get("mapWindow");
    var mapWidth = mapContainer.offsetWidth - 2;
    var mapHeight = mapContainer.offsetHeight - 2;
    return new MQSize(mapWidth, mapHeight);
}

function setMapWindowCoordinates() {

    var topLeftLL = chMap.pixToLL(new MQPoint(0, 0));    
    var mapSize = getMapSize();
    var bottomRightLL = chMap.pixToLL(new MQPoint(mapSize.getWidth(), mapSize.getHeight()));

    // Set the hidden form values to the current map window coordinates. 
    $get("chMinLat").value = topLeftLL.getLatitude();
    $get("chMinLng").value = topLeftLL.getLongitude();
    $get("chMaxLat").value = bottomRightLL.getLatitude();
    $get("chMaxLng").value = bottomRightLL.getLongitude();
    $get("chCenterLat").value = chMap.getCenter().getLatitude();
    $get("chCenterLng").value = chMap.getCenter().getLongitude();
    $get("chMarketZoomLevel").value = chMap.getZoomLevel();
    if (document.getElementById("MapDetail")){
        var el = document.getElementById("MapDetail");
        el.innerHTML = "Lat: " + chMap.getCenter().getLatitude() + "<BR>";
        el.innerHTML = el.innerHTML + "Long: " + chMap.getCenter().getLongitude() + "<BR>";
        el.innerHTML = el.innerHTML + "Zoom: " + chMap.getZoomLevel() + "<BR>";
    }
}

function updateMapPoints() {

    var minLat = $get("chMinLat").value;
    var minLng = $get("chMinLng").value;
    var maxLat = $get("chMaxLat").value;
    var maxLng = $get("chMaxLng").value;
    var maxResults = $get('chMaxPoints').value;

    var currentZoom = chMap.getZoomLevel();

    if (currentZoom < chStateZoomLevel) {
        getStatePoints(minLat, minLng, maxLat, maxLng, maxResults);
    }
    else {
//        if (currentZoom < chOrigZoomLevel) {
        if (currentZoom < chMarketZoomLevel) {
            getMarketPoints(minLat, minLng, maxLat, maxLng, maxResults);
        }
        else {
            getCommunityPoints(chSearchParams, minLat, minLng, maxLat, maxLng, maxResults);
        }
    }
}

// Redirect function
function Redirect(e, url, newWin) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
    if (url) {
        if (newWin) {
            window.open(url);
        }
        else {
            window.location.href = url;
        }
    }
}

function CenterPointOnMap(latitude, longitude) {
    chMap.setCenter(new MQLatLng(latitude, longitude));
}

if (typeof ('Sys') !== 'undefined') Sys.Application.notifyScriptLoaded();
