ova.js 100 KB
Newer Older
daniel cebrian committed
1 2
/* 
Open Video Annotation v1.0 (http://openvideoannotation.org/)
3
Copyright (C) 2014 CHS (Harvard University), Daniel Cebrian Robles and Phil Desenne 
daniel cebrian committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
License: https://github.com/CtrHellenicStudies/OpenVideoAnnotation/blob/master/License.rst

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// ----------------Utilities---------------- //
var _ref;
var __bind = function(fn, me) { 
    return function() { 
        return fn.apply(me, arguments); 
    }; 
};
var __hasProp = {}.hasOwnProperty;
var __extends = function(child, parent) { 
    for (var key in parent) { 
        if (__hasProp.call(parent, key)) 
            child[key] = parent[key]; 
    } 
    function ctor() { 
        this.constructor = child; 
    } 

    ctor.prototype = parent.prototype; 
    child.prototype = new ctor(); 
    child.__super__ = parent.prototype; 
    return child; 
};
var createDateFromISO8601 = function(string) {
daniel cebrian committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  var d, date, offset, regexp, time, _ref;
  regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" + "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
  d = string.match(new RegExp(regexp));
  offset = 0;
  date = new Date(d[1], 0, 1);
  if (d[3]) {
    date.setMonth(d[3] - 1);
  }
  if (d[5]) {
    date.setDate(d[5]);
  }
  if (d[7]) {
    date.setHours(d[7]);
  }
  if (d[8]) {
    date.setMinutes(d[8]);
  }
  if (d[10]) {
    date.setSeconds(d[10]);
  }
  if (d[12]) {
    date.setMilliseconds(Number("0." + d[12]) * 1000);
  }
  if (d[14]) {
    offset = (Number(d[16]) * 60) + Number(d[17]);
    offset *= (_ref = d[15] === '-') != null ? _ref : {
      1: -1
    };
  }
  offset -= date.getTimezoneOffset();
  time = Number(date) + (offset * 60 * 1000);
  date.setTime(Number(time));
  return date;
};
77
var Util = typeof Util != 'undefined' ? Util : {};
daniel cebrian committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
Util.mousePosition = function(e, offsetEl) {
  var offset, _ref1;
  if ((_ref1 = $(offsetEl).css('position')) !== 'absolute' && _ref1 !== 'fixed' && _ref1 !== 'relative') {
    offsetEl = $(offsetEl).offsetParent()[0];
  }
  offset = $(offsetEl).offset();
  return {
    top: e.pageY - offset.top,
    left: e.pageX - offset.left
  };
};




93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
// ----------------Load videojs-Annotation Plugin---------------- //
(function () {
    // -- Load Annotation plugin in videojs
    function vjsAnnotation_(options){
        var player = this;
    
        // variables to know if it is ready
    
        player.annotations = new vjsAnnotation(player, options);
    
        // When the DOM, Range Slider and the video media is loaded
        function initialVideoFinished(event) {
            // -- wait for plugins -- //
            var wrapper = $('.annotator-wrapper').parent()[0];
            var annotator = $.data(wrapper, 'annotator');
            
            // wait for Annotator and the Share plugin
            if (typeof Annotator.Plugin["Share"] === 'function') {
                if (typeof annotator.isShareLoaded != 'undefined' && annotator.isShareLoaded) {
                    annotator.unsubscribe('shareloaded', initialVideoFinished);
                } else {
                    annotator.subscribe('shareloaded', initialVideoFinished);
                    return false;
                }
            }
        
            var plugin = player.annotations;
        
            // All components will be initialize after they have been loaded by videojs
            for (var index in plugin.components) {
                plugin.components[index].init_();
            }

            player.annotations.BigNewAn.show();
        
            // set the position of the big buttom
            plugin.setposBigNew(plugin.options.posBigNew);
        
            if(!options.showDisplay) 
                plugin.hideDisplay();
            if(!options.showStatistics) 
                plugin.hideStatistics();
        
        
            // Get current instance of annotator 
            player.annotator = annotator;
            plugin.annotator = annotator;
        
            // get annotations
            var allannotations = annotator.plugins['Store'].annotations;
            plugin.refreshDisplay();
        
            // -- Listener to Range Slider Plugin
            player.rangeslider.rstb.on('mousedown', function(){plugin._onMouseDownRS(event)});
            // Open the autoPlay from the API
            if (player.autoPlayAPI) {
                var OnePlay = function () {
                    player.annotations.showAnnotation(player.autoPlayAPI);
                    $('html, body').animate({
                        scrollTop: $("#" + player.id_).offset().top},
                        'slow');
                };
                if (player.techName == 'Youtube')
                    setTimeout(OnePlay, 100); // fix the delay playing youtube
                else
                    OnePlay();
            }
        
            // set the number of Annotations to display
            plugin.refreshDesignPanel();
        
            // check full-screen change
            player.on('fullscreenchange', function() {
                if (player.isFullScreen) {
                    $(player.annotator.wrapper[0]).addClass('vjs-fullscreen');
                } else {
                    $(player.annotator.wrapper[0]).removeClass('vjs-fullscreen');
                }
                plugin.refreshDesignPanel();
            });
        
            // loaded plugin
            plugin.loaded = true;
        }
        player.one('loadedRangeSlider', initialVideoFinished); // Loaded RangeSlider
    
        console.log("Loaded Annotation Plugin");
    }
    videojs.plugin('annotations', vjsAnnotation_);


    // -- Plugin
    function vjsAnnotation(player, options) {
        var player = player || this;
    
        this.player = player;
        
        this.components = {}; // holds any custom components we add to the player

        options = options || {}; // plugin options
        
        if(!options.hasOwnProperty('posBigNew')) 
            options.posBigNew = 'none'; // ul = up left || ur = up right || bl = below left || br = below right || c = center
        if(!options.hasOwnProperty('showDisplay')) 
            options.showDisplay = false; 
        if(!options.hasOwnProperty('showStatistics')) 
            options.showStatistics = false; 
        
        this.options = options;
    
        this.init();
    }
daniel cebrian committed
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    // -- Methods
    vjsAnnotation.prototype = {
        /* Constructor */
        init: function() {
            var player = this.player || {};
            var controlBar = player.controlBar;
            var seekBar = player.controlBar.progressControl.seekBar;
                
            this.updatePrecision = 3;
            
            // Components and Quick Aliases
            this.BigNewAn = this.components.BigNewAnnotation = player.BigNewAnnotation;
            this.AnConBut = this.components.AnContainerButtons = controlBar.AnContainerButtons;
            this.ShowSt = this.components.ShowStatistics = this.AnConBut.ShowStatistics;
            this.NewAn = this.components.NewAnnotation = this.AnConBut.NewAnnotation;
            this.ShowAn =this.components.ShowAnnotations = this.AnConBut.ShowAnnotations;
            this.BackAnDisplay = this.components.BackAnDisplay = controlBar.BackAnDisplay; // Background of the panel
            this.AnDisplay = this.components.AnDisplay = controlBar.BackAnDisplay.AnDisplay; // Panel with all the annotations
            this.AnStat = this.components.AnStat = controlBar.BackAnDisplay.AnStat; // Panel with statistics of the number of annotations
            this.BackAnDisplayScroll = this.components.BackAnDisplayScroll = controlBar.BackAnDisplayScroll; // Back Panel with all the annotations
            this.backDSBar = this.components.BackAnDisplayScrollBar = this.BackAnDisplayScroll.BackAnDisplayScrollBar; // Scroll Bar
            this.backDSBarSel = this.components.ScrollBarSelector = this.backDSBar.ScrollBarSelector; // Scroll Bar Selector
            this.backDSTime = this.components.BackAnDisplayScrollTime = this.BackAnDisplayScroll.BackAnDisplayScrollTime; // Back Panel with time of the annotations in the scroll
            this.rsd = this.components.RangeSelectorDisplay = controlBar.BackAnDisplay.RangeSelectorDisplay; // Selection the time to display the annotations
            this.rsdl = this.components.RangeSelectorLeft = this.rsd.RangeSelectorLeft;
            this.rsdr = this.components.RangeSelectorRight = this.rsd.RangeSelectorRight;
            this.rsdb = this.components.RangeSelectorBar = this.rsd.RangeSelectorBar;
            this.rsdbl = this.components.RangeSelectorBarL = this.rsdb.RangeSelectorBarL;
            this.rsdbr = this.components.RangeSelectorBarR = this.rsdb.RangeSelectorBarR;
            this.rs = player.rangeslider;
            
            // local variables
            this.editing = false;
            
            var wrapper = $('.annotator-wrapper').parent()[0];
            var annotator = $.data(wrapper, 'annotator');
            var self = this;
            // Subscribe to Annotator changes
            annotator.subscribe("annotationsLoaded", function (annotations) {
                if(self.loaded)
                    self.refreshDisplay();
            });
            annotator.subscribe("annotationUpdated", function (annotation) {
               if(self.loaded)
                    self.refreshDisplay();
            });
            annotator.subscribe("annotationDeleted", function (annotation) {
                var annotations = annotator.plugins['Store'].annotations;
                var tot = typeof annotations !== 'undefined' ? annotations.length : 0;
                var attempts = 0; // max 100
                // This is to watch the annotations object, to see when is deleted the annotation
                var ischanged = function() {
                    var new_tot = annotator.plugins['Store'].annotations.length;
                    if (attempts < 100)
                        setTimeout(function(){
                            if (new_tot !== tot) {
                                if(self.loaded)
                                    self.refreshDisplay();
                            } else {
                                attempts++;
                                ischanged();
                            }
                        }, 100); // wait for the change in the annotations
                };
                ischanged();
            });
            
            this.BigNewAn.hide(); // Hide until the video is load
        },
        newan: function(start, end) {
            var player = this.player;
            var annotator = this.annotator;
            var sumPercent = 10; // percentage for the last mark
            var currentTime = player.currentTime();
            var lastTime = this._sumPercent(currentTime, sumPercent); 
            
            var start = typeof start !== 'undefined' ? start : currentTime;
            var end = typeof end !== 'undefined' ? end : lastTime;
                
            this._reset();
            
            // set position RS and pause the player
            player.showSlider();
            player.pause();
            
            player.setValueSlider(start, end);
            
            // This variable is to say the editor that we want create a VideoJS annotation
            annotator.editor.VideoJS = this.player.id_;
            
            annotator.adder.show();
            
            this._setOverRS(annotator.adder);

            // Open a new annotator dialog
            annotator.onAdderClick();
        },
        showDisplay: function() {
            this._reset();
            // show
            this.BackAnDisplay.removeClass('disable'); // show the Container
            this.BackAnDisplayScroll.removeClass('disable'); // show the scroll
            // active button
            this.ShowAn.addClass('active');
            this.options.showDisplay =true;
        },
        hideDisplay: function() {
            // hide
            this.BackAnDisplay.addClass('disable'); // hide the Container
            this.BackAnDisplayScroll.addClass('disable'); // hide the scroll
            // no active button
            videojs.removeClass(this.ShowAn.el_, 'active');
            this.options.showDisplay =false;
        },
        showStatistics: function() {
            this._reset();
            // show
            this.BackAnDisplay.removeClass('disable'); // show the Container
            this.AnStat.removeClass('disable'); // show Statistics
            // mode (this mode will hide the annotations to show the statistics in the container)
            this.BackAnDisplay.addClass('statistics'); // mode statistics 
            // paint
            this.AnStat.paintCanvas(); // refresh canvas
            // active button
            this.ShowSt.addClass('active');
            this.options.showStatistics =true;
        },
        hideStatistics: function() {
            // hide
            this.BackAnDisplay.addClass('disable'); // hide the Container
            this.AnStat.addClass('disable'); // hide Statistics
            // remove mode statistics
            this.BackAnDisplay.removeClass('statistics');
            // no active button
            this.ShowSt.removeClass('active');
            this.options.showStatistics = false;
        },
        showAnnotation: function(annotation) {
            var isVideo = this._isVideoJS(annotation);
            if (isVideo) {
                var start = annotation.rangeTime.start;
                var end = annotation.rangeTime.end;
                var duration = this.player.duration();
                var isPoint = videojs.round(start, 3) == videojs.round(end, 3);
                
                this._reset();
            
                // show the range slider
                this.rs.show();
            
                // set the slider position
                this.rs.setValues(start, end);
            
                // lock the player        
                this.rs.lock();
            
                // play
                if (!isPoint)
                    this.rs.playBetween(start, end);
                    
                // fix small bar
                var width = Math.min(1, Math.max(0.005, (this.rs._percent(end - start)))) * 100;
                this.rs.bar.el_.style.width = width + '%';
                    
                // Add the annotation object to the bar 
                var bar = isPoint ? this.rs[((duration - start) / duration < 0.1) ? 'left' : 'right'].el_ : this.rs.bar.el_;
                var holder = $(this.rs.left.el_).parent()[0];
                $(holder).append('<span class="annotator-hl"></div>');
                $(bar).appendTo( $(holder).find('.annotator-hl'));
            
                var span = $(bar).parent()[0];
                $.data(span, 'annotation', annotation); // Set the object in the span
            
                // set the editor over the range slider
                this._setOverRS(this.annotator.editor.element);
                this.annotator.editor.checkOrientation();
            
                // hide the panel
                this.rs.hidePanel();
            }
        },
        hideAnnotation: function() {
            this.rs.hide();
            this.rs.showPanel();
            
            // remove the last single showed annotation
            var holder = $(this.rs.left.el_).parent()[0];
            var holderRight = $(this.rs.right.el_).parent()[0];
            if ($(holder).find('.annotator-hl').length > 0) {
                $($(holder).find('.annotator-hl')[0].children[0]).appendTo(holder);
                $(holder).find('.annotator-hl').remove();
            } else if ($(holderRight).find('.annotator-hl').length > 0) {
                $($(holderRight).find('.annotator-hl')[0].children[0]).appendTo(holderRight);
                $(holderRight).find('.annotator-hl').remove();
            }
        },
        editAnnotation: function(annotation, editor) {
            // This will be usefull when we are going to edit an annotation.
            if (this._isVideoJS(annotation)) {
                this.hideDisplay();
                var player = this.player;
                var editor = editor || this.annotator.editor;
                
                // show the slider and set in the position
                player.showSlider();
                player.unlockSlider();
                player.setValueSlider(annotation.rangeTime.start, annotation.rangeTime.end);
                
                // show the time panel
                player.showSliderPanel();
                
                // set the editor over the range slider
                this._setOverRS(editor.element);
                editor.checkOrientation();
                
                // set the VideoJS variable
                editor.VideoJS = player.id_;
            }
        },
        refreshDisplay: function() {
            var count = 0;
            var allannotations = this.annotator.plugins['Store'].annotations;
            
            // Sort by date the Array
            this._sortByDate(allannotations);
            
            // reset the panel
            $(this.AnDisplay.el_).find('span').remove(); // remove the last html items
            $(this.player.el_).find('.vjs-anpanel-annotation .annotation').remove(); // remove a deleted annotation without span wrapper
            
            for (var item in allannotations) {
                var an = allannotations[item];
                
                // check if the annotation is a video annotation
                if (this._isVideoJS(an)){
                    var div = document.createElement('div');
                    var span = document.createElement('span');
                    var start = this.rs._percent(an.rangeTime.start) * 100;
                    var end = this.rs._percent(an.rangeTime.end) * 100;
                    var width;
                    span.appendChild(div);
                    span.className = "annotator-hl";
                    width = Math.min(100, Math.max(0.2, end - start));
                    div.className = "annotation";
                    div.id = count;
                    div.style.top = count + "em";
                    div.style.left = start + '%';
                    div.style.width = width + '%';
                    div.start = an.rangeTime.start;
                    div.end = an.rangeTime.end;
                    this.AnDisplay.el_.appendChild(span);
                    
                    // detect point annotations
                    if (videojs.round(start, 0) == videojs.round(end, 0)) {
                        $(div).css('width', '');
                        $(div).addClass("point");
                    }
                    
                    // Set the object in the div
                    $.data(span, 'annotation', an);
                    // Add the highlights to the annotation
                    an.highlights = $(span);
                    
                    count++;
                }
daniel cebrian committed
471
            };
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
            var start = this.rs._seconds(parseFloat(this.rsdl.el_.style.left) / 100);
            var end = this.rs._seconds(parseFloat(this.rsdr.el_.style.left) / 100);
                
            this.showBetween(start, end, this.rsdl.include, this.rsdr.include);
        },
        showBetween: function (start, end, includeLeft, includeRight) {
            var duration = this.player.duration();
            var start = start || 0;
            var end = end || duration;
            var annotationsHTML = $.makeArray($(this.player.el_).find('.vjs-anpanel-annotation .annotator-hl'));
            var count = 0;
            for (var index in annotationsHTML) {
                var an = $.data(annotationsHTML[index], 'annotation');
                var expressionLeft = includeLeft ? (an.rangeTime.end >= start) : (an.rangeTime.start >= start);
                var expressionRight = includeRight ? (an.rangeTime.start <= end) : (an.rangeTime.end <= end);
                if (this._isVideoJS(an) && expressionLeft && expressionRight && typeof an.highlights[0] !== 'undefined') {
                    var annotationHTML = an.highlights[0].children[0];
                    annotationHTML.style.marginTop = (-1 * parseFloat(annotationHTML.style.top) + count) + 'em';
                    $(an.highlights[0]).show();
                    count++;
                } else if (this._isVideoJS(an) && typeof an.highlights[0] !== 'undefined') {
                    $(an.highlights[0]).hide();
                    an.highlights[0].children[0].style.marginTop = '';
                }
            }
            // Set the times in the scroll time panel
            this.backDSTime.setTimes();
        },
        setposBigNew: function(pos) {
            var pos = pos || 'ul';
            var el = this.player.BigNewAnnotation.el_;
            videojs.removeClass(el, 'ul');
            videojs.removeClass(el, 'ur');
            videojs.removeClass(el, 'c');
            videojs.removeClass(el, 'bl');
            videojs.removeClass(el, 'br');
            videojs.addClass(el, pos);
        },
        pressedKey: function (key) {
            var player = this.player;
            var rs = this.player.rs;
            if (typeof key !== 'undefined' && key == 73) { // -- i key
                this._reset();
                
                // show slider
                this.rs.show();
                // hide other elements
                this.rs._reset();
                this.rs.setValue(0, player.currentTime());
                this.rs.right.el_.style.visibility = 'hidden';
                this.rs.tpr.el_.style.visibility = 'hidden';
                this.rs.ctpr.el_.style.visibility = 'hidden';
                this.rs.bar.el_.style.visibility = 'hidden';
                this.lastStartbyKey = player.currentTime();
            } else if (typeof key!='undefined' && key==79) { // -- o key
                if (this.rs.bar.el_.style.visibility == 'hidden') { // the last action was to type the i key
                    var start = this.lastStartbyKey != 'undefined' ? this.lastStartbyKey:0;
                    this.newan(start, player.currentTime());
                } else {
                    this.newan(player.currentTime(), player.currentTime());
                }
            }
        },
        refreshDesignPanel: function() {
            var player = this.player;
            var emtoPx = parseFloat($(this.backDSBar.el_).css('width'));
            var playerHeight = parseFloat($(player.el_).css('height'));
            var controlBarHeight = parseFloat($(player.controlBar.el_).css('height'));
            var newHeight = (playerHeight - controlBarHeight) / emtoPx - 5;
            this.BackAnDisplay.el_.style.height = this.backDSBar.el_.style.height = (newHeight + 'em');
            this.BackAnDisplay.el_.style.top = this.backDSBar.el_.style.top = "-" + (newHeight + 3 + 'em');
            this.BackAnDisplayScroll.el_.children[0].style.top = "-" + (newHeight + 5 + 'em');
            this.backDSTime.el_.children[0].style.top = "-" + (newHeight + 5 + 'em');
        },
        _reset: function() {
            // Hide all the components
            this.hideDisplay();
            this.hideAnnotation();
            this.hideStatistics();
            this.player.annotator.adder.hide(); 
            this.player.annotator.editor.hide();
            this.player.annotator.viewer.hide();
            
            // make visible all the range slider element that maybe were hidden in pressedKey event
            this.rs.right.el_.style.visibility = '';
            this.rs.tpr.el_.style.visibility = '';
            this.rs.ctpr.el_.style.visibility = '';
            this.rs.bar.el_.style.visibility = '';
            
            // by default the range slider must be unlocked
            this.rs.unlock();
            
            // whether there is a playing selection
            this.rs.bar.suspendPlay(); 
            
            // refresh the design
            this.refreshDesignPanel();
        },
        _setOverRS: function(elem) {
            var annotator = this.player.annotator;
            var wrapper = $('.annotator-wrapper')[0];
            var positionLeft = videojs.findPosition(this.rs.left.el_);
            var positionRight = videojs.findPosition(this.rs.right.el_);
            var positionAnnotator = videojs.findPosition(wrapper);
            var positionAdder = {};
                
            elem[0].style.display = 'block'; // Show the adder
            
            if (this.player.isFullScreen) {
                positionAdder.top = positionLeft.top;
                positionAdder.left = positionLeft.left + (positionRight.left - positionLeft.left) / 2;
            } else {
                positionAdder.left = positionLeft.left + (positionRight.left - positionLeft.left) / 2 - positionAnnotator.left;
                positionAdder.top = positionLeft.top - positionAnnotator.top;
            }
            
            elem.css(positionAdder);
        },
        _onMouseDownRS: function(event) {
            event.preventDefault();
        
            if (!this.rs.options.locked) {
                videojs.on(document, "mousemove", videojs.bind(this, this._onMouseMoveRS));
                videojs.on(document, "mouseup", videojs.bind(this, this._onMouseUpRS));
            }
        },
        _onMouseMoveRS: function(event) {
            var player = this.player;
            var annotator = player.annotator;
            var rs = player.rangeslider;
            annotator.editor.element[0].style.display = 'none';
            rs.show();
            this._setOverRS(annotator.adder);
        },
        _onMouseUpRS: function(event) {
            videojs.off(document, "mousemove", this._onMouseMoveRS, false);
            videojs.off(document, "mouseup", this._onMouseUpRS, false);
            
            var player = this.player;
            var annotator = player.annotator;
            var rs = player.rangeslider;
            annotator.editor.element[0].style.display = 'block';
            
            this._setOverRS(annotator.editor.element);
        },
        _sumPercent: function(seconds, percent) {
            // the percentage is in %
            var duration = this.player.duration();
            var seconds = seconds || 0;
            var percent = percent || 10;
            percent = Math.min(100, Math.max(0, percent));
            
            if (isNaN(duration)) {
                return 0;
            }
            return Math.min(duration, Math.max(0, seconds + duration * percent / 100));
        },
        // Detect if we are creating or editing a video-js annotation
        _EditVideoAn: function () {
            var annotator = this.annotator;
            var isOpenVideojs = (typeof this.player != 'undefined');
            var VideoJS = annotator.editor.VideoJS;
            return (isOpenVideojs && typeof VideoJS!='undefined' && VideoJS!==-1);
        },
        // Detect if the annotation is a video-js annotation
        _isVideoJS: function (an) {
            var player = this.player;
            var rt = an.rangeTime;
            var isOpenVideojs = (typeof this.player !== 'undefined');
            var isVideo = (typeof an.media !== 'undefined' && (an.media === 'video' || an.media === 'audio'));
            var isContainer = (typeof an.target !== 'undefined' && an.target.container == player.id_ );
            var isNumber = (typeof rt !== 'undefined' && !isNaN(parseFloat(rt.start)) && isFinite(rt.start) && !isNaN(parseFloat(rt.end)) && isFinite(rt.end));
            var isSource = false;
            if (isContainer) {
                // Compare without extension
                var isYoutube = (isOpenVideojs && typeof this.player.techName !== 'undefined') ? (this.player.techName === 'Youtube') : false;
                var targetSrc = isYoutube ? an.target.src : an.target.src.substring(0, an.target.src.lastIndexOf("."));
                var playerSrc = isYoutube ? player.options_.sources[0].src : player.options_.sources[0].src.substring(0, player.options_.sources[0].src.lastIndexOf("."));
                isSource = (targetSrc === playerSrc);
            }
            return (isOpenVideojs && isVideo && isContainer && isSource && isNumber);
        },
        _sortByDate: function (annotations, type) {
            var type = type || 'asc'; // asc => The value [0] will be the most recent date
            annotations.sort(function(a, b) {
                a = new Date(typeof a.updated !== 'undefined' ? createDateFromISO8601(a.updated) : '');
                b = new Date(typeof b.updated !== 'undefined' ? createDateFromISO8601(b.updated) : '');
                if (type == 'asc')
                    return (b < a) ? -1 : ((b > a) ? 1 : 0);
                else
                    return (a < b) ? -1 : ((a > b) ? 1 : 0);
            });
        }
    };
