content: sub init()
      m.video = m.top.findNode("video")
      m.controlsOverlay = m.top.findNode("controlsOverlay")
      m.videoTitle = m.top.findNode("videoTitle")
      m.timeDisplay = m.top.findNode("timeDisplay")
      
      m.video.observeField("state", "onVideoStateChanged")
      m.video.observeField("position", "onPositionChanged")
      
      m.top.observeField("videoContent", "onVideoContentChanged")
      
      ' Controls timer
      m.controlsTimer = CreateObject("roSGNode", "Timer")
      m.controlsTimer.duration = 3
      m.controlsTimer.observeField("fire", "hideControls")
  end sub
  
  sub onVideoContentChanged(event as Object)
      videoData = event.getData()
      if videoData <> invalid and videoData.content <> invalid
          m.video.content = videoData.content
          m.videoTitle.text = videoData.content.title
          m.video.control = "play"
          m.video.setFocus(true)
          
          ' Show controls initially
          showControls()
      end if
  end sub
  
  sub onVideoStateChanged(event as Object)
      state = event.getData()
      print "Video state changed to: "; state
      
      if state = "finished" or state = "error"
          m.top.finished = true
      end if
  end sub
  
  sub onPositionChanged(event as Object)
      position = event.getData()
      duration = m.video.duration
      
      if duration > 0
          posMinutes = int(position / 60)
          posSeconds = int(position) mod 60
          durMinutes = int(duration / 60)
          durSeconds = int(duration) mod 60
          
          timeString = stri(posMinutes).trim() + ":" + right("0" + stri(posSeconds).trim(), 2) + " / " + stri(durMinutes).trim() + ":" + right("0" + stri(durSeconds).trim(), 2)
          m.timeDisplay.text = timeString
      end if
  end sub
  
  sub showControls()
      m.controlsOverlay.visible = true
      m.controlsTimer.control = "stop"
      m.controlsTimer.control = "start"
  end sub
  
  sub hideControls(event as Object)
      m.controlsOverlay.visible = false
  end sub
  
  function onKeyEvent(key as String, press as Boolean) as Boolean
      handled = false
      
      if press
          if key = "OK" or key = "play" or key = "pause"
              showControls()
              handled = false ' Let video handle the key
          else if key = "back"
              m.video.control = "stop"
              m.top.finished = true
              handled = true
          else if key = "left" or key = "right" or key = "fastforward" or key = "rewind"
              showControls()
              handled = false ' Let video handle the key
          else if key = "up" or key = "down"
              showControls()
              handled = true
          else
              showControls()
          end if
      end if
      
      return handled
  end function
  