:: Place In Line

<<silently>>

<<set $slavesInLine = []>>
<<set _activeSlaveIndex = $slaveIndices[$activeSlave.ID]>>
<<set _SL = $slaves.length>>

<<if ($activeSlave.assignmentVisible == 1)>>
	<<for _pil = _activeSlaveIndex - 1; _pil != _activeSlaveIndex; _pil-->> /* loops backwards through the $slaves array */
		<<if _pil < 0>>
			<<set _pil = _SL>>
			<<continue>>
		<</if>>
		<<if $slaves[_pil].assignmentVisible == 1>>
			<<set $slavesInLine.push(_pil)>> /* index of the previous slave in line */
			<<break>>
		<</if>>
	<</for>>
	<<for _pil = _activeSlaveIndex + 1; _pil != _activeSlaveIndex; _pil++>> /* this loops forwards through the $slaves array */
		<<if _pil == _SL>>
			<<set _pil = -1>>
			<<continue>>
		<</if>>
		<<if $slaves[_pil].assignmentVisible == 1>>
			<<set $slavesInLine.push(_pil)>> /* index of the next slave in line */
			<<break>>
		<</if>>
	<</for>>
<<else>>
	<<for _pil = _activeSlaveIndex - 1; _pil != _activeSlaveIndex; _pil-->> /* loops backwards through the $slaves array */
		<<if _pil < 0>>
			<<set _pil = _SL>>
			<<continue>>
		<</if>>
		<<if ($slaves[_pil].assignment == $activeSlave.assignment)>>
			<<set $slavesInLine.push(_pil)>> /* index of the previous slave in line */
			<<break>>
		<</if>>
	<</for>>
	<<for _pil = _activeSlaveIndex + 1; _pil != _activeSlaveIndex; _pil++>> /* this loops forwards through the $slaves array */
		<<if _pil == _SL>>
			<<set _pil = -1>>
			<<continue>>
		<</if>>
		<<if ($slaves[_pil].assignment == $activeSlave.assignment)>>
			<<set $slavesInLine.push(_pil)>> /* index of the next slave in line */
			<<break>>
		<</if>>
	<</for>>
<</if>>

<<if $slavesInLine.length == 0>> /* if there are no other slaves available, set previous/next slave to self */
	<<set $slavesInLine[0] = _activeSlaveIndex>>
	<<set $slavesInLine[1] = _activeSlaveIndex>>
<</if>>

<</silently>>
