size = { 698, 405 }

defineProperty("avion_lit1", globalPropertyf("cl300/avion_lit1"))
defineProperty("avion_lit2", globalPropertyf("cl300/avion_lit2"))

list_texts  = {}
list_colors = {}
for i = 1,17 do
	list_texts[i]  = globalPropertys("cl300/cklst_line_"..i.."_text")
	list_colors[i] = globalPropertyi("cl300/cklst_line_"..i.."_color")
	defineProperty("cklst_line_"..i.."_text",  list_texts[i])
	defineProperty("cklst_line_"..i.."_color", list_colors[i])
end
defineProperty("cklst_line_title_text", globalPropertys("cl300/cklst_line_title_text"))


local font1 = loadFont('font_16.fnt')

cklst_page   = 0
cklst_row    = 1
cklst_max    = 0
cklst_offset = 0

color_white  = {1,  1,  1,  0xFFFFFF}
color_purple = {0.8,0.4,0.8,0xCC66CC}
color_green  = {0,  0.8,0  ,0x00CC00}

colors = {color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white}
line   = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""}
title  = {""}

last_states = {-99, cklst_row, cklst_offset}


chklist_enter = findCommand("cl300/chklist_enter")
 function chklistHandler(phase)
     if 0 == phase then
		 colors[cklst_row] = color_green
		 cklst_row = cklst_row + 1
		 if cklst_row > cklst_max then cklst_row = 1 end
		 colors[cklst_row] = color_purple
	 end
     return 1
 end

 registerCommandHandler(chklist_enter, 1, chklistHandler);

chklist_skip = findCommand("cl300/chklist_skip")
 function chklist2Handler(phase)
     if 0 == phase then
		 colors[cklst_row] = color_white
		 cklst_row = cklst_row + 1
		 if cklst_row > cklst_max then cklst_row = 1 end
		 colors[cklst_row] = color_purple
     end
     return 1
 end

 registerCommandHandler(chklist_skip, 1, chklist2Handler);

chklist_jright = findCommand("cl300/chklist_jright")
 function chklistjrightHandler(phase)
     if 0 == phase then
		cklst_page = cklst_page + 1
		if cklst_page > 12 then cklst_page = 0 end
     end
     return 1
 end

 registerCommandHandler(chklist_jright, 1, chklistjrightHandler);

chklist_jleft = findCommand("cl300/chklist_jleft")
 function chklistjleftHandler(phase)
     if 0 == phase then
		cklst_page = cklst_page - 1
		if cklst_page < 0 then cklst_page = 12 end
     end
     return 1
 end

 registerCommandHandler(chklist_jleft, 1, chklistjleftHandler);

function update()

cklst_offset = (cklst_row > 17 and 17 or 0)

