;****************************************************************************************************************

<header>
    <var vCurrentTrackIndex=0>
    <var vDeleteHappened=0>
</header>

;****************************************************************************************************************

<state st_ManageTrack>
	<uselayer ui_Header2/>
    <uselayer ui_Background/>
	<uselayer ui_Title/>
	<uselayer ui_Footer2/>
	<uselayer ui_ManageTrack/>
	
	<script init>
		Title.SET "Manage Tracks"

		run sc_UseGestures

		;TRACKING_IS_RECORDING vTmp
		;runif vTmp 0 'btnMT_Record.SHOW, btnMT_Stop.HIDE'
		;runif vTmp 1 'btnMT_Record.HIDE, btnMT_Stop.SHOW'
	</script>
</state>

<script sc_TrackRecord_Start>
	TRACKING_RECORD
</script>

<script sc_btnMT_Record_OnRelease>
	run sc_TrackRecord_Start
	run sc_ReturnToMap
</script>

<script sc_btnMT_Stop_OnRelease>
	TRACKING_STOP_RECORDING
</script>

<script sc_btnMT_Delete>
	vTmp.VALUEMODEL "mydata.tracks.list.index"
	vCurrentTrackIndex.SET vTmp
	vTmp.DROPMODEL

    INITMESSAGEBOX 1 "Track will be deleted." 'TRACKING_DELETE vCurrentTrackIndex, vDeleteHappened.SET 1, PREVSTATE'
</script>


<script sc_TrackLogClearAll>
	INITMESSAGEBOX 1 "This will clear all of the saved track logs!" 'TRACKING_DELETE_ALL'
</script>
;****************************************************************************************************************

<state st_ManageTrackInfo>
	<uselayer ui_Header2/>
	<uselayer ui_Background/>
	<uselayer ui_Title/>
	<uselayer ui_Footer2/>
	<uselayer ui_ManageTrackInfo/>
	
	<script init>
		Title.SET "Track Information"
		
		vTmp.VALUEMODEL "mydata.tracks.list.index"
		vCurrentTrackIndex.SET vTmp
		vTmp.DROPMODEL

		TRACKING_INFO vCurrentTrackIndex
				
		vtmp.boolmodel "mydata.tracks.list.current.visible"
		runif vtmp 0 'vTrackColor.set "0", btn_TL_fts.DISABLE' 
		else_run sc_MTI_SetRadioStateFromColor
		vtmp.DROPMODEL
		
		vDeleteHappened.SET 0		
	</script>
	
	<script done>
		runif vDeleteHappened 0 'run sc_ManageTrackInfo_done'
	</script>
		
	
</state>

<script sc_MTI_SetRadioStateFromColor>
	ntmp.valuemodel "mydata.tracks.list.current.color"
	runif ntmp 5 'vTrackColor.set "1"'
	runif ntmp 8 'vTrackColor.set "2"'
	runif ntmp 0 'vTrackColor.set "3"'
	runif ntmp 1 'vTrackColor.set "4"'
	runif ntmp 4 'vTrackColor.set "5"'
	runif ntmp 9 'vTrackColor.set "6"'
	runif ntmp 7 'vTrackColor.set "7"'
	runif ntmp 6 'vTrackColor.set "8"'
	ntmp.dropmodel
	btn_TL_fts.ENABLE
</script>

<script sc_ManageTrackInfo_done>
	ntmp.valuemodel "mydata.tracks.list.current.color"
	vtmp.boolmodel "mydata.tracks.list.current.visible"

		runif vTrackColor "0" 'vtmp.set 0' 
		else_run 'vtmp.set 1'

		runif vTrackColor "1" 'ntmp.set 5'
		runif vTrackColor "2" 'ntmp.set 8'
		runif vTrackColor "3" 'ntmp.set 0'
		runif vTrackColor "4" 'ntmp.set 1'
		runif vTrackColor "5" 'ntmp.set 4'
		runif vTrackColor "6" 'ntmp.set 9'
		runif vTrackColor "7" 'ntmp.set 7'
		runif vTrackColor "8" 'ntmp.set 6'
		
	ntmp.dropmodel
	vtmp.dropmodel
</script>


<script sc_btnMTI_Rename_OnRelease>
    vTmp.TEXTMODEL_WSTR "mydata.tracks.list.current.name"
    vInputDest.SET vTmp
    vTmp.DROPMODEL
    
    inpSI_Text.VALUE vInputDest
	TRACKING_IS_DEFAULTNAME vCurrentTrackIndex vTmp
	runif vTmp 1 'inpSI_Text.VALUE ""'
	inpSI_Text.emptytitle "<Enter new track name>" 
	
	vKeyTableDone.SET sc_RenameTrack
	vInputDoneScript.SET sc_RenameTrack
	vInputDoneScriptSet.set 1
	vRequiredField.SET 1
	Title.SET "New track name"
	NEXTSTATE st_SimpleInputText