daniel cebrian committed
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    // ----------------CREATE new Components for video-js---------------- //

    // --Charge the new Component into videojs
    videojs.ControlBar.prototype.options_.children.AnContainerButtons = {}; // Container with the css for the buttons
    videojs.ControlBar.prototype.options_.children.BackAnDisplay = {}; // Range Slider Time Bar
    videojs.ControlBar.prototype.options_.children.BackAnDisplayScroll = {}; // Range Slider Time Bar
    videojs.options.children.BigNewAnnotation = {}; // Big Button New Annotation



    // -- Player--> BigNewAnnotation

    /**
     * Create a New Annotation with big Button
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.BigNewAnnotation = videojs.Button.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Button.call(this, player, options);
        }
    });

    videojs.BigNewAnnotation.prototype.init_ = function() {
        this.an = this.player_.annotations;
        // Hide Button if the user has selected readOnly in the Annotator options
        var opts = this.an.options.optionsAnnotator;
        if (typeof opts !== 'undefined' && typeof opts.readOnly !== 'undefined' && opts.readOnly)
            this.hide();
    };
daniel cebrian committed
699

700 701 702 703 704 705 706
    videojs.BigNewAnnotation.prototype.createEl = function() {
        return videojs.Button.prototype.createEl.call(this, 'div', {
            className: 'vjs-big-new-annotation vjs-menu-button vjs-control',
            innerHTML: '<div class="vjs-big-menu-button vjs-control">A</div>',
            title: 'New Annotation',
        });
    };
daniel cebrian committed
707

708 709 710
    videojs.BigNewAnnotation.prototype.onClick = function() {
        this.an.newan();
    };
daniel cebrian committed
711

712
    // -- Player--> ControlBar--> AnContainerButtons
daniel cebrian committed
713

714 715 716 717 718 719
    /**
     * Container for the button CSS
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
daniel cebrian committed
720

721 722 723 724 725 726
    videojs.AnContainerButtons = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
727

728
    videojs.AnContainerButtons.prototype.init_ = function() {};
daniel cebrian committed
729 730


731 732 733 734 735 736 737
    videojs.AnContainerButtons.prototype.options_ = {
        children: {
            'ShowStatistics': {},
            'ShowAnnotations': {},
            'NewAnnotation': {},
        }
    };
daniel cebrian committed
738

739 740 741 742 743
    videojs.AnContainerButtons.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-container-button-annotation vjs-menu-button vjs-control',
        });
    };
daniel cebrian committed
744

745
    // -- Player--> ControlBar--> AnContainerButtons--> ShowStatistics
daniel cebrian committed
746

747 748 749 750 751 752
    /**
     * Button for show/hide the chart with statistics of the annotation's number
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
daniel cebrian committed
753

754 755 756 757 758 759
    videojs.ShowStatistics = videojs.Button.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Button.call(this, player, options);
        }
    });
daniel cebrian committed
760

761 762 763
    videojs.ShowStatistics.prototype.init_ = function() {
        this.an = this.player_.annotations;
    };
daniel cebrian committed
764

765 766 767 768 769 770
    videojs.ShowStatistics.prototype.createEl = function() {
        return videojs.Button.prototype.createEl.call(this, 'div', {
            className: 'vjs-statistics-annotation vjs-menu-button vjs-control',
            title: 'Show the Statistics',
        });
    };
daniel cebrian committed
771

772 773 774 775
    videojs.ShowStatistics.prototype.onClick = function() {
        if (!this.an.options.showStatistics) this.an.showStatistics();
        else this.an.hideStatistics();
    };
daniel cebrian committed
776 777 778



779
    // -- Player--> ControlBar--> AnContainerButtons--> ShowAnnotations
daniel cebrian committed
780

781 782 783 784 785 786
    /**
     * Button for show/hide the annotation panel
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
daniel cebrian committed
787

788 789 790 791 792 793
    videojs.ShowAnnotations = videojs.Button.extend({
      /** @constructor */
      init: function(player, options) {
        videojs.Button.call(this, player, options);
      }
    });
