﻿//on dom ready
$(document).ready(function() {
    //loadMap();
    window.setTimeout(loadMap, 400);
});

function loadMap()
{
    if (typeof geomarkers != 'undefined')
    {
        $("#map").googleMap(40.765722, -73.977642, 15, {
                controls: ["GSmallZoomControl" ],
                markers: geomarkers,
                mapType: G_NORMAL_MAP
            });
    }
}



/////////////// googleMap class /////////////////

$.googleMap = {
	maps: {},
	marker: function(m, index) {
		if (!m) {
			return null;
		} else {

			var marker = new GMarker(new GLatLng(m.lat, m.lon));
			if (m.building) {
			    //attach click handlers to buildings on map
				GEvent.addListener(marker, "click", function() {
    				//call server side to get listing info tip
    				$.ajax({
    				    url: "helper.axd",
    				    data: {q:'listinginfo',buildingid:m.building,lid:m.lid},
    				    success: function(data) {
    				        //marker.openInfoWindowHtml(data);
    				    }
    				});
  				});
			}
			return marker;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

	// Default to New York
	if (lat == null) lat = 40.781061;
	if (lng == null) lng = -73.97438;
	if (!zoom) zoom = 15;

	// Sanitize options
	if (!options || typeof options != 'object')	options = {};
	options.mapOptions = options.mapOptions || {};
	options.markers = options.markers || [];
	options.controls = options.controls || {};
	options.mapType = options.mapType || G_NORMAL_MAP;

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
		// Create a map and a shortcut to it at the same time
	var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
		// Center and zoom the map
       	if (options.markers.length>0)
       	    map.setCenter(new GLatLng(options.markers[0].marker.lat, options.markers[0].marker.lon), zoom);
       	else
       	    map.setCenter(new GLatLng(lat, lng), zoom);
       	
       	map.setMapType(options.mapType);
       	// Add controls to our map
       	for (var i = 0; i < options.controls.length; i++) {
	       	var c = options.controls[i];
	       	eval("map.addControl(new " + c + "());");
       	}
       	// If we have markers, put them on the map
       	var marker = null;
       	var manager = new GMarkerManager(map);
       	var batch = [];
       	if (options.markers) {
       	    for (var i = 0; i < options.markers.length; i++) {
	       	    if (marker = $.googleMap.marker(options.markers[i].marker, i)) 
	       	    {
	       	        map.addOverlay(marker);
   	                GEvent.trigger(marker, "click");
	       	        //batch.push(marker);
                }
       	    }
       	}
       	//manager.addMarkers(batch, zoom);
       	//manager.refresh();
    });
};



