var geo = new GClientGeocoder();
var reasons=[];
reasons[G_GEO_SUCCESS]            = "Success";
reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address";
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
reasons[G_GEO_BAD_KEY]            = "Bad API Key";
reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries";
reasons[G_GEO_SERVER_ERROR]       = "Server error";

var map = null;

// Standard jQuery header
(function($) {

$(document).ready(function() {  						 
	  
	map = new GMap2(document.getElementById('map'));
	var NZ = new GLatLng(-40.913513,174.902344);
	map.setCenter(NZ,4);
	
	$("#Form_RegionForm_Region").change(function()
	{
		
		map.clearOverlays();
		$region = $(this).val();
		
		if ($region == 0) {
			
			$location = "New Zealand";
			$zoom = 4;
			
		} else {
			
			$location = $region+",New Zealand";
			$zoom = 7;
			
		}
		
		geo.getLocations($location, function (result){
				
			if (result.Status.code == G_GEO_SUCCESS) {
				var p = result.Placemark[0].Point.coordinates;
				
				var lat=p[1];
				var lng=p[0];
				
				var inNZ = new GLatLng(lat,lng);
				map.setCenter(inNZ,$zoom);
				var counter = 0;
				
				$.ajax({
				  url: "HomePage_Controller/getSurveyResultsCSV?Region="+$region,
				  cache: false,
				  success: function(html){
					
					lines = html.split("\r\n");
					
					for (var i = 0; i < lines.length; i++) {
						
						if (surveyResult = lines[i].split(",")) {
							
							if (surveyResult.length >= 4) {
								
								function addToMap(result) {
									
									surveyResult = lines[counter].split(",")
									passData.address = surveyResult[0];
									passData.city = surveyResult[1];
									passData.name = surveyResult[2];
									passData.onething = surveyResult[3];
									passData.icon = surveyResult[4];
									
									counter++;
									
									if (result.Status.code == G_GEO_SUCCESS) {
										var p = result.Placemark[0].Point.coordinates;
										
										var lat=p[1];
										var lng=p[0];
										
										var point = new GLatLng(lat,lng);
										
										//marker = new GMarker(point);
										var label = new ELabel(point, 
										"<div class='inside'><img src='themes/savethechildren/images/"+passData.icon+"'><p>"+passData.name+", "+passData.city+"<br/>"+passData.onething+"</p></div>",
										"map-overlay",
										null,null,true);
										
										map.addOverlay(label);
										
									} else {
										var reason="Code "+result.Status.code;
										if (reasons[result.Status.code]) {
											reason = reasons[result.Status.code]
										}
										geocode = false;
									} //if
										
								}
								
								function callGeocoder(data) {
								 
								  geo.getLocations(data.address, addToMap);
								
								}
						
								passData = new Object();
								passData.address = surveyResult[0];
								callGeocoder(passData);
								
							}
							
						}
						
					  } //for
					  
					} //success
				  
				});
			
			} else {
				
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				}
				geocode = false;
			}
			
		});
		
	
	});
	
});
 
// Standard jQuery footer
})(jQuery);
