﻿var map = null;
var geocoder = null;
var baseIcon = getBaseIcon();
var markerIndex = 0;
var gmarkers = [];
var side_bar_html = "";

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        geocoder = new GClientGeocoder();
        map.setUIToDefault();
        showAddress("12206 1/2 Walraven Dr., Huffman, Texas 77336", "Corporate Office");
        addPointer("Forest Colony, Porter, TX 77365", "Forest Colony", "Porter, Texas");
        addPointer("Bradbury Forest Dr, Spring, TX 77373", "Bradbury Forest", "Spring Texas");
        addPointer("12655 Northpoint Blvd, Tomball, TX‎ 77375", "Villages at Northpoint", "Tomball, Texas");
        addPointer("12023 Via Siena Ln, Cypress, TX 77429", "Tuscany", "Cypress, Texas");
        addPointer("McCall Sound Blvd, Magnolia, TX 77355", "McCall Sound", "Magnolia, Texas");
        addPointer("Commons Breeze Dr, Huffman, TX 77336", "The Commons at Lake Houston", "Huffman, Texas");
        addPointer("2945 Auburn Woods Dr, Pearland, TX 77581", "Villages at Mary's Creek", "Pearland, Texas");
        addPointer("Baytown, TX 77520", "Lakewood Estates", "Baytown, Texas");
    }
}
function showAddress(address, subdivName) {
    if (geocoder) {
        geocoder.getLatLng(address,
                        function(point) {
                            if (!point) {
                                alert(address + " not found");
                            } else {
                                map.setCenter(point, 8);
                                var marker = new GMarker(point);
                                GEvent.addListener(marker, "click", function() {
                                    marker.openInfoWindowHtml("<b>Corporate Office</b>");
                                });
                                //map.addOverlay(createMarker(point, markerIndex++, address, subdivName));
                                map.addOverlay(marker);
                            }
                        }
                    );
    }
}
function addPointer(address, subdivName, showText) {
    if (geocoder) {
        geocoder.getLatLng(address,
                        function(point) {
                            if (!point) {
                                alert(address + " not found");
                            } else {
                                //var marker = new GMarker(point);
                                map.addOverlay(createMarker(point, markerIndex++, showText, subdivName));
                            }
                        }
                    );
    }
}
function getBaseIcon() {
    var returnIcon = new GIcon(G_DEFAULT_ICON);
    returnIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    returnIcon.iconSize = new GSize(20, 34);
    returnIcon.shadowSize = new GSize(37, 34);
    returnIcon.iconAnchor = new GPoint(9, 34);
    returnIcon.infoWindowAnchor = new GPoint(9, 2);
    return returnIcon;
}

function createMarker(point, index, name, subDiv) {
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<b>" + subDiv + "</b><br/>");
    });
    gmarkers.push(marker);
    document.getElementById("side_bar").innerHTML += "<p><strong>" + letter + ".</strong>&nbsp;&nbsp;<a href=\"javascript:myclick(" + (gmarkers.length - 1) + ")\">" + subDiv + "</a><br />" + name + "</p>";
    return marker;
}

function myclick(i) {
    GEvent.trigger(gmarkers[i], "click");
}
initialize();