daniel cebrian committed
794

795 796 797
    videojs.ShowAnnotations.prototype.init_ = function() {
        this.an = this.player_.annotations;
    };
daniel cebrian committed
798

799 800 801 802 803 804
    videojs.ShowAnnotations.prototype.createEl = function() {
        return videojs.Button.prototype.createEl.call(this, 'div', {
            className: 'vjs-showannotations-annotation vjs-menu-button vjs-control',
            title: 'Show Annotations',
        });
    };
daniel cebrian committed
805

806 807 808 809
    videojs.ShowAnnotations.prototype.onClick = function() {
        if (!this.an.options.showDisplay) this.an.showDisplay();
        else this.an.hideDisplay();
    };
daniel cebrian committed
810 811 812



813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    // -- Player--> ControlBar--> AnContainerButtons--> NewAnnotation

    /**
     * Create a New Annotation
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.NewAnnotation = videojs.Button.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Button.call(this, player, options);
        }
    });

    videojs.NewAnnotation.prototype.init_ = function() {
        this.an = this.player_.annotations;
        // Hide Button if the user has selected readOnly in the Annotator options
        var opts = this.an.options.optionsAnnotator;
        if (typeof opts !== 'undefined' && typeof opts.readOnly !== 'undefined' && opts.readOnly)
            this.hide();
    };
daniel cebrian committed
835

836 837 838 839 840 841
    videojs.NewAnnotation.prototype.createEl = function() {
        return videojs.Button.prototype.createEl.call(this, 'div', {
            className: 'vjs-new-annotation vjs-menu-button vjs-control',
            title: 'New Annotation',
        });
    };
daniel cebrian committed
842

843 844 845
    videojs.NewAnnotation.prototype.onClick = function() {
        this.an.newan();
    };
daniel cebrian committed
846 847 848



849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    // -- Player--> ControlBar--> BackAnDisplay

    /**
     * The background annotations panel
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.BackAnDisplay = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });

    videojs.BackAnDisplay.prototype.init_ = function() {
        this.an = this.player_.annotations
            self = this;
        // Fix error resizing the display panel. The scroll always went up.
        $(this.el_).watch('font-size', function() {
            self.an.backDSBarSel.setPosition(self.an.BackAnDisplayScroll.currentValue, false);
        });
daniel cebrian committed
871

872
    };
daniel cebrian committed
873

874 875 876 877 878 879 880
    videojs.BackAnDisplay.prototype.options_ = {
        children: {
            'RangeSelectorDisplay': {},
            'AnDisplay': {},
            'AnStat': {},
        }
    };
daniel cebrian committed
881

882 883 884 885 886
    videojs.BackAnDisplay.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-back-anpanel-annotation',
        });
    };
daniel cebrian committed
887 888 889



890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay

    /**
     * The selector to show the annotations in a time selection
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.RangeSelectorDisplay = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
        videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
        }
    });

    videojs.RangeSelectorDisplay.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        var duration = this.an.player.duration();
        this.start = 0;
        this.end = duration;
        
        // set the selection area in the extreme position
        this.setPosition(0, 0, false);
        this.setPosition(1, this.rs._percent(duration), false);
    };
daniel cebrian committed
918

919 920 921 922 923 924 925
    videojs.RangeSelectorDisplay.prototype.options_ = {
        children: {
            'RangeSelectorLeft': {},
            'RangeSelectorRight': {},
            'RangeSelectorBar': {},
        }
    };
daniel cebrian committed
926

927 928 929 930 931
    videojs.RangeSelectorDisplay.prototype.createEl = function(){
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-rangeselector-anpanel-annotation',
        });
    };
daniel cebrian committed
932

933 934 935 936 937 938 939 940 941
    videojs.RangeSelectorDisplay.prototype.onMouseDown = function(event) {
        event.preventDefault();
        // videojs.blockTextSelection();
        
        videojs.on(document, "mousemove", videojs.bind(this, this.onMouseMove));
        videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
        
        videojs.removeClass(this.an.rsdb.el_, 'disable');
    };
daniel cebrian committed
942

943 944 945 946 947 948
    videojs.RangeSelectorDisplay.prototype.onMouseUp = function(event) {
        videojs.off(document, "mousemove", this.onMouseMove, false);
        videojs.off(document, "mouseup", this.onMouseUp, false);
        
        videojs.addClass(this.an.rsdb.el_, 'disable');
    };
daniel cebrian committed
949

950 951 952 953 954 955 956 957 958 959
    videojs.RangeSelectorDisplay.prototype.onMouseMove = function(event) {
        var left = this.calculateDistance(event);
        if (this.an.rsdl.pressed)
            this.setPosition(0, left);
        else if (this.an.rsdr.pressed)
            this.setPosition(1, left);
        
        // move the frame to the position of the arrow
        this.an.player.currentTime(this.rs._seconds(left));
    };
daniel cebrian committed
960

961 962 963 964
    videojs.RangeSelectorDisplay.prototype.calculateDistance = function(event) {
        var rstbX = this.getRSTBX();
        var rstbW = this.getRSTBWidth();
        var handleW = this.getWidth();
daniel cebrian committed
965

966 967 968
        // Adjusted X and Width, so handle doesn't go outside the bar
        rstbX = rstbX + (handleW / 2);
        rstbW = rstbW - handleW;
daniel cebrian committed
969

970 971 972
        // Percent that the click is through the adjusted area
        return Math.max(0, Math.min(1, (event.pageX - rstbX) / rstbW));
    };
daniel cebrian committed
973

974 975 976 977 978 979 980 981 982 983
    videojs.RangeSelectorDisplay.prototype.getRSTBWidth = function() {
        return this.el_.offsetWidth;
    };
    videojs.RangeSelectorDisplay.prototype.getRSTBX = function() {
        return videojs.findPosition(this.el_).left;
    };
    videojs.RangeSelectorDisplay.prototype.getWidth = function() {
        var arrow = $(this.an.rsdl.el_).find('.vjs-selector-arrow')[0];
        return arrow.offsetWidth; // does not matter left or right
    };
daniel cebrian committed
984

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    videojs.RangeSelectorDisplay.prototype.setPosition = function(index, left, changeTime) {
        // index = 0 for left side, index = 1 for right side
        var index = index || 0;
        var changeTime = typeof changeTime !== 'undefined' ? changeTime : true;

        // Check for invalid position
        if(isNaN(left)) 
            return false;
        
        // Check index between 0 and 1
        if (!(index === 0 || index === 1))
            return false;
        // Alias
        var ObjLeft = this.an.rsdl.el_;
        var ObjRight = this.an.rsdr.el_;
        var Obj = this.an[index === 0 ? 'rsdl' : 'rsdr'].el_;
        
        // Check if left arrow is over the right arrow
        if ((index === 0 ? this.updateLeft(left) : this.updateRight(left))) {
            if (index === 1) { // right
                Obj.style.left = (left * 100) + '%';
                Obj.style.width = ((1 - left) * 100) + '%';
            } else { // left
                Obj.style.left = (left * 100) + '%';
                Obj.style.width = ((left) * 100) + '%';
            }
            
            this[index === 0 ? 'start' : 'end'] = this.rs._seconds(left);
        
            // Fix the problem  when you press the button and the two arrow are underhand
            // left.zIndex = 10 and right.zIndex=20. This is always less in this case:
            if (index === 0 && (left * 100) >= 90)
                $(ObjLeft).find('.vjs-selector-arrow')[0].style.zIndex = 25;
            else
                $(ObjLeft).find('.vjs-selector-arrow')[0].style.zIndex = 10;
            
            
            // -- Panel
            var rsdbl = this.an.rsdbl.el_,
                rsdbr = this.an.rsdbr.el_,
                distance = parseFloat(ObjRight.style.left) - parseFloat(ObjLeft.style.left);
            if (index === 0)
                rsdbl.children[0].innerHTML = videojs.formatTime(this.rs._seconds(left));
            else
                rsdbr.children[0].innerHTML = videojs.formatTime(this.rs._seconds(left));
            if (typeof distance !== 'undefined' && distance <= 12.5) {
                if (parseFloat(ObjLeft.style.left) < 7) {
                    rsdbl.style.top = (-1.5) + 'em';
                    rsdbl.style.left = 1 + 'em';
                } else {
                    rsdbl.style.left = (-2.5) + 'em';
                    rsdbl.style.top = '';
                }
                    
                if (parseFloat(ObjRight.style.left) > 93) {
                    rsdbr.style.top = (-1.5) + 'em';
                    rsdbr.style.right = 1 + 'em';
                } else {
                    rsdbr.style.right = (-2.5) + 'em';
                    rsdbr.style.top = '';
                }
            } else {
                rsdbl.style.left = 1 + 'em';
                rsdbr.style.right = 1 + 'em';
                rsdbl.style.top = '';
                rsdbr.style.top = '';
            }
            
            
            var start = this.rs._seconds(parseFloat(ObjLeft.style.left) / 100);
            var end = this.rs._seconds(parseFloat(ObjRight.style.left) / 100);
                
            if (changeTime)
                this.an.showBetween(start, end, this.an.rsdl.include, this.an.rsdr.include);
        }
        return true;
    };
daniel cebrian committed
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    videojs.RangeSelectorDisplay.prototype.updateLeft = function(left) {
        var rightVal = this.an.rsdr.el_.style.left !== '' ? this.an.rsdr.el_.style.left : 100;
        var right = parseFloat(rightVal) / 100;
        var bar = this.an.rsdb.el_;
        
        var width = videojs.round((right - left), this.an.updatePrecision); // round necessary for not get 0.6e-7 for example that it's not able for the html css width
        if(left <= (right+0.00001)) {
                bar.style.left = (left * 100) + '%';
                bar.style.width = (width * 100) + '%';
                return true;
        }
        return false;
    };
            
    videojs.RangeSelectorDisplay.prototype.updateRight = function(right) {
        var leftVal = this.an.rsdl.el_.style.left !== '' ? this.an.rsdl.el_.style.left : 0;
        var left = parseFloat(leftVal) / 100;
        var bar = this.an.rsdb.el_;
            
        var width = videojs.round((right - left), this.an.updatePrecision); // round necessary for not get 0.6e-7 for example that it's not able for the html css width
        
        if((right+0.00001) >= left) {
            bar.style.width = (width * 100) + '%';
            bar.style.left = ((right  - width) * 100) + '%';
            return true;
        }
        return false;
    };        



    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay--> RangeSelectorLeft

    /**
     * Left Time selector
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.RangeSelectorLeft = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
            this.on('dblclick', this.ondblclick);
            this.pressed = false; // to know when is mousedown
            this.include = true; // to know when we want to include the boundary time in the selection or not
        }
    });

    videojs.RangeSelectorLeft.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        videojs.addClass(this.el_, 'include');
    };
daniel cebrian committed
1118

1119 1120 1121 1122 1123 1124
    videojs.RangeSelectorLeft.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-leftselector-anpanel-annotation',
            innerHTML: '<div class="vjs-selector-arrow" title="Left Annotation Selector"></div><div class="vjs-leftselector-back"></div>'
        });
    };
daniel cebrian committed
1125 1126


1127 1128 1129 1130 1131 1132 1133 1134
    videojs.RangeSelectorLeft.prototype.onMouseDown = function(event) {
        event.preventDefault();
        
        this.pressed = true;
        videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
        videojs.addClass(this.el_, 'active');
        videojs.addClass(this.el_.parentNode, 'active');
    };
daniel cebrian committed
1135

1136 1137 1138 1139 1140 1141
    videojs.RangeSelectorLeft.prototype.onMouseUp = function(event) {
        videojs.off(document, "mouseup", this.onMouseUp, false);
        videojs.removeClass(this.el_, 'active');
        videojs.removeClass(this.el_.parentNode, 'active');
        this.pressed = false;
    };
daniel cebrian committed
1142

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    videojs.RangeSelectorLeft.prototype.ondblclick = function(event) {
        if (this.include) {
            this.include = false;
            videojs.removeClass(this.el_, 'include');
        } else {
            this.include = true;
            videojs.addClass(this.el_, 'include');
        }
        var left = this.an.rsd.calculateDistance(event);
        this.an.rsd.setPosition(0, left);
    };
daniel cebrian committed
1154 1155 1156



1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay--> RangeSelectorRight

    /**
     * Right Time selector
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.RangeSelectorRight = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
            this.on('dblclick', this.ondblclick);
            this.pressed = false; // to know when is mousedown
            this.include = true; // to know when we want to include the boundary time in the selection or not
        }
    });

    videojs.RangeSelectorRight.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        videojs.addClass(this.el_, 'include');
    };
daniel cebrian committed
1181

1182 1183 1184 1185 1186 1187
    videojs.RangeSelectorRight.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-rightselector-anpanel-annotation',
            innerHTML: '<div class="vjs-selector-arrow" title="Right Annotation Selector"></div><div class="vjs-rightselector-back"></div>'
        });
    };
daniel cebrian committed
1188

1189 1190 1191 1192 1193 1194 1195 1196
    videojs.RangeSelectorRight.prototype.onMouseDown = function(event) {
        event.preventDefault();
        
        this.pressed = true;
        videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
        videojs.addClass(this.el_, 'active');
        videojs.addClass(this.el_.parentNode, 'active');
    };
daniel cebrian committed
1197

1198 1199 1200 1201 1202 1203
    videojs.RangeSelectorRight.prototype.onMouseUp = function(event) {
        videojs.off(document, "mouseup", this.onMouseUp, false);
        videojs.removeClass(this.el_, 'active');
        videojs.removeClass(this.el_.parentNode, 'active');
        this.pressed = false;
    };
daniel cebrian committed
1204

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    videojs.RangeSelectorRight.prototype.ondblclick = function(event) {
        if (this.include){
            this.include = false;
            videojs.removeClass(this.el_, 'include');
        }else{
            this.include = true;
            videojs.addClass(this.el_, 'include');
        }
        var left = this.an.rsd.calculateDistance(event);
        this.an.rsd.setPosition(1, left);
    };
daniel cebrian committed
1216 1217 1218



1219
    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay--> RangeSelectorBar
daniel cebrian committed
1220

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    /**
     * Bar to display the selected Time
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.RangeSelectorBar = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
1234

1235 1236 1237
    videojs.RangeSelectorBar.prototype.init_ = function() {
        videojs.addClass(this.el_, 'disable');
    };
daniel cebrian committed
1238

1239 1240 1241 1242 1243 1244
    videojs.RangeSelectorBar.prototype.options_ = {
        children: {
            'RangeSelectorBarL': {},
            'RangeSelectorBarR': {},
        }
    };
daniel cebrian committed
1245

1246 1247 1248 1249 1250
    videojs.RangeSelectorBar.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-barselector-anpanel-annotation',
        });
    };
daniel cebrian committed
1251 1252 1253



1254
    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay--> RangeSelectorBar--> RangeSelectorBarL
daniel cebrian committed
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    /**
     * This is the left time panel for RangeSelectorBar
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.RangeSelectorBarL = videojs.Component.extend({
      /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
1268

1269
    videojs.RangeSelectorBarL.prototype.init_ = function() {};
daniel cebrian committed
1270

1271 1272 1273 1274 1275 1276
    videojs.RangeSelectorBarL.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-barselector-left',
            innerHTML: '<span class="vjs-time-text">00:00</span>',
        });
    };
daniel cebrian committed
1277 1278 1279



1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    // -- Player--> ControlBar--> BackAnDisplay--> RangeSelectorDisplay--> RangeSelectorBar--> RangeSelectorBarR
    /**
     * This is the right time panel for RangeSelectorBar
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
    videojs.RangeSelectorBarR = videojs.Component.extend({
      /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
1293

1294
    videojs.RangeSelectorBarR.prototype.init_ = function() {};
daniel cebrian committed
1295

1296 1297 1298 1299 1300 1301
    videojs.RangeSelectorBarR.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-barselector-right',
            innerHTML: '<span class="vjs-time-text">00:00</span>'
        });
    };
daniel cebrian committed
1302 1303 1304



1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    // -- Player--> ControlBar--> BackAnDisplay--> AnDisplay

    /**
     * Show the annotations in a panel
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.AnDisplay = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
            this.on('mouseover', this.onMouseOver);
        }
    });

    videojs.AnDisplay.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        this.transition = false;
    };
daniel cebrian committed
1328

1329 1330 1331 1332 1333
    videojs.AnDisplay.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-anpanel-annotation',
        });
    };
daniel cebrian committed
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    videojs.AnDisplay.prototype.onMouseDown = function(event) {
        var elem = $(event.target).parents('.annotator-hl').andSelf();
        var _self = this;
        if (elem.hasClass("annotator-hl")) {
            videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
            // Clone the bar box to make the animation
            var boxup = document.createElement('div');
            var ElemTop = parseFloat(elem[1].style.top);
            var ElemMargin = parseFloat(elem[1].style.marginTop);
            var emtoPx = parseFloat($(elem[1]).css('height'));
            var isPoint = $(elem[1]).hasClass("point");
                
            boxup.className = isPoint ? "boxup-dashed-line point" : "boxup-dashed-line";
            boxup.style.left = elem[1].style.left;
            boxup.style.width = elem[1].style.width;
        
            boxup.style.top = (ElemTop + ElemMargin - this.el_.scrollTop / emtoPx) + 'em';
            elem[0].parentNode.parentNode.appendChild(boxup);
        }
    }
daniel cebrian committed
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
    videojs.AnDisplay.prototype.onMouseUp = function(event) {
        if (typeof this.lastelem === 'undefined')
            return false;
        var elem = this.lastelem;
        var _self = this;
        if (elem.hasClass("annotator-hl")) {
            var annotation = elem.map(function() {
                return $(this).data("annotation");
            })[0];
            var displayHeight = (-1) * parseFloat($(this.el_).parent()[0].style.top);
            var emtoPx = parseFloat($(elem[1]).css('height'));
            if (typeof $(elem).parent().parent().find('.boxup-dashed-line')[0] !== 'undefined') {
                $(elem).parent().parent().find('.boxup-dashed-line')[0].style.top = (displayHeight - 2) + 'em';
            }
            
            this.an.player.pause();
            this.transition = true;
            window.setTimeout(function () {
                _self.an.showAnnotation(annotation);
                _self.transition = false;
                _self.onCloseViewer();
            }, 900);
        }
        videojs.off(document, "mouseup", this.onMouseUp, false);
    };
daniel cebrian committed
1381

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
    videojs.AnDisplay.prototype.onMouseOver = function(event) {
        if (!this.transition && !this.an.rsdl.pressed && !this.an.rsdr.pressed) {
            var annotator = this.an.annotator;
            var elem = $(event.target).parents('.annotator-hl').andSelf();
        
            // if there is a opened annotation then show the new annotation mouse over
            if (typeof annotator !== 'undefined' && annotator.viewer.isShown() && elem.hasClass("annotator-hl")) {
                // hide the last open viewer
                annotator.viewer.hide();
                // get the annotation over the mouse
                var annotations = elem.map(function() {
                    return $(this).data("annotation");
                });
                // show the annotation in the viewer
                annotator.showViewer($.makeArray(annotations), Util.mousePosition(event, annotator.wrapper[0]));
            }
        
            // create dashed line
            elem.addClass('active');
            if (typeof elem !== 'undefined' && $(elem[1]).hasClass('annotation')) {
                // create dashed line under the bar
                var dashed = document.createElement('div');
                var boxdown = document.createElement('div');
                var DisplayHeight = parseFloat(this.an.BackAnDisplay.el_.style.height);
                var ElemMarginTop = elem[1].style.marginTop !== '' ? parseFloat(elem[1].style.marginTop) : 0;
                var ElemTop = parseFloat(elem[1].style.top) + ElemMarginTop;
                var emtoPx = parseFloat($(elem[1]).css('height'));
                var isPoint = $(elem[1]).hasClass("point");

                dashed.className = isPoint ? 'dashed-line point' : 'dashed-line';
                boxdown.className = "box-dashed-line";
                dashed.style.left = boxdown.style.left = elem[1].style.left;
                dashed.style.width = boxdown.style.width = isPoint ? '0' : elem[1].style.width;
                dashed.style.top = ((ElemTop + 1) - this.el_.scrollTop / emtoPx) + 'em';
                dashed.style.height = ((DisplayHeight - ElemTop + 2) + this.el_.scrollTop / emtoPx) + 'em'; // get the absolute value of the top to put in the height
                boxdown.style.top = (DisplayHeight + 2) + 'em';
                elem[0].parentNode.parentNode.appendChild(dashed);
                elem[0].parentNode.parentNode.appendChild(boxdown);
                
                $(this.player).find('.vjs-play-progress').css('z-index', 2);
                $(this.player).find('.vjs-seek-handle').css('z-index', 2);
            }
        
            // store the last selected item
            if (elem.hasClass("annotator-hl"))
                this.lastelem = elem;
        }
    };
daniel cebrian committed
1430

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    videojs.AnDisplay.prototype.onCloseViewer = function() {
        if (!this.transition) {
            if (typeof this.lastelem !== 'undefined')
                this.lastelem.removeClass('active');
            // remove dashed line
            if (typeof this.lastelem !== 'undefined' && this.lastelem.hasClass("annotator-hl")) {
                $(this.lastelem).parent().parent().find('.dashed-line').remove();
                $(this.lastelem).parent().parent().find('.box-dashed-line').remove();
                $(this.lastelem).parent().parent().find('.boxup-dashed-line').remove();
                $(this.player).find('.vjs-play-progress').css('z-index', "");
                $(this.player).find('.vjs-seek-handle').css('z-index', "");
            }
        }
    };
daniel cebrian committed
1445 1446


1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    videojs.AnDisplay.prototype.countVisibles = function() {
        var AnArray = $.makeArray(this.el_.children);
        // Count visible annotations in Panel
        var count = 0;
        for (var index in AnArray) {
            var an = AnArray[index];
            if (an.style.display !== 'none') {
                count++;
            }
        }
        return count;
    };
daniel cebrian committed
1459 1460 1461



1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
    // -- Player--> ControlBar--> BackAnDisplay--> AnStat

    /**
     * Display with a chart with the statistics of the number of Annotations
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.AnStat = videojs.Component.extend({
        /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.marginTop = 20;
            this.marginBottom = 0;
        }
    });

    videojs.AnStat.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        this.canvas = this.el_.children[0];
    };
daniel cebrian committed
1485

1486 1487 1488 1489 1490 1491
    videojs.AnStat.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-anstat-annotation',
            innerHTML: '<canvas class="vjs-char-anstat-annotation">Your browser does not support the HTML5 canvas tag.</canvas>',
        });
    };
daniel cebrian committed
1492

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    videojs.AnStat.prototype.paintCanvas = function() {
        var ctx = this.canvas.getContext("2d");
        var points = this._getPoints();
        var w = this._getWeights(points);
        var maxEn = this._getMaxArray(points, 'entries');
        var TotAn = this.an.AnDisplay.el_.children.length;
        var duration = this.an.player.duration();

        // set the position of the canvas
        this.canvas.style.marginTop = Math.round(this.marginTop) + 'px';
        
        // Add the Max Concentration and Number of annotations
        if($(this.canvas).parent().find('.vjs-totan-anstat-annotation').length === 0) {
            $(this.canvas).parent().append('<div class="vjs-totan-anstat-annotation">');
            $(this.canvas).parent().append('<div class="vjs-maxcon-anstat-annotation">');
        }
        var textCanvas = $(this.canvas).parent().find('.vjs-totan-anstat-annotation')[0];
        textCanvas.innerHTML = TotAn + ' total annotations';
        var textCanvas = $(this.canvas).parent().find('.vjs-maxcon-anstat-annotation')[0];
        textCanvas.innerHTML = 'Max Annotations = ' + maxEn;
        
        // Added dashed line function to paint
        if (window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype.lineTo) {
            CanvasRenderingContext2D.prototype.dashedLine = function(x1, y1, x2, y2, dashLen) {
                if (dashLen === undefined) dashLen = 2;

                this.beginPath();
                this.moveTo(x1, y1);

                var dX = x2 - x1;
                var dY = y2 - y1;
                var dashes = Math.floor(Math.sqrt(dX * dX + dY * dY) / dashLen);
                var dashX = dX / dashes;
                var dashY = dY / dashes;

                var q = 0;
                while (q++ < dashes) {
                 x1 += dashX;
                 y1 += dashY;
                 this[q % 2 == 0 ? 'moveTo' : 'lineTo'](x1, y1);
                }
                this[q % 2 == 0 ? 'moveTo' : 'lineTo'](x2, y2);

                this.stroke();
                this.closePath();
            };
        };    
            
        
        // set the canvas size
        this.canvas.height = this.an.AnDisplay.el_.offsetHeight - (this.marginTop + this.marginBottom);
        this.canvas.width  = this.an.AnDisplay.el_.offsetWidth;
        
        ctx.beginPath();
        ctx.strokeStyle = "rgb(255, 163, 0)";
        var lastSe = 0;
        var lastEn = 0;
        ctx.moveTo(0, maxEn * w.Y); // Move pointer to 0, 0
        for (var index in points) {
            var p = points[index];
            var x1 = lastSe * w.X, y1 = (maxEn - lastEn) * w.Y; // Old Point
            var x2 = p.second * w.X, y2 = (maxEn - p.entries) * w.Y; // New Point
            // new line
            ctx.lineTo(x2, y1); // move horizontally to the new point
            ctx.moveTo(x2, y1); // Move pointer
            ctx.lineTo(x2, y2); // move vertically to the new point height
            ctx.moveTo(x2, y2); // Prepare pointer for a new instance
            // new rectangle under the curve
            ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
            ctx.fillRect(x1, y1, (x2 - x1), (maxEn * w.Y - y1));
            
            // store the last point
            lastSe = p.second;
            lastEn = p.entries;
        }
        // set the graphic to the end of the video
        ctx.lineTo(lastSe * w.X, maxEn * w.Y); 
        ctx.moveTo(lastSe * w.X, maxEn * w.Y); 
        ctx.lineTo(duration * w.X, maxEn * w.Y);
        ctx.stroke();
        
        // dashed line down
        ctx.beginPath();
        ctx.dashedLine(0, maxEn * w.Y, duration * w.X, maxEn * w.Y, 8);
        ctx.stroke();
        // dashed line top
        ctx.beginPath();
        ctx.dashedLine(0, 0, duration * w.X, 0, 8);
        ctx.stroke();
    };
daniel cebrian committed
1583

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    videojs.AnStat.prototype._getWeights = function(points){
        var weight = {};
        var panel = $(this.an.AnDisplay.el_);
        var maxSe = this.an.player.duration();
        var maxEn = this._getMaxArray(points, 'entries');
        var panelW = parseFloat(panel.css('width'));
        var panelH = parseFloat(panel.css('height')) - (this.marginTop + this.marginBottom);
        weight.X = maxSe != 0 ? (panelW / maxSe) : 0;
        weight.Y = maxEn != 0 ? (panelH / maxEn) : 0;
        return weight;
    };
daniel cebrian committed
1595

1596 1597 1598 1599 1600 1601 1602 1603 1604
    videojs.AnStat.prototype._getMaxArray = function(points, variable) {
        var highest = 0;
        var tmp;
        for (var index in points) {
            tmp = points[index][variable];
            if (tmp > highest) highest = tmp;
        }
        return highest;
    };
daniel cebrian committed
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    videojs.AnStat.prototype._getPoints = function() {
        var points = [];
        var allannotations = this.an.annotator.plugins.Store.annotations;
        for (var index in allannotations) {
            var an = allannotations[index];
            var start, end;
            if (this.an._isVideoJS(an)) {
                start = an.rangeTime.start;
                end = an.rangeTime.end;
                // start
                if (!this._isFound(points, start)) {
                    points.push({
                        second:an.rangeTime.start,
                        entries:this._getNumberAnnotations(start)
                    });
                    if (an.rangeTime.start == an.rangeTime.end){ // is a point
                        points.push({
                            second:an.rangeTime.end,
                            entries:this._getNumberAnnotations(end, true)
                        });
                    }
                }
                // end
                if (!this._isFound(points, end)) {
                    points.push({
                        second:an.rangeTime.end,
                        entries:this._getNumberAnnotations(end, true)
                    });
                }
                    
                found = false;
            }
        }
        points.sort(function(a, b) {
            return parseFloat(a.second) - parseFloat(b.second)
        });
        return points;
    };
daniel cebrian committed
1644

1645 1646 1647 1648 1649 1650 1651 1652
    videojs.AnStat.prototype._isFound = function(array, elem) {
        var found = false;
        for (var indexA in array) {
            if(typeof array[indexA].second !== 'undefined' && array[indexA].second == elem)
                found = true;
        }
        return found;
    };
daniel cebrian committed
1653

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
    videojs.AnStat.prototype._getNumberAnnotations = function(time, end) {
        var num = (typeof end !== 'undefined' && end) ? -1 : 0;
        var allannotations = this.an.annotator.plugins['Store'].annotations;
        for (var index in allannotations) {
            var an = allannotations[index];
            if (this.an._isVideoJS(an)) {
                if(an.rangeTime.start <= time && an.rangeTime.end >= time)
                    num++;
            }
        }
        return num;
    };
daniel cebrian committed
1666

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    // -- Player--> ControlBar--> BackAnDisplayScroll

    /**
     * The background annotations panel
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.BackAnDisplayScroll = videojs.Component.extend({
          /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
            this.UpValue = 0.1;
            this.currentValue = 0;
        }
    });

    videojs.BackAnDisplayScroll.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        this.mousedownID = -1;
        var self = this;
        var direction;
            
        // Firefox
        $(this.an.AnDisplay.el_).bind('DOMMouseScroll', function(e) {
            if (e.originalEvent.detail > 0)
                direction = self.UpValue;
            else 
                direction = -self.UpValue;
            self.an.backDSBarSel.setPosition(self.getPercentScroll() + direction);
            return false;
        });
daniel cebrian committed
1702

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
        // IE, Opera, Safari
        $(this.an.AnDisplay.el_).bind('mousewheel', function(e) {
            if (e.originalEvent.wheelDelta < 0) 
                direction = self.UpValue;
            else 
                direction = -self.UpValue;
            self.an.backDSBarSel.setPosition(self.getPercentScroll() + direction);
            return false;
        });
    };
daniel cebrian committed
1713

1714 1715 1716 1717 1718 1719
    videojs.BackAnDisplayScroll.prototype.options_ = {
        children: {
            'BackAnDisplayScrollBar': {},
            'BackAnDisplayScrollTime': {},
        }
    };
daniel cebrian committed
1720

1721 1722 1723 1724 1725 1726
    videojs.BackAnDisplayScroll.prototype.createEl = function() {
      return videojs.Component.prototype.createEl.call(this, 'div', {
        className: 'vjs-scroll-anpanel-annotation',
        innerHTML: '<div class="vjs-up-scroll-annotation"></div><div class="vjs-down-scroll-annotation"></div>',
      });
    };
daniel cebrian committed
1727

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    videojs.BackAnDisplayScroll.prototype.onMouseDown = function(event) {
        var self = this;
        if (event.target.className === 'vjs-scrollbar-anpanel-annotation') {
            // change position with a click in the scrollbar
            this.an.backDSBarSel.onMouseMove(event);
            return false;
        } else if (event.target.className === 'vjs-scrollbar-selector') {
            // change position with scrollbar
            // this event is controlled by this.an.backDSBarSel
            return false;
        } else {
            // change position with arrows
            var direction = event.target.className=='vjs-down-scroll-annotation' ? this.UpValue : -this.UpValue;
            videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
            if(parseInt(this.mousedownID, 10) === -1) {  // Prevent multimple loops!
                this.mousedownID = setInterval(function () {
                    var pos = Math.max(0, Math.min(1, self.getPercentScroll() + direction));
                    self.an.backDSBarSel.setPosition(pos);
                }, 100);
            }
        }
    };
daniel cebrian committed
1750

1751 1752 1753 1754 1755 1756 1757 1758
    videojs.BackAnDisplayScroll.prototype.onMouseUp = function(event) {
        videojs.off(document, "mouseup", this.onMouseUp, false);
        var self = this;
        if(parseInt(this.mousedownID, 10) != -1) { // Only stop if exists
            clearInterval(this.mousedownID);
            self.mousedownID = -1;
        }
    };
daniel cebrian committed
1759

1760 1761 1762 1763 1764 1765
    videojs.BackAnDisplayScroll.prototype.getPercentScroll = function() {
        var scroll = this.an.AnDisplay.el_;
        var maxscroll = scroll.scrollHeight - scroll.offsetHeight;
        var currentValue = scroll.scrollTop;
        return Math.max(0, Math.min(1, maxscroll !== 0 ? (currentValue / maxscroll) : 0));
    };
daniel cebrian committed
1766

1767 1768 1769 1770 1771 1772
    videojs.BackAnDisplayScroll.prototype.setPercentScroll = function(percent) {
        var scroll = this.an.AnDisplay.el_;
        var maxscroll = scroll.scrollHeight-scroll.offsetHeight;
        percent = Math.max(0, Math.min(1, percent ? percent : 0));
        scroll.scrollTop = Math.round(maxscroll * percent);
    };
daniel cebrian committed
1773 1774 1775



1776
    // -- Player--> ControlBar--> BackAnDisplayScroll--> BackAnDisplayScrollBar
daniel cebrian committed
1777

1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
    /**
     * The Scroll bar for the display
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.BackAnDisplayScrollBar = videojs.Component.extend({
          /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
1791

1792
    videojs.BackAnDisplayScrollBar.prototype.init_ = function() {};
daniel cebrian committed
1793

1794 1795 1796 1797 1798
    videojs.BackAnDisplayScrollBar.prototype.options_ = {
        children: {
            'ScrollBarSelector': {},
        }
    };
daniel cebrian committed
1799

1800 1801 1802 1803 1804
    videojs.BackAnDisplayScrollBar.prototype.createEl = function() {
      return videojs.Component.prototype.createEl.call(this, 'div', {
        className: 'vjs-scrollbar-anpanel-annotation',
      });
    };
daniel cebrian committed
1805 1806 1807



1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
    // -- Player--> ControlBar--> BackAnDisplayScroll--> BackAnDisplayScrollBar--> ScrollBarSelector

    /**
     * The Scroll bar for the display
     * @param {videojs.Player|Object} player
     * @param {Object=} options
     * @constructor
     */
     
    videojs.ScrollBarSelector = videojs.Component.extend({
          /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
            this.on('mousedown', this.onMouseDown);
        }
    });

    videojs.ScrollBarSelector.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
        videojs.addClass(this.an.backDSBar.el_, 'disable');
    };
