﻿
// Get corners coordinates
function GetCoord(bHotel) {
    var llTop = map.PixelToLatLong(new VEPixel(0, 0)).toString()

    var sTop = llTop.toString();
    sTop = sTop.toString().replace(" ", "");
    sTop = sTop.toString().replace('"', '');
    var TopArray = new Array();
    TopArray = sTop.split(',');

    var llBottom = map.PixelToLatLong(new VEPixel(map.GetTop(), map.GetLeft())).toString()

    var sBottom = llBottom.toString();
    sBottom = sBottom.toString().replace(" ", "");
    sBottom = sBottom.toString().replace('"', '');
    var BottomArray = new Array();
    BottomArray = sBottom.split(',');
     
    //try
    //{   

    MinicardsNY.GetCoordinates.AddLocation(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, SucceededCallback);
    MinicardsNY.GetCoordinates.LoadCoordinates(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, SucceededCallback);

    if (bHotel == true) {
        MinicardsNY.GetCoordinates.AddLocation2(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], false, SucceededCallback);
        MinicardsNY.GetCoordinates.LoadCoordinates2(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], false, SucceededCallback);
    }
    else {
        MinicardsNY.GetCoordinates.AddLocation2(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], true, SucceededCallback);
        MinicardsNY.GetCoordinates.LoadCoordinates2(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], true, SucceededCallback);
    }

    //}
    //catch(e)
    //{
    //    alert(e.message);
    //}
}

