size = { 2048, 2048 }



defineProperty("altitude_dial_ft", globalPropertyi("sim/cockpit2/autopilot/altitude_dial_ft"))
defineProperty("nav1_freq_hz", globalPropertyi("sim/cockpit/radios/nav1_freq_hz"))
defineProperty("nav1_has_dme", globalPropertyi("sim/cockpit2/radios/indicators/nav1_has_dme"))
defineProperty("nav1_nav_id1", globalPropertys("sim/cockpit2/radios/indicators/nav1_nav_id"))
defineProperty("nav1_dme_distance_nm", globalPropertyf("sim/cockpit2/radios/indicators/nav1_dme_distance_nm"))

defineProperty("nav2_freq_hz", globalPropertyi("sim/cockpit/radios/nav2_freq_hz"))
defineProperty("nav2_has_dme", globalPropertyi("sim/cockpit2/radios/indicators/nav2_has_dme"))
defineProperty("nav2_nav_id1", globalPropertys("sim/cockpit2/radios/indicators/nav2_nav_id"))
defineProperty("nav2_dme_distance_nm", globalPropertyf("sim/cockpit2/radios/indicators/nav2_dme_distance_nm"))

defineProperty("gps_nav_id", globalPropertys("sim/cockpit2/radios/indicators/gps_nav_id"))
defineProperty("gps_dme_distance_nm", globalPropertyf("sim/cockpit2/radios/indicators/gps_dme_distance_nm"))
defineProperty("gps_dme_time_secs", globalPropertyi("sim/cockpit/radios/gps_dme_time_secs"))
defineProperty("gps_has_dme", globalPropertyi("sim/cockpit2/radios/indicators/gps_has_dme"))
defineProperty("gps_dest_index", globalPropertyi("sim/cockpit/gps/destination_index"))

defineProperty("HSI_source_select_pilot", globalPropertyi("sim/cockpit2/radios/actuators/HSI_source_select_pilot"))


font1 = loadFont('font_3.fnt')
font2 = loadFont('font_4.fnt')

nav1_tab = {0,0,0,0,0}
nav2_tab = {0,0,0,0,0}
title = {"LOC1", "LOC2","FMS"}
color = {{0,0,0},{0,0,0},{1,1,1}}
nav1_id = get(nav1_nav_id1)
nav2_id = get(nav2_nav_id1)
gps_distance = get(gps_dme_distance_nm)
gps_time = {0,0}
gps_time_hours = 0
gps_time_min = 0
zeros = {0,0}



function splitbyelem(s)
local tb = {}
for i=1,string.len(s) do
tb[i] = string.sub(s, i,i)
end
 return tb
end

function update ()



nav1_tab = splitbyelem(get(nav1_freq_hz))
nav2_tab = splitbyelem(get(nav2_freq_hz))

if get(gps_dest_index) > 0 then
title[3] = get(gps_nav_id)

gps_time = {math.modf(get(gps_dme_time_secs)/60)}
if gps_time[1] < 100 then gps_time_hours = gps_time[1] else gps_time_hours = 99 end
gps_time_min = math.floor(get(gps_dme_time_secs) -  gps_time[1]*60)
if gps_time_hours > 9 then zeros[1] = "" else zeros[1] = "0" end
if gps_time_min > 9 then zeros[2] = "" else zeros[2] = "0" end
gps_distance = math.floor(get(gps_dme_distance_nm)*10)/10
	if math.floor(gps_distance) - gps_distance == 0 then
	gps_distance = string.format("%s%s", gps_distance,".0")
	end
if get(HSI_source_select_pilot) ~= 2 then color[3] = {0.2,1.0,0.2} else color[3] = {0.8, 0.0, 0.7, 1.0} end
else
color[3] = {1.0,1.0,1.0}
title[3] = "FMS"
gps_time_hours = "--"
gps_time_min = "--"
gps_distance = "  ---"
zeros = {"",""}
end


