// vim: et:ts=4:sw=4:sts=4:ft=javascript

'use strict';

Ox.BlockVideoTimeline = function(options, self) {

    self = self || {};
    var that = Ox.Element({}, self)
        .defaults({
            duration: 0,
            find: '',
            getImageURL: null,
            'in': 0,
            out: 0,
            position: 0,
            results: [],
            showPointMarkers: false,
            showSubtitles: false,
            subtitles: [],
            width: 0
        })
        .options(options || {})
        .addClass('OxBlockVideoTimeline')
        .css({
            position: 'absolute',
        })
        .bind({
            mousedown: mousedown,
            mouseleave: mouseleave,
            mousemove: mousemove
        })
        .bindEvent({
            drag: function(data) {
                mousedown(data);
            }
        });

    self.$images = [];
    self.$interfaces = [];
    self.$lines = [];
    self.$tooltip = Ox.Tooltip({
        animate: false
    }).css({
        textAlign: 'center'
    });
    self.height = 16;
    self.lines = getLines();
    self.margin = 8;

    setCSS();

    self.$image = Ox.SmallVideoTimelineImage({
            duration: self.options.duration,
            editing: self.options.editing,
            'in': self.options['in'],
            out: self.options.out,
            results: self.options.results,
            subtitles: self.options.showSubtitles ? self.options.subtitles : [],
            timeline: self.options.getImageURL,
            width: Math.round(self.options.duration),
            type: self.options.type
        });

    Ox.loop(self.lines, function(i) {
        addLine(i);
    });

    self.$positionMarker = $('<img>')
        .attr({
            src: Ox.UI.getImageURL('markerPosition')
        })
        .addClass('OxMarkerPosition')
        .appendTo(that.$element);
    setPositionMarker();

    if (self.options.showPointMarkers) {
        self.$pointMarker = {};
        ['in', 'out'].forEach(function(point) {
            var titlecase = Ox.toTitleCase(point);
            self.$pointMarker[point] = $('<img>')
                .addClass('OxMarkerPoint' + titlecase)
                .attr({
                    src: Ox.UI.getImageURL('marker' + titlecase)
                })
                .appendTo(that.$element);
            setPointMarker(point);
        });
    }

    function addLine(i) {
        self.$lines[i] = $('<div>')
            .css({
                position: 'absolute',
                left: (self.margin / 2) + 'px',
                top: i * (self.height + self.margin) + 'px',
                width: self.options.width + 'px',
                height: '24px',
                overflow: 'hidden'
            })
            .appendTo(that.$element);
        self.$images[i] = self.$image.clone()
            .css({
                position: 'absolute',
                marginLeft: (-i * self.options.width) + 'px'
            })
            .appendTo(self.$lines[i]);
        self.$interfaces[i] = $('<div>')
            // OxTarget and OxSpecialTarget are needed for InfoList
            .addClass('OxInterface OxTarget OxSpecialTarget')
            .css({
                top: '2px',
                width:  Math.round(self.options.duration) + 'px',
                height: '20px',
                marginLeft: (-i * self.options.width) + 'px',
                //background: 'rgba(255, 0, 0, 0.1)',
            })
            .appendTo(self.$lines[i]);
    }

    function getLines() {
        return Math.ceil(self.options.duration / self.options.width);
    }

    function getPosition(e) {
        //FIXME: this might still be broken in opera according to http://acko.net/blog/mouse-handling-and-absolute-positions-in-javascript
        return (e.offsetX ? e.offsetX : e.clientX - $(e.target).offset().left);
    }

    function getSubtitle(position) {
        var subtitle = '';
        Ox.forEach(self.options.subtitles, function(v) {
            if (v['in'] <= position && v.out > position) {
                subtitle = v;
                return false;
            }
        });
        return subtitle;
    }

    function mousedown(e) {
        if ($(e.target).is('.OxInterface')) {
            self.options.position = getPosition(e);
            setPositionMarker();
            // fixme: check if this pattern is better
            // than the one used for list selection
            if (!self.triggered) {
                Ox.Log('Video', 'trigger............')
                that.triggerEvent('position', {
                    position: self.options.position
                });
                self.triggered = true;
                setTimeout(function() {
                    self.triggered = false;
                }, 250);
            }
        }
    }

    function mouseleave() {
        self.$tooltip.hide();
    }

    function mousemove(e) {
        var position, subtitle;
        if ($(e.target).is('.OxInterface')) {
            position = getPosition(e);
            subtitle = getSubtitle(position);
            self.$tooltip.options({
                    title: subtitle ?
                        '<span class=\'OxBright\'>' +
                        Ox.highlight(subtitle.text, self.options.find, 'OxHighlight').replace(/\n/g, '<br/>') +
                        '</span><br/>' +
                        Ox.formatDuration(subtitle['in'], 3) + ' - ' + Ox.formatDuration(subtitle['out'], 3) :
                        Ox.formatDuration(position)
                })
                .show(e.clientX, e.clientY);
        } else {
            self.$tooltip.hide();
        }
    }

    function setCSS() {
        that.css({
			width: (self.options.width + self.margin) + 'px',
			height: ((self.height + self.margin) * self.lines) + 4 + 'px'
			// fixme: the + 4 represent the margin-bottom in the video editor
			// is there a better way to get a proper margin-bottom?
        });
    }

    function setPoint(point) {
        setPointMarker(point);
        self.$image.options(point, self.options[point]);
        updateTimelines();
    }

    function setPointMarker(point) {
        var position = Math.round(self.options[point]);
        self.$pointMarker[point].css({
            left: (position % self.options.width) + 'px',
            top: (parseInt(position / self.options.width) *
                (self.height + self.margin) + 15) + 'px'
        });
    }

    function setPositionMarker() {
        var position = Math.round(self.options.position);
        self.$positionMarker.css({
            left: (position % self.options.width) - 1 + 'px',
            top: (parseInt(position / self.options.width) *
                (self.height + self.margin) + 2) + 'px'
        });
    }

    function setResults() {
        self.$image.options({results: self.options.results});
        updateTimelines();
    }

    function setWidth() {
        self.lines = getLines();
		setCSS();
		Ox.loop(self.lines, function(i) {
            if (self.$lines[i]) {
                self.$lines[i].css({
                    width: self.options.width + 'px'
                });
                self.$images[i].css({
                    marginLeft: (-i * self.options.width) + 'px'
                });
                self.$interfaces[i].css({
                    marginLeft: (-i * self.options.width) + 'px'
                });
            } else {
                addLine(i);
            }
        });
        while (self.$lines.length > self.lines) {
            self.$lines[self.$lines.length - 1].remove();
            self.$lines.pop();
            self.$images.pop();
        }
        setPositionMarker();
        if (self.options.showPointMarkers) {
            setPointMarker('in');
            setPointMarker('out');
        }
    }

    function updateTimelines() {
        self.$lines.forEach(function($line, i) {
            $($line.children()[0]).replaceWith(
                self.$images[i] = self.$image.clone()
                    .css({
                        position: 'absolute',
                        marginLeft: (-i * self.options.width) + 'px'
                    })
            );
        });
    }

    self.setOption = function(key, value) {
        if (key == 'in' || key == 'out') {
            self.options.showPointMarkers && setPoint(key);
        } else if (key == 'position') {
            setPositionMarker();
        } else if (key == 'results') {
            setResults();
        } else if (key == 'width') {
            setWidth();
        }
    };

    return that;

};