function GetCoordFiltered(bHotel, bSetBounds, bRefresh) {

    var campaignId = document.getElementById(getClientID("hfCampaignId"));
    var categoryId = document.getElementById(getClientID("hfCategoryId"));
    var nicheOptionId = document.getElementById(getClientID("hfNicheOptionId"));
    var classId = document.getElementById(getClientID("hfClassId"));
    var brandId = document.getElementById(getClientID("hfBrandId"));
    var categoryTypeId = document.getElementById(getClientID("hfCategoryTypeId"));
    var searchQuery = document.getElementById(getClientID("txtSearch"));

    var currentPage = document.getElementById(getClientID("hfCurrentPage"));
    var hfCentLat = document.getElementById(getClientID("hfCentLat"));
    var hfCentLong = document.getElementById(getClientID("hfCentLong"));
    var hfZoomLevel = document.getElementById(getClientID("hfZoomLevel"));

    if (bRefresh == true) map.Clear();

    if (bSetBounds == true) {
        //if we didn't have any parameters we focus the map on all results
        //Note: if we remove "window.location.hash" statement the map will bound on search results, but will get a bug on first page change
        if ((hfCentLat.value == 0 && hfCentLong.value == 0) || (window.location.search == "" && window.location.hash == "")) {
            if (bHotel == true) {
                MinicardsNY.GetCoordinates.LoadBounds(0, 0, 0, 0, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", currentPage.value, searchQuery.value, SucceededCallback);
            }
            else {
                MinicardsNY.GetCoordinates.LoadBounds(0, 0, 0, 0, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
            }
        }
        else {
            //if we have a fix location we display only that area
            if (hfCentLat.value != 0 && hfCentLong.value != 0 && hfZoomLevel.value != 0) {

                var zoom = parseInt(hfZoomLevel.value);
                var topLat = parseFloat(hfCentLat.value) + (Math.pow(2, (19 - zoom)) * (0.00032));
                var topLong = parseFloat(hfCentLong.value) + (Math.pow(2, (19 - zoom)) * (-0.00034));
                var bottomLat = parseFloat(hfCentLat.value) + (Math.pow(2, (19 - zoom)) * (-0.00030));
                var bottomLong = parseFloat(hfCentLong.value) + (Math.pow(2, (19 - zoom)) * (0.00032));

                if (bHotel == true) {
                    MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", currentPage.value, searchQuery.value, SucceededCallback);
                }
                else {
                    MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, false, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
                }
            }
        }
    }

    if (bSetBounds != true) {

        //var point = new VELatLong(0, 0)
        //point.Latitude, point.Longitude
        var centerPoint = map.GetCenter();
        var zoom = map.GetZoomLevel();

        var topLat = parseFloat(centerPoint.Latitude) + (Math.pow(2, (19 - zoom)) * (0.00032));
        var topLong = parseFloat(centerPoint.Longitude) + (Math.pow(2, (19 - zoom)) * (-0.00034));
        var bottomLat = parseFloat(centerPoint.Latitude) + (Math.pow(2, (19 - zoom)) * (-0.00030));
        var bottomLong = parseFloat(centerPoint.Longitude) + (Math.pow(2, (19 - zoom)) * (0.00032));

        //set coordinates to old center hidden fields
        var hfOldCentLat = document.getElementById(getClientID("hfOldCentLat"));
        hfOldCentLat.value = document.getElementById(getClientID("hfCentLat")).value;
        var hfOldCentLong = document.getElementById(getClientID("hfOldCentLong"));
        hfOldCentLong.value = document.getElementById(getClientID("hfCentLong")).value;
        var hfOldZoomLevel = document.getElementById(getClientID("hfOldZoomLevel"));
        hfOldZoomLevel.value = document.getElementById(getClientID("hfZoomLevel")).value;

        //var centLat = document.getElementById(getClientID("hfCentLat"));
        //var centLong = document.getElementById(getClientID("hfCentLong"));
        //var zoomLevel = document.getElementById(getClientID("hfZoomLevel"));
        //centLat.setAttribute("value", centerPoint.Latitude);
        //centLong.setAttribute("value", centerPoint.Longitude);
        //zoomLevel.setAttribute("value", zoom);

        //we set center values to hidden fields
        hfCentLat.value = centerPoint.Latitude;
        hfCentLong.value = centerPoint.Longitude;
        //set the current value of zoom
        hfZoomLevel.value = zoom;

        if (bHotel == true) {
            MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", currentPage.value, searchQuery.value, SucceededCallback);
            //MinicardsNY.GetCoordinates.AddLocationFiltered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", currentPage.value, searchQuery.value, SucceededCallback);

            //MinicardsNY.GetCoordinates.LoadCoordinates2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", 0, searchQuery.value, SucceededCallback);
            //MinicardsNY.GetCoordinates.AddLocation2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", currentPage.value, searchQuery.value, map.GetZoomLevel(), SucceededCallback);
        }
        else {
            MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, false, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
            //MinicardsNY.GetCoordinates.AddLocationFiltered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, false, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);

            //MinicardsNY.GetCoordinates.LoadCoordinates2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), 0, searchQuery.value, SucceededCallback);
            //MinicardsNY.GetCoordinates.AddLocation2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, true, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), currentPage.value, searchQuery.value, map.GetZoomLevel(), SucceededCallback);
        }
    }
}

function GetSearchCoordFiltered(bHotel, bSetBounds, bRefresh, iCurrentPage) {

    var campaignId = document.getElementById(getClientID("hfCampaignId"));
    var categoryId = document.getElementById(getClientID("hfCategoryId"));
    var nicheOptionId = document.getElementById(getClientID("hfNicheOptionId"));
    var classId = document.getElementById(getClientID("hfClassId"));
    var brandId = document.getElementById(getClientID("hfBrandId"));
    var categoryTypeId = document.getElementById(getClientID("hfCategoryTypeId"));
    var resultType = document.getElementById(getClientID("hfType"));
    var searchQuery = document.getElementById(getClientID("hfSearchQuery"));

    var currentPage = document.getElementById(getClientID("hfCurrentPage"));
    var hfCentLat = document.getElementById(getClientID("hfCentLat"));
    var hfCentLong = document.getElementById(getClientID("hfCentLong"));
    var hfZoomLevel = document.getElementById(getClientID("hfZoomLevel"));

    if (bRefresh == true) map.Clear();

    if (bSetBounds == true) {
        //if we didn't have any parameters we focus the map on all results
        if ((hfCentLat.value == 0 && hfCentLong.value == 0) || window.location.search == "") {
            MinicardsNY.GetCoordinates.LoadSearchBounds(0, 0, 0, 0, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), categoryTypeId.getAttribute("value"), resultType.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
        }
        else {
            //if we have a fix location we display only that area
            if (hfCentLat.value != 0 && hfCentLong.value != 0 && hfZoomLevel.value != 0) {

                var zoom = parseInt(hfZoomLevel.value);
                var topLat = parseFloat(hfCentLat.value) + (Math.pow(2, (19 - zoom)) * (0.00032));
                var topLong = parseFloat(hfCentLong.value) + (Math.pow(2, (19 - zoom)) * (-0.00034));
                var bottomLat = parseFloat(hfCentLat.value) + (Math.pow(2, (19 - zoom)) * (-0.00030));
                var bottomLong = parseFloat(hfCentLong.value) + (Math.pow(2, (19 - zoom)) * (0.00032));

                MinicardsNY.GetCoordinates.LoadSearchCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), categoryTypeId.getAttribute("value"), resultType.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
            }
        }
    }

    if (bSetBounds != true) {

        //var point = new VELatLong(0, 0)
        //point.Latitude, point.Longitude
        var centerPoint = map.GetCenter();
        var zoom = map.GetZoomLevel();

        var topLat = parseFloat(centerPoint.Latitude) + (Math.pow(2, (19 - zoom)) * (0.00032));
        var topLong = parseFloat(centerPoint.Longitude) + (Math.pow(2, (19 - zoom)) * (-0.00034));
        var bottomLat = parseFloat(centerPoint.Latitude) + (Math.pow(2, (19 - zoom)) * (-0.00030));
        var bottomLong = parseFloat(centerPoint.Longitude) + (Math.pow(2, (19 - zoom)) * (0.00032));

        //set coordinates to old center hidden fields
        var hfOldCentLat = document.getElementById(getClientID("hfOldCentLat"));
        hfOldCentLat.value = document.getElementById(getClientID("hfCentLat")).value;
        var hfOldCentLong = document.getElementById(getClientID("hfOldCentLong"));
        hfOldCentLong.value = document.getElementById(getClientID("hfCentLong")).value;
        var hfOldZoomLevel = document.getElementById(getClientID("hfOldZoomLevel"));
        hfOldZoomLevel.value = document.getElementById(getClientID("hfZoomLevel")).value;

        //we set center values to hidden fields
        hfCentLat.value = centerPoint.Latitude;
        hfCentLong.value = centerPoint.Longitude;
        //set the current value of zoom
        hfZoomLevel.value = zoom;

        MinicardsNY.GetCoordinates.LoadSearchCoordinatesFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), categoryTypeId.getAttribute("value"), resultType.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
        //MinicardsNY.GetCoordinates.AddSearchLocationFiltered(topLat, topLong, bottomLat, bottomLong, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), categoryTypeId.getAttribute("value"), resultType.getAttribute("value"), currentPage.value, searchQuery.value, SucceededCallback);
    }
}