if get(nav1_has_dme) == 1 then
	if get(nav1_freq_hz) <= 11195 and nav1_tab[4] % 2 == 1 then title[1] = "LOC1" else title[1] = "VOR1" end
	nav1_dme = math.floor(get(nav1_dme_distance_nm)*10)/10
		if math.floor(nav1_dme) - nav1_dme == 0 then
		nav1_dme = string.format("%s%s", nav1_dme,".0")
		end
else

	title[1] = "LOC1"

	nav1_dme = " ---"
end

if get(nav2_has_dme) == 1 then
	if get(nav2_freq_hz) <= 11195 and nav2_tab[4] % 2 == 1 then title[2] = "LOC2" else title[2] = "VOR2" end
	nav2_dme = math.floor(get(nav2_dme_distance_nm)*10)/10
		if math.floor(nav2_dme) - nav2_dme == 0 then
		nav2_dme = string.format("%s%s", nav2_dme,".0")
		end
else
	title[2] = "LOC2"
	nav2_dme = " ---"
end

if # get(nav1_nav_id1) > 0 then
nav1_id = get(nav1_nav_id1)
	if get(HSI_source_select_pilot) ~= 0 then color[1] = {0.2,1.0,0.2} else color[1] = {0.8, 0.0, 0.7, 1.0} end
else
color[1] = {1.0,1.0,1.0}
nav1_id = "---"
end

if # get(nav2_nav_id1) > 0 then
nav2_id = get(nav2_nav_id1)
	if get(HSI_source_select_pilot) ~= 1 then color[2] = {0.2,1.0,0.2} else color[2] = {0.8, 0.0, 0.7, 1.0} end
else
color[2] = {1.0,1.0,1.0}
nav2_id = "---"
end


--print("nav1_tab[4]", nav1_tab[4])
end

components = {

	clickable {
       position = {1650,53,16,32},

       cursor = {
            x = -35,
            y = 20,
            width = 31,
            height = 41,
            shape = loadImage("arrows_big_left.png"),

        },

        onMouseClick = function()
		set(altitude_dial_ft, math.ceil(get(altitude_dial_ft)/100) * 100 - 100)
		return true
        end

    },

	clickable {
       position = {1668,53,16,32},

       cursor = {
            x = 10,
            y = 20,
            width = 31,
            height = 41,
            shape = loadImage("arrows_big_right.png"),

        },

        onMouseClick = function()
        set(altitude_dial_ft, math.floor(get(altitude_dial_ft)/100) * 100 + 100)
		return true
        end
    },

		rectangle {
        position = { 1467, 1577, 272, 54 };
        color = { 0.0, 0.0, 0.0, 1.0 };
		visible = true,
    },



}

function draw()

drawAll(components)

drawText(font1, 1575, 1617, title[1],  color[1][1], color[1][2], color[1][3])
drawText(font1, 1575, 1599, nav1_id,  color[1][1], color[1][2], color[1][3])
drawText(font2, 1545, 1581, "DME                 NM",  color[1][1], color[1][2], color[1][3])
drawText(font1, 1567, 1581, nav1_dme,  color[1][1], color[1][2], color[1][3])
--drawText(font2, 1567, 1596, get(xfmc_Status),  color[1][1], color[1][2], color[1][3])
--drawText(font2, 1567, 1593, table.concat(xfmc_state),  color[1][1], color[1][2], color[1][3])
drawText(font1, 1680, 1617, title[2],  color[2][1], color[2][2], color[2][3])
drawText(font1, 1680, 1599, nav2_id,  color[2][1], color[2][2], color[2][3])
drawText(font2, 1650, 1581, "DME                 NM",  color[2][1], color[2][2], color[2][3])
drawText(font1, 1672, 1581, nav2_dme,  color[2][1], color[2][2], color[2][3])
drawText(font1, 1460, 1617, title[3],  color[3][1], color[3][2], color[3][3])
drawText(font1, 1460, 1599, zeros[1] .. gps_time_hours .. ":" .. zeros[2] .. gps_time_min,  color[3][1], color[3][2], color[3][3])
drawText(font2, 1450, 1581, "                    NM",  color[3][1], color[3][2], color[3][3])
drawText(font1, 1450, 1581, gps_distance,  color[3][1], color[3][2], color[3][3])


end