if last_states[1] ~= cklst_page then

	cklst_row = 1
	colors = {color_purple,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white,color_white}

	if get(cklst_page) == 0 then
		title = "                APU START"
		line[1] = "DOCS/ SAFETY EQP / VOR                            CHECKED"
		line[2] = "SWITCHES / CBS                                               CHECKED"
		line[3] = "GEAR HANDLE                                                         DOWN"
		line[4] = "L / R AND AUX HYD PUMPS                                       OFF"
		line[5] = "AIR SOURCE                                                                  OFF"
		line[6] = "STBY INST SWITCH                                ON/60 SEC MIN"
		line[7] = "L BATT                                                  ON/20 VOLTS MIN"
		line[8] = "R BATT                                                                               ON"
		line[9] = "CAS                                              APU/NORMAL FAULTS"
		line[10] = "LIGHTS                                                             NAV/AS REQ"
		line[11] = "L/R FUEL PUMPS                                                       AUTO"
		line[12] = "APU                                                                              START"
		line[13] = "ELECTRICS                                                               CHECK"
		line[14] = "L AND R BLEEDS                                                         OFF"
		line[15] = "APU BLEED                                                                      ON"
		line[16] = "XBLEED                                                                        OPEN"
		line[17] = "AIR SOURCE                                                              NORM"
		cklst_max = 17

	end

	if get(cklst_page) == 1 then
		if cklst_row < 18 then
		title = "            INITIAL CHECK 1/2"
		else
		title = "            INITIAL CHECK 2/2"
		end
		line[1] = "O2 MASKS/GOOGLES                                     CHECKED"
		line[2] = "OXYGEN QUANTITY                                              CHECK"
		line[3] = "FUEL QUANTITY                                                     CHECK"
		line[4] = "STBY INSTRUMENT                                         CHECKED"
		line[5] = "CLOCK                                                                        RESET"
		line[6] = "NWS                                                                                  OFF"
		line[7] = "RAM AIR                                                   CHECKED / OFF"
		line[8] = "AIR COND/BLEEDS                                 CHECKED/SET"
		line[9] = "PRESSURIZATION PANEL                SET/LNDG ELEV"
		line[10] = "ANTI-ICE                                      WING SOURCE NORM"
		line[11] = "PROBES                                                                          OFF"
		line[12] = "REVERSION PANEL                                           NORMAL"
		line[13] = "LTS/EMER LTS                                               SET/ARMED"
		line[14] = "L/R HYD PUMP/BRAKES                                      ON/SET"
		line[15] = "AUX HYD PUMP                                       AUTO/CHEKED"
		line[16] = "HYD PANEL                                                                 AUTO"
		line[17] = "GUST LOCK                                                                     ON"
		line[18] = "ENGINE RUN SWITCHES                                            OFF"
		line[19] = "TRIM SYSTEM                                       CHEKED/PRI/SET"
		line[20] = "SYSTEMS TEST                                              COMPLETE"
		line[21] = "ELT                                                                             AS REQ"
		line[22] = "CABIN CONTROL PANEL                                   AS REQ"
		line[23] = "PAX OXYGEN PANEL                                               AUTO"
		line[24] = "ALTIMETERS                                                                   SET"
		line[25] = "FMS                                                              INITIALIZE/SET"
		line[26] = "AVIONICS                                                                         SET"
		line[27] = "V-SPEEDS                                                               POSTED"
		line[28] = "CAS MESSAGES                                               CHECKED"
		line[29] = "AIRSHOW/LGTS/GALLEY                           ON/AS REQ"
		cklst_max = 29

	end


	if get(cklst_page) == 2 then
		title = "             ENGINE START"
		line[1] = "WX/CLEARANCE/FMS                     BRIEFED/LOADED"
		line[2] = "DOOR/BCN/SIGNS                                CLOSED/ON/ON"
		line[3] = "PARK/EMER BRAKE                   SET/PRESS>2000 PSI"
		line[4] = "CHOCKS                                                              REMOVED"
		line[5] = "GUST LOCK                                                                   OFF"
		line[6] = "THRUST LEVERS                                                         IDLE"
		line[7] = "ENGINE RUN SWITCHES                                             ON"
		line[8] = "ENGINE START (<350°)                            AREA CLEAR"
		cklst_max = 8
	end


	if get(cklst_page) == 3 then
		title = "             AFTER START"
		line[1] = "YAW DAMPER                                                                  ON"
		line[2] = "WING ANTI-ICE                                   ON (2 MIN CHECK)"
		line[3] = "WINDSHIELD HEATS                                                      ON"
		line[4] = "FLAPS                                                SET FOR TAKE OFF"
		line[5] = "HYDRAULICS                                                      CHECKED"
		line[6] = "FLT AND GND SPOILERS                   CHECKED/AUTO"
		line[7] = "FLIGHT CONTROLS                                         CHECKED"
		line[8] = "GENS & ELECTRICS                                         CHECKED"
		line[9] = "TRIMS                                                   CALCULATED/SET"
		line[10] = "PFD BRIEF                                                        COMPLETE"
		line[11] = "DEPARTURE BRIEF                                       COMPLETE"
		line[12] = "RADAR/TERRAIN                                                   AS REQ"
		line[13] = "ANTI-ICE PANEL                               CHECKED/AS REQ"
		line[14] = "PASSENGERS                                                       BRIEFED"
		line[15] = "NWS                                                                                     ON"
		line[16] = "CAS                                                                        CHECKED"
		cklst_max = 16
	end

	if get(cklst_page) == 4 then
		title = "                      TAXI"
		line[1] = "BRAKES/NWS                                                          CHECKED"
		line[2] = "FUEL BALANCE                                                      CHECKED"
		line[3] = "THRUST REV                                                            CHECKED"
		line[4] = "ENGINE INST & N1 BUGS          CHCKED & AGREE < 1 %"
		line[5] = "FLAPS                                                       SET FOR TAKEOFF"
		cklst_max = 5
	end

	if get(cklst_page) == 5 then
		title = "                   LINE-UP"
		line[1] = "CABIN SIGN                                                           CYCLE / ON"
		line[2] = "PROBES / ANTI-ICE                                            ON / AS REQ"
		line[3] = "LIGHTS & STROBES                                          ON / AS REQ"
		line[4] = "TRANSPONDER                                                                    ON"
		line[5] = "CAS                                                     CHECKED & STACKED"
		cklst_max = 5
	end


	if get(cklst_page) == 6 then
		title = "           AFTER TAKE-OFF"
		line[1] = "LDG GEAR & FLAPS                                                       UP"
		line[2] = "TAXI LGT                                                                         OFF"
		line[3] = "ANTI-ICE                                                                     AS REQ"
		line[4] = "THRUST LEVERS                  CLIMB DETENT / AS REQ"
		line[5] = "L / R HYDRAULIC PUMPS                                           OFF"
		line[6] = "BLEEDS                                   TRANSFERRED / AS REQ"
		line[7] = "PRESSURIZATION                                             CHECKED"
		line[8] = "APU                                                  SHUTDOWN / AS REQ"
		line[9] = "CAS                                                                         CHECKED"
		cklst_max = 9
	end



	if get(cklst_page) == 7 then
		title = "  FL 180 CLIMB / TRANS ALT"
		line[1] = "ALTIMITERS                                                                     SET"
		line[2] = "O2 MASK                                                               CHECKED"
		line[3] = "CABIN SIGN                                                               AS REQ"
		line[4] = "EXTERIOR LIGHTS                                                 AS REQ"
		cklst_max = 4
	end

	if get(cklst_page) == 8 then
		title = "    18,000 FT / TRANS LVL"
		line[1] = "ALTIMITERS                                                                     SET"
		line[2] = "FUEL QTY / BALANCE                                     CHECKED"
		line[3] = "LANDING ELEV                                              CONFIRMED"
		line[4] = "ANTI-ICE                                                                    AS REQ"
		line[5] = "EXTERIOR LIGHTS                                                         ON"
		line[6] = "LANDING DATA                                                    POSTED"
		line[7] = "LANDING / MFD BRIEF                                  COMPLETE"
		cklst_max = 7
	end


	if get(cklst_page) == 9 then
		title = "          APPROACH 10,000 FT"
		line[1] = "APU & BLEEDS                                                        AS REQ"
		line[2] = "CABIN SIGN                                                              AS REQ"
		line[3] = "L/R HYDRALIC PUMPS                                             AUTO"
		cklst_max = 3
	end


	if get(cklst_page) == 10 then
		title = "          BEFORE LANDING"
		line[1] = "LANDING GEAR                                    DOWN, 3 GREEN"
		line[2] = "N LDG LIGHT                             ON / CLEARED TO LND"
		line[3] = "FLAPS                                                                                SET"
		line[4] = "CAS                                                                        CHECKED"
		cklst_max = 4
	end

	if get(cklst_page) == 11 then
		title = "           AFTER LANDING"
		line[1] = "PROBES                                                                            OFF"
		line[2] = "WINDSHIELD HEAT                                    OFF / AS REQ"
		line[3] = "ANTI-ICE                                                         OFF / AS REQ"
		line[4] = "FLAPS                                                              RETRACTED"
		line[5] = "LIGHTS / STROBES                                              AS / REQ"
		line[6] = "TRANSPONDER                                        STBY / AS REQ"
		cklst_max = 6
	end


	if get(cklst_page) == 12 then
		title = "                 SHUTDOWN"
		line[1] = "CHOCKS / BRAKES                                                      SET"
		line[2] = "NWS                                                                                  OFF"
		line[3] = "ENGINE BLEEDS                                                          OFF"
		line[4] = "ENGINE RUN SWITCHES                                           OFF"
		line[5] = "ANTI-ICE / WINDSHIELD HEAT                                OFF"
		line[6] = "L / R PTU HYDRAULICS                                             OFF"
		line[7] = "AUX HYD                                    OFF (R HYD < 1800 PSI)"
		line[8] = "GUST LOCK                                                                    ON"
		line[9] = "COCKPIT LIGHTNING                      RESET FOR 'DAY'"
		line[10] = "APU                                                                  SHUTDOWN"
		line[11] = "AIR SOURCE                                                                 OFF"
		line[12] = "LTS / EMER LTS                                                           OFF"
		line[13] = "STBY INST                                                                     OFF"
		line[14] = "L / R BATT                                                                      OFF"
		line[15] = "PARK BRAKE                                                         AS REQ"
		line[16] = "CABIN                                            CLEAN / RESTOCKED"
		cklst_max = 16
	end
	
	-- clear empty rows
	if cklst_max < 17 then
		for i = cklst_max+1,17 do
			line[i] = ""
		end
	elseif cklst_max > 17 then
		for i = cklst_max+1,34 do
			line[i] = ""
		end
	end
	
	set(cklst_line_title_text,  title)
	for i = 1,17 do
		set(list_colors[i], colors[i][4])
		set(list_texts[i],  line[i])
	end
	