function GetCoordSimple(bHotel, bSetBounds, bRefresh, iCurrentPage) {
    
    var campaignId = document.getElementById(getClientID("hfCampaignId"));
    var categoryId = document.getElementById(getClientID("hfCategoryId"));
    var nicheOptionId = document.getElementById(getClientID("hfNicheOptionId"));
    var classId = document.getElementById(getClientID("hfClassId"));
    var brandId = document.getElementById(getClientID("hfBrandId"));
    var categoryTypeId = document.getElementById(getClientID("hfCategoryTypeId"));

    if (bRefresh == true) map.Clear();

    if (bSetBounds == true) {
        if (bHotel == true) {
            //MinicardsNY.GetCoordinates.LoadBounds(0, 0, 0, 0, bHotel, bSetBounds, true, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", iCurrentPage, "", SucceededCallback);
            MinicardsNY.GetCoordinates.LoadCoordinatesBounded(0, 0, 0, 0, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", iCurrentPage, "", SucceededCallback);
        }
        else {
            //MinicardsNY.GetCoordinates.LoadBounds(0, 0, 0, 0, bHotel, bSetBounds, true, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), iCurrentPage, "", SucceededCallback);
            MinicardsNY.GetCoordinates.LoadCoordinatesBounded(0, 0, 0, 0, bHotel, bSetBounds, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), iCurrentPage, "", SucceededCallback);
        }
    }

    var llTop = map.PixelToLatLong(new VEPixel(0, 0)).toString()

    var sTop = llTop.toString();
    sTop = sTop.toString().replace(" ", "");
    sTop = sTop.toString().replace('"', '');
    var TopArray = new Array();
    TopArray = sTop.split(',');

    //width:962px; height:600px;
    var llBottom = map.PixelToLatLong(new VEPixel(960, 600)).toString()
    //var llBottom = map.PixelToLatLong(new VEPixel(map.GetTop(), map.GetLeft())).toString()

    var sBottom = llBottom.toString();
    sBottom = sBottom.toString().replace(" ", "");
    sBottom = sBottom.toString().replace('"', '');
    var BottomArray = new Array();
    BottomArray = sBottom.split(',');

    if (bSetBounds != true){
        if (bHotel == true) {
            //MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", iCurrentPage, 0, "", SucceededCallback);
            //MinicardsNY.GetCoordinates.LoadCoordinates2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), classId.getAttribute("value"), brandId.getAttribute("value"), "0", 0, 0, "", SucceededCallback);
        }
        else {
            //MinicardsNY.GetCoordinates.LoadCoordinatesFiltered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], bHotel, false, true, false, false, campaignId.getAttribute("value"), "0", nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), iCurrentPage, 0, "", SucceededCallback);
            //MinicardsNY.GetCoordinates.LoadCoordinates2Filtered(TopArray[0], TopArray[1], BottomArray[0], BottomArray[1], !bHotel, false, true, false, false, campaignId.getAttribute("value"), categoryId.getAttribute("value"), nicheOptionId.getAttribute("value"), "0", "0", categoryTypeId.getAttribute("value"), 0, 0, "", SucceededCallback);
        }
    }
}






// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, userContext, methodName) {
    var message;
    switch (methodName) {
        case ("LoadCoordinates"):
            {
                var i = 0;
                var mapCoordinates = new Array();

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        var name = mapData[2];
                        var desc = mapData[3];
                        var icon_number = mapData[4];
                        if (lat != 0 && lng != 0) {
                            loaded = true;
                            AddPushpin(lat, lng, name, desc);
                        }
                    }
                    catch (e) {
                        var s = e.message;
                    } //alert(e.message);}
                    i++;
                }

                break;
            }
        case ("LoadCoordinates2"):
            {
                var i = 0;
                var mapCoordinates = new Array();

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        var name = mapData[2];
                        var desc = mapData[3];
                        var icon_number = mapData[4];
                        if (lat != 0 && lng != 0) {
                            loaded = true;
                            AddPushpin2(lat, lng, name, desc);
                        }
                    }
                    catch (e) {
                        var s = e.message;
                    } //alert(e.message);}
                    i++;
                }
                break;
            }
        case ("AddLocation"): case ("AddLocationFiltered"): case ("AddSearchLocationFiltered"):
            {
                var RsltElem = document.getElementById("ResultId");
                RsltElem.innerHTML = result;
                break;
            }
        case ("AddLocation2"): case ("AddLocation2Filtered"):
            {
                var RsltElem = document.getElementById("ResultId2");
                RsltElem.innerHTML = result;
                //test();
                break;
            }
        case ("LoadBounds"): case ("LoadSearchBounds"):
            {
                var i = 0;
                var mapCoordinates = new Array();
                var points = new Array();

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        points[i] = new VELatLong(lat, lng);
                    }
                    catch (e) {
                        var s = e.message;
                    }
                    i++;
                }

                //set rectangular bound for the points
                map.SetMapView(points);

                //if we have less than 2 pins, we set a standard zoom
                if (map.GetZoomLevel() > 14) {
                    map.SetZoomLevel(14);
                    break;
                }

                break;
            }
        case ("LoadCoordinatesBounded"):
            {
                var i = 0;
                var mapCoordinates = new Array();
                var points = new Array();

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        var name = mapData[2];
                        var desc = mapData[3];
                        var is_hotel = mapData[4];
                        var image_url = mapData[5];
                        var icon_number = mapData[6];
                        if (lat != 0 && lng != 0) {
                            loaded = true;
                            //AddHotelPushpin(lat, lng, name, desc);
                            points[i] = new VELatLong(lat, lng);
                            AddCustomPushpin(lat, lng, name, desc, is_hotel, image_url, icon_number);
                        }
                    }
                    catch (e) {
                        var s = e.message;
                    } //alert(e.message);}
                    i++;
                }

                //set rectangular bound for the points
                //map.SetMapView(points);

                map.SetZoomLevel(11);

                break;
            }
        case ("LoadCoordinatesFiltered"):case ("LoadSearchCoordinatesFiltered"):
            {
                var i = 0;
                var mapCoordinates = new Array();
                var points = new Array();

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        var name = mapData[2];
                        var desc = mapData[3];
                        var is_hotel = mapData[4];
                        var image_url = mapData[5];
                        var icon_number = mapData[6];
                        if (lat != 0 && lng != 0) {
                            loaded = true;
                            //AddHotelPushpin(lat, lng, name, desc);
                            points[i] = new VELatLong(lat, lng);
                            AddCustomPushpin(lat, lng, name, desc, is_hotel, image_url, icon_number);
                        }
                    }
                    catch (e) {
                        var s = e.message;
                    } //alert(e.message);}
                    i++;
                }

                //if it's the a post back
                //if (document.getElementById(getClientID("hfIsPostback")).value == true) { }

                //force a postback for the updatepanel with the results
                postCoordinates();

                //set rectangular bound for the points
                //if (methodName == "LoadCoordinatesBounded") map.SetMapView(points);

                break;
            }
        case ("LoadCoordinates2Filtered"):
            {
                var i = 0;
                var mapCoordinates = new Array();
                var points = new Array();

                var origin = map.GetCenter();
                var radius_zoom = document.getElementById("ctl00_ContentPlaceHolder1_hfRadiusZoom").value;
                var radius = Math.pow(2, (19 - map.GetZoomLevel())) * (0.02 * radius_zoom);

                for (var item in result) {
                    mapCoordinates[i] = result[item];

                    try {
                        var mapData = new Array();
                        mapData = mapCoordinates[i].toString().split('#,#');

                        var lat = mapData[0];
                        var lng = mapData[1];
                        var name = mapData[2];
                        var desc = mapData[3];
                        var is_hotel = mapData[4];
                        var image_url = mapData[5];
                        var icon_number = mapData[6];
                        if (lat != 0 && lng != 0) {
                            loaded = true;
                            points[i] = new VELatLong(lat, lng);
                            AddCustomPushpin(lat, lng, name, desc, is_hotel, image_url, icon_number);

                            //var d = distance(origin, points[i]);
                            //if (Math.abs(d) <= Math.abs(radius)) {
                            //    AddCustomPushpin(lat, lng, name, desc, is_hotel, image_url, icon_number);
                            //}
                        }
                    }
                    catch (e) {
                        var s = e.message;
                    } //alert(e.message);}
                    i++;
                }

                //set rectangular bound for the points
                //if (methodName == "LoadCoordinatesBounded") map.SetMapView(points);

                drawCircle(origin, radius);

                break;
            }
    }

}


