﻿var defaultZipToTestFor = "Enter ZIP Code";
var _pathToIcons = "/app_themes/default/images/mapIcons/";
var _iconTypeToDisplay = "";
var map;
var baseIcon;

var postPathForSearchResults = "/_procSearch/" +
    "_buildResultsForGMaps" +
    ".aspx";

$(document).ready(function() {



    defaultZipToTestFor = $("#txtPostal").val(defaultZipToTestFor);

});

function setSearch() {

    var zipCode = $("#txtPostal").val();
    var iType = $("#lstType option:selected").val();
    startSearch(zipCode, iType);

}

function startSearch(zipCode, iType) {

    var goodForStart = true;
    _iconTypeToDisplay = iType;

    if (zipCode == defaultZipToTestFor || zipCode == "") {
        goodForStart = false;
        $("#zipError").show();
    }
    else {
        goodForStart = true;
        $("#zipError").hide();
    }

    if (goodForStart) {

        $("#divStart").hide();
        $("#results").show();
        $("#divSearching").show();

        // make an ajax call
        $.ajax({
            type: "POST",
            url: postPathForSearchResults,
            data: "z=" +
            encodeURI(zipCode) +
            "&t=" + encodeURI(iType),

            success: function(msg) {

                $("#divSearchResults").html(msg);
                procResults();

            },

            error: function(msg) {


                alert("Search Error!!!");

            }
        });

    }

}

function procResults() {

    $("#divSearchTarget").hide();

    var resultsCount = parseInt($("#hdnCount").val());

    // do we have any results for them?
    if (resultsCount == "0") {

        // no results...sad boosh..
        $("#divNoResults").show();

    } else {



        $("#divNoResults").hide();
        $("#startBody").hide();

        // we have results so now is the time to get going with google maps

        // first we show the map
        $("#divSearchTarget").show();

        // we'll need the zipcode lat long...waiting for us in the new div from ajaz call

        var startLat = $("#hdnZipLat").val();
        var startLong = $("#hdnZipLong").val();

        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("divSearchTarget"));
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

            baseIcon = new GIcon();
            baseIcon.shadow = _pathToIcons + "shadow.png";
            baseIcon.iconSize = new GSize(40, 29);
            baseIcon.shadowSize = new GSize(37, 34);
            baseIcon.iconAnchor = new GPoint(40, 29);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);            

            geocoder = new GClientGeocoder();


            // now we loop all of our results


            $('div.mapListing').each(function() {

                var thisId = $(this).find("span.specID").text();

               // alert(thisId);


                $("#divTest").html($("#divTest").html() + "<br class='clear'/>" + thisId);

                var point;
                var currentPointGood = true;

                // is there a specific lat/long for this location?
                var getLat = $(this).find("span.specLat").text();
                var getLong = $(this).find("span.specLong").text();

                if (getLat != "" && getLat != "") {

               //     $("#divTest").html($("#divTest").html() + "<br class='clear'/> USING CACHED DATA!<hr class='clear'/>");

                    point = new GLatLng(getLat, getLong);
                    map.addOverlay(createMarker(point, $(this).html()));

                }
                else {

                    var thisAddress = $(this).find("div.displayAddress").text();
                    thisAddress = thisAddress.replace(/\n/g, "");
                    thisAddress = thisAddress.replace(/\t/g, "");
                    thisAddress = thisAddress.replace("  ", "");
                    thisAddress = thisAddress.replace("&nbsp;", "");

                    doGeoCodeForAddress(thisAddress, $(this).html(), thisId);

                  //  $("#divTest").html($("#divTest").html() + "<br class='clear'/> no saved data... searching for: " + thisAddress + "<hr class='clear'/>");

                }


            });
        }


    }


    $("#divSearching").hide();
}

function createMarker(point, info) {
    // Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.


    var icon = new GIcon(baseIcon);
    icon.image = _pathToIcons + "type" + _iconTypeToDisplay + ".png";

    var marker = new GMarker(point, icon);

    map.setCenter(point, 13);
    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(info);
    });


    return marker;
}

function doGeoCodeForAddress(address, thisHtml, thisListingId) {
    geocoder.getLatLng(
    address,
    function(newPoint) {
        if (newPoint) {

            map.addOverlay(createMarker(newPoint, thisHtml));

            // make an ajax call
            $.ajax({
                type: "POST",
                url: "/_updateLatLong/_update.aspx",
                data: "lat=" + escape(newPoint.lat()) +
                "&long=" + escape(newPoint.lng()) +
                "&id=" + escape(thisListingId)
            });

        }
    }
  );
}


