﻿





var markers = new Array();
var kmlFile;
var mapLayer = new MapLayer();
var map = null;
var searchPolygon = null;
var stateZoom = 6;
var cityZoom = 10;

function initialize()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("mapPlaceHolder"));
        map.setCenter(new GLatLng(38.548165,-96.28418), 3);
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
        
        var tempIcon = new GIcon(G_DEFAULT_ICON);
        tempIcon.image = imageLinkHelper;
        tempIcon.iconSize = new GSize(32, 32);
        imageLinkHelper = tempIcon;
        
        initializeMarkers();
        
        GEvent.addListener(map, "moveend", OnMapMoved); 
    }
}

function OnMapMoved()
{
    mapLayer.ShowOnMapAndPolygon(map.getCenter());
    
    var markers = mapLayer.GetMarkers();
    var tmpMCounter = 0;
    var currentBounds = map.getBounds();
    for(; tmpMCounter < markers.length; tmpMCounter++)
    {
        
        if(!currentBounds.containsLatLng(markers[tmpMCounter].GetGmMarker().getLatLng()))
        {
            markers[tmpMCounter].GetGmMarker().hide();
        }
        else
        {
            markers[tmpMCounter].GetGmMarker().show();
        }
    }
    
    ShowGrid(false, true);
}

function initializeMarkers()
{
    geoData.ImageLink = imageLinkHelper;
    var markerCounter = 0;
    for(; markerCounter < geoData.Places.length; markerCounter++)
    {
        geoData.Places[markerCounter].Image = imageLinkHelper;
        mapLayer.AddMarker(new MapMarker(geoData.Places[markerCounter]));
    }
	mapLayer.ShowOnMap(map);
	ShowGrid(false, false);
}

function ShowGrid(afterSearch, withGrid)
{
    var markers = mapLayer.GetMarkers();
    markers.sort(sortMarkerByDistance);
    var markerCounter = 0;
    var visibleMarkerCounter = 0;
    var valToAppend = "";
    //$("#mapSBContent").find("tr").remove();
    $("#mapSBContent").empty();
    var nearestFound = true;
    if(afterSearch)
    {
        var tmpCounter = 0;
        nearestFound = false;
        for(; tmpCounter < markers.length; tmpCounter++)
        {
            if(markers[tmpCounter].GetData().Distance > 100)
            {
                markers[tmpCounter].GetGmMarker().hide();
            }
            else
            {
                markers[tmpCounter].GetGmMarker().show();
                nearestFound = true;
            }
        }
        if(nearestFound)
        {
            map.setZoom(7);
        }
        else
        {
            map.setZoom(7);
        }
    }
    
    var firstGridRecord = false;
    for(; markerCounter < markers.length; markerCounter++)
    {
        var chkVakue = "checked=\"true\"";
        if(markers[markerCounter].GetGmMarker().isHidden())
        {
            chkVakue = "";
        }
        else
        {
            var topItemStyle = "border-top: solid 1px Blue;";
            if(!firstGridRecord)
            {
                firstGridRecord = true;
                topItemStyle = "";
            }
            visibleMarkerCounter++;
            valToAppend += "<tr id=\"trMarker_" + markerCounter + "\" style=\"display:block;margin-bottom:8px;" + topItemStyle + "\"><td>"
                        + "<input " + chkVakue + " type=\"checkbox\" onclick=\"chkChanged(this, " + markerCounter + ");\" />"
                        + "</td><td>"
                        + markers[markerCounter].GetData().Name + "<br />"
                        + "Distance: " + markers[markerCounter].GetData().Distance + " miles <br />"
                        + "Address: " + markers[markerCounter].GetData().Address + "<br />"
                        + "Phone: " + markers[markerCounter].GetData().Phone + "<br />"
                        + "<a style=\"color:#009440;\" href=" + markers[markerCounter].GetData().Link + ">View My Profile</a>"
                        + "</td></tr>";
        }
    }

    if((afterSearch || withGrid) && visibleMarkerCounter != 0)
    {
        $("#mapSBCount").find("tr").find("td").html(visibleMarkerCounter + " Licensed GreenPlumbers");
        $("#mapSBContent").html(valToAppend);
    }
    if(((afterSearch || withGrid) && visibleMarkerCounter == 0) || (!afterSearch && !withGrid))
    {
        $("#mapSBCount").find("tr").find("td").html(geoData.NotFoundHeaderMessage);
        $("#mapSBContent").html("<tr><td>" + geoData.NotFoundMessage + "</td></tr>");
    }
}