daniel cebrian committed
1830 1831


1832 1833 1834 1835 1836
    videojs.ScrollBarSelector.prototype.createEl = function() {
        return videojs.Component.prototype.createEl.call(this, 'div', {
            className: 'vjs-scrollbar-selector',
        });
    };
daniel cebrian committed
1837

1838 1839 1840 1841 1842
    videojs.ScrollBarSelector.prototype.onMouseDown = function(event) {
        event.preventDefault();
        videojs.on(document, "mousemove", videojs.bind(this, this.onMouseMove));
        videojs.on(document, "mouseup", videojs.bind(this, this.onMouseUp));
    }
daniel cebrian committed
1843

1844 1845 1846 1847
    videojs.ScrollBarSelector.prototype.onMouseUp = function(event) {
        videojs.off(document, "mousemove", this.onMouseMove, false);
        videojs.off(document, "mouseup", this.onMouseUp, false);
    };
daniel cebrian committed
1848

1849 1850 1851 1852 1853
    videojs.ScrollBarSelector.prototype.onMouseMove = function(event) {
        var top = this.calculateDistance(event);
        top = this.parseMaxPercent(top); // set the max value fixing the height of the handle
        this.setPosition(top);
    }
daniel cebrian committed
1854

1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
    videojs.ScrollBarSelector.prototype.calculateDistance = function(event) {
        var scrollY = this.getscrollY();
        var scrollH = this.getscrollHeight();
        var handleH = this.getHeight();
        
        // Adjusted X and Width, so handle doesn't go outside the bar
        scrollY = scrollY + (handleH);
        scrollH = scrollH - (handleH);
        // Adjusted X and Width, so handle doesn't go outside the bar
        // Percent that the click is through the adjusted area
        return Math.max(0, Math.min(1, (event.pageY - scrollY) / scrollH));
    };
