var map = null;
var geocoder;
var xml;
var markers;
var address;
var newIcon;

$(document).ready(function () {
  
    $(".opacify").fadeTo(1, 0.5);
    $(".opacify").hover(
        function () {
            $(this).fadeTo("fast", 1);
        },
        function () {
            $(this).fadeTo("normal", 0.5);
        }
    );
    $("#ucInterested").corner();
    
    if($("#map").get(0) != null) {    
        map = new GMap2($("#map").get(0));
    }
    if (map != null) {
        map.setCenter(new GLatLng(33.85, -118), 9);
        map.addControl(new GSmallZoomControl3D() );
        
        // Create new Icons.
        newIcon = MapIconMaker.createFlatIcon({label:"BMT", width: 32, height: 32, primaryColor: "#00ff00"});
    
        // Create new geocoding obj
        geocoder = new GClientGeocoder();
    
        // Download the data in data.xml and load it on the map
        GDownloadUrl("./data.xml", function(data) {
            xml = GXml.parse(data);
            markers = xml.documentElement.getElementsByTagName("marker");
            for(var i=0; i<markers.length; i++) {
                address = markers[i].getAttribute("address");
                geocoder.getLocations(address, addToMap);
            }
        });
    }
    
});

//function addAddressToMap(response) {
//  map.clearOverlays();
//  if (!response || response.Status.code != 200) {
//    alert("\"" + address + "\" not found");
//  } else {
//    place = response.Placemark[0];
//    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
//    marker = new GMarker(point, {icon:newIcon});
//    map.addOverlay(marker);
//    marker.openInfoWindowHtml(place.address + '<br>';
//  }
//}


// Adds point to the map
function addToMap(response) {
    //map.clearOverlays();
    if (!response || response.Status.code != 200) {
        //alert("\"" + address + "\" not found");
    } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
    marker = new GMarker(point, {icon:newIcon});
    map.addOverlay(marker);
//    GEvent.addListener(marker, "click", function() {
//        var myHtml = "<b>" + place.address + "</b><br/>";
//        map.openInfoWindowHtml(point, myHtml);
//    });
    }
}