end -- if page changed

if last_states[3] ~= cklst_offset then
	
	set(cklst_line_title_text,  title)
	for i = 1,17 do
		set(list_colors[i], colors[cklst_offset+i][4])
		set(list_texts[i],  line[cklst_offset+i])
	end

elseif last_states[2] ~= cklst_row then
	
	-- update last and current row
	set(list_colors[cklst_row     - cklst_offset], colors[cklst_row][4])
	set(list_colors[last_states[2]- cklst_offset], colors[last_states[2]][4])
	set(list_texts[ cklst_row     - cklst_offset], line[cklst_row])
	set(list_texts[ last_states[2]- cklst_offset], line[last_states[2]])
	
end -- if row changed

last_states = {cklst_page, cklst_row, cklst_offset}

end

components = {
	rectangle {
        position = { 0, 0, 698, 405 };
        color = { 0.0, 0.0, 0.0, 1.0 };
		visible = function ()
		return get(avion_lit1) == 0 and get(avion_lit2) == 0
		end,
    },
};

function getColor(r)
	if r == cklst_row then
		return color_purple
	else
		return colors[r]
	end
end

function draw()

drawText(font1, 350, 346, title,  color_white[1],color_white[2],color_white[3])

for i = 1,17 do
	drawText(font1, 280, (18-i)*18+10, line[cklst_offset+i], colors[cklst_offset+i][1],colors[cklst_offset+i][2],colors[cklst_offset+i][3])
end

drawAll(components)

end
