// Script that has a different behavior if the song is stopped vs recording for jamming
// Arguments:  
//		(no argument)
//
// Author: LoopToGo
//
// Description  : 	Start recording if the song is stopped otherwise, starts the Jam mode if not already jamming and over a record loop
//					Change track if already in jam mode
//					Change section if double click

ifNot $isRecording
	shortcut "Ctrl+Space"  	// Start recording
	globalVariable gJammingTrack	1 // We always start with the first track

	endScriptDynamic 	 	// No need to do anything else
endIf

// Check for double click
variable deltaClick $sysTime - $gLastSysTime

if $deltaClick < 500
	variable doubleClick 1	// The user called this script twice in a very short time
	globalVariable gLastSysTime 0	// To avoid triple click
else
	variable doubleClick 0	// The user called this script once
	globalVariable gLastSysTime $sysTime	// We use gloval variable because we want the next script to use this variable.
endIf


if $doubleClick
	//print DoubleClick!
	shortcut "Shift+Down"	// Select next section
	shortcut "Ctrl+Shift+G"	// Goto selected section at end of current section
	endScriptDynamic
endIf

ifNot $isInJamMode

	unselectAllLoops // We will use selectedLoop to confirm that the jam mode has been enabled so we need to unselectAllLoops first

	selectTrack $gJammingTrack

	shortcut J
	
	//print "Overdub now" $gJammingTrack

	shortcutByID 19431 + $gJammingTrack // Overdub now (Use 17383 to start in record mode)

	// Lock all track but selected
	unselectAllLoops // We will use selectedLoop to confirm that the jam mode has been enabled so we need to unselectAllLoops first

	for track 1 $nbTracks

		variable cond1 $track = $gJammingTrack
		variable cond2 $track = $previousTrack
		
		ifNot $cond1 + $cond2 // Both conditions must be false!
			//print Locking track $track
							
			shortcutByID 17383 + $track 	// record now (to unlock)
			shortcutByID 21479 + $track 	// Lock temporary (jam lock)
		endIf
	endFor

	selectTrack $gJammingTrack
		
else // is already in jam mode

	//print "Arm Track for lock" $gJammingTrack
	shortcutByID 25575 + $gJammingTrack	// Arm Track for lock

	
	// let change track (next track or first if current is last)
	variable previousTrack $gJammingTrack
	globalVariable gJammingTrack	$gJammingTrack + 1
	
	if $gJammingTrack > $nbTracks
		globalVariable gJammingTrack	1
	endIf
	

	//print "Arm Track for Record" $gJammingTrack

	shortcutByID 13287 + $gJammingTrack // Arm Track for record 
	
	endScriptDynamic
	
endIf
	
			

					
		


