//
var map = null;
var markers = null;
var geocoder = null;

var viaAJAX = false;

//

google.load( "maps", "2.x" );

function initializeGMaps()
{

	if ( GBrowserIsCompatible() )
	{
		
		map = new GMap2( $( 'localServicesMap' ) );
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.addControl( new GSmallMapControl(), new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( 0, 0 ) ) );
		
		map.setCenter( new GLatLng( 54.788929, -4.240723 ), 5, G_NORMAL_MAP );

	}

}

function setupGMap( latitude, longitude, zoom )
{

	if( null == zoom )
	{
		zoom = 14;
	}
	
	if ( latitude != null && longitude != null )
	{

		map.setZoom( zoom );
		$( 'localServicesMap' ).show();
		map.checkResize();
		map.panTo( new GLatLng( latitude, longitude ) );
		
	}

}

function addPinToMap( data, latitude, longitude )
{

	var labelTemplate = '<div style="font-size: 10px;"><b>NAME</b>ADDRESS TELEPHONE</div>';

	var labelBody = labelTemplate.replace( /NAME/, data[ 0 ] );

	if ( data[ 1 ].length > 0 && data[ 1 ] != 'null' )
	{
		labelBody = labelBody.replace( /ADDRESS/, "<br />" + data[ 1 ] );
	}
	else
	{
		labelBody = labelBody.replace( /ADDRESS/, "" );
	}
	if ( data[ 2 ].length > 0 && data[ 2 ] != 'null' )
	{
		labelBody = labelBody.replace( /TELEPHONE/, "<br />" + data[ 2 ] );
	}
	else
	{
		labelBody = labelBody.replace( /TELEPHONE/, "" );
	}

	if ( null != map )
	{

		if ( null != latitude && null != longitude )
		{
			markers = new Array();
			var myMarker = new GMarker( new GLatLng( latitude, longitude ) );
			map.addOverlay( myMarker );
			GEvent.addListener( myMarker, "click", function()
			{
	
				myMarker.openInfoWindowHtml( labelBody );
			} );
			markers.push( myMarker );
		}

	}
	else
	{
		displayError( "Could not display markers. Failed to initialise Google Maps." );
	}

}

function populateServices( sid )
{

	var serviceSelect = $( 'service_id' );
	var subSelect = $( 'subservice_id' );

	if ( serviceSelect.options.length == 1 )
	{
		serviceSelect.options.length = 0;

		serviceSelect.options[ serviceSelect.options.length ] = new Option(
		        "Select a Service", "" );
		for ( var x = 0; x < services.length; x++ )
		{
			if ( services[ x ][ 2 ] == '' )
			{
				serviceSelect.options[ serviceSelect.options.length ] = new Option(
				        services[ x ][ 1 ], services[ x ][ 0 ] );
				if ( services[ x ][ 0 ] == sid )
				{
					serviceSelect.options[ serviceSelect.options.length - 1 ].selected = true;
				}
			}
		}

		subSelect.options.length = 0;
		subSelect.options[ subSelect.options.length ] = new Option(
		        "Select a service", "" );
		subSelect.options[ subSelect.options.length - 1 ].selected = true;
	}
	else if ( subSelect.options.length > 1 )
	{
		executeServicesSearch();
	}

}

function populateSubServices( suid )
{

	var selectIndex = $( 'service_id' ).selectedIndex;
	var selectedValue = $( 'service_id' )[ selectIndex ].value;
	var subSelect = $( 'subservice_id' );

	subSelect.options.length = 0;

	if ( selectedValue != "" )
	{
		subSelect.options[ subSelect.options.length ] = new Option(
		        "Select a type", "" );
		subSelect.options[ subSelect.options.length ] = new Option( "All", "A" );
		for ( var x = 0; x < services.length; x++ )
		{
			if ( services[ x ][ 2 ] == selectedValue )
			{
				subSelect.options[ subSelect.options.length ] = new Option(
				        services[ x ][ 1 ], services[ x ][ 0 ] );
				if ( services[ x ][ 0 ] == suid )
				{
					subSelect.options[ subSelect.options.length - 1 ].selected = true;
				}
			}
		}
		
		if ( suid == 'A' )
		{
			subSelect.options[ 1 ].selected = true;
		}
		
	}
	else
	{
		subSelect.options[ subSelect.options.length ] = new Option(
		        "Select a service", "" );
		subSelect.options[ 1 ].selected = true;
	}

}

function populateCities( cities )
{

	var citSelect = $( 'city_id' );

	citSelect.options.length = 0;

	citSelect.options[ citSelect.options.length ] = new Option(
	        "Select a city", "" );

	for ( var x = 0; x < cities.length; x++ )
	{
		var cityValues = cities[ x ].split( '|' );

		citSelect.options[ citSelect.options.length ] = new Option(
		        cityValues[ 1 ], cityValues[ 0 ] );
	}
}

function executeCitiesSearch()
{

	var county = $( 'county_id' );

	if ( null != map )
	{
		map.clearOverlays();
	}

	if ( county.value != "" )
	{
		fetchCities( county.value );
	}

}

function lookupCityLocation()
{

	if ( null == geocoder )
	{
		geocoder = new GClientGeocoder();
		geocoder.reset()
		geocoder.setBaseCountryCode( "UK" );
	}

	var city = $( 'city_id' );
	var cityName = city.options[ city.selectedIndex ].text;
	var county = $( 'county_id' );
	var countyName = county.options[ county.selectedIndex ].text;

	if ( city.value != "" )
	{
		geocoder.getLatLng( cityName + "," + countyName + ",UK", function(
		        point )
		{

			setupGMap( point.y, point.x );
		} );
	}

}

function executeServicesSearch()
{

	var county = $( 'county_id' );
	var city = $( 'city_id' );
	var service = $( 'service_id' );
	var type = $( 'subservice_id' );
	
	if ( null != map )
	{
		if ( null != markers )
		{
			while ( markers.length > 0 )
			{
				map.removeOverlay( markers.pop() );
			}
		}
		map.clearOverlays();
	}
	
	if ( type.value != "" )
	{
		if ( viaAJAX )
		{
			fetchServices( city.value, service.value, type.value );
		}
		else
		{
			if( city.value != "" && service.value != "" )
			{
				document.location = "asfl_localservices.php?ctid=" + county.value + "&cid=" + city.value + "&sid=" + service.value + "&suid=" + type.value;
			}
		}
	}

}