function sortMarkerByDistance(a, b)
{
    if(!a || a == null || a == 'undefined'
        || !b || b == null || b == 'undefined')
    {
        return 0;
    }
    //if(a.GetData().Distance == NaN)
    if(a.GetData().Distance != a.GetData().Distance)
    {
        return 1;
    }
    //if(b.GetData().Distance == NaN)
    if(b.GetData().Distance != b.GetData().Distance)
    {
        return -1;
    }
    return (a.GetData().Distance - b.GetData().Distance);
}

function Search()
{
    var searchText = document.getElementById("txtSearch");
    if(searchText == null || searchText == "undefined")
    {
        alert("Wrong search conditions");
        return;
    }
    if(searchText.value.length == 0)
    {
        map.setCenter(new GLatLng(38.548165,-96.28418), 3);
        mapLayer.ShowOnMapAndPolygon(map.getBounds(), new GLatLng(38.548165,-96.28418));
        ShowGrid(false);
    }
    else
    {
        geocoder = new GClientGeocoder();
        geocoder.getLocations(searchText.value, DisplaySearchResult);
    }
}

function DisplaySearchResult(gResponse)
{
    if(!gResponse || gResponse.Status.code !=200)
    {
        alert("Can't process your address. Please try another one. "+gResponse+"; "+gResponse.Status.code);
    }
    var rPlace = gResponse.Placemark[0];
    var rPoint = new GLatLng(rPlace.Point.coordinates[1],
                          rPlace.Point.coordinates[0]);
    map.setCenter(rPoint);
    mapLayer.ShowOnMapAndPolygon(rPoint);
    
    
    
   // if(rPlace.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea)
    //{
    //    map.setZoom(cityZoom);
    //}
   // else
   /// {
    //    map.setZoom(stateZoom);
    //}
    //mapLayer.ShowOnMapAndPolygon(map.getBounds(), rPoint);
    
    ShowGrid(true, true);
}

function chkChanged(chkBox, markerCounter)
{
    if(chkBox.checked)
    {
        mapLayer.GetMarkers()[markerCounter].GetGmMarker().show();
    }
    else
    {
        mapLayer.GetMarkers()[markerCounter].GetGmMarker().hide();
    }
}

function PrintGP()
{
    var markers = mapLayer.GetMarkers();
    var markerCounter = 0;
    var visibleMarkerCounter = 0;
    var printData = "<table><tr><th></th><th>Name</th><th>Address</th><th>Phone</th></tr>";
    for(; markerCounter < markers.length; markerCounter++)
    {
        if(!(markers[markerCounter].GetGmMarker().isHidden()))
        {
            visibleMarkerCounter++;
            printData += "<tr><td>" + visibleMarkerCounter + "</td>"
                      + "<td>" + markers[markerCounter].GetData().Name + "</td>"
                      + "<td>" + markers[markerCounter].GetData().Address + "</td>"
                      + "<td>" + markers[markerCounter].GetData().Phone + "</td>"
                      + "</tr>";
        }
    }
    printData += "</table>";
        
    if (window.print) {
        winId = window.open('','printForm');
        with (winId.document) {
            write('<body onLoad="window.focus();window.print()">' + printData + '<\/body>');
            close();
        }
    }
}