/*****************************************************************************
 * File: store-locator.js
 * 
 * Check that our PHP connector works, if so load map stuff.
 *
 *****************************************************************************/

var map_delay;
// Check the WordPress environment was loaded
//
if (typeof add_base == 'undefined') {
    alert('SLPLUS: The PHP JavaScript connector did not load.');
} else if (typeof GLatLng == 'undefined' ) {    
    alert('SLPLUS: Google Map Interface did not load.');
    
// Load the map script if no errors
//
} else {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = add_base + '/js/store-locator-map.js';
    head.appendChild(script);
	// map_delay=setTimeout( 'sl_load()', 500);
  
    // Load the email form script if we want it
    //
    if (slp_use_email_form) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = add_base + '/js/store-locator-emailform.js';
        head.appendChild(script);
    }
}



function loadCities( county ) {
		current_county=county;
		jQuery('#search-county-anchor').text( county );
		jQuery( '#search-city-anchor').html('Looking Up Towns');
		jQuery.get(
			baseurl + '/wp-content/plugins/store-locator-plus/core/js/getCitiesOptions.php',
			{
				county:county,
				lookup:slp_lookup
			},
			function( data ) {
					jQuery( '#search-city-anchor').html('Select A Town');
					jQuery( '#city').html( data );
			},
			'html'
		);
		// having trouble with this lat/lng thing.
		//
        geocoder.getLatLng(county +', '+sl_google_map_country, 
            function(latlng) {
				// "when the county is selected, the zoom should be the same for both suppliers and stores"
				// if ( slp_lookup == 'supplier' ){ map.setZoom( 7 ); }
				// else { map.setZoom(3); }
				map.setZoom( 8 );
				map.panTo(latlng);
			});
		delayCB();
}
function findCity(){
		var county = current_county;
		var city = current_city;
		jQuery( 'ul#city').hide();
		jQuery( 'a#search-city-anchor').text(current_city);

		if ( city == '' ) return false;
        geocoder.getLatLng(city+', '+county +', '+sl_google_map_country, 
            function(latlng) {
				if ( slp_lookup == 'supplier' ){ map.setZoom( 10 ); }
				else { map.setZoom(13); }
                map.panTo(latlng);
				// "when the city is selected, stores and suppliers have different zoom levels"
				// map.setZoom( 12 );
				delayCB();
			});
}
		

// need to connect this to moving the map.
// including when we pan it above.
// 
var delay;
function delayCB(){
		clearTimeout( delay );
		delay = setTimeout( 'repopulateResults()', 500);
}

function repopulateResults(){
	var bounds = map.getBounds();
	var latlng = bounds.getCenter();
	sl_load_locations(map,latlng.lat(),latlng.lng());
}