function drawCircle(origin, radius) {
    var earthRadius = 6371;

    //latitude in radians
    var lat = (origin.Latitude * Math.PI) / 180;

    //longitude in radians
    var lon = (origin.Longitude * Math.PI) / 180;
    //angular distance covered on earth's surface
    var d = parseFloat(radius) / earthRadius;
    var points = new Array();
    for (i = 0; i <= 360; i++) {
        var point = new VELatLong(0, 0)
        var bearing = i * Math.PI / 180; //rad
        point.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
        point.Longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(point.Latitude))) * 180) / Math.PI;
        point.Latitude = (point.Latitude * 180) / Math.PI;
        points.push(point);
    }

    var circle = new VEShape(VEShapeType.Polyline, points);
    circle.HideIcon();
    map.AddShape(circle);
    //map.SetMapView(points);
}

//function distance(latlong, latlong2) {
//    var lat1 = latlong.Latitude;
//    var lon1 = latlong.Longitude;
//    var lat2 = latlong2.Latitude;
//    var lon2 = latlong2.Longitude;
//    var earthRadius = 6371; //appxoximate radius in miles

//    var factor = Math.PI / 180;
//    var dLat = (lat2 - lat1) * factor;
//    var dLon = (lon2 - lon1) * factor;
//    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * factor) * Math.cos(lat2 * factor) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
//    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
//    var d = earthRadius * c;
//    return d;
//}

function AddCustomPushpin(latitude, longitude, name, description, is_hotel, image_url, icon_number) {
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));
    shape.SetTitle(name);
    shape.SetDescription(description);
    if (image_url != "noimage")
        shape.SetPhotoURL(image_url);
    //shape.SetPhotoURL("http://www.minicardsnyc.com/minicardimages/File_4684895c-3812-4b2f-ab5f-939025cf3257.jpg");

    var x = new VECustomIconSpecification();
    if (icon_number == null || icon_number == 0) {
        x.Image = "/images/map_icons/mapicons";
        if (is_hotel == 1) x.Image += "_hotels.png";
        else x.Image += "_attractions.png";
    }
    else {
        x.Image = "/images/map_icons/mapicons";
        if (is_hotel == 1) x.Image += "_hotels_" + icon_number + ".png";
        else x.Image += "_attractions_" + icon_number + ".png";
    }
    x.ImageOffset = new VEPixel(12, 12);
    x.TextContent = "";
    x.TextSize = 12;
    x.TextBold = true;
    x.ForeColor = new VEColor(255, 0, 0, 1);
    x.TextOffset = new VEPixel(11, -5);

    shape.SetCustomIcon(x);

    map.AddShape(shape);
}

function AddPushpin(latitude, longitude, name, description) {
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));
    shape.SetTitle(name);
    shape.SetDescription(description);
    map.AddShape(shape);
}

function AddPushpin2(latitude, longitude, name, description) {
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));
    shape.SetTitle(name);
    shape.SetDescription(description);

    var x = new VECustomIconSpecification();
    x.Image = "./images/marker.png";
    x.ImageOffset = new VEPixel(12, 12);
    x.TextContent = "";
    x.TextSize = 12;
    x.TextBold = true;
    x.ForeColor = new VEColor(255, 0, 0, 1);
    x.TextOffset = new VEPixel(11, -5);

    shape.SetCustomIcon(x);

    map.AddShape(shape);
}


