// Adjust the selected loops to their corresponding section
// Arguments:  
//		$arg1 (optionnal) : Allowed ratio. Default is 6
//
// Author: LoopToGo
//
// Description  : 	Will adjust the start and end of all selected loops to fit the corresponding section. 
//					Will do nothing if there is no selected loops or if there is no section
//					Use the middle of the loop to find the corresponding section
//					Will do nothing	if the loop is bigger than the section by a ratio of allowedRatio or more


variable allowedRatio 3

if $arg1
	variable allowedRatio $arg1
endIf	


if $nbSelectedLoops*$nbSections // Do nothing if no loop is selected or if there is no section

	variable oldSongBar $songBar
	
	
	for loopNum 1 $nbSelectedLoops

		variable _loopID selectedLoopID & $loopNum 
		variable loopID $$_loopID
		
		// print $_loopID : $loopID

		//getLoopName lName $loopID 
		//getLoopType lType $loopID 
		//getLoopTrack lTrack $loopID 
		getLoopStart lStart $loopID 
		getLoopEnd lEnd $loopID 
		
		variable loopLength $lEnd - $lStart
		
		//print $lTrack : $lName [ & $lType & ] $lStart $lEnd
		
		variable middleLoop $lStart + $lEnd
		variable middleLoop $middleLoop/2
		
		setBar $middleLoop
				
		variable sectionLength $currentSectionEnd - $currentSectionStart
		
				
		if $currentSectionEnd > $currentSectionStart
			if $loopLength < $allowedRatio*$sectionLength		// We do not adjust a loop that is far bigger than the section
				setLoopStartAndEnd $loopID $currentSectionStart $currentSectionEnd
			endIf
		endIf
		
	endFor


	setBar $oldSongBar
	
	//shortcut T
	//shortcut T

endIf