daniel cebrian committed
1867

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
    videojs.ScrollBarSelector.prototype.getscrollHeight = function() {
        return this.el_.parentNode.offsetHeight;
    };
    videojs.ScrollBarSelector.prototype.getscrollY = function() {
        return videojs.findPosition(this.el_.parentNode).top;
    };
    videojs.ScrollBarSelector.prototype.getHeight = function() {
        return this.el_.offsetHeight;
    };
    videojs.ScrollBarSelector.prototype.parseMaxHeight = function(top) {
        var scrollH = this.getscrollHeight();
        var handleH = this.getHeight();
        var percent = handleH / scrollH;
        return Math.max(0, Math.min(1 - percent, top));
    };
daniel cebrian committed
1883

1884 1885 1886 1887 1888 1889 1890 1891 1892
    videojs.ScrollBarSelector.prototype.parseMaxPercent = function(top) {
        var scrollH = this.getscrollHeight();
        var handleH = this.getHeight();
        var percent = handleH / scrollH;
        var newTop = top;
        if (top >= (1 - percent))
            newTop = 1;
        return newTop;
    };
daniel cebrian committed
1893

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
    videojs.ScrollBarSelector.prototype.setPosition = function(top, showBar) {
        var showBar = typeof showBar !== 'undefined' ? showBar : true;
        
        // Check for invalid position
        if (isNaN(top)) 
            return false;
        
        // Check if there is enough annotations to scroll
        if (!this.isScrollable())
            return false;
            
        // Show the Scrollbar
        if (showBar) {
            videojs.removeClass(this.an.backDSBar.el_, 'disable')
        }
        
        // Alias
        var Obj = this.el_;
        var scroll = this.an.BackAnDisplayScroll;
        var scrollTime = this.an.backDSTime;
        
        Obj.style.top = (this.parseMaxHeight(top) * 100) + '%';
        scroll.setPercentScroll(top);
        
        // Set the times in the scroll time panel
        scrollTime.setTimes();
        
        // Hide the Scrollbar in 1 sec
        if(showBar) {
            var _self = this;
            if (typeof this.Timeout !== 'undefined')
                clearTimeout(this.Timeout);
            this.Timeout = window.setTimeout(function () {
                videojs.addClass(_self.an.backDSBar.el_, 'disable');
            }, 1000);
        }
        
        // set current position
        this.an.BackAnDisplayScroll.currentValue = top;
        return true;
    }
