

jQuery(function() {
var st, ci, hl;
$('img#loading').hide();
//mapstraction

    // Now initialise the map
    var mapstraction = new Mapstraction('gmap','google');
    mapstraction.addControls({
        zoom: 'small',
        map_type: true
    });

var glat= '28.304381';
var glon = '-82.089844';

	 //center map
    mapstraction.setCenterAndZoom(
        new LatLonPoint(glat, glon),
        2 //zoom
    );


//form controls
$('#lstState').change(function(){
st = this.value;
$('img#loading').show();
$('#instructions').html('<em>Finding cities...</em>').show();
var dropDown = '';
//get cities
 $.getJSON("ambassador_query.aspx?state="+st,
null,
   function(j){

 $.each(j, function(i){
dropDown += '<option value="'+j[i]+'">'+j[i]+'</option>';
 });
  	$('form select#lstCity').html('<option value="" selected="selected" >Now Choose City</option>'+dropDown).find('option:eq(0)').each(function(){
	this.selected=true;
	});
$('img#loading').fadeOut('1500');
$('#instructions').html('<em>Now choose a city...</em>').show('slow');
 });//end get cities
 

});//state change

$('#lstCity').change(function(){
ci = this.value;

$('img#loading').show();
$('#instructions').html('<em>Finding Ambassadors...</em>').show();
//get the coords
 $.getJSON("ambassador_query.aspx?state="+st+"&city="+ci,
null,
   function(j){
var hl =  '';
 $.each(j, function(i){


//do google stuff here
if(this.ChurchPostalCode.length == 4) this.ChurchPostalCode = '0'+this.ChurchPostalCode;
gAddress = this.ChurchAddress+', '+this.ChurchCity+', '+this.ChurchStateProv+', '+this.ChurchPostalCode;
var bubbleHtml = '<strong>'+this.AmbassadorName+'</strong><br />'+this.ChurchName+'<br />'+this.ChurchAddress+'<br />'+this.ChurchCity+', '+this.ChurchStateProv+' '+this.ChurchPostalCode+'<br />'+this.ChurchPhone+'<br /><a href="mailto:'+this.AmbassadorEmail+'">'+this.AmbassadorEmail+'</a>';
hl += '<p id="church-'+i+'">'+bubbleHtml+'</p>';
showAddress(gAddress , bubbleHtml,i);
//end google

});
$('#ambassadorInfo').html(hl).addClass('bHighlight');

$('img#loading').fadeOut('1500');
$('#instructions').html('<em>Your map is ready:</em>').show();
});//end coord json




});//city change



function showAddress(address, bubbleHtml,i) {
//alert(i);
      // Create new geocoding object
      geocoder = new GClientGeocoder();

      // Retrieve location information, pass it to addToMap()
      geocoder.getLocations(address, function(response){
	//alert(response.Placemark[0].Point.coordinates[1]);
  var mHtml = '<div class="bubble">'+bubbleHtml+'</div>';
  var glat = response.Placemark[0].Point.coordinates[1];
  var glon = response.Placemark[0].Point.coordinates[0];
  //alert(glat+'+'+glon);
  var marker = new Marker(new LatLonPoint(glat, glon));
//marker.setIcon('/images/mapIcon6.gif',[30,30]);

        marker.setInfoBubble(mHtml);		
		 mapstraction.addMarker(marker);
	if(i==0) {
	marker.openBubble();
	 //center map
    mapstraction.setCenterAndZoom(
        new LatLonPoint(glat, glon),
        9 //zoom
    );
}
	  });
	  

}
});//document ready


