jQuery(document).ready(function () {
	if (GBrowserIsCompatible()) {
    	map = new GMap2(document.getElementById("map"));
    	map.addControl(new GLargeMapControl());
    	map.enableDoubleClickZoom();
    	map.setCenter(new GLatLng(43.67919, -79.34026), 1);
    	window.setTimeout(setupAllMarkers, 0);
    	
    	// collect the organization id to zoom to (if any)
    	var id = gup('id');
    	if (id > 0) {
    		panTo(id);
    	} else {
    		map.setCenter(new GLatLng(43.67919, -79.34026), 1);
    	}
  	}
});

var map = null;
var mgr = null;

function getMarker(id, lat, lng) {
	var latlng = new GLatLng(lat, lng);
  	var marker = new GMarker(latlng, { icon: getMarkerIcon() });
  	
  	GEvent.addListener(marker,"click", function() { panTo(id); });
	
	return marker;
}
        
function getMarkerIcon() {
	// organization
	var icon = new GIcon();
	icon.image = 'http://www.amdalliance.com/en/images/pin.png';
	icon.iconAnchor = new GPoint(16, 16);
    icon.infoWindowAnchor = new GPoint(16, 0);
    icon.iconSize = new GSize(32, 32);
    icon.shadow = 'http://www.amdalliance.com/en/images/pin-shadow.png';
    icon.shadowSize = new GSize(59, 32);
    
	return icon;
}

function setupAllMarkers() {
	mgr = new MarkerManager(map);
	//mgr.addMarkers(getMarkers3(), 1);
	mgr.addMarkers(getMarkers8(), 0);
	mgr.refresh();
}

function panTo(id) {
	new Ajax.Request('ajax/map/global_member_info.php?id='+ id, {
		method:'get',
 		onSuccess: function(transport) {
 			// collect JSON response, and update the map
			var theObject = transport.responseJSON;
			var latlng = new GLatLng(theObject['lat'], theObject['long']);
			map.openInfoWindowHtml(latlng, theObject['html']);
			map.setZoom(11);
		},
		onFailure: function() { alert('Something went wrong...') }
	 });
	 
	jQuery('html, body').animate({scrollTop: 350}, 'slow', 'swing');
}

function gup( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	
	if( results == null )
    	return "";
	else
		return results[1];
}