daniel cebrian committed
1935

1936 1937 1938 1939 1940 1941 1942 1943 1944
    videojs.ScrollBarSelector.prototype.isScrollable = function() {
        var scroll = this.an.AnDisplay.el_;
        var emtoPx = parseFloat($(scroll).find('.annotation').css('height'));
        var minTop = parseInt(scroll.offsetHeight/emtoPx);
        
        // Count visible annotations in Panel
        var count = this.an.AnDisplay.countVisibles();
        return (count > minTop);
    }
daniel cebrian committed
1945 1946 1947



1948
    // -- Player--> ControlBar--> BackAnDisplayScroll--> BackAnDisplayScrollTime
daniel cebrian committed
1949

1950 1951 1952 1953 1954 1955
    videojs.BackAnDisplayScrollTime = videojs.Component.extend({
          /** @constructor */
        init: function(player, options) {
            videojs.Component.call(this, player, options);
        }
    });
daniel cebrian committed
1956

1957 1958 1959 1960
    videojs.BackAnDisplayScrollTime.prototype.init_ = function() {
        this.rs = this.player_.rangeslider;
        this.an = this.player_.annotations;
    };
daniel cebrian committed
1961

1962 1963 1964 1965 1966 1967
    videojs.BackAnDisplayScrollTime.prototype.createEl = function() {
      return videojs.Component.prototype.createEl.call(this, 'div', {
        className: 'vjs-scrolltime-anpanel-annotation',
        innerHTML: '<div class="vjs-up-scrolltime-annotation"><span class="vjs-time-text"></span></div><div class="vjs-down-scrolltime-annotation"><span class="vjs-time-text"></span></div>',
      });
    };
daniel cebrian committed
1968

1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
    videojs.BackAnDisplayScrollTime.prototype.setTimes = function() {
        var AnPos = this.getAnnotationPosition();
        var AnEl = this.getElements(AnPos);
        var AnTimes = this.getTimes(AnEl);
        if (AnTimes.top != 'Invalid Date') {
            $(this.el_).find('.vjs-up-scrolltime-annotation')[0].style.visibility = '';
            $(this.el_).find('.vjs-up-scrolltime-annotation span')[0].innerHTML = AnTimes.top;
        } else {
            $(this.el_).find('.vjs-up-scrolltime-annotation')[0].style.visibility = 'hidden';
        }
        if (AnTimes.bottom != 'Invalid Date') {
            $(this.el_).find('.vjs-down-scrolltime-annotation')[0].style.visibility = '';
            $(this.el_).find('.vjs-down-scrolltime-annotation span')[0].innerHTML = AnTimes.bottom;
        } else {
            $(this.el_).find('.vjs-down-scrolltime-annotation')[0].style.visibility = 'hidden';
        }
    };
