on init
	hide_inst_icon
	make_perfview
	set_ui_width_px(1000)
	set_ui_height_px(750)
	set_script_title("")
	set_skin_offset(0)
	disable_logging($NI_LOG_MESSAGE)
	disable_logging($NI_LOG_WARNING)
	disable_logging($NI_LOG_WATCHING)
   reset_ksp_timer
   SET_CONDITION(NO_SYS_SCRIPT_RLS_TRIG)
   declare $GC:=0
	message("")


	declare $CN:=0
	while ($CN<128)
		set_key_type($CN, $NI_KEY_TYPE_NONE)
		set_key_color($CN, $KEY_COLOR_WHITE)
		remove_keyrange($CN)
		inc($CN)
	end while
	declare $SL_CNT := 0
	declare %SL_ALLOW[16] := (1)
	make_persistent     (%SL_ALLOW)
	read_persistent_var (%SL_ALLOW)

	if (%SL_ALLOW[0] = 1)
		set_keyrange(48, 69, "Sample Layer A")
		$CN:=48
		while ($CN<70)
			set_key_color($CN, $KEY_COLOR_RED)
			inc($CN)
		end while
	else
		$CN:=48
		while ($CN<70)
			remove_keyrange($CN)
			set_key_color($CN, $KEY_COLOR_WHITE)
			inc($CN)
		end while
	end if
	if (%SL_ALLOW[1] = 1)
		set_keyrange(65, 69, "Sample Layer B")
		$CN:=65
		while ($CN<70)
			set_key_color($CN, $KEY_COLOR_BLUE)
			inc($CN)
		end while
	else
		$CN:=65
		while ($CN<70)
			remove_keyrange($CN)
			set_key_color($CN, $KEY_COLOR_WHITE)
			inc($CN)
		end while
	end if

	declare %KR_ENABLED[16] := (1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
	declare %KR_S[16] := (48,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
	declare %KR_E[16] := (69,69,127,127,127,127,127,127,127,127,127,127,127,127,127,127)
	declare %SL_FNP[16] := (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
	declare $SL_A_NCYCL := 0
	declare $SL_B_NCYCL := 0
	declare $SL_C_NCYCL := 0
	declare $SL_D_NCYCL := 0
	declare $SL_E_NCYCL := 0
	declare $SL_F_NCYCL := 0
	declare $SL_G_NCYCL := 0
	declare $SL_H_NCYCL := 0
	declare $SL_I_NCYCL := 0
	declare $SL_J_NCYCL := 0
	declare $SL_K_NCYCL := 0
	declare $SL_L_NCYCL := 0
	declare $SL_M_NCYCL := 0
	declare $SL_N_NCYCL := 0
	declare $SL_O_NCYCL := 0
	declare $SL_P_NCYCL := 0

	declare %SL_A_NCYCL[128] := (0)
	declare %SL_B_NCYCL[128] := (0)
	declare %SL_C_NCYCL[128] := (0)
	declare %SL_D_NCYCL[128] := (0)
	declare %SL_E_NCYCL[128] := (0)
	declare %SL_F_NCYCL[128] := (0)
	declare %SL_G_NCYCL[128] := (0)
	declare %SL_H_NCYCL[128] := (0)
	declare %SL_I_NCYCL[128] := (0)
	declare %SL_J_NCYCL[128] := (0)
	declare %SL_K_NCYCL[128] := (0)
	declare %SL_L_NCYCL[128] := (0)
	declare %SL_M_NCYCL[128] := (0)
	declare %SL_N_NCYCL[128] := (0)
	declare %SL_O_NCYCL[128] := (0)
	declare %SL_P_NCYCL[128] := (0)

	declare $SL_CHAIN_DEF_CNT := 0
	declare $SL_CHAIN_REL_CNT := 0
	declare $CHPOS_DEF := 0
	declare $CHPOS_REL := 0
	declare %SL_CHAIN_DEF_PB_ARR[16]
	declare %SL_CHAIN_REL_PB_ARR[16]
	declare %SL_CHAIN_DEF_ARR[16]
	declare %SL_CHAIN_REL_ARR[16]
			%SL_CHAIN_DEF_ARR[0] := 1
			%SL_CHAIN_DEF_ARR[1] := 1
			%SL_CHAIN_DEF_ARR[2] := 0
			%SL_CHAIN_DEF_ARR[3] := 0
			%SL_CHAIN_DEF_ARR[4] := 0
			%SL_CHAIN_DEF_ARR[5] := 0
			%SL_CHAIN_DEF_ARR[6] := 0
			%SL_CHAIN_DEF_ARR[7] := 0
			%SL_CHAIN_DEF_ARR[8] := 0
			%SL_CHAIN_DEF_ARR[9] := 0
			%SL_CHAIN_DEF_ARR[10] := 0
			%SL_CHAIN_DEF_ARR[11] := 0
			%SL_CHAIN_DEF_ARR[12] := 0
			%SL_CHAIN_DEF_ARR[13] := 0
			%SL_CHAIN_DEF_ARR[14] := 0
			%SL_CHAIN_DEF_ARR[15] := 0
			%SL_CHAIN_REL_ARR[0] := 0
			%SL_CHAIN_REL_ARR[1] := 0
			%SL_CHAIN_REL_ARR[2] := 0
			%SL_CHAIN_REL_ARR[3] := 0
			%SL_CHAIN_REL_ARR[4] := 0
			%SL_CHAIN_REL_ARR[5] := 0
			%SL_CHAIN_REL_ARR[6] := 0
			%SL_CHAIN_REL_ARR[7] := 0
			%SL_CHAIN_REL_ARR[8] := 0
			%SL_CHAIN_REL_ARR[9] := 0
			%SL_CHAIN_REL_ARR[10] := 0
			%SL_CHAIN_REL_ARR[11] := 0
			%SL_CHAIN_REL_ARR[12] := 0
			%SL_CHAIN_REL_ARR[13] := 0
			%SL_CHAIN_REL_ARR[14] := 0
			%SL_CHAIN_REL_ARR[15] := 0
	declare $SL_CHAIN_DEF_CNT_MAX := 1
	declare $SL_CHAIN_REL_CNT_MAX := 1
	declare polyphonic $SL_A_NOTE_ID := -1
	declare polyphonic $SL_B_NOTE_ID := -1
	declare polyphonic $SL_C_NOTE_ID := -1
	declare polyphonic $SL_D_NOTE_ID := -1
	declare polyphonic $SL_E_NOTE_ID := -1
	declare polyphonic $SL_F_NOTE_ID := -1
	declare polyphonic $SL_G_NOTE_ID := -1
	declare polyphonic $SL_H_NOTE_ID := -1
	declare polyphonic $SL_I_NOTE_ID := -1
	declare polyphonic $SL_J_NOTE_ID := -1
	declare polyphonic $SL_K_NOTE_ID := -1
	declare polyphonic $SL_L_NOTE_ID := -1
	declare polyphonic $SL_M_NOTE_ID := -1
	declare polyphonic $SL_N_NOTE_ID := -1
	declare polyphonic $SL_O_NOTE_ID := -1
	declare polyphonic $SL_P_NOTE_ID := -1
	declare %YOFFSET[16] := (0)
	%YOFFSET[0] := 0
	%YOFFSET[1] := 818
	%YOFFSET[2] := 1636
	%YOFFSET[3] := 2454
	%YOFFSET[4] := 3272
	%YOFFSET[5] := 4090
	%YOFFSET[6] := 4908
	%YOFFSET[7] := 5726
	%YOFFSET[8] := 6544
	%YOFFSET[9] := 7362
	%YOFFSET[10] := 8180
	%YOFFSET[11] := 8998
	%YOFFSET[12] := 9816
	%YOFFSET[13] := 10634
	%YOFFSET[14] := 11452
	%YOFFSET[15] := 12270
	declare $PANELITEMCNT := 0
	declare %PANEL1_UIID[999] := (-1)
	declare %PANEL2_UIID[999] := (-1)
	declare %PANEL3_UIID[999] := (-1)
	declare %PANEL4_UIID[999] := (-1)
	declare %PANEL5_UIID[999] := (-1)
	declare %PANEL6_UIID[999] := (-1)
	declare %PANEL7_UIID[999] := (-1)
	declare %PANEL8_UIID[999] := (-1)
	declare %PANEL9_UIID[999] := (-1)
	declare %PANEL10_UIID[999] := (-1)
	declare %PANEL11_UIID[999] := (-1)
	declare %PANEL12_UIID[999] := (-1)
	declare %PANEL13_UIID[999] := (-1)
	declare %PANEL14_UIID[999] := (-1)
	declare %PANEL15_UIID[999] := (-1)
	declare %PANEL16_UIID[999] := (-1)
	declare %PANELITEMS[16] := (0)
   declare $NEWPANEL := 0

   declare %SLTUN_OCT[16]  := (0)
   declare %SLTUN_SEMI[16] := (0)
   declare %SLTUN_FINE[16] := (0)
	make_persistent (%SLTUN_OCT)
	make_persistent (%SLTUN_SEMI)
	make_persistent (%SLTUN_FINE)
	read_persistent_var (%SLTUN_OCT)
	read_persistent_var (%SLTUN_SEMI)
	read_persistent_var (%SLTUN_FINE)

	declare %INIT_DONE[1]
			%INIT_DONE[0]:=0

	declare %RESET_DONE[1]
			%RESET_DONE[0]:=0

	load_array(%RESET_DONE,1)

	if (%RESET_DONE[0]=1)
	end if

	declare $PREVIEW_NOTE_ID := -1

	declare ui_switch $DEBUG_WP_SWITCH
	set_control_par (get_ui_id($DEBUG_WP_SWITCH),$CONTROL_PAR_HIDE,16)
	declare                        %DEBUG_WP_STATE[1]
	                               %DEBUG_WP_STATE[0]:=1



	%PANELITEMS[0]:=0
	%PANELITEMS[1]:=0
	%PANELITEMS[2]:=0
	%PANELITEMS[3]:=0
	%PANELITEMS[4]:=0
	%PANELITEMS[5]:=0
	%PANELITEMS[6]:=0
	%PANELITEMS[7]:=0
	%PANELITEMS[8]:=0
	%PANELITEMS[9]:=0
	%PANELITEMS[10]:=0
	%PANELITEMS[11]:=0
	%PANELITEMS[12]:=0
	%PANELITEMS[13]:=0
	%PANELITEMS[14]:=0
	%PANELITEMS[15]:=0

	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL1_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL2_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL3_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL4_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL5_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL6_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL7_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL8_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL9_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL10_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL11_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL12_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL13_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL14_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL15_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL16_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while

	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL1_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,0)
		set_control_par(%PANEL1_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,$HIDE_PART_BG)
		inc($PANELITEMCNT)
	end while

end on
on persistence_changed
	save_array(%INIT_DONE,1)
	%RESET_DONE[0]:=0
	save_array(%RESET_DONE,1)
end on
function UPDATE_PANEL_ITEMS
	{ }
end function

function _FUNC_HIDE_PANELS
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL1_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL2_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL3_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL4_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL5_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL6_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL7_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL8_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL9_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL10_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL11_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL12_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL13_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL14_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL15_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while
	$PANELITEMCNT:=0
	while($PANELITEMCNT<0)
		set_control_par(%PANEL16_UIID[$PANELITEMCNT],$CONTROL_PAR_HIDE,16)
		inc($PANELITEMCNT)
	end while

end function

on ui_control ($DEBUG_WP_SWITCH)
	%DEBUG_WP_STATE[0] := 0
	save_array(%DEBUG_WP_STATE,1)
end on

on note
	disallow_group($ALL_GROUPS)
	disallow_group($ALL_GROUPS)
	ignore_event($EVENT_ID)
	set_event_par($EVENT_ID, 0, 2)
	$GC:=0
	while ($GC<16)
		if (%SL_CHAIN_REL_ARR[$GC] = 0)
			if (%KR_ENABLED[$GC] = 1)
				if (in_range($EVENT_NOTE, %KR_S[$GC], %KR_E[$GC]))
					if (%SL_FNP[$GC] = 0)
						set_event_par_arr($EVENT_ID, $EVENT_PAR_ALLOW_GROUP, %SL_ALLOW[$GC], $GC)
					end if
				end if
			end if
		end if
		inc($GC)
	end while
	play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1)
end on
