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}
nav1_dme = ""
nav2_dme = ""

-- JackZero: Show the active NAV SOURCE on the left side (so it's visible on the PFD as well):
offsets = {0,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

-- JackZero: Show the active NAV SOURCE on the left side (so it's visible on the PFD as well):
if get(HSI_source_select_pilot) == 0 then
    offsets = {1460, 1575, 1680} -- NAV1 NAV2 FMS
elseif get(HSI_source_select_pilot) == 1 then
    offsets = {1575, 1460, 1680} -- NAV2 NAV1 FMS
else
    offsets = {1575, 1680, 1460} -- FMS NAV1 NAV2
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)

-- NAV1
drawText(font1, offsets[1],    1617, title[1],  color[1][1], color[1][2], color[1][3])
drawText(font1, offsets[1],    1599, nav1_id,  color[1][1], color[1][2], color[1][3])
drawText(font2, offsets[1]-30, 1581, "DME                 NM",  color[1][1], color[1][2], color[1][3])
drawText(font1, offsets[1]-8,  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])

-- NAV2
drawText(font1, offsets[2],    1617, title[2],  color[2][1], color[2][2], color[2][3])
drawText(font1, offsets[2],    1599, nav2_id,  color[2][1], color[2][2], color[2][3])
drawText(font2, offsets[2]-30, 1581, "DME                 NM",  color[2][1], color[2][2], color[2][3])
drawText(font1, offsets[2]-8,  1581, nav2_dme,  color[2][1], color[2][2], color[2][3])

-- FMS
drawText(font1, offsets[3],    1617, title[3],  color[3][1], color[3][2], color[3][3])
drawText(font1, offsets[3],    1599, zeros[1] .. gps_time_hours .. ":" .. zeros[2] .. gps_time_min,  color[3][1], color[3][2], color[3][3])
drawText(font2, offsets[3]-10, 1581, "                    NM",  color[3][1], color[3][2], color[3][3])
drawText(font1, offsets[3]-10, 1581, gps_distance,  color[3][1], color[3][2], color[3][3])



end