daniel cebrian committed
1986

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
    videojs.BackAnDisplayScrollTime.prototype.getAnnotationPosition = function() {
        var backDSBarSel = this.an.backDSBarSel;
        var percent = backDSBarSel.parseMaxPercent(parseFloat(backDSBarSel.el_.style.top) / 100);
        var scroll = this.an.AnDisplay.el_;
        var maxTop = scroll.scrollHeight;
        var minTop = scroll.offsetHeight;
        var maxBottom = maxTop - minTop;
        var minBottom = 0;
        var pos = {};
        
        percent = percent || 0;
        pos.top = Math.max(minTop, Math.min(maxTop, maxBottom * percent + scroll.offsetHeight));
        pos.bottom = Math.max(minBottom, Math.min(maxBottom, maxBottom * percent));
        return pos;
    };
daniel cebrian committed
2002

2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
    videojs.BackAnDisplayScrollTime.prototype.getElements = function(AnPos) {
        var AnPos = AnPos || {};
        var scroll = this.an.AnDisplay.el_;
        var emtoPx = parseFloat($(scroll).find('.annotation').css('height'));
        var maxTop = parseInt(scroll.scrollHeight / emtoPx);
        var minTop = parseInt(scroll.offsetHeight / emtoPx);
        var maxBottom = (maxTop - minTop);
        var minBottom = 0;
        var AnEl = {};
        AnEl.top = Math.max(minTop, Math.min(maxTop, parseInt(AnPos.top / emtoPx)));
        AnEl.bottom = Math.max(minBottom, Math.min(maxBottom, parseInt(AnPos.bottom / emtoPx)));
        return AnEl;
    };
daniel cebrian committed
2016

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
    videojs.BackAnDisplayScrollTime.prototype.getTimes = function(AnEl) {
        var AnEl = AnEl || {};
        var AnTimes = {};
        var TopEl, BottomEl, AnTop, AnBottom;
        var AnArray = $.makeArray(this.an.AnDisplay.el_.children);
        AnEl.top = AnEl.top || 0;
        AnEl.bottom = AnEl.bottom || 0;
        
        // Get HTML Elements
        var count = 0;
        var lastEl;
        for (var index in AnArray) {
            var an = AnArray[index];
            if (an.style.display !== 'none') {
                if (count == AnEl.bottom) {
                    TopEl = an;
                } else if (count == AnEl.top) {
                    BottomEl = an;
                }
                lastEl = an;
                count++;
            }
        }
        if (typeof BottomEl === 'undefined')
            BottomEl = lastEl;
            
        // Annotation Element
        AnTop = typeof TopEl !== 'undefined' ? $.data(TopEl, 'annotation') : undefined;
        AnBottom = typeof BottomEl !== 'undefined' ? $.data(BottomEl, 'annotation') : undefined;
        // Update of the element
        AnTimes.top = (typeof AnTop !== 'undefined' && typeof AnTop.updated !== 'undefined') ? AnTop.updated : '';
        AnTimes.bottom = (typeof AnBottom !=='undefined' && typeof AnBottom.updated !== 'undefined') ? AnBottom.updated : '';
        // Format
        AnTimes.top = new Date(AnTimes.top !== '' ? createDateFromISO8601(AnTimes.top) : '');
        AnTimes.bottom = new Date(AnTimes.bottom != '' ? createDateFromISO8601(AnTimes.bottom) : '');
        return AnTimes;
    };
}) ();
daniel cebrian committed
2055

2056
// ----------------Plugin for Annotator to setup videojs---------------- //
daniel cebrian committed
2057 2058

Annotator.Plugin.VideoJS = (function(_super) {
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
    __extends(VideoJS, _super);

    // constructor
    function VideoJS() {
        this.pluginSubmit = __bind(this.pluginSubmit, this);
        _ref = VideoJS.__super__.constructor.apply(this, arguments);
        this.__indexOf = [].indexOf || function(item) { 
            for (var i = 0, l = this.length; i < l; i++) { 
                if (i in this && this[i] === item) 
                    return i; 
            } 
            return -1; 
        };
        return _ref;
    };

    VideoJS.prototype.field = null;
    VideoJS.prototype.input = null;

    VideoJS.prototype.pluginInit = function() {
        console.log("VideoJS-pluginInit");
        // Check that annotator is working
        if (!Annotator.supported()) {
            return;
        }
        
        // -- Editor
        this.field = this.annotator.editor.addField({
            id: 'vjs-input-rangeTime-annotations',
            type: 'input', // options (textarea, input, select, checkbox)
            submit: this.pluginSubmit,
            EditVideoAn: this.EditVideoAn
        });
        
        // Modify the element created with annotator to be an invisible span
        var select = '<li><span id="vjs-input-rangeTime-annotations"></span></li>';
        var newfield = Annotator.$(select);
        Annotator.$(this.field).replaceWith(newfield);
        this.field = newfield[0];
        
        // -- Listener for Open Video Annotator
        this.initListeners();
        
        return this.input = $(this.field).find(':input');
    };
    

    // New JSON for the database
    VideoJS.prototype.pluginSubmit = function(field, annotation) {
        console.log("Plug-pluginSubmit");
        // Select the new JSON for the Object to save
        if (this.EditVideoAn()) {
            var annotator = this.annotator;
            var index = annotator.editor.VideoJS;
            var player = annotator.mplayer[index];
            var rs = player.rangeslider;
            var time = rs.getValues();
            var isYoutube = (player && typeof player.techName !== 'undefined') ? (player.techName === 'Youtube') : false;
            var isNew = typeof annotation.media === 'undefined';
            var ext;
            var type = player.options_.sources[0].type.split("/") || "";
            
            if (isNew) 
                annotation.media = typeof type[0] !== 'undefined' ? type[0] : "video"; // - media (by default: video)
            
            annotation.target = annotation.target || {}; // - target
            annotation.target.container = player.id_ || ""; // - target.container
            annotation.target.src = player.options_.sources[0].src || ""; // - target.src (media source)
            ext = (player.options_.sources[0].src.substring(player.options_.sources[0].src.lastIndexOf("."))).toLowerCase(); 
            ext = isYoutube ? 'Youtube' : ext; // The extension for youtube
            annotation.target.ext = ext || ""; // - target.ext (extension)
            annotation.rangeTime =     annotation.rangeTime || {};    // - rangeTime
            annotation.rangeTime.start = time.start || 0; // - rangeTime.start
            annotation.rangeTime.end = time.end || 0; // - rangeTime.end
            annotation.updated = new Date().toISOString(); // - updated
            if (typeof annotation.created === 'undefined')
                annotation.created = annotation.updated; // - created
            
            // show the new annotation
            var eventAn = isNew ? "annotationCreated" : "annotationUpdated";
            function afterFinish(){
                player.annotations.showAnnotation(annotation);
                annotator.unsubscribe(eventAn, afterFinish);
            };
            annotator.subscribe(eventAn, afterFinish); // show after the annotation is in the back-end
        } else {
            if (typeof annotation.media === 'undefined')
                annotation.media = "text"; // - media
daniel cebrian committed
2147
            
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
            annotation.updated = new Date().toISOString(); // - updated
            
            if (typeof annotation.created === 'undefined')
                annotation.created = annotation.updated; // - created
        }
        return annotation.media;
    };
    
    
    // ------ Methods    ------ //
    // Detect if we are creating or editing a video-js annotation
    VideoJS.prototype.EditVideoAn =  function () {
        var wrapper = $('.annotator-wrapper').parent()[0];
        var annotator = window.annotator = $.data(wrapper, 'annotator');
        var isOpenVideojs = (typeof annotator.mplayer !== 'undefined');
        var VideoJS = annotator.editor.VideoJS;
        return (isOpenVideojs && typeof VideoJS !== 'undefined' && VideoJS !== -1);
    };
    
    
    // Detect if the annotation is a video-js annotation
    VideoJS.prototype.isVideoJS = function (an) {
        var wrapper = $('.annotator-wrapper').parent()[0];
        var annotator = window.annotator = $.data(wrapper, 'annotator');
        var rt = an.rangeTime;
        var isOpenVideojs = (typeof annotator.mplayer !== 'undefined');
        var isVideo = (typeof an.media !== 'undefined' && (an.media === 'video' || an.media === 'audio'));
        var isNumber = (typeof rt !== 'undefined' && !isNaN(parseFloat(rt.start)) && isFinite(rt.start) && !isNaN(parseFloat(rt.end)) && isFinite(rt.end));
        return (isOpenVideojs && isVideo && isNumber);
    };
    
    // Delete Video Annotation
    VideoJS.prototype._deleteAnnotation = function(an) {
        var target = an.target || {};
        var container = target.container || {};
        var player = this.annotator.mplayer[container];
        
        var annotator = this.annotator;
        var annotations = annotator.plugins.Store.annotations;
        var tot = typeof annotations !== 'undefined' ? annotations.length : 0;
        var attempts = 0; // max 100
            
        // This is to watch the annotations object, to see when is deleted the annotation
        var ischanged = function() {
            var new_tot = annotator.plugins.Store.annotations.length;
            if (attempts < 100)
                setTimeout(function() {
                    if (new_tot !== tot) {
                        player.annotations.refreshDisplay(); // Reload the display of annotation
                    } else {
daniel cebrian committed
2198 2199 2200
                        attempts++;
                        ischanged();
                    }
2201
                }, 100); // wait for the change in the annotations
daniel cebrian committed
2202 2203
        };
        ischanged();
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
        
        player.rangeslider.hide(); // Hide Range Slider
    };
    
    
    // --Listeners
    VideoJS.prototype.initListeners = function () {
        var wrapper = $('.annotator-wrapper').parent()[0];
        var annotator = $.data(wrapper, 'annotator');
        var EditVideoAn = this.EditVideoAn;
        var isVideoJS = this.isVideoJS;
        var self = this;
            
        // local functions
        // -- Editor
        function annotationEditorHidden(editor) {
            if (EditVideoAn()){
                var index = annotator.editor.VideoJS;
                annotator.mplayer[index].rangeslider.hide(); // Hide Range Slider
                annotator.an[index].refreshDisplay(); // Reload the display of annotations
            }
            annotator.editor.VideoJS=-1;
            annotator.unsubscribe("annotationEditorHidden", annotationEditorHidden);
        };
        function annotationEditorShown(editor, annotation) {
            for (var index in annotator.an){
                annotator.an[index].editAnnotation(annotation, editor);
            }
            annotator.subscribe("annotationEditorHidden", annotationEditorHidden);
        };
        // -- Annotations
        function annotationDeleted(annotation) {
            
            if (isVideoJS(annotation))
                self._deleteAnnotation(annotation);
        };
        // -- Viewer
        function hideViewer(){
            for (var index in annotator.an) {
                annotator.an[index].AnDisplay.onCloseViewer();
            }
            annotator.viewer.unsubscribe("hide", hideViewer);
        };
        function annotationViewerShown(viewer, annotations) {
            
            var separation = viewer.element.hasClass(viewer.classes.invert.y) ? 5 : -5;
            var newpos = {
                top: parseFloat(viewer.element[0].style.top)+separation,
                left: parseFloat(viewer.element[0].style.left)
            };
            viewer.element.css(newpos);
            
            // Remove the time to wait until disapear, to be more faster that annotator by default
            viewer.element.find('.annotator-controls').removeClass(viewer.classes.showControls);
            
            annotator.viewer.subscribe("hide", hideViewer);
        };    
        
        // subscribe to Annotator
        annotator.subscribe("annotationEditorShown", annotationEditorShown)
            .subscribe("annotationDeleted", annotationDeleted)
            .subscribe("annotationViewerShown", annotationViewerShown);
    };
    return VideoJS;
daniel cebrian committed
2268 2269 2270 2271 2272

})(Annotator.Plugin);



