function InitActions() {
	$('div.ButtonPoint').bind('click', function() {	
		if( $(this).hasClass( 'Close' ) ) {
			$(this).removeClass( 'Close' );
			ChangeMapState( 'View' );
		} else {
			$(this).addClass( 'Close' );
			ChangeMapState( 'Circle' );
		}
		/*CircleDisplay( Map.getCenter() );
		MapAction = 'CircleEdit';
		MapSearchCircle( Map.getCenter() );*/		
	});	
	
	$( 'span.SearchCountry' ).bind('click', function() {
		var CountryID = $(this).attr('id');
		CountryID = CountryID.substr(10); // remove prefix MapCountry
		ChangeMapState( 'Country', CountryID );
	});	
	
	$( 'input#searchTextField' ).bind('click', function() {
		$(this).val('');
	});	
}

function MapSearchCircle( LatLng ) {
	
	SearchCircle();
	MapChangeCenter( LatLng );
	
	
	/*if( MapAction == 'CircleAdd' ) {
		CircleDisplay( LatLng );
		CircleAddEvents();
		
		SearchCircle();
		
		MapChangeCenter( LatLng );		
		MapAction = 'CircleEdit';
	} else if( MapAction == 'CircleEdit' ) {
    	ResetMarkers();
    	SearchCircle();
    	MapChangeCenter( LatLng );
	} else if( MapAction == 'Keyword' ) {
    	ResetMarkers();
    	SearchCircle();
    	ChangeMapState( 'Circle' );
    	MapChangeCenter( LatLng );
	}*/
}

function MapSearchCountry( CountryID ) {
    SearchCountry( CountryID );
}

function MapSearchKeyword( LatLng ) {
	RemoveCircle();
	ChangeMapState( 'Keyword' );
	CircleDisplay( LatLng );
	CircleAddEvents();
	ResetMarkers();
	SearchCircle();
	MapChangeCenter( LatLng );
}

function ChangeMapState( NewMapAction, LatLngOrCountry ) {
	var CurrentMapAction = MapAction;
	var UpdateMapAction = NewMapAction;
	
	switch( NewMapAction ) {
		case 'Circle':
			switch( CurrentMapAction ) {
				case 'CircleAdd':
					CircleDisplay( LatLngOrCountry );
					CircleAddEvents();
					MapSearchCircle( LatLngOrCountry );
					HideMessage();
					UpdateMapAction = 'CircleEdit';
					ZoomToDefault();
					break;
				case 'CircleEdit':
					ResetMarkers();
					MapSearchCircle( LatLngOrCountry );
					UpdateMapAction = 'CircleEdit';
					break;
				default:
					ResetMarkers();
					DisplayMessage( 1 );
					$( '#MapButtonPoint' ).html( MapCircleButtonCloseText );
					UpdateMapAction = 'CircleAdd';					
					break;
			}
			break;
			
		case 'Country':
			RemoveCircle();
			ResetMarkers();
			MapSearchCountry( LatLngOrCountry );
			$( '#MapButtonPoint' ).html( MapCircleButtonText );
			if( $( '#MapButtonPoint' ).hasClass( 'Close' ) ) {
				$( '#MapButtonPoint' ).removeClass( 'Close' );
			}			
			break;
			
		case 'Keyword':
			switch( CurrentMapAction ) {
				default:
					RemoveCircle();
					ResetMarkers();
					ZoomToDefault();
					CircleDisplay( LatLngOrCountry );
					CircleAddEvents();
					MapSearchCircle( LatLngOrCountry );
					HideMessage();
					$( '#MapButtonPoint' ).html( MapCircleButtonCloseText );
					if( $( '#MapButtonPoint' ).hasClass( 'Close' ) ) {
					} else {
						$( '#MapButtonPoint' ).addClass( 'Close' );
					}
					UpdateMapAction = 'CircleEdit';					
					break;
			}			
			break;
			
		case 'View':
		default:
			// set to defaults
			RemoveCircle();
			HideMessage();
			ZoomToDefault();
			ResetMarkers();
			$( '#MapButtonPoint' ).html( MapCircleButtonText );
			NewMapAction = 'View';
			break;
	}
	MapAction = UpdateMapAction;
}
	
function ChangeMapStateOld( NewMapAction ) {
	if( NewMapAction == 'Circle' ) {
		if( MapAction == 'View' ) {			
			MapAction = 'CircleAdd';
			DisplayMessage( 1 );
			$( '#MapButtonPoint' ).html( MapCircleButtonCloseText );
			ZoomToDefault();
		} else if( MapAction == 'CircleEdit' ) {
			MapAction = 'View';
			RemoveCircle();
			$( '#MapButtonPoint' ).html( MapCircleButtonText );
		} else if( MapAction == 'CountryEdit' ) {
			ResetMarkers();
			MapAction = 'CircleAdd';
			DisplayMessage( 1 );
			$( '#MapButtonPoint' ).html( MapCircleButtonCloseText );
			ZoomToDefault();
		} else if( MapAction == 'CircleAdd' ) {			
			MapAction = 'View';
			HideMessage();
			$( '#MapButtonPoint' ).html( MapCircleButtonText );
		} else if( MapAction == 'Keyword' ) {			
			MapAction = 'CircleAdd';
			$( '#MapButtonPoint' ).html( MapCircleButtonCloseText );
			ZoomToDefault();
		}
	
	} else if( NewMapAction == 'Country' ) {
		if( MapAction == 'View' ) {
			MapAction = 'CountryEdit';
		} else if( MapAction == 'CircleAdd'
			|| MapAction == 'CircleEdit' ) {
			MapAction = 'CountryEdit';
			RemoveCircle();
			$( '#MapButtonPoint' ).html( MapCircleButtonText );
		}
	
	} else if( NewMapAction == 'Keyword' ) {
		if( MapAction == 'CircleAdd' ) {
			HideMessage();
			$( '#MapButtonPoint' ).html( MapCircleButtonText );			
		}
		MapAction = 'Keyword';
		ZoomToDefault();
	}
}

function DisplayMessage( MessageID ) {
	$( '#SearchCountry' ).hide();
	$( '#MapProduct' ).hide();
	$( '#MapMessage' ).show();
}

function HideMessage() {
	$( '#MapMessage' ).hide();
	$( '#MapProduct' ).hide();
	$( '#SearchCountry' ).show();
}
