
function initialize(box)

	dofile(box:get_config("${Path_Data}") .. "/plugins/stimulation/lua-stimulator-stim-codes.lua")

	-- Settings
	number_of_trials = box:get_setting(2)
	wait_for_beep_duration = box:get_setting(3)
	wait_for_cue_duration = box:get_setting(4)
	display_cue_duration = box:get_setting(5)
	feedback_duration = box:get_setting(6)
	end_of_trial_min_duration = box:get_setting(7)
	end_of_trial_max_duration = box:get_setting(8)
	number_of_class = box:get_setting(9)
	classes = {}
	id = 9
	for i = 1, number_of_class do
		table.insert(classes,1,_G[box:get_setting(id+i)])
	end

	-- initializes random seed
	math.randomseed(os.time())

	-- fill the sequence table with predifined order
	sequence = {}
	for i = 1, number_of_trials do
		for j = 1, number_of_class do
			table.insert(sequence, 1, classes[j])
		end
	end

	-- randomize the sequence
	for i = 1, number_of_trials do
		a = math.random(1, number_of_trials*number_of_class)
		b = math.random(1, number_of_trials*number_of_class)
		swap = sequence[a]
		sequence[a] = sequence[b]
		sequence[b] = swap
	end

end

function process(box)

	local t=0

	-- manages baseline
	box:send_stimulation(1, OVTK_StimulationId_ExperimentStart, t, 0)
	t = t + 1

	-- manages trials
	for i = 1, number_of_trials*number_of_class do

		-- first display cross on screen
		box:send_stimulation(1, OVTK_GDF_Start_Of_Trial, t, 0)
		box:send_stimulation(1, OVTK_GDF_Cross_On_Screen, t, 0)
		t = t + wait_for_beep_duration

		-- warn the user the cue is going to appear
		box:send_stimulation(1, OVTK_StimulationId_Beep, t, 0)
		t = t + wait_for_cue_duration

		-- display cue
		box:send_stimulation(1, sequence[i], t, 0)
		t = t + display_cue_duration

		-- provide feedback
		box:send_stimulation(1, OVTK_GDF_Feedback_Continuous, t, 0)
		t = t + feedback_duration

		-- ends trial
		box:send_stimulation(1, OVTK_GDF_End_Of_Trial, t, 0)
		t = t + math.random(math.floor(end_of_trial_min_duration), math.ceil(end_of_trial_max_duration))

	end

	-- send end for completeness	
	box:send_stimulation(1, OVTK_GDF_End_Of_Session, t, 0)
	t = t + 1
	box:send_stimulation(1, OVTK_StimulationId_Train, t, 0)
	t = t + 1	

	-- used to cause the acquisition scenario to stop
	box:send_stimulation(1, OVTK_StimulationId_ExperimentStop, t, 0)
	
end
