player/js/metadataplayer-core.js
changeset 3 5a4dd4e6bbe7
child 18 b8a45e2fd6fd
equal deleted inserted replaced
2:30e0ed21127c 3:5a4dd4e6bbe7
       
     1 
       
     2 /* 
       
     3  *
       
     4   __  __      _            _       _              _                       
       
     5  |  \/  | ___| |_ __ _  __| | __ _| |_ __ _ _ __ | | __ _ _   _  ___ _ __ 
       
     6  | |\/| |/ _ \ __/ _` |/ _` |/ _` | __/ _` | '_ \| |/ _` | | | |/ _ \ '__|
       
     7  | |  | |  __/ || (_| | (_| | (_| | || (_| | |_) | | (_| | |_| |  __/ |   
       
     8  |_|  |_|\___|\__\__,_|\__,_|\__,_|\__\__,_| .__/|_|\__,_|\__, |\___|_|   
       
     9                                            |_|            |___/         
       
    10 
       
    11  *  Copyright 2010-2012 Institut de recherche et d'innovation 
       
    12  *	contributor(s) : Karim Hamidou, Samuel Huron, Raphael Velt, Thibaut Cavalie
       
    13  *	 
       
    14  *	contact@iri.centrepompidou.fr
       
    15  *	http://www.iri.centrepompidou.fr 
       
    16  *	 
       
    17  *	This software is a computer program whose purpose is to show and add annotations on a video .
       
    18  *	This software is governed by the CeCILL-C license under French law and
       
    19  *	abiding by the rules of distribution of free software. You can  use, 
       
    20  *	modify and/ or redistribute the software under the terms of the CeCILL-C
       
    21  *	license as circulated by CEA, CNRS and INRIA at the following URL
       
    22  *	"http://www.cecill.info". 
       
    23  *	
       
    24  *	The fact that you are presently reading this means that you have had
       
    25  *	knowledge of the CeCILL-C license and that you accept its terms.
       
    26 */
       
    27 /* Initialization of the namespace */
       
    28 
       
    29 if (typeof window.IriSP === "undefined") {
       
    30     window.IriSP = {};
       
    31 }
       
    32 
       
    33 if (typeof IriSP.jQuery === "undefined" && typeof window.jQuery !== "undefined" && parseFloat(window.jQuery().jquery) >= 1.7) {
       
    34     IriSP.jQuery = window.jQuery;
       
    35 }
       
    36 
       
    37 if (typeof IriSP._ === "undefined" && typeof window._ !== "undefined" && parseFloat(window._.VERSION) >= 1.4) {
       
    38     IriSP._ = window._;
       
    39 }
       
    40 /* utils.js - various utils that don't belong anywhere else */
       
    41 
       
    42 IriSP.jqEscape = function(_text) {
       
    43     return _text.replace(/(:|\.)/g,'\\$1');
       
    44 };
       
    45 
       
    46 IriSP.getLib = function(lib) {
       
    47     if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") {
       
    48         return IriSP.libFiles.cdn[lib];
       
    49     }
       
    50     if (typeof IriSP.libFiles.locations[lib] == "string") {
       
    51         return IriSP.libFiles.locations[lib];
       
    52     }
       
    53     if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") {
       
    54         return IriSP.libFiles.defaultDir + '/' + IriSP.libFiles.inDefaultDir[lib];
       
    55     }
       
    56 }
       
    57 
       
    58 IriSP._cssCache = [];
       
    59 
       
    60 IriSP.loadCss = function(_cssFile) {
       
    61     if (IriSP._(IriSP._cssCache).indexOf(_cssFile) === -1) {
       
    62         IriSP.jQuery("<link>", {
       
    63             rel : "stylesheet",
       
    64             type : "text/css",
       
    65             href : _cssFile
       
    66         }).appendTo('head');
       
    67         IriSP._cssCache.push(_cssFile);
       
    68     }
       
    69 };
       
    70 
       
    71 IriSP.textFieldHtml = function(_text, _regexp, _extend) {
       
    72     var list = [],
       
    73         positions = [],
       
    74         text = _text.replace(/(^\s+|\s+$)/g,'');
       
    75     
       
    76     function addToList(_rx, _startHtml, _endHtml) {
       
    77         while(true) {
       
    78             var result = _rx.exec(text);
       
    79             if (!result) {
       
    80                 break;
       
    81             }
       
    82             var end = _rx.lastIndex,
       
    83                 start = result.index;
       
    84             list.push({
       
    85                 start: start,
       
    86                 end: end,
       
    87                 startHtml: (typeof _startHtml === "function" ? _startHtml(result) : _startHtml),
       
    88                 endHtml: (typeof _endHtml === "function" ? _endHtml(result) : _endHtml)
       
    89             });
       
    90             positions.push(start);
       
    91             positions.push(end);
       
    92         }
       
    93     }
       
    94     
       
    95     if (_regexp) {
       
    96         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
       
    97     }
       
    98     
       
    99     addToList(/(https?:\/\/)?\w+\.\w+\S+/gm, function(matches) {
       
   100         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">'
       
   101     }, '</a>');
       
   102     addToList(/@([\d\w]{1,15})/gm, function(matches) {
       
   103         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'
       
   104     }, '</a>');
       
   105     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
       
   106     addToList(/[\n\r]+/gm, '', '<br />');
       
   107     
       
   108     IriSP._(_extend).each(function(x) {
       
   109         addToList.apply(null, x);
       
   110     });
       
   111     
       
   112     positions = IriSP._(positions)
       
   113         .chain()
       
   114         .uniq()
       
   115         .sortBy(function(p) { return parseInt(p) })
       
   116         .value();
       
   117     
       
   118     var res = "", lastIndex = 0;
       
   119     
       
   120     for (var i = 0; i < positions.length; i++) {
       
   121         var pos = positions[i];
       
   122         res += text.substring(lastIndex, pos);
       
   123         for (var j = list.length - 1; j >= 0; j--) {
       
   124             var item = list[j];
       
   125             if (item.start < pos && item.end >= pos) {
       
   126                 res += item.endHtml;
       
   127             }
       
   128         }
       
   129         for (var j = 0; j < list.length; j++) {
       
   130             var item = list[j];
       
   131             if (item.start <= pos && item.end > pos) {
       
   132                 res += item.startHtml;
       
   133             }
       
   134         }
       
   135         lastIndex = pos;
       
   136     }
       
   137     
       
   138     res += text.substring(lastIndex);
       
   139     
       
   140     return res;
       
   141     
       
   142 };
       
   143 
       
   144 IriSP.log = function() {
       
   145     if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) {
       
   146         console.log.apply(console, arguments);
       
   147     }
       
   148 };
       
   149 
       
   150 IriSP.attachDndData = function(jqSel, data) {
       
   151 	jqSel.attr("draggable", "true").on("dragstart", function(_event) {
       
   152 		var d = (typeof data === "function" ? data.call(this) : data);
       
   153 		try {
       
   154 			IriSP._(d).each(function(v, k) {
       
   155 				if (v) {
       
   156 					_event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v);
       
   157 				}
       
   158 			});
       
   159 		} catch(err) {
       
   160 			_event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d));
       
   161 		}
       
   162 	});
       
   163 };
       
   164 
       
   165 IriSP.FakeClass = function(properties) {
       
   166     var _this = this,
       
   167         noop = (function() {});
       
   168     IriSP._(properties).each(function(p) {
       
   169         _this[p] = noop
       
   170     });
       
   171 }
       
   172 
       
   173 /* js is where data is stored in a standard form, whatever the serializer */
       
   174 
       
   175 //TODO: Separate Project-specific data from Source
       
   176 
       
   177 IriSP.Model = (function (ns) {
       
   178     
       
   179     function pad(n, x, b) {
       
   180         b = b || 10;
       
   181         var s = (x).toString(b);
       
   182         while (s.length < n) {
       
   183             s = "0" + s;
       
   184         }
       
   185         return s;
       
   186     }
       
   187     
       
   188     function rand16(n) {
       
   189         return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16);
       
   190     }
       
   191     
       
   192     var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
       
   193     
       
   194     var charsub = [
       
   195         '[aáàâä]',
       
   196         '[cç]',
       
   197         '[eéèêë]',
       
   198         '[iíìîï]',
       
   199         '[oóòôö]',
       
   200         '[uùûü]'
       
   201     ];
       
   202     
       
   203     var removeChars = [
       
   204         String.fromCharCode(768), String.fromCharCode(769), String.fromCharCode(770), String.fromCharCode(771), String.fromCharCode(807),
       
   205         "{", "}", "(", ")", "[", "]", "【", "】", "、", "・", "‥", "。", "「", "」", "『", "』", "〜", ":", "!", "?", " ",
       
   206         ",", " ", ";", "(", ")", ".", "*", "+", "\\", "?", "|", "{", "}", "[", "]", "^", "#", "/"
       
   207     ];
       
   208     
       
   209 var Model = {},
       
   210     _SOURCE_STATUS_EMPTY = Model._SOURCE_STATUS_EMPTY = 0,
       
   211     _SOURCE_STATUS_WAITING = Model._SOURCE_STATUS_WAITING = 1,
       
   212     _SOURCE_STATUS_READY = Model._SOURCE_STATUS_READY = 2,
       
   213     extendPrototype = Model.extendPrototype = function(toClass, fromClass) {
       
   214         var fromP = fromClass.prototype,
       
   215             toP = toClass.prototype;
       
   216         for (var k in fromP) {
       
   217             if (fromP.hasOwnProperty(k)) {
       
   218                 toP[k] = fromP[k];
       
   219             }
       
   220         }
       
   221     },
       
   222     getUID = Model.getUID = function() {
       
   223         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
       
   224     },
       
   225     isLocalURL = Model.isLocalURL = function(url) {
       
   226         var matches = url.match(/^(\w+:)\/\/([^/]+)/);
       
   227         if (matches) {
       
   228             return(matches[1] === document.location.protocol && matches[2] === document.location.host)
       
   229         }
       
   230         return true;
       
   231     },
       
   232     regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) {
       
   233         var _testOnly = _testOnly || false,
       
   234             _iexact = _iexact || false;
       
   235         function escapeText(_text) {
       
   236             return _text.replace(/([\\\*\+\?\|\{\[\}\]\(\)\^\$\.\#\/])/gm, '\\$1');
       
   237         }
       
   238         var _source = 
       
   239             typeof _textOrArray === "string"
       
   240             ? escapeText(_textOrArray)
       
   241             : ns._(_textOrArray).map(escapeText).join("|"),
       
   242             _flags = 'im';
       
   243         if (!_testOnly) {
       
   244             _source = '(' + _source + ')';
       
   245             _flags += 'g';
       
   246         }
       
   247         if (_iexact) {
       
   248             _source = '^' + _source + '$';
       
   249         }
       
   250         return new RegExp( _source, _flags);
       
   251     },
       
   252     fullTextRegexps = Model.fullTextRegexps = function(_text) {
       
   253         var remsrc = "[\\" + removeChars.join("\\") + "]",
       
   254             remrx = new RegExp(remsrc,"gm"),
       
   255             txt = _text.toLowerCase().replace(remrx,"")
       
   256             res = [],
       
   257             charsrx = ns._(charsub).map(function(c) {
       
   258                 return new RegExp(c);
       
   259             }),
       
   260             src = "";
       
   261         for (var j = 0; j < txt.length; j++) {
       
   262             if (j) {
       
   263                 src += remsrc + "*";
       
   264             }
       
   265             var l = txt[j];
       
   266             ns._(charsub).each(function(v, k) {
       
   267                 l = l.replace(charsrx[k], v);
       
   268             });
       
   269             src += l;
       
   270         }
       
   271         return "(" + src + ")";
       
   272     },
       
   273     isoToDate = Model.isoToDate = function(_str) {
       
   274         // http://delete.me.uk/2005/03/iso8601.html
       
   275         var 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})))?)?)?)?";
       
   276         var d = _str.match(new RegExp(regexp));
       
   277     
       
   278         var offset = 0;
       
   279         var date = new Date(d[1], 0, 1);
       
   280     
       
   281         if (d[3]) { date.setMonth(d[3] - 1); }
       
   282         if (d[5]) { date.setDate(d[5]); }
       
   283         if (d[7]) { date.setHours(d[7]); }
       
   284         if (d[8]) { date.setMinutes(d[8]); }
       
   285         if (d[10]) { date.setSeconds(d[10]); }
       
   286         if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
       
   287         if (d[14]) {
       
   288             offset = (Number(d[16]) * 60) + Number(d[17]);
       
   289             offset *= ((d[15] == '-') ? 1 : -1);
       
   290         }
       
   291     
       
   292         offset -= date.getTimezoneOffset();
       
   293         time = (Number(date) + (offset * 60 * 1000));
       
   294         var _res = new Date();
       
   295         _res.setTime(Number(time));
       
   296         return _res;
       
   297     },
       
   298     dateToIso = Model.dateToIso = function(_d) {
       
   299         var d = _d ? new Date(_d) : new Date();
       
   300         return d.getUTCFullYear()+'-'  
       
   301             + pad(2, d.getUTCMonth()+1)+'-'  
       
   302             + pad(2, d.getUTCDate())+'T'  
       
   303             + pad(2, d.getUTCHours())+':'  
       
   304             + pad(2, d.getUTCMinutes())+':'  
       
   305             + pad(2, d.getUTCSeconds())+'Z'  
       
   306     };
       
   307 
       
   308 /*
       
   309  * List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
       
   310  */
       
   311 var List = Model.List = function(_directory) {
       
   312     Array.call(this);
       
   313     this.directory = _directory;
       
   314     this.idIndex = [];
       
   315     this.__events = {};
       
   316     if (typeof _directory == "undefined") {
       
   317         console.trace();
       
   318         throw "Error : new List(directory): directory is undefined";
       
   319     }
       
   320     var _this =  this;
       
   321     this.on("clear-search", function() {
       
   322         _this.searching = false;
       
   323         _this.regexp = undefined;
       
   324         _this.forEach(function(_element) {
       
   325             _element.found = undefined;
       
   326         });
       
   327         _this.trigger("search-cleared");
       
   328     })
       
   329 };
       
   330 
       
   331 List.prototype = new Array();
       
   332 
       
   333 List.prototype.hasId = function(_id) {
       
   334     return ns._(this.idIndex).include(_id);
       
   335 };
       
   336 
       
   337 /* On recent browsers, forEach and map are defined and do what we want.
       
   338  * Otherwise, we'll use the Underscore.js functions
       
   339  */
       
   340 if (typeof Array.prototype.forEach === "undefined") {
       
   341     List.prototype.forEach = function(_callback) {
       
   342         var _this = this;
       
   343         ns._(this).forEach(function(_value, _key) {
       
   344             _callback(_value, _key, _this);
       
   345         });
       
   346     }
       
   347 };
       
   348 
       
   349 if (typeof Array.prototype.map === "undefined") {
       
   350     List.prototype.map = function(_callback) {
       
   351         var _this = this;
       
   352         return ns._(this).map(function(_value, _key) {
       
   353             return _callback(_value, _key, _this);
       
   354         });
       
   355     }
       
   356 };
       
   357 
       
   358 List.prototype.pluck = function(_key) {
       
   359     return this.map(function(_value) {
       
   360         return _value[_key];
       
   361     });
       
   362 };
       
   363 
       
   364 /* We override Array's filter function because it doesn't return an List
       
   365  */
       
   366 List.prototype.filter = function(_callback) {
       
   367     var _this = this,
       
   368         _res = new List(this.directory);
       
   369     _res.addElements(ns._(this).filter(function(_value, _key) {
       
   370         return _callback(_value, _key, _this);
       
   371     }));
       
   372     return _res;
       
   373 };
       
   374 
       
   375 List.prototype.slice = function(_start, _end) {
       
   376     var _res = new List(this.directory);
       
   377     _res.addElements(Array.prototype.slice.call(this, _start, _end));
       
   378     return _res;
       
   379 };
       
   380 
       
   381 List.prototype.splice = function(_start, _end) {
       
   382     var _res = new List(this.directory);
       
   383     _res.addElements(Array.prototype.splice.call(this, _start, _end));
       
   384     this.idIndex.splice(_start, _end);
       
   385     return _res;
       
   386 };
       
   387 
       
   388 /* Array has a sort function, but it's not as interesting as Underscore.js's sortBy
       
   389  * and won't return a new List
       
   390  */
       
   391 List.prototype.sortBy = function(_callback) {
       
   392     var _this = this,
       
   393         _res = new List(this.directory);
       
   394     _res.addElements(ns._(this).sortBy(function(_value, _key) {
       
   395         return _callback(_value, _key, _this);
       
   396     }));
       
   397     return _res;
       
   398 };
       
   399 
       
   400 /* Title and Description are basic information for (almost) all element types,
       
   401  * here we can search by these criteria
       
   402  */
       
   403 List.prototype.searchByTitle = function(_text, _iexact) {
       
   404     var _iexact = _iexact || false,
       
   405         _rgxp = regexpFromTextOrArray(_text, true, _iexact);
       
   406     return this.filter(function(_element) {
       
   407         return _rgxp.test(_element.title);
       
   408     });
       
   409 };
       
   410 
       
   411 List.prototype.searchByDescription = function(_text, _iexact) {
       
   412     var _iexact = _iexact || false,
       
   413         _rgxp = regexpFromTextOrArray(_text, true, _iexact);
       
   414     return this.filter(function(_element) {
       
   415         return _rgxp.test(_element.description);
       
   416     });
       
   417 };
       
   418 
       
   419 List.prototype.searchByTextFields = function(_text, _iexact) {
       
   420     var _iexact = _iexact || false,
       
   421         _rgxp =  regexpFromTextOrArray(_text, true, _iexact);
       
   422     return this.filter(function(_element) {
       
   423         var keywords = (_element.keywords || _element.getTagTexts() || []).join(", ");
       
   424         return _rgxp.test(_element.description) || _rgxp.test(_element.title) || _rgxp.test(keywords);
       
   425     });
       
   426 };
       
   427 
       
   428 List.prototype.search = function(_text) {
       
   429     if (!_text) {
       
   430         this.trigger("clear-search");
       
   431         return this;
       
   432     }
       
   433     this.searching = true;
       
   434     this.trigger("search", _text);
       
   435     var rxsource = fullTextRegexps(_text)
       
   436         rgxp = new RegExp(rxsource,"im"),
       
   437         this.regexp = new RegExp(rxsource,"gim");
       
   438     var res = this.filter(function(_element, _k) {
       
   439         var titlematch = rgxp.test(_element.title),
       
   440             descmatch = rgxp.test(_element.description),
       
   441             _isfound = !!(titlematch || descmatch);
       
   442         _element.found = _isfound;
       
   443         _element.trigger(_isfound ? "found" : "not-found");
       
   444         return _isfound;
       
   445     });
       
   446     this.trigger(res.length ? "found" : "not-found",res);
       
   447     return res;
       
   448 };
       
   449 
       
   450 List.prototype.getTitles = function() {
       
   451     return this.map(function(_el) {
       
   452         return _el.title;
       
   453     });
       
   454 };
       
   455 
       
   456 List.prototype.addId = function(_id) {
       
   457     var _el = this.directory.getElement(_id)
       
   458     if (!this.hasId(_id) && typeof _el !== "undefined") {
       
   459         this.idIndex.push(_id);
       
   460         Array.prototype.push.call(this, _el);
       
   461     }
       
   462 };
       
   463 
       
   464 List.prototype.push = function(_el) {
       
   465     if (typeof _el === "undefined") {
       
   466         return;
       
   467     }
       
   468     var _index = (ns._(this.idIndex).indexOf(_el.id));
       
   469     if (_index === -1) {
       
   470         this.idIndex.push(_el.id);
       
   471         Array.prototype.push.call(this, _el);
       
   472     } else {
       
   473         this[_index] = _el;
       
   474     }
       
   475 };
       
   476 
       
   477 List.prototype.addIds = function(_array) {
       
   478     var _l = _array.length,
       
   479         _this = this;
       
   480     ns._(_array).forEach(function(_id) {
       
   481         _this.addId(_id);
       
   482     });
       
   483 };
       
   484 
       
   485 List.prototype.addElements = function(_array) {
       
   486     var _this = this;
       
   487     ns._(_array).forEach(function(_el) {
       
   488         _this.push(_el);
       
   489     });
       
   490 };
       
   491 
       
   492 List.prototype.removeId = function(_id, _deleteFromDirectory) {
       
   493     var _deleteFromDirectory = _deleteFromDirectory || false,
       
   494         _index = (ns._(this.idIndex).indexOf(_id));
       
   495     if (_index !== -1) {
       
   496         this.splice(_index,1);
       
   497     }
       
   498     if (_deleteFromDirectory) {
       
   499         delete this.directory.elements[_id];
       
   500     }
       
   501 };
       
   502 
       
   503 List.prototype.removeElement = function(_el, _deleteFromDirectory) {
       
   504     var _deleteFromDirectory = _deleteFromDirectory || false;
       
   505     this.removeId(_el.id);
       
   506 };
       
   507 
       
   508 List.prototype.removeIds = function(_list, _deleteFromDirectory) {
       
   509     var _deleteFromDirectory = _deleteFromDirectory || false,
       
   510         _this = this;
       
   511     ns._(_list).forEach(function(_id) {
       
   512         _this.removeId(_id);
       
   513     });
       
   514 };
       
   515 
       
   516 List.prototype.removeElements = function(_list, _deleteFromDirectory) {
       
   517     var _deleteFromDirectory = _deleteFromDirectory || false,
       
   518         _this = this;
       
   519     ns._(_list).forEach(function(_el) {
       
   520         _this.removeElement(_el);
       
   521     });
       
   522 };
       
   523 
       
   524 List.prototype.on = function(_event, _callback) {
       
   525     if (typeof this.__events[_event] === "undefined") {
       
   526         this.__events[_event] = [];
       
   527     }
       
   528     this.__events[_event].push(_callback);
       
   529 };
       
   530 
       
   531 List.prototype.off = function(_event, _callback) {
       
   532     if (typeof this.__events[_event] !== "undefined") {
       
   533         this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) {
       
   534             return _fn === _callback;
       
   535         });
       
   536     }
       
   537 };
       
   538 
       
   539 List.prototype.trigger = function(_event, _data) {
       
   540     var _list = this;
       
   541     ns._(this.__events[_event]).each(function(_callback) {
       
   542         _callback.call(_list, _data);
       
   543     });
       
   544 };
       
   545 
       
   546 /* A simple time management object, that helps converting millisecs to seconds and strings,
       
   547  * without the clumsiness of the original Date object.
       
   548  */
       
   549 
       
   550 var Time = Model.Time = function(_milliseconds) {
       
   551     this.milliseconds = 0;
       
   552     this.setMilliseconds(_milliseconds);
       
   553 };
       
   554 
       
   555 Time.prototype.setMilliseconds = function(_milliseconds) {
       
   556     var _ante = this.milliseconds;
       
   557     switch(typeof _milliseconds) {
       
   558         case "string":
       
   559             this.milliseconds = parseInt(_milliseconds);
       
   560             break;
       
   561         case "number":
       
   562             this.milliseconds = Math.floor(_milliseconds);
       
   563             break;
       
   564         case "object":
       
   565             this.milliseconds = parseInt(_milliseconds.valueOf());
       
   566             break;
       
   567         default:
       
   568             this.milliseconds = 0;
       
   569     }
       
   570     if (this.milliseconds === NaN) {
       
   571         this.milliseconds = _ante;
       
   572     }
       
   573 };
       
   574 
       
   575 Time.prototype.setSeconds = function(_seconds) {
       
   576     this.milliseconds = 1000 * _seconds;
       
   577 };
       
   578 
       
   579 Time.prototype.getSeconds = function() {
       
   580     return this.milliseconds / 1000;
       
   581 };
       
   582 
       
   583 Time.prototype.getHMS = function() {
       
   584     var _totalSeconds = Math.abs(Math.floor(this.getSeconds()));
       
   585     return {
       
   586         hours : Math.floor(_totalSeconds / 3600),
       
   587         minutes : (Math.floor(_totalSeconds / 60) % 60),
       
   588         seconds : _totalSeconds % 60,
       
   589         milliseconds: this.milliseconds % 1000
       
   590     } 
       
   591 };
       
   592 
       
   593 Time.prototype.add = function(_milliseconds) {
       
   594     this.milliseconds += new Time(_milliseconds).milliseconds;
       
   595 };
       
   596 
       
   597 Time.prototype.valueOf = function() {
       
   598     return this.milliseconds;
       
   599 };
       
   600 
       
   601 Time.prototype.toString = function(showCs) {
       
   602     var _hms = this.getHMS(),
       
   603         _res = '';
       
   604     if (_hms.hours) {
       
   605         _res += _hms.hours + ':'
       
   606     }
       
   607     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
       
   608     if (showCs) {
       
   609         _res += "." + Math.floor(_hms.milliseconds / 100)
       
   610     }
       
   611     return _res;
       
   612 };
       
   613 
       
   614 /* Reference handles references between elements
       
   615  */
       
   616 
       
   617 var Reference = Model.Reference = function(_source, _idRef) {
       
   618     this.source = _source;
       
   619     this.id = _idRef;
       
   620     if (typeof _idRef === "object") {
       
   621         this.isList = true;
       
   622     } else {
       
   623         this.isList = false;
       
   624     }
       
   625     this.refresh();
       
   626 };
       
   627 
       
   628 Reference.prototype.refresh = function() {
       
   629     if (this.isList) {
       
   630         this.contents = new List(this.source.directory);
       
   631         this.contents.addIds(this.id);
       
   632     } else {
       
   633         this.contents = this.source.getElement(this.id);
       
   634     }
       
   635     
       
   636 };
       
   637 
       
   638 Reference.prototype.getContents = function() {
       
   639     if (typeof this.contents === "undefined" || (this.isList && this.contents.length != this.id.length)) {
       
   640         this.refresh();
       
   641     }
       
   642     return this.contents;
       
   643 };
       
   644 
       
   645 Reference.prototype.isOrHasId = function(_idRef) {
       
   646     if (this.isList) {
       
   647         return (ns._(this.id).indexOf(_idRef) !== -1)
       
   648     } else {
       
   649         return (this.id == _idRef);
       
   650     }
       
   651 };
       
   652 
       
   653 /* */
       
   654 
       
   655 var BaseElement = Model.Element = function(_id, _source) {
       
   656     this.elementType = 'element';
       
   657     this.title = "";
       
   658     this.description = "";
       
   659     this.__events = {}
       
   660     if (typeof _source === "undefined") {
       
   661         return;
       
   662     }
       
   663     if (typeof _id === "undefined" || !_id) {
       
   664         _id = getUID();
       
   665     }
       
   666     this.id = _id;
       
   667     this.source = _source;
       
   668     if (_source !== this) {
       
   669         this.source.directory.addElement(this);
       
   670     }
       
   671 };
       
   672 
       
   673 BaseElement.prototype.toString = function() {
       
   674     return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : '');
       
   675 };
       
   676 
       
   677 BaseElement.prototype.setReference = function(_elementType, _idRef) {
       
   678     this[_elementType] = new Reference(this.source, _idRef);
       
   679 };
       
   680 
       
   681 BaseElement.prototype.getReference = function(_elementType) {
       
   682     if (typeof this[_elementType] !== "undefined") {
       
   683         return this[_elementType].getContents();
       
   684     }
       
   685 };
       
   686 
       
   687 BaseElement.prototype.getRelated = function(_elementType, _global) {
       
   688     _global = (typeof _global !== "undefined" && _global);
       
   689     var _this = this;
       
   690     return this.source.getList(_elementType, _global).filter(function(_el) {
       
   691         var _ref = _el[_this.elementType];
       
   692         return _ref && _ref.isOrHasId(_this.id);
       
   693     });
       
   694 };
       
   695 
       
   696 BaseElement.prototype.on = function(_event, _callback) {
       
   697     if (typeof this.__events[_event] === "undefined") {
       
   698         this.__events[_event] = [];
       
   699     }
       
   700     this.__events[_event].push(_callback);
       
   701 };
       
   702 
       
   703 BaseElement.prototype.off = function(_event, _callback) {
       
   704     if (typeof this.__events[_event] !== "undefined") {
       
   705         this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) {
       
   706             return _fn === _callback;
       
   707         });
       
   708     }
       
   709 };
       
   710 
       
   711 BaseElement.prototype.trigger = function(_event, _data) {
       
   712     var _element = this;
       
   713     ns._(this.__events[_event]).each(function(_callback) {
       
   714         _callback.call(_element, _data);
       
   715     });
       
   716 };
       
   717 
       
   718 /* */
       
   719 
       
   720 var Playable = Model.Playable = function(_id, _source) {
       
   721     BaseElement.call(this, _id, _source);
       
   722     if (typeof _source === "undefined") {
       
   723         return;
       
   724     }
       
   725     this.elementType = 'playable';
       
   726     this.currentTime = new Time();
       
   727     this.volume = .5;
       
   728     this.paused = true;
       
   729     this.muted = false;
       
   730     this.loadedMetadata = false;
       
   731     var _this = this;
       
   732     this.on("play", function() {
       
   733         _this.paused = false;
       
   734     });
       
   735     this.on("pause", function() {
       
   736         _this.paused = true;
       
   737     });
       
   738     this.on("timeupdate", function(_time) {
       
   739         _this.currentTime = _time;
       
   740         _this.getAnnotations().filter(function(_a) {
       
   741             return (_a.end <= _time || _a.begin > _time) && _a.playing
       
   742         }).forEach(function(_a) {
       
   743             _a.playing = false;
       
   744             _a.trigger("leave");
       
   745             _this.trigger("leave-annotation",_a);
       
   746         });
       
   747         _this.getAnnotations().filter(function(_a) {
       
   748             return _a.begin <= _time && _a.end > _time && !_a.playing
       
   749         }).forEach(function(_a) {
       
   750             _a.playing = true;
       
   751             _a.trigger("enter");
       
   752             _this.trigger("enter-annotation",_a);
       
   753         });
       
   754     });
       
   755     this.on("loadedmetadata", function() {
       
   756         _this.loadedMetadata = true;
       
   757     });
       
   758 };
       
   759 
       
   760 extendPrototype(Playable, BaseElement);
       
   761 
       
   762 Playable.prototype.getCurrentTime = function() { 
       
   763     return this.currentTime;
       
   764 };
       
   765 
       
   766 Playable.prototype.getVolume = function() {
       
   767     return this.volume;
       
   768 };
       
   769 
       
   770 Playable.prototype.getPaused = function() {
       
   771     return this.paused;
       
   772 };
       
   773 
       
   774 Playable.prototype.getMuted = function() {
       
   775     return this.muted;
       
   776 };
       
   777 
       
   778 Playable.prototype.setCurrentTime = function(_time) {
       
   779     this.trigger("setcurrenttime",_time);
       
   780 };
       
   781 
       
   782 Playable.prototype.setVolume = function(_vol) {
       
   783     this.trigger("setvolume",_vol);
       
   784 };
       
   785 
       
   786 Playable.prototype.setMuted = function(_muted) {
       
   787     this.trigger("setmuted",_muted);
       
   788 };
       
   789 
       
   790 Playable.prototype.play = function() {
       
   791     this.trigger("setplay");
       
   792 };
       
   793 
       
   794 Playable.prototype.pause = function() {
       
   795     this.trigger("setpause");
       
   796 };
       
   797 
       
   798 Playable.prototype.show = function() {};
       
   799 
       
   800 Playable.prototype.hide = function() {};
       
   801 
       
   802 /* */
       
   803 
       
   804 var Media = Model.Media = function(_id, _source) {
       
   805     Playable.call(this, _id, _source);
       
   806     this.elementType = 'media';
       
   807     this.duration = new Time();
       
   808     this.video = '';
       
   809     var _this = this;
       
   810 };
       
   811 
       
   812 extendPrototype(Media, Playable);
       
   813 
       
   814 /* Default functions to be overriden by players */
       
   815     
       
   816 Media.prototype.setDuration = function(_durationMs) {
       
   817     this.duration.setMilliseconds(_durationMs);
       
   818 };
       
   819 
       
   820 Media.prototype.getAnnotations = function() {
       
   821     return this.getRelated("annotation");
       
   822 };
       
   823 
       
   824 Media.prototype.getAnnotationsByTypeTitle = function(_title) {
       
   825     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
       
   826     if (_annTypes.length) {
       
   827         return this.getAnnotations().filter(function(_annotation) {
       
   828             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
       
   829         });
       
   830     } else {
       
   831         return new List(this.source.directory)
       
   832     }
       
   833 };
       
   834 
       
   835 /* */
       
   836 
       
   837 var Tag = Model.Tag = function(_id, _source) {
       
   838     BaseElement.call(this, _id, _source);
       
   839     this.elementType = 'tag';
       
   840 };
       
   841 
       
   842 extendPrototype(Tag, BaseElement);
       
   843 
       
   844 Tag.prototype.getAnnotations = function() {
       
   845     return this.getRelated("annotation");
       
   846 };
       
   847 
       
   848 /* */
       
   849 var AnnotationType = Model.AnnotationType = function(_id, _source) {
       
   850     BaseElement.call(this, _id, _source);
       
   851     this.elementType = 'annotationType';
       
   852 };
       
   853 
       
   854 extendPrototype(AnnotationType, BaseElement);
       
   855 
       
   856 AnnotationType.prototype.getAnnotations = function() {
       
   857     return this.getRelated("annotation");
       
   858 };
       
   859 
       
   860 /* Annotation
       
   861  * */
       
   862 
       
   863 var Annotation = Model.Annotation = function(_id, _source) {
       
   864     BaseElement.call(this, _id, _source);
       
   865     this.elementType = 'annotation';
       
   866     this.begin = new Time();
       
   867     this.end = new Time();
       
   868     this.tag = new Reference(_source, []);
       
   869     this.playing = false;
       
   870     var _this = this;
       
   871     this.on("click", function() {
       
   872         _this.getMedia().setCurrentTime(_this.begin);
       
   873     });
       
   874 };
       
   875 
       
   876 extendPrototype(Annotation, BaseElement);
       
   877 
       
   878 Annotation.prototype.setBegin = function(_beginMs) {
       
   879     this.begin.setMilliseconds(Math.max(0,_beginMs));
       
   880     this.trigger("change-begin");
       
   881     if (this.end < this.begin) {
       
   882         this.setEnd(this.begin);
       
   883     }
       
   884 };
       
   885 
       
   886 Annotation.prototype.setEnd = function(_endMs) {
       
   887     this.end.setMilliseconds(Math.min(_endMs, this.getMedia().duration.milliseconds));
       
   888     this.trigger("change-end");
       
   889     if (this.end < this.begin) {
       
   890         this.setBegin(this.end);
       
   891     }
       
   892 };
       
   893 
       
   894 Annotation.prototype.setDuration = function(_durMs) {
       
   895     this.setEnd(_durMs + this.begin.milliseconds);
       
   896 };
       
   897 
       
   898 Annotation.prototype.setMedia = function(_idRef) {
       
   899     this.setReference("media", _idRef);
       
   900 };
       
   901 
       
   902 Annotation.prototype.getMedia = function() {
       
   903     return this.getReference("media");
       
   904 };
       
   905 
       
   906 Annotation.prototype.setAnnotationType = function(_idRef) {
       
   907     this.setReference("annotationType", _idRef);
       
   908 };
       
   909 
       
   910 Annotation.prototype.getAnnotationType = function() {
       
   911     return this.getReference("annotationType");
       
   912 };
       
   913 
       
   914 Annotation.prototype.setTags = function(_idRefs) {
       
   915     this.setReference("tag", _idRefs);
       
   916 };
       
   917 
       
   918 Annotation.prototype.getTags = function() {
       
   919     return this.getReference("tag");
       
   920 };
       
   921 
       
   922 Annotation.prototype.getTagTexts = function() {
       
   923     return this.getTags().getTitles();
       
   924 };
       
   925 
       
   926 Annotation.prototype.getDuration = function() {
       
   927     return new Time(this.end.milliseconds - this.begin.milliseconds)
       
   928 };
       
   929 
       
   930 /* */
       
   931 
       
   932 var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) {
       
   933     BaseElement.call(this, _mashup.id + "_" + _annotation.id, _annotation.source);
       
   934     this.elementType = 'mashedAnnotation';
       
   935     this.annotation = _annotation;
       
   936     this.begin = new Time();
       
   937     this.end = new Time();
       
   938     this.duration = new Time();
       
   939     this.title = this.annotation.title;
       
   940     this.description = this.annotation.description;
       
   941     this.color = this.annotation.color;
       
   942     var _this = this;
       
   943     this.on("click", function() {
       
   944         _mashup.setCurrentTime(_this.begin);
       
   945     });
       
   946     this.on("enter", function() {
       
   947         _this.annotation.trigger("enter");
       
   948     });
       
   949     this.on("leave", function() {
       
   950         _this.annotation.trigger("leave");
       
   951     });
       
   952 };
       
   953 
       
   954 extendPrototype(MashedAnnotation, BaseElement);
       
   955 
       
   956 MashedAnnotation.prototype.getMedia = function() {
       
   957     return this.annotation.getReference("media");
       
   958 };
       
   959 
       
   960 MashedAnnotation.prototype.getAnnotationType = function() {
       
   961     return this.annotation.getReference("annotationType");
       
   962 };
       
   963 
       
   964 MashedAnnotation.prototype.getTags = function() {
       
   965     return this.annotation.getReference("tag");
       
   966 };
       
   967 
       
   968 MashedAnnotation.prototype.getTagTexts = function() {
       
   969     return this.annotation.getTags().getTitles();
       
   970 };
       
   971 
       
   972 MashedAnnotation.prototype.getDuration = function() {
       
   973     return this.annotation.getDuration();
       
   974 };
       
   975 
       
   976 MashedAnnotation.prototype.setBegin = function(_begin) {
       
   977     this.begin.setMilliseconds(_begin);
       
   978     this.duration.setMilliseconds(this.annotation.getDuration());
       
   979     this.end.setMilliseconds(_begin + this.duration);
       
   980 };
       
   981 
       
   982 /* */
       
   983 
       
   984 var Mashup = Model.Mashup = function(_id, _source) {
       
   985     Playable.call(this, _id, _source);
       
   986     this.elementType = 'mashup';
       
   987     this.duration = new Time();
       
   988     this.segments = new List(_source.directory);
       
   989     this.loaded = false;
       
   990     var _this = this;
       
   991     this._updateTimes = function() {
       
   992         _this.updateTimes();
       
   993         _this.trigger("change");
       
   994     }
       
   995     this.on("add", this._updateTimes);
       
   996     this.on("remove", this._updateTimes);
       
   997 };
       
   998 
       
   999 extendPrototype(Mashup, Playable);
       
  1000 
       
  1001 Mashup.prototype.updateTimes = function() {
       
  1002     var _time = 0;
       
  1003     this.segments.forEach(function(_segment) {
       
  1004         _segment.setBegin(_time);
       
  1005         _time = _segment.end;
       
  1006     });
       
  1007     this.duration.setMilliseconds(_time);
       
  1008 };
       
  1009 
       
  1010 Mashup.prototype.addAnnotation = function(_annotation, _defer) {
       
  1011     var _mashedAnnotation = new MashedAnnotation(this, _annotation),
       
  1012         _defer = _defer || false;
       
  1013     this.segments.push(_mashedAnnotation);
       
  1014     _annotation.on("change-begin", this._updateTimes);
       
  1015     _annotation.on("change-end", this._updateTimes);
       
  1016     if (!_defer) {
       
  1017         this.trigger("add");
       
  1018     }
       
  1019 };
       
  1020 
       
  1021 Mashup.prototype.addAnnotationById = function(_elId, _defer) {
       
  1022     var _annotation = this.source.getElement(_elId),
       
  1023         _defer = _defer || false;
       
  1024     if (typeof _annotation !== "undefined") {
       
  1025         this.addAnnotation(_annotation, _defer);
       
  1026     }
       
  1027 };
       
  1028 
       
  1029 Mashup.prototype.addAnnotations = function(_segments) {
       
  1030     var _this = this;
       
  1031     ns._(_segments).forEach(function(_segment) {
       
  1032         _this.addAnnotation(_segment, true);
       
  1033     });
       
  1034     this.trigger("add");
       
  1035 };
       
  1036 
       
  1037 Mashup.prototype.addAnnotationsById = function(_segments) {
       
  1038     var _this = this;
       
  1039     ns._(_segments).forEach(function(_segment) {
       
  1040         _this.addAnnotationById(_segment, true);
       
  1041     });
       
  1042     this.trigger("add");
       
  1043 };
       
  1044 
       
  1045 Mashup.prototype.removeAnnotation = function(_annotation, _defer) {
       
  1046     var _defer = _defer || false;
       
  1047     _annotation.off("change-begin", this._updateTimes);
       
  1048     _annotation.off("change-end", this._updateTimes);
       
  1049     this.segments.removeId(this.id + "_" + _annotation.id);
       
  1050     if (!_defer) {
       
  1051         this.trigger("remove");
       
  1052     }
       
  1053 };
       
  1054 
       
  1055 Mashup.prototype.removeAnnotationById = function(_annId, _defer) {
       
  1056     var _defer = _defer || false;
       
  1057     var _annotation = this.source.getElement(_annId);
       
  1058 
       
  1059     if (_annotation) {
       
  1060         this.removeAnnotation(_annotation, _defer);
       
  1061     }
       
  1062     if (!_defer) {
       
  1063         this.trigger("remove");
       
  1064     }
       
  1065 };
       
  1066 
       
  1067 Mashup.prototype.setAnnotations = function(_segments) {
       
  1068     while (this.segments.length) {
       
  1069         this.removeAnnotation(this.segments[0].annotation, true);
       
  1070     }
       
  1071     this.addAnnotations(_segments);
       
  1072 };
       
  1073 
       
  1074 Mashup.prototype.setAnnotationsById = function(_segments) {
       
  1075     while (this.segments.length) {
       
  1076         this.removeAnnotation(this.segments[0].annotation, true);
       
  1077     }
       
  1078     this.addAnnotationsById(_segments);
       
  1079 };
       
  1080 
       
  1081 Mashup.prototype.hasAnnotation = function(_annotation) {
       
  1082     return !!ns._(this.segments).find(function(_s) {
       
  1083         return _s.annotation === _annotation
       
  1084     });
       
  1085 };
       
  1086 
       
  1087 Mashup.prototype.getAnnotation = function(_annotation) {
       
  1088     return ns._(this.segments).find(function(_s) {
       
  1089         return _s.annotation === _annotation
       
  1090     });
       
  1091 };
       
  1092 
       
  1093 Mashup.prototype.getAnnotationById = function(_id) {
       
  1094     return ns._(this.segments).find(function(_s) {
       
  1095         return _s.annotation.id === _id
       
  1096     });
       
  1097 };
       
  1098 
       
  1099 Mashup.prototype.getAnnotations = function() {
       
  1100     return this.segments;
       
  1101 };
       
  1102 
       
  1103 Mashup.prototype.getOriginalAnnotations = function() {
       
  1104     var annotations = new List(this.source.directory);
       
  1105     this.segments.forEach(function(_s) {
       
  1106         annotations.push(_s.annotation);
       
  1107     });
       
  1108     return annotations;
       
  1109 };
       
  1110 
       
  1111 Mashup.prototype.getMedias = function() {
       
  1112     var medias = new List(this.source.directory);
       
  1113     this.segments.forEach(function(_annotation) {
       
  1114         medias.push(_annotation.getMedia())
       
  1115     })
       
  1116     return medias;
       
  1117 };
       
  1118 
       
  1119 Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
       
  1120     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
       
  1121     if (_annTypes.length) {
       
  1122         return this.getAnnotations().filter(function(_annotation) {
       
  1123             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
       
  1124         });
       
  1125     } else {
       
  1126         return new List(this.source.directory)
       
  1127     }
       
  1128 };
       
  1129 
       
  1130 Mashup.prototype.getAnnotationAtTime = function(_time) {
       
  1131     var _list = this.segments.filter(function(_annotation) {
       
  1132         return _annotation.begin <= _time && _annotation.end > _time;
       
  1133     });
       
  1134     if (_list.length) {
       
  1135         return _list[0];
       
  1136     } else {
       
  1137         return undefined;
       
  1138     }
       
  1139 };
       
  1140 
       
  1141 Mashup.prototype.getMediaAtTime = function(_time) {
       
  1142     var _annotation = this.getAnnotationAtTime(_time);
       
  1143     if (typeof _annotation !== "undefined") {
       
  1144         return _annotation.getMedia();
       
  1145     } else {
       
  1146         return undefined;
       
  1147     }
       
  1148 };
       
  1149 
       
  1150 /* */
       
  1151 
       
  1152 var Source = Model.Source = function(_config) {
       
  1153     BaseElement.call(this, false, this);
       
  1154     this.status = _SOURCE_STATUS_EMPTY;
       
  1155     this.elementType = "source";
       
  1156     if (typeof _config !== "undefined") {
       
  1157         var _this = this;
       
  1158         ns._(_config).forEach(function(_v, _k) {
       
  1159             _this[_k] = _v;
       
  1160         })
       
  1161         this.callbackQueue = [];
       
  1162         this.contents = {};
       
  1163         this.get();
       
  1164     }
       
  1165 };
       
  1166 
       
  1167 extendPrototype(Source, BaseElement);
       
  1168 
       
  1169 Source.prototype.addList = function(_listId, _contents) {
       
  1170     if (typeof this.contents[_listId] === "undefined") {
       
  1171         this.contents[_listId] = new List(this.directory);
       
  1172     }
       
  1173     this.contents[_listId].addElements(_contents);
       
  1174 };
       
  1175 
       
  1176 Source.prototype.getList = function(_listId, _global) {
       
  1177     _global = (typeof _global !== "undefined" && _global);
       
  1178     if (_global) {
       
  1179         return this.directory.getGlobalList().filter(function(_e) {
       
  1180             return (_e.elementType === _listId);
       
  1181         });
       
  1182     } else {
       
  1183         return this.contents[_listId] || new IriSP.List(this.directory);
       
  1184     }
       
  1185 };
       
  1186 
       
  1187 Source.prototype.forEach = function(_callback) {
       
  1188     var _this = this;
       
  1189     ns._(this.contents).forEach(function(_value, _key) {
       
  1190         _callback.call(_this, _value, _key);
       
  1191     })
       
  1192 };
       
  1193 
       
  1194 Source.prototype.getElement = function(_elId) {
       
  1195     return this.directory.getElement(_elId);
       
  1196 };
       
  1197 
       
  1198 Source.prototype.get = function() {
       
  1199     this.status = _SOURCE_STATUS_WAITING;
       
  1200     this.handleCallbacks();
       
  1201 };
       
  1202 
       
  1203 /* We defer the callbacks calls so they execute after the queue is cleared */
       
  1204 Source.prototype.deferCallback = function(_callback) {
       
  1205     var _this = this;
       
  1206     ns._.defer(function() {
       
  1207         _callback.call(_this);
       
  1208     });
       
  1209 };
       
  1210 
       
  1211 Source.prototype.handleCallbacks = function() {
       
  1212     this.status = _SOURCE_STATUS_READY;
       
  1213     while (this.callbackQueue.length) {
       
  1214         this.deferCallback(this.callbackQueue.splice(0,1)[0]);
       
  1215     }
       
  1216 };
       
  1217 Source.prototype.onLoad = function(_callback) {
       
  1218     if (this.status === _SOURCE_STATUS_READY) {
       
  1219         this.deferCallback(_callback);
       
  1220     } else {
       
  1221         this.callbackQueue.push(_callback);
       
  1222     }
       
  1223 };
       
  1224 
       
  1225 Source.prototype.serialize = function() {
       
  1226     return this.serializer.serialize(this);
       
  1227 };
       
  1228 
       
  1229 Source.prototype.deSerialize = function(_data) {
       
  1230     this.serializer.deSerialize(_data, this);
       
  1231 };
       
  1232 
       
  1233 Source.prototype.getAnnotations = function(_global) {
       
  1234     _global = (typeof _global !== "undefined" && _global);
       
  1235     return this.getList("annotation", _global);
       
  1236 };
       
  1237 
       
  1238 Source.prototype.getMedias = function(_global) {
       
  1239     _global = (typeof _global !== "undefined" && _global);
       
  1240     return this.getList("media", _global);
       
  1241 };
       
  1242 
       
  1243 Source.prototype.getTags = function(_global) {
       
  1244     _global = (typeof _global !== "undefined" && _global);
       
  1245     return this.getList("tag", _global);
       
  1246 };
       
  1247 
       
  1248 Source.prototype.getMashups = function(_global) {
       
  1249     _global = (typeof _global !== "undefined" && _global);
       
  1250     return this.getList("mashup", _global);
       
  1251 };
       
  1252 
       
  1253 Source.prototype.getAnnotationTypes = function(_global) {
       
  1254     _global = (typeof _global !== "undefined" && _global);
       
  1255     return this.getList("annotationType", _global);
       
  1256 };
       
  1257 
       
  1258 Source.prototype.getAnnotationsByTypeTitle = function(_title, _global) {
       
  1259     _global = (typeof _global !== "undefined" && _global);
       
  1260     var _res = new List(this.directory),
       
  1261         _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title);
       
  1262     _annTypes.forEach(function(_annType) {
       
  1263         _res.addElements(_annType.getAnnotations(_global));
       
  1264     })
       
  1265     return _res;
       
  1266 };
       
  1267 
       
  1268 Source.prototype.getDuration = function() {
       
  1269     var _m = this.currentMedia;
       
  1270     if (typeof _m !== "undefined") {
       
  1271         return this.currentMedia.duration;
       
  1272     }
       
  1273 };
       
  1274 
       
  1275 Source.prototype.getCurrentMedia = function(_opts) {
       
  1276     if (typeof this.currentMedia === "undefined") {
       
  1277         if (_opts.is_mashup) {
       
  1278             var _mashups = this.getMashups();
       
  1279             if (_mashups.length) {
       
  1280                 this.currentMedia = _mashups[0];
       
  1281             }
       
  1282         } else {
       
  1283             var _medias = this.getMedias();
       
  1284             if (_medias.length) {
       
  1285                 this.currentMedia = _medias[0];
       
  1286             }
       
  1287         }
       
  1288     }
       
  1289     return this.currentMedia;
       
  1290 };
       
  1291 
       
  1292 Source.prototype.merge = function(_source) {
       
  1293     var _this = this;
       
  1294     _source.forEach(function(_value, _key) {
       
  1295         _this.getList(_key).addElements(_value);
       
  1296     });
       
  1297 };
       
  1298 
       
  1299 /* */
       
  1300 
       
  1301 var RemoteSource = Model.RemoteSource = function(_config) {
       
  1302     Source.call(this, _config);
       
  1303 };
       
  1304 
       
  1305 extendPrototype(RemoteSource, Source);
       
  1306 
       
  1307 RemoteSource.prototype.get = function() {
       
  1308     this.status = _SOURCE_STATUS_WAITING;
       
  1309     var _this = this,
       
  1310         urlparams = this.url_params || {},
       
  1311         dataType = (isLocalURL(this.url) ? "json" : "jsonp");
       
  1312     urlparams.format = dataType;
       
  1313     ns.jQuery.ajax({
       
  1314         url: this.url,
       
  1315         dataType: dataType,
       
  1316         data: urlparams,
       
  1317         traditional: true,
       
  1318         success: function(_result) {
       
  1319             _this.deSerialize(_result);
       
  1320             _this.handleCallbacks();
       
  1321         }
       
  1322     });
       
  1323 };
       
  1324 
       
  1325 /* */
       
  1326 
       
  1327 var Directory = Model.Directory = function() {
       
  1328     this.remoteSources = {};
       
  1329     this.elements = {};
       
  1330 };
       
  1331 
       
  1332 Directory.prototype.remoteSource = function(_properties) {
       
  1333     if (typeof _properties !== "object" || typeof _properties.url === "undefined") {
       
  1334         throw "Error : Directory.remoteSource(configuration): configuration.url is undefined";
       
  1335     }
       
  1336     var _config = ns._({ directory: this }).extend(_properties);
       
  1337     _config.url_params = _config.url_params || {};
       
  1338     var _hash = _config.url + "?" + ns.jQuery.param(_config.url_params);
       
  1339     if (typeof this.remoteSources[_hash] === "undefined") {
       
  1340         this.remoteSources[_hash] = new RemoteSource(_config);
       
  1341     }
       
  1342     return this.remoteSources[_hash];
       
  1343 };
       
  1344 
       
  1345 Directory.prototype.newLocalSource = function(_properties) {
       
  1346     var _config = ns._({ directory: this }).extend(_properties),
       
  1347         _res = new Source(_config);
       
  1348     return _res;
       
  1349 };
       
  1350 
       
  1351 Directory.prototype.getElement = function(_id) {
       
  1352     return this.elements[_id];
       
  1353 };
       
  1354 
       
  1355 Directory.prototype.addElement = function(_element) {
       
  1356     this.elements[_element.id] = _element;
       
  1357 };
       
  1358 
       
  1359 Directory.prototype.getGlobalList = function() {
       
  1360     var _res = new List(this);
       
  1361     _res.addIds(ns._(this.elements).keys());
       
  1362     return _res;
       
  1363 };
       
  1364 return Model;
       
  1365 
       
  1366 })(IriSP);
       
  1367 
       
  1368 /* END js */
       
  1369 
       
  1370 /* HTML player, to be reused in a widget, or elsewhere */
       
  1371 
       
  1372 IriSP.htmlPlayer = function(media, jqselector, options) {
       
  1373     
       
  1374     var opts = options || {},
       
  1375         videoURL = opts.video || media.video;
       
  1376     
       
  1377     if (typeof opts.url_transform === "function") {
       
  1378         videoURL = opts.url_transform(videoURL);
       
  1379     }
       
  1380         
       
  1381     var videoEl = IriSP.jQuery('<video>');
       
  1382     
       
  1383     videoEl.attr({
       
  1384         width : opts.width || undefined,
       
  1385         height : opts.height || undefined,
       
  1386         controls : opts.controls || undefined,
       
  1387         autoplay : opts.autostart || opts.autoplay || undefined
       
  1388     });
       
  1389     
       
  1390     if(typeof videoURL === "string"){
       
  1391         videoEl.attr("src",videoURL);
       
  1392     } else {
       
  1393         for (var i = 0; i < videoURL.length; i++) {
       
  1394             var _srcNode = IriSP.jQuery('<source>');
       
  1395             _srcNode.attr({
       
  1396                 src: videoURL[i].src,
       
  1397                 type: videoURL[i].type
       
  1398             });
       
  1399             videoEl.append(_srcNode);
       
  1400         }
       
  1401     }
       
  1402     
       
  1403     jqselector.html(videoEl);
       
  1404     
       
  1405     var mediaEl = videoEl[0];
       
  1406     
       
  1407     // Binding HTML video functions to media events
       
  1408     media.on("setcurrenttime", function(_milliseconds) {
       
  1409         try {
       
  1410             mediaEl.currentTime = (_milliseconds / 1000);
       
  1411         } catch (err) {
       
  1412             
       
  1413         }
       
  1414     });
       
  1415     
       
  1416     media.on("setvolume", function(_vol) {
       
  1417         media.volume = _vol;
       
  1418         try {
       
  1419             mediaEl.volume = _vol;
       
  1420         } catch (err) {
       
  1421             
       
  1422         }
       
  1423     });
       
  1424     
       
  1425     media.on("setmuted", function(_muted) {
       
  1426         media.muted = _muted;
       
  1427         try {
       
  1428             mediaEl.muted = _muted;
       
  1429         } catch (err) {
       
  1430             
       
  1431         }
       
  1432     });
       
  1433     
       
  1434     media.on("setplay", function() {
       
  1435         try {
       
  1436             mediaEl.play();
       
  1437         } catch (err) {
       
  1438             
       
  1439         }
       
  1440     });
       
  1441     
       
  1442     media.on("setpause", function() {
       
  1443         try {
       
  1444             mediaEl.pause();
       
  1445         } catch (err) {
       
  1446             
       
  1447         }
       
  1448     });
       
  1449     
       
  1450     // Binding DOM events to media
       
  1451     function getVolume() {
       
  1452         media.muted = mediaEl.muted;
       
  1453         media.volume = mediaEl.volume;
       
  1454     }
       
  1455     
       
  1456     videoEl.on("loadedmetadata", function() {
       
  1457         getVolume();
       
  1458         media.trigger("loadedmetadata");
       
  1459         media.trigger("volumechange");
       
  1460     })
       
  1461     
       
  1462     videoEl.on("timeupdate", function() {
       
  1463         media.trigger("timeupdate", new IriSP.Model.Time(1000*mediaEl.currentTime));
       
  1464     });
       
  1465     
       
  1466     videoEl.on("volumechange", function() {
       
  1467         getVolume();
       
  1468         media.trigger("volumechange");
       
  1469     })
       
  1470     
       
  1471     videoEl.on("play", function() {
       
  1472         media.trigger("play");
       
  1473     });
       
  1474     
       
  1475     videoEl.on("pause", function() {
       
  1476         media.trigger("pause");
       
  1477     });
       
  1478     
       
  1479     videoEl.on("seeking", function() {
       
  1480         media.trigger("seeking");
       
  1481     });
       
  1482     
       
  1483     videoEl.on("seeked", function() {
       
  1484         media.trigger("seeked");
       
  1485     });
       
  1486     
       
  1487     
       
  1488 };