 ###
 class Time
   value: 0 # time in milliseconds
-  constructor: (arg) ->
-    if typeof(arg) == 'number'
-      @value = arg
-    else
-      @value = arg.value
+
+  # creates a new Time object from either
+  # - number of milliseconds
+  # - a time-arithmetic string (using the given fps and tempo)
+  # - another Time object's value
+  constructor: (arg=0, fps, tempo) ->
+    @value = switch typeof(arg)
+      when 'number' then arg
+      when 'string' then @eval(arg, fps, tempo)
+      else arg.value
 
   # adds the given time object or millisecond value
   add: (time) -> @value += if typeof(time) == 'number' then time else time.value


  clone: -> new Time(@value)
  equals: (time) -> @value == if typeof(time) == 'number' then time else time.value
  toString: -> "Time(#{@value}ms)"

  # Returns its position relative to the given Timespan.
  normalizedTo: (span) -> math.fit(@value, span.from.value, span.to.value, 0, 1)

  toFrame: (fps=60) -> Math.round(@value / (1000 / fps))

  # factory methods
  @s: (seconds) -> new Time seconds * 1000
  @ms: (milliseconds) -> new Time milliseconds





    segments

  # checks wether this timespan intersects with the given one
  overlaps: (other) ->
    from = @from.value
    to = @to.value
    from2 = other.from.value
    to2 = other.to.value

    (to2 >= from and to2 <= to) or
    (from2 >= from and from2 <= to) or
    (from2 <= from and to2 >= to)

  clone: -> new Timespan(@from, @to)
  toString: -> "Timespan(#{@from} - #{@to})"

