$(document).ready(function()
{
	//Get clients location by IP to center the map
	$.get('/services/get-country-from-ip-address',{},function(country)
	{
		var client_country_longitude 	= $('longitude',country).text();
		var client_country_latitude 	= $('latitude',country).text();
		var set_zoom_as 				= ($('name',country).text() == 'New Zealand') ? 6 : 4;
		
		//Show YES members on a map
		$.get('/services/geocoded-contacts-data',{},function(response)
		{
			$('#map').removeClass('loading');
			
			var map = new GMap2(document.getElementById("map"));
			
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(client_country_latitude, client_country_longitude), set_zoom_as);	
			
			function wheel_zoom(a) 
			{ 
				(a.detail || -a.wheelDelta) < 0 ? map.zoomIn() : map.zoomOut(); 
			}
			
			map.enableContinuousZoom();
			map.enableDoubleClickZoom();
			
			GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheel_zoom);
	 		GEvent.addDomListener(document.getElementById("map"), "mousewheel", wheel_zoom);
	 				
			$('contact',response).each(function(){
				var point 		= new GLatLng($('latitude',this).text(),$('longitude',this).text());
				var marker 		= new GMarker(point);
				var company 	= $('company',this).text();
				var contact_id 	= $('id',this).text();
				
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(company);
					select_contact(contact_id);
				});
				
				map.addOverlay(marker);
			});
		});
	});
	
	function select_contact(contact_id)
	{
		$("#contact_id option[@value='"+contact_id+"']").attr("selected","selected");
	}
});