﻿var map;
var startAddress = "";
var zoomLevel;
var newCenter;
var isInfoWindow;
var isAReset;
var recordCount;
var hideResultsTrigger = true;
var emailPanelId;

function googleStart(addr) {
    startAddress = addr;
    google.load("maps", "2", { "callback": getLocationInfo });
}

function setMap() {
    var mapDiv = document.getElementById("gMap");
    if (mapDiv != null) {
        map = new google.maps.Map2(mapDiv);
        map.setMapType(G_NORMAL_MAP);
        var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10));
        map.addControl(new GLargeMapControl3D(), topLeft);
        
        GEvent.addListener(map, "infowindowopen", function() { isInfoWindow = true; });
        GEvent.addListener(map, "infowindowclose", function() { isInfoWindow = false; });
        GEvent.addListener(map, "moveend", function(marker, point) { if (!isAReset && (zoomLevel != map.getZoom())) { if (point != null) { newCenter = point; } else { newCenter = map.getCenter(); } zoomLevel = map.getZoom(); resetMap(); } });

        resetMap();
    }
}

function getCityandStateListings(lat, lng) {
    getListingsFromClick(lat, lng);
}

function getCityandStateListingsByCity(city) {
    getListingsFromGroup(city);
}

function getDistinctGroups(lat, lng) {
    getListingGroups(lat, lng);
}

function resetMap() {
    if (!isInfoWindow) {
        showSpinner();
        map.clearOverlays();
        setHiddenLatLng(newCenter);
        isAReset = true;
        map.setCenter(newCenter, zoomLevel);
        isAReset = false;
        if (zoomLevel <= 10) {
            if (SkipGroupListings()) {
                zoomLevel = 11;
                getCityandStateListings(newCenter.y, newCenter.x);
            }
            else {
                getDistinctGroups(newCenter.y, newCenter.x);
            }  
        }
        else {
            getCityandStateListings(newCenter.y, newCenter.x);
        }
    }
}

function resetMapGroup(city) {
    if (!isInfoWindow) {
        showSpinner();
        map.clearOverlays();
        setHiddenLatLng(newCenter);
        isAReset = true;
        map.setCenter(newCenter, zoomLevel);
        isAReset = false;
        getCityandStateListingsByCity(city);
    }
}

function getLocationInfo() {
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(startAddress, function(point) {
        if (!point) {
            //alert("address not found");
        }
        else {
            isInfoWindow = false;
            zoomLevel = 9;
            newCenter = point;
            setHiddenLatLng(newCenter);
            setMap();
        }
    });
}



function createMarker(point, html) {    
    var newIcon = new GIcon();
    newIcon.image = "img/mapicon.png";
    newIcon.shadow = "img/mapiconshadow.png";
    newIcon.iconSize = new GSize(21, 31);
    newIcon.shadowSize = new GSize(31, 21);
    newIcon.iconAnchor = new GPoint(5, 20);
    newIcon.infoWindowAnchor = new GPoint(5, 2);
    newIcon.infoShadowAnchor = new GPoint(14, 25);

    var marker = new GMarker(point, newIcon);
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml(html);
    });

    addMarkerToMap(marker);
}

function createMarkerSmall(point, group) {
    var newIcon = new GIcon();
    newIcon.image = "img/mapicon_group.png";
    newIcon.shadow = "img/mapiconshadow.png";
    newIcon.iconSize = new GSize(23, 24);
    newIcon.shadowSize = new GSize(24, 23);
    newIcon.iconAnchor = new GPoint(5, 14);
    newIcon.infoWindowAnchor = new GPoint(5, 2);
    newIcon.infoShadowAnchor = new GPoint(14, 25);

    markerOptions = {icon:newIcon, title:group.GroupName}
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function(point) { showSpinner(); newCenter = point; zoomLevel = 11; resetMapGroup(group.CityName); });
    addMarkerToMap(marker);
}

function addMarkerToMap(marker) {
    map.addOverlay(marker);
}

function listingGet_CallBack(response) {
    if (!response) {
        alert("problem");
    }
    
    
    var _listings = response.Rows;
    var markerHtml;


    for (var i = 0; i < _listings.length; i++) {
        var _l = _listings[i];        
        markerHtml = "<div class=\"infoDiv\" align=\"left\">";
        markerHtml += "<div class=\"infoDivImage\"><img src=\"listingimage.aspx?key=" + _l.ListingId + "\" alt=\"\" /></div>";
        markerHtml += _l.ListPriceFormatted + "<br />";
        markerHtml += "<b>" + _l.ParsedAddress + "</b><br /><a href=\"javascript:showListingDetails(\'" + _l.ListingId + "');\" title=\"See Full Listing\">See More...</a></div>";
        if (_l.Latitude != 0 && _l.Longitude != 0) {
            var point = new GLatLng(_l.Latitude, _l.Longitude);
            createMarker(point, markerHtml);
        }
    }

    hideSpinner();

}

function hideSpinner() {
    var spinner = document.getElementById("gLoading");
    if (spinner != null) {
        new Effect.Fade(spinner, { duration: .2, from: 1, to: 0 });
    }
}

function showSpinner() {
    var spinner = document.getElementById("gLoading");
    if (spinner != null) {
        new Effect.Appear(spinner, { duration: .2, from: 0, to: 1 });
    }
}

function setText(element, text) {
    if (typeof element.textContent != 'undefined') {
        element.textContent = text;
    }
    else if (typeof element.innerText != 'undefined') {
        element.innerText = text;
    }
}

function failure_CallBack(response) {
    alert(response.get_message());
}

function showListingDetails(listingid) {
    Modalbox.show('framelanding.aspx?lid=' + listingid, { title: 'Full Listing Details', overlayOpacity: .65, width: 640, height: 580 });
}

function showEmailLogin() {
    Modalbox.show('emailLanding.aspx?f=login', { title: 'Email Login', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 450, height: 300 });
}

function showEmailLearn() {
    Modalbox.show('emailLanding.aspx?f=learn', { title: 'Email Accounts', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 450, height: 300 });
}

function saveListing(id) {
    Modalbox.show('emailLanding.aspx?f=sl&id=' + id, { title: 'Email Accounts', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 450, height: 300 });
}

function saveSearch() {
    Modalbox.show('emailLanding.aspx?f=ss', { title: 'Save Search', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 450, height: 300 });
}

function emailLogout() {
    Modalbox.show('emailLanding.aspx?f=lo', { title: 'Log Out', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 450, height: 300 });
}

function showAgentEmail(id) {
    Modalbox.show('profilelanding.aspx?f=email&id=' + id, { title: 'Agent Email', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 620, height: 580 });
}

function showAgentProfile(id) {
    Modalbox.show('profilelanding.aspx?f=pro&id=' + id, { title: 'Agent Profile', slideDownDuration: .5, slideUpDuration: .5, overlayOpacity: .65, width: 620, height: 580 });
}



function hideMB() {
    Modalbox.hide();
    setTimeout("refreshPage()", 1450);
}

function hideMBAfterSave() {
    Modalbox.hide();
    setTimeout("refreshPage()", 1450);
    
}

function refreshPage() {
    __doPostBack(emailPanelId, '');
}



/*
function callGetListings(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    }
    else {
        var place = response.Placemark[0];        
        var city;
        var state;
        if (parseInt(place.AddressDetails.Accuracy) >= 4) {
            state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != null) {
                city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
            }
            else {
                if (place.AddressDetails.Country.AdministrativeArea.Locality != null) {
                    city = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                }
            }
        }
        else {
            state = "";
            city = "";
        }
        getListingsFromClick(city, state);
    }
    
}

*/