REBOL [
    title: "Daylight Map"
    author: "Christopher Ross-Gill"
    date: 11-Mar-2002
    version: 0.5.0
    type: 'link-app
    rights: http://creativecommons.org/licenses/by-sa/3.0/
]

time: now/time
map-path: http://www.ross-gill.com/r/daylight
bg-metal: any [load-image http://www.ross-gill.com/r/bg-metal.png 187.187.187]
btn-up.png: any [load-image http://www.ross-gill.com/r/btn-up.png :btn-up.png]
btn-dn.png: any [load-image http://www.ross-gill.com/r/btn-dn.png :btn-dn.png]

ctx-map: context [
    day: load-image map-path/map.jpg
    night: load-image map-path/map-night.jpg
    shade: load-image map-path/map-shading.png
    zones: load-image map-path/map-zones.png
    pin: load #{
        47494638396104000400B30000000000FF9933EEEEEE1400039F722740101840
        10180000020005C4CC535040111F4060AE401130CC535040459200000121F904
        01000002002C00000000040004000004085000204398964E11003B
    }
    set 'user-id map-path/user-id.r
    locations: load read map-path/locations.r
    set 'people copy []
    foreach [person location] locations [append people person]
]

ctx-time: context [
    time: now - now/zone
    gmt: time/time
    gmt-hour: gmt/hour

    if gmt-hour < 0 [gmt-hour: gmt-hour + 24]
    if gmt-hour > 23 [gmt-hour: gmt-hour - 24]

    set 'shade-offset to-pair reduce [14 * (12 - gmt-hour) 0]
    if greater? shade-offset/x 0 [
        shade-offset/x: shade-offset/x - 342
    ]

    zone-map: [
        255.0.0 -11:00 255.153.0 -10:00 255.255.0 -9:00
        153.255.0 -8:00 0.255.0 -7:00 0.255.153 -6:00
        0.255.255 -5:00 0.153.255 -4:00 0.0.255 -3:00
        153.0.255 -2:00 255.0.255 -1:00 255.0.153 +0:00
        204.0.0 +1:00 204.102.0 +2:00 204.204.0 +3:00
        102.204.0 +4:00 0.204.0 +5:00 0.204.102 +6:00
        0.204.204 +7:00 0.102.204 +8:00 0.0.204 +9:00
        102.0.204 +10:00 204.0.204 +11:00 204.0.102 +12:00
        102.102.0 +3:30 0.102.0 +5:30 0.0.102 +9:30 102.0.102 +13:00
    ]

    own-time: true
    set 'get-time has [gmt zone zone-offset zone-time][
        gmt: now - now/zone
        either own-time [
            out: gmt + now/zone
        ][
            zone: pick ctx-map/zones ((pin/offset/y - 1) * 342) + pin/offset/x
            if none? zone-offset: select zone-map zone [
                zone-offset: 0:00
            ]
            zone-time: gmt + zone-offset
            out: zone-time
        ]
        out: mold out/time
        if 7 > length? out [append out ":00"]
        return out
    ]
]

map-face: center-face layout compose [
    backdrop bg-metal effect [tile]
    space 10
    image 344x173 ctx-map/day edge [size: 1x1 color: black]
    with [pane: reduce [
        make-face/spec 'image [offset: shade-offset image: ctx-map/shade]
        make-face/spec 'image [offset: shade-offset + 342x0 image: ctx-map/shade]
        pin: make-face/spec 'image [offset: 93x44 image: ctx-map/pin]
        edit-user: make-face/spec 'image [
            offset: 0x0
            image: ctx-map/zones
            user-data: false
            effect: [alphamul 0]
            feel: make feel [
                engage: func [face action event][
                    if face/user-data [
                        pin/offset: event/offset - 2x2 show pin
                        oset/text: mold event/offset - 2x2
                        show oset
                    ]
                ]
            ]
        ]
    ]]

    across
    pin?: btn "Show Pin" [
        edit-user/user-data: false
        either pin/show? [
            ctx-time/own-time: true
            pin?/text: "Show Pin"
            tool-panel/pane: none
            hide pin
            show pin?
            show tool-panel
            edit-user/effect: [alphamul 0] show edit-user
        ][
            ctx-time/own-time: false
            pin?/text: "Hide Pin"
            show pin
            show pin?
            show tool-panel
        ]
    ]
    get-coords: btn "Get Coordinates" [
        ctx-time/own-time: false
        clock/text: get-time
        show clock

        edit-user/user-data: true
        edit-user/effect: [alphamul 34] show edit-user
        pin?/text: "Hide Pin"
        tool-panel/pane: x-coords
        oset/text: mold pin/offset
        show pin
        show pin?
        show tool-panel
    ]
    clock: text bold 120 get-time rate 1 feel [
        engage: func [face action event][
            face/text: get-time
            show face
        ]
    ]
    return
    tool-panel: box 342x80
    below return
    user-list: text-list 200x300 data people [
        pin/offset: select ctx-map/locations user-list/picked
        if pin/show? [show pin]
        clock/text: get-time
        show clock
    ]
]

x-coords: make-face/spec 'text [
    size: 342x70
    text: {^/^/Drag the pin and use the coordinates to add yourself to the 'pin list' (daylight/locations).}
    pane: reduce [
       oset: make-face/spec 'text [size: 100x25 text: "0x0"]
    ]
    append flags 'as-is
]

if exists? user-id [hide get-coords]
pin/show?: false

view map-face