'Display the Incidents on a Simile Timeline
http://www.simile-widgets.org/wiki/Reference_Documentation_for_Timeline
@ToDo: Play button
http://www.simile-widgets.org/wiki/Timeline_Moving_the_Timeline_via_Javascript'

@staticmethod
def irs_timeline(r, **attr):

    if ((r.representation == 'html') and (r.name == 'ireport')):
        T = current.T
        db = current.db
        s3db = current.s3db
        request = current.request
        response = current.response
        s3 = response.s3
        itable = s3db.doc_image
        dtable = s3db.doc_document
        s3.scripts.append(('/%s/static/scripts/simile/timeline/timeline-api.js' % request.application))
        if s3.debug:
            s3.scripts.append(('/%s/static/scripts/S3/s3.timeline.js' % request.application))
        else:
            s3.scripts.append(('/%s/static/scripts/S3/s3.timeline.min.js' % request.application))
        if r.record:
            rows = [r.record]
        else:
            r.resource.load(limit=2000)
            rows = r.resource._rows
        data = {'dateTimeFormat': 'iso8601'}
        now = request.utcnow
        tl_start = tl_end = now
        events = []
        for row in rows:
            start = (row.datetime or '')
            if start:
                if (start < tl_start):
                    tl_start = start
                if (start > tl_end):
                    tl_end = start
                start = start.isoformat()
            end = (row.expiry or '')
            if end:
                if (end > tl_end):
                    tl_end = end
                end = end.isoformat()
            query = ((itable.deleted == False) & (itable.doc_id == row.doc_id))
            image = db(query).select(itable.url, limitby=(0, 1)).first()
            if image:
                image = (image.url or '')
            link = URL(args=[row.id])
            events.append({'start': start, 'end': end, 'title': row.name, 'caption': (row.message or ''), 'description': (row.message or ''), 'image': (image or ''), 'link': (link or '')})
        data['events'] = events
        data = json.dumps(data, separators=SEPARATORS)
        code = ''.join(('S3.timeline.data=', data, '\nS3.timeline.tl_start="', tl_start.isoformat(), '"\nS3.timeline.tl_end="', tl_end.isoformat(), '"\nS3.timeline.now="', now.isoformat(), '"\n'))
        s3.js_global.append(code)
        item = DIV(_id='s3timeline', _class='s3-timeline')
        output = dict(item=item)
        if attr.get('rheader'):
            rheader = attr['rheader'](r)
            if rheader:
                output['rheader'] = rheader
        output['title'] = T('Incident    Timeline')
        response.view = 'timeline.html'
        return output
    else:
        raise HTTP(405, current.ERROR.BAD_METHOD)