</script>

<script sc_btnMTI_Replay_OnRelease>
	run sc_DisableGestures 1

	vTmp.VALUEMODEL "mydata.tracks.list.index"
	vCurrentTrackIndex.SET vTmp
	vTmp.DROPMODEL

	runif vMapMode_saved L"" 'vMapMode_saved.SET vMapMode'

	TRACKING_PLAY vCurrentTrackIndex

	vFlyoverOrSimulate.set 0
	vFlyOverNextState.Set st_ManageTrack
	vStateMode.SET ""

	run sc_FollowOn
	STATE st_NavigateMap
</script>

<script sc_btnMTI_Export_OnRelease>
    TRACKING_EXPORT vCurrentTrackIndex vTmp
	INITMESSAGEBOX 0 vTmp
</script>

<script sc_RenameTrack>
	inpSI_Text.get "vTmp"
	TRACKING_SETNAME vCurrentTrackIndex vTmp
	PREVSTATE
</script>

<script sc_FitTrackToScreen>
	;Zoom level must not be restored when entering map state again => vSetZoomSettings.SET 0
	vSetZoomSettings.SET 0

	vMapState.set 4
	; Following 3 commands are execute by st_NavigateMap_Init (Init vMapState 4 => 2D North up)
	;map.SetMapState 4
	;run sc_2Dmode
	;map.SETCARPOSITION 50 50

	; Follow should switch off before fitting, else it will override center position of fitting track
	run sc_P_return_to_map 0
	run sc_FollowOff
	tracking_fittoscreen vCurrentTrackIndex
</script>

;**************************************************************************************************

<state st_Tracking_Options>
	<uselayer ui_Header2/>
	<uselayer ui_Background/>
	<uselayer ui_Title/>
	<uselayer ui_Footer2/>

	<uselayer ui_tracking_options/>
	<script init>
		Title.SET "Track Log Options"

		hsc_tracksave_time.value vTrackGpsSaveInterval
		run sc_set_tracksave_time_txt
		hsc_adv_tracksdb.value vTrackLimitDBSizeValue
		run sc_set_adv_tracksdb_txt
		tracking_getcurrentdbsize vTmp
		txt_adv_tracksdb_info2.text vTmp
		vRecordTrack.SET %mydata.tracks.record_auto_track 
	</script>
	
	<script sc_set_tracksave_time_txt>
		hsc_tracksave_time.get "vTrackGpsSaveInterval"
		txt_tracksave_time_value.text vTrackGpsSaveInterval
	</script>
	
	<script sc_set_adv_tracksdb_txt>
		hsc_adv_tracksdb.get "vTrackLimitDBSizeValue"
	
		tracking_getmaxdbsize vTmp
		runif vtmp L"~" 'vtmp.set "No Limit", vTrackLimitDBSizeEnable.set 0'
		else_run 'vTrackLimitDBSizeEnable.set 1'
		txt_adv_tracksdb_value.text vTmp

	</script>
	
	;*************240x240******************************************************
	<script sc_set_advtracksdb_sld>	
		runif vTrackLimitDBSizeValue 999999 'vTrackLimitDBSizeEnable.set 0'
		else_run 'vTrackLimitDBSizeEnable.set 1'
	</script>
	
</state>

<script sc_TO_AutoOn>
; the init script will trigger a fake "autoon" notification. We ignore it here...
	runifnot vRecordTrack %mydata.tracks.record_auto_track sc_TO_AutoOn_Really
</script>

<script sc_TO_AutoOn_Really>
	runif %mydata.tracks.tracking 1 sc_btnMT_Stop_OnRelease
	INITMESSAGEBOX 2 "Would you like to see the automatic logs on the map?" sc_TO_AutoOn_yes 'mydata.tracks.auto_track_visible 0, mydata.tracks.record_auto_track 1'
</script>

<script sc_TO_AutoOn_yes>
	INITMESSAGEBOX 0 "The automatic logs will only be visible for a few days. After that you may put them back on the map manually." 'mydata.tracks.auto_track_visible 1, mydata.tracks.record_auto_track 1'
</script>

<script sc_TO_AutOff>
	runifnot vRecordTrack %mydata.tracks.record_auto_track 'mydata.tracks.record_auto_track 0'
</script>