2273
// ----------------PUBLIC OBJECT TO CONTROL THE ANNOTATIONS---------------- //
daniel cebrian committed
2274

2275
// The name of the plugin that the user will write in the html
daniel cebrian committed
2276 2277 2278
OpenVideoAnnotation = ("OpenVideoAnnotation" in window) ? OpenVideoAnnotation : {};

OpenVideoAnnotation.Annotator = function (element, options) {
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
    // local variables
    var $ = jQuery;
    var options = options || {};
    options.optionsAnnotator = options.optionsAnnotator || {};
    options.optionsVideoJS = options.optionsVideoJS || {};
    options.optionsRS = options.optionsRS || {};
    options.optionsOVA = options.optionsOVA || {};
    
    
    // if there isn't store optinos it will create a uri and limit variables for the Back-end of Annotations 
    if (typeof options.optionsAnnotator.store === 'undefined')
        options.optionsAnnotator.store = {};
    var store = options.optionsAnnotator.store;
    if (typeof store.annotationData === 'undefined')
        store.annotationData = {};
    if (typeof store.annotationData.uri === 'undefined'){
        var uri = location.protocol + '//' + location.host + location.pathname;
        store.annotationData.store = {uri: uri};
    }
    if (typeof store.loadFromSearch === 'undefined')
        store.loadFromSearch = {};
    if (typeof store.loadFromSearch.uri === 'undefined')
        store.loadFromSearch.uri = uri;
    if (typeof store.loadFromSearch.limit === 'undefined')
        store.loadFromSearch.limit = 10000;
    
    // global variables
    this.currentUser = null;

    // -- Init all the classes --/
    // Annotator
    this.annotator = $(element).annotator(options.optionsAnnotator.annotator).data('annotator');
    options.optionsOVA.optionsAnnotator = options.optionsAnnotator.annotator; // send the Annotator's options to OVA
    
    
    // Video-JS
    /*    
        mplayers -> Array with the html of all the video-js
        mplayer -> Array with all the video-js that will be in the plugin
    */
daniel cebrian committed
2319 2320
    var mplayers = $(element).find('div .video-js').toArray();
    var mplayer = this.mplayer = {};
2321 2322 2323 2324 2325 2326 2327 2328 2329
    for (var index in mplayers) {
        var id = mplayers[index].id;
        var mplayer_ = videojs(mplayers[index], options.optionsVideoJS);
        // solve a problem with firefox. In Firefox the src() function is loaded before charge the optionsVideoJS, and the techOrder are not loaded
        if (vjs.IS_FIREFOX && typeof options.optionsVideoJS.techOrder !== 'undefined'){
            mplayer_.options_.techOrder = options.optionsVideoJS.techOrder;
            mplayer_.src(mplayer_.options_['sources']);
        }
        this.mplayer[id] = mplayer_;
daniel cebrian committed
2330
    }
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
    
    
    // Video-JS
    this.annotator.an = {}; // annotations video-js plugin to annotator
    for (var index in this.mplayer) {
        // to be their own options is necessary to extend deeply the options with all the childrens
        this.mplayer[index].rangeslider($.extend(true, {}, options.optionsRS));
        this.mplayer[index].annotations($.extend(true, {}, options.optionsOVA));
        this.annotator.an[index]=this.mplayer[index].annotations;
    }

    
    // -- Experimental Global function for Open Video Annotator -- //
    this.setCurrentUser = function (user) {
        this.currentUser = user;
        this.annotator.plugins["Permissions"].setUser(user);
    }
    
    // Local function to setup the keyboard listener
    var focusedPlayer = this.focusedPlayer = ''; // variable to know the focused player
    var lastfocusPlayer = this.lastfocusPlayer = ''; 
    
    function onKeyUp(e) {
        // skip the text areas
        if (e.target.nodeName.toLowerCase() !== 'textarea')
            mplayer[focusedPlayer].annotations.pressedKey(e.which);
    };
    
    (this._setupKeyboard = function() {
        $(document).mousedown(function(e) {
            focusedPlayer = '';
            
            // Detects if a player was click
            for (var index in mplayer) {
                if ($(mplayer[index].el_).find(e.target).length)
                    focusedPlayer = mplayer[index].id_;
            }
            
            // Enter if we change the focus between player or go out of the player
            if (lastfocusPlayer !== focusedPlayer) {
                $(document).off("keyup", onKeyUp); // Remove the last listener
                // set the key listener
                if (focusedPlayer !== '')
                    $(document).on("keyup", onKeyUp);
            }
            
            lastfocusPlayer = focusedPlayer;
        });
        
    }) (this);
    
    // -- Activate all the plugins -- //
    // Annotator
    if (typeof options.optionsAnnotator.auth !== 'undefined')
        this.annotator.addPlugin('Auth', options.optionsAnnotator.auth);
        
    if (typeof options.optionsAnnotator.permissions !== 'undefined')
        this.annotator.addPlugin("Permissions", options.optionsAnnotator.permissions);
    
    if (typeof options.optionsAnnotator.store !== 'undefined')
        this.annotator.addPlugin("Store", options.optionsAnnotator.store);

    if (typeof options.optionsAnnotator.diacriticMarks !== 'undefined' && typeof Annotator.Plugin["Diacritics"] === 'function')
        this.annotator.addPlugin("Diacritics", options.optionsAnnotator.diacriticMarks);
    
daniel cebrian committed
2396
    if (typeof Annotator.Plugin["Geolocation"] === 'function') 
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
        this.annotator.addPlugin("Geolocation", options.optionsAnnotator.geolocation);
        
    if (typeof Annotator.Plugin["Share"] === 'function') 
        this.annotator.addPlugin("Share", options.optionsAnnotator.share);
        
    this.annotator.addPlugin("VideoJS"); // it is obligatory to have
    
    if (typeof Annotator.Plugin["RichText"] === 'function') 
        this.annotator.addPlugin("RichText", options.optionsAnnotator.richText);
        
    if (typeof Annotator.Plugin["Reply"] === 'function') 
        this.annotator.addPlugin("Reply");
daniel cebrian committed
2409
            
2410
    if (typeof Annotator.Plugin["Flagging"] === 'function') 
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
        this.annotator.addPlugin("Flagging");

    if (typeof options.optionsAnnotator.highlightTags !== 'undefined')
        this.annotator.addPlugin("HighlightTags", options.optionsAnnotator.highlightTags);
    
    // Will be add the player and the annotations plugin for video-js in the annotator
    this.annotator.mplayer = this.mplayer;
    this.annotator.editor.VideoJS = -1;
    
    this.options = options;
    
    return this;
daniel cebrian committed
2423 2424 2425 2426
}



2427
// ----------------Local Functions for Open Video Annotator---------------- //
daniel cebrian committed
2428

2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
// --local functions
// if the annotation is a video return true
OpenVideoAnnotation.Annotator.prototype._isVideo = function(an) {
    // Detect if the annotation is a Open Video Annotation
    var an = an || {};
    var rt = an.rangeTime;
    var isVideo = (typeof an.media !== 'undefined' && (an.media === 'video' || an.media === 'audio'));
    var hasContainer = (typeof an.target !== 'undefined' && typeof an.target.container !== 'undefined');
    var isNumber = (typeof rt !== 'undefined' && !isNaN(parseFloat(rt.start)) && isFinite(rt.start) && !isNaN(parseFloat(rt.end)) && isFinite(rt.end));
    return (isVideo && hasContainer && isNumber);
daniel cebrian committed
2439 2440
}

2441 2442 2443
// if the ova has been loaded and the video is opened return true
OpenVideoAnnotation.Annotator.prototype._isloaded = function(idElem) {
    return typeof this.mplayer[idElem].annotations.loaded !== 'undefined';
daniel cebrian committed
2444 2445
}

2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
// ----------------Public Functions for Open Video Annotator---------------- //

// Create a new video annotation
OpenVideoAnnotation.Annotator.prototype.newVideoAn = function(idElem) {
    var player = this.mplayer[idElem];
    if (typeof player.play !== 'undefined') {
        player.play();
        player.one('playing', function() {
            player.annotations.newan();
            $('html, body').animate({
                scrollTop: $("#" + player.id_).offset().top
            }, 'slow');
            player.pause();
        });
    }
daniel cebrian committed
2461 2462
};

2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
// Show the annotation display
OpenVideoAnnotation.Annotator.prototype.showDisplay = function(idElem) {
    if (this._isloaded(idElem))
        return this.mplayer[idElem].annotations.showDisplay();
};

// Hide the annotation display
OpenVideoAnnotation.Annotator.prototype.hideDisplay = function(idElem) {
    if (this._isloaded(idElem))
        return this.mplayer[idElem].annotations.hideDisplay();
};

// Refresh the annotation display
OpenVideoAnnotation.Annotator.prototype.refreshDisplay = function(idElem) { 
    if (this._isloaded(idElem))
        return this.mplayer[idElem].annotations.hideDisplay();
};

// Set the position of the big new annotation button
OpenVideoAnnotation.Annotator.prototype.setposBigNew = function(idElem, position) {
    if (this._isloaded(idElem))
        return this.mplayer[idElem].annotations.setposBigNew(position);
};

OpenVideoAnnotation.Annotator.prototype.playTarget = function (annotationId) {
    var allannotations = this.annotator.plugins.Store.annotations;
    var ovaId = annotationId;
    var mplayer = this.mplayer;
    
    for (var item in allannotations) {
        var an = allannotations[item];
        if (typeof an.id != 'undefined' && an.id == ovaId) { // this is the annotation
            if (this._isVideo(an)) { // It is a video
                for (var index in mplayer) {
                    var player = mplayer[index];
                    if (player.id_ == an.target.container && player.tech.options_.source.src === an.target.src){
                        var anFound = an;
                        
                        var playFunction = function() {
                            // Fix problem with youtube videos in the first play. The plugin don't have this trigger
                            if (player.techName === 'Youtube') {
                                var startAPI = function() {
                                    player.annotations.showAnnotation(anFound);
                                }
                                if (player.annotations.loaded)
                                    startAPI();
                                else
                                    player.one('loadedRangeSlider', startAPI); // show Annotations once the RangeSlider is loaded
                            } else {
                                player.annotations.showAnnotation(anFound);
                            }
                            
                            $('html, body').animate({
                                scrollTop: $("#"+player.id_).offset().top
                            }, 'slow');
                        };
                        if (player.paused()) {
                            player.play();
                            player.one('playing', playFunction);
                        } else {
                            playFunction();
                        }
                        
                        return false; // this will stop the code to not set a new player.one.
                    }
                }
            } else { // It is a text
                var hasRanges = typeof an.ranges !== 'undefined' && typeof an.ranges[0] !== 'undefined';
                var startOffset = hasRanges ? an.ranges[0].startOffset : '';
                var endOffset = hasRanges ? an.ranges[0].endOffset : '';
    
                if (typeof startOffset !== 'undefined' && typeof endOffset !== 'undefined') { 
                
                    $(an.highlights).parent().find('.annotator-hl').removeClass('api'); 
                    // change the color
                    $(an.highlights).addClass('api'); 
                    // animate to the annotation
                    $('html, body').animate({
                        scrollTop: $(an.highlights[0]).offset().top
                    }, 'slow');
                }
            }
        }
    }
daniel cebrian committed
2547
}