$(document).ready(function(){
	
	
	
	$("#checkcoords").click(function(){		
		address = $("#BranchCheckStreetname").val()+' '+$("#BranchCheckNumber").val()+', '+$("#BranchCheckCity").val()+', Nederland';
		
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status){
			if(status == google.maps.GeocoderStatus.OK){
				//$("#BranchLatlon").val(results[0].geometry.location);
				if(($("#BranchCheckStreetname").val()!='') && ($("#BranchCheckCity").val()!='')){
					$("#checkcoords_result").html('<p><strong>Adres gevonden:</strong> '+results[0].formatted_address+'</p>');
					$("#DealerLat").val(results[0].geometry.location.lat());
					$("#DealerLng").val(results[0].geometry.location.lng());

					$("#savecoords_form").slideDown();
				}
				else{
					$("#checkcoords_result").html('<p><strong>U dient minimaal een straatnaam en plaats op te geven.</strong></p>');
				}
			}
			else{
				$("#checkcoords_result").html('<p><strong>Adres niet gevonden.</strong> Controleer de straatnaam en wijzig eventueel het nummer als het gaat om een reeks.<br />Bijvoorbeeld Teststraat 15-17, wijzigen naar Teststraat 15. Nog geen correct resultaat? Laat het huisnummer leeg.</p>');
			}
		});
		
		return false;
	});
	
	
	
	
	//zipcode search
	$("#dealers_search_zipcode #SearchAddForm .submit input").click(function(){
		
		geocoder = new google.maps.Geocoder();
		var address = $("#SearchZipcode").val();
		radius = $('#SearchRadius').val();

		geocoder.geocode( { 'address': address}, function(results, status){
			if(status == google.maps.GeocoderStatus.OK){
				latlon = results[0].geometry.location;
				
				dataString = 'data[Coordinates][lat]='+results[0].geometry.location.lat()+'&data[Coordinates][lng]='+results[0].geometry.location.lng()+'&data[Coordinates][radius]='+radius;

				action = webroot+'dealers/findinradius';

				//alert(latlon.toSource());
				//console.debug(latlon);
				
				$.ajax({
					type: "POST",
					url: action,
					data: dataString,
					success: function(msg) {
						$("#markerselector").html(msg);	      
					}
				});
			}
			else{
				$("#markerselector").html('Postcode onjuist.');	
			}
		});
		
		
		return false;
	});
	
	
	
});


function googleInit(){
	//declare infowindow object	for googlemaps
	infowindow = new google.maps.InfoWindow();
}

function googleGeoCode(address){
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address}, function(results, status){
		if(status == google.maps.GeocoderStatus.OK){
			//alert(results[0].geometry.location);
			//googlePlaceMarker(name,results[0].geometry.location,popuptext);
			return results[0].geometry.location;
		}
		else{
			return 'error';
		}
	});
}

var markersArray = [];
function googlePlaceMarker(name,lat,lon,popuptext){	
	var marker = new google.maps.Marker({
        map: map, 
        position: new google.maps.LatLng(lat,lon),
        title: name,
        icon: 'http://www.popal.nl/img/googlemarker.png'
    });
    
    google.maps.event.addListener(marker, 'click', function() {
    	infowindow.setContent(popuptext); 
		infowindow.open(map,marker);
	});
	
	markersArray.push(marker);
}


function googleClearMarkers() {
	if(markersArray){
		for(i in markersArray){
			markersArray[i].setMap(null);
		}
		markersArray.length = 0;
	}
}


function gooogleCreateBranchMap(){
	googleLatLon = new google.maps.LatLng(52.132633, 5.291266);
	
	myOptions = {
		zoom: 7,
		center: googleLatLon,
		mapTypeId: google.maps.MapTypeId.TERRAIN,
		mapTypeControlOptions: {
		   mapTypeIds: []
		}

	};
	
	map = new google.maps.Map(document.getElementById("dealers_map"),myOptions);
	
	google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });

}

function googleZoomInToMarker(i){
	map.setZoom(9);
	google.maps.event.trigger(markersArray[i], 'click');
}
