require "js"
  
document = JS.global[:document]
button = document.getElementById("button")
target = document.getElementById("time")
result = document.getElementById("result")
button.addEventListener "click" do |e|
  keisan(target, result)
end

def keisan(target, result)
  today = Time.now + 9 * 60 * 60
  now = Time.new(today.year, today.month, today.day, today.hour, today.min, 0, "+09:00")
  hour = target[:value].split(":")[0].to_i
  min = target[:value].split(":")[1].to_i
  if today.hour >= hour && today.min >= min 
    day = now + 24 * 60 * 60
    target_date = Time.new(now.year, day.month, day.day, hour, min, 0, "+09:00")
  else
    target_date = Time.new(now.year, now.month, now.day, hour, min, 0, "+09:00")
  end
  if (target_date - now) < 0
    day = now + 24 * 60 * 60
    target_date = Time.new(now.year, day.month, day.day, hour, min, 0, "+09:00")
  end
  after_hour = ((target_date - now) / 60 / 60).to_i
  after_min = ((target_date - now) /60 % 60).to_i
  result[:innerText] = "#{after_hour}時間#{after_min}分後"
end
