﻿// RM3SMedia Class

RM3SMedia = function(id, title, description, streamingURLs, tags, rootChapter, chapters, comments) 
{
  this.id = id;
  this.title = title;
  this.description = description;
  this.streamingURLs = streamingURLs;
  this.tags = tags;
  this.rootChapter = (rootChapter == null ? new RM3SChapter() : rootChapter);
  this.chapters = (chapters == null ? new Array() : chapters);
  this.comments = comments;
  this.password = "";
  this.dimensions = {width: 0, height: 0};
  this.slidesFolder = "";
  this.styles = {h_background: null, v_background: null};
  //this.permissions = new RM3SPermissions("View");
}

RM3SMedia.prototype = {
	loadFromXML: function(xml) {
	  if(xml == null) return;
	  var _x = getTextXmlNodeValue(xml, "media");
	  if(_x != null) xml = _x;
	  this.id = getTextXmlNodeValue(xml, "id");
	  this.title = getTextXmlNodeValue(xml, "title");
	  this.description = getTextXmlNodeValue(xml, "description");
	  //this.permissions.loadFromXML(xml);
	  this.password = getTextXmlNodeValue(xml, "password");
	  this.slidesFolder = getTextXmlNodeValue(xml, "slidesFolder");
	  var dim_xml = getTextXmlNodeValue(xml, "dimensions");
	  var w = getTextXmlNodeValue(dim_xml, "width");
	  var h = getTextXmlNodeValue(dim_xml, "height");
		this.dimensions.width = (w == null ? 0 : parseInt(w));
		this.dimensions.height = (h == null ? 0 : parseInt(h));
		if(this.password == null) this.password = "";
		
		var styles_xml = getTextXmlNodeValue(xml, "styles");
		if(styles_xml != null && styles_xml != "") {
			var v_background = getTextXmlNodeValue(styles_xml, "v_background");
			if(v_background != null) this.styles.v_background = v_background;
			var h_background = getTextXmlNodeValue(styles_xml, "h_background");
			if(h_background != null) this.styles.h_background = h_background;
		}
    this.streamingURLs = new Array();
	  var streamingURLs_xml = getTextXmlNodeValue(xml, "streamingURLs");
	  if(streamingURLs_xml != null && streamingURLs_xml != "") {
	    var streamingURLs = getTextXmlNodesList(streamingURLs_xml, "streamingURL");
	    var _id = null;
	    var _localURL = null;
	    var _publicURL = null;
	    var _stxml = null;
	    for(var i = 0, n = (streamingURLs == null ? 0 : streamingURLs.length); i < n; i++) {
				_stxml = streamingURLs[i] == null ? "" : streamingURLs[i];
				_id = getTextXmlNodeValue(_stxml, "id");
				_localURL = getTextXmlNodeValue(_stxml, "localURL");
				_publicURL = getTextXmlNodeValue(_stxml, "publicURL");
	      if(_publicURL == null) {
					_id = "";
					_localURL = "";
					_publicURL = _stxml;
	      }
	      this.streamingURLs.push({id: _id, localURL: _localURL, publicURL: _publicURL});
	  
	    }
	  }
	  this.tags = new Array();
	  var tags_xml = getTextXmlNodeValue(xml, "tags");
	  if(tags_xml != null && tags_xml != "") {
	    var tags = getTextXmlNodesList(tags_xml, "tag");
	    var tag = null;
	    for(var i = 0, n = (tags == null ? 0 : tags.length); i < n; i++) {
	      tag = new RM3STag();
	      tag.loadFromXML(tags[i]);
	      this.tags.push(tag);
	    }
	  }
	  (this.rootChapter = new RM3SChapter()).loadFromXML(getTextXmlNodeValue(xml, "rootChapter"));
	  this.chapters = new Array();
	  var chapters_xml = getTextXmlNodeValue(xml, "chapters");
	  if(chapters_xml != null && chapters_xml != "") {
	    var chapters = getTextXmlNodesList(chapters_xml, "chapter");
	    var chapter = null;
	    for(var i = 0, n = (chapters == null ? 0 : chapters.length); i < n; i++) {
	      chapter = new RM3SChapter();
	      chapter.loadFromXML(chapters[i]);
	      this.chapters.push(chapter);
	    }
	  }
	  this.calcDurations();
    this.calcChaptersTime("");
	},
	
	saveToXML: function() {
    var xml = '<?xml version="1.0" encoding="utf-8" ?>';
    xml += '<media>';
    xml += '<id>' + this.id + '</id>';
    xml += '<title>' + this.title + '</title>';
    xml += '<description>' + this.description + '</description>';
    xml += '<streamingURLs>';
		var _streamingURL = null;
    for(var i = 0, n = (this.streamingURLs == null ? 0 : this.streamingURLs.length); i < n; i++) {
			_streamingURL = this.streamingURLs[i];
      if(_streamingURL != null) xml += '<streamingURL><id>' + _streamingURL.id + '</id><localURL>' + _streamingURL.localURL + '</localURL><publicURL>' + _streamingURL.publicURL + '</publicURL></streamingURL>';
    }
    xml += '</streamingURLs>';
    xml += '<password>' + (this.password == null ? "" : this.password) + '</password>';
    xml += '<slidesFolder>' + (this.slidesFolder == null ? "" : this.slidesFolder) + '</slidesFolder>';
    if(this.dimensions != null)
			xml += '<dimensions><width>' + (this.dimensions.width == null ? "" : this.dimensions.width) + '</width><height>' + (this.dimensions.height == null ? "" : this.dimensions.height) + '</height></dimensions>';
    if(this.styles != null)
			xml += '<styles>' + 
								(this.styles.v_background == null ? "" : '<v_background>' + this.styles.v_background + '</v_background>') + 
								(this.styles.h_background == null ? "" : '<h_background>' + this.styles.h_background + '</h_background>') + 
							'</styles>';					
    xml += '<tags>';
    var tag = null;
    for(var i = 0, n = (this.tags == null ? 0 : this.tags.length); i < n; i++) {
      tag = this.tags[i];
      if(tag != null) xml += tag.saveToXML();
    }
    xml += '</tags>';
    xml += '<rootChapter>';
    xml += '<tagids>';
    for(var i = 0, n = (this.rootChapter == null || this.rootChapter.tagids == null ? 0 : this.rootChapter.tagids.length); i < n; i++) {
      xml += '<tagid>' + this.rootChapter.tagids[i] + '</tagid>';
    }
    xml += '</tagids>';
    xml += '</rootChapter>';
    xml += '<chapters>';
    var chapter = null;
    for(var i = 0, n = (this.chapters == null ? 0 : this.chapters.length); i < n; i++) {
      chapter = this.chapters[i];
      if(chapter != null) {
        xml += '<chapter><id>' + chapter.id + '</id><title>' + chapter.title + '</title><description>' + chapter.description + '</description>';
        if(chapter.parentid != null && chapter.parentid != "") xml += '<parentid>' + chapter.parentid + '</parentid>';
        xml += '<tagids>'
        for(var j = 0, k = (chapter.tagids == null ? 0 : chapter.tagids.length); j < k; j++) {
          xml += '<tagid>' + chapter.tagids[j] + '</tagid>';
        }
        xml += '</tagids>'
        xml += '</chapter>'
      }
    }
    xml += '</chapters>';
    xml += '</media>';
    
    return xml;
	},
	
	getTagById: function(id) {
    if(this.tags != null) {
      for(var i = 0, n = this.tags.length; i < n; i++) {
        if(this.tags[i] != null && this.tags[i].id == id) return this.tags[i];
      }
    }
    return null;
	},
	
	getChapterById: function(id) {
    if(this.chapters != null) {
      for(var i = 0, n = this.chapters.length; i < n; i++) {
        if(this.chapters[i] != null && this.chapters[i].id == id) return this.chapters[i];
      }
    }
    return null;
	},
	
	getTagByTime: function(timeInSecs, timeDelta) {
	  timeInSecs = parseFloat(timeInSecs);
	  if(timeInSecs == null || isNaN(timeInSecs)) timeInSecs = 0.0;
	  if(timeDelta == null || isNaN(timeDelta)) timeDelta = 0.0;
    var t = null;
    if(this.tags != null) {
      for(var i = 0, n = this.tags.length; i < n; i++) {
        if(this.tags[i] != null && this.tags[i].time <= (timeInSecs + timeDelta) && (t == null || this.tags[i].time > t.time)) t = this.tags[i];
      }
    }
    return t;
	},
	
	getChaptersContainTag: function(tagId) {
	  var tagids = null;
	  var chaptersList = new Array();
    if(this.chapters != null) {
      for(var i = 0, n = this.chapters.length; i < n; i++) {
        tagids = (this.chapters[i] == null ? null : this.chapters[i].tagids);
        if(tagids != null) {
          for(var j = 0, k = tagids.length; j < k; j++) {
            if(tagids[j] != null && tagids[j] == tagId) {
              chaptersList.push(this.chapters[i]);
              j = k;
            }
          }
        }
      }
    }
    return chaptersList;
	},
	
	isSubChapter: function(chapter, sub) {
	  if(chapter == null) return false;
	  while(sub != null && sub.parentid != null && sub.parentid != "") {
	    if(sub.parentid == chapter.id) return true;
	    sub = this.getChapterById(sub.parentid);
	  }
	  return false;
	},
	
	calcDurations: function(endTime) {
	  // calculates duration of all tags and chapters
    if(this.tags != null) {
      this.orderTagsListByTime();
      for(var i = 0, n = this.tags.length; i < n - 1; i++) {
        if(this.tags[i] != null && this.tags[i + 1].id != null) this.tags[i].duration = this.tags[i + 1].time - this.tags[i].time;
      }
      if(this.tags.length > 0&& this.tags[this.tags.length - 1] != null) {
        this.tags[this.tags.length - 1].duration = (endTime != null && endTime > 0 ? endTime - this.tags[this.tags.length - 1].time : null);
      }
    }
    // for chapters
    if(this.chapters != null) {
      this.orderTagsByTime();
      this.calcChaptersDuration("");
    }
    
    return null;
	},

  calcChaptersDuration: function(parentId) {
    var t = 0;
    if(this.chapters != null) {
      var tag = null;
      var d = 0;
      for(var i = 0, n = this.chapters.length; i < n; i++) {
        if(this.chapters[i].parentid == parentId) {
          d = 0;
          tagids = (this.chapters[i] == null ? null : this.chapters[i].tagids);
          if(tagids != null && tagids.length > 0) {
            for(var j = 0, k = tagids.length; j < k; j++) {
              tag = this.getTagById(tagids[j]);
              if(tag != null && tag.duration != null && tag.duration > 0) d += tag.duration;
            }
          }
          this.chapters[i].duration = d + (this.chapters[i].id == parentId ? 0 : this.calcChaptersDuration(this.chapters[i].id));
          t += this.chapters[i].duration;
        }
      }
    }
    return t;
  },
	
  calcChaptersTime: function(parentId) {
    var t = 0;
    var q = 0;
    var m = -1;
    if(this.chapters != null) {
      var tag = null;
      for(var i = 0, n = this.chapters.length; i < n; i++) {
        if(this.chapters[i].parentid == parentId) {
          t = -1;
          tagids = (this.chapters[i] == null ? null : this.chapters[i].tagids);
          if(tagids != null && tagids.length > 0) {
            tag = this.getTagById(tagids[0]);
            if(tag != null && tag.time != null && tag.time >= 0) t = tag.time;
          }
          q = (this.chapters[i].id == parentId ? -1 : this.calcChaptersTime(this.chapters[i].id));
          t = (q >= 0 && (q < t || t < 0) ? q : t);
          this.chapters[i].time = t;
          if(m < 0 || t < m) m = t;
        }
      }
    }
    return m;
  },
	
	addChapter: function(chapter) {
	  if(chapter == null) return null;
    if(this.chapters == null) this.chapters = new Array();
    if(this.getChapterById(chapter.id) != null) return this.updateChapter(chapter);
    return this.chapters.push(chapter);
	},
	
	updateChapter: function(chapter) {
	  if(chapter == null) return null;
    if(this.chapters == null) return this.addChapter(chapter);
    for(var i = 0, n = this.chapters.length; i < n; i++) {
      if(this.chapters[i] != null && this.chapters[i].id == chapter.id) return (this.chapters[i] = chapter);
    }
    return this.addChapter(chapter);
	},
	
	deleteChapter: function(chapterId) {
	  if(this.chapters == null) return null;
	  var ch = this.getChapterById(chapterId);
	  if(ch == null) return null;
    var pId = ch.parentid; 
    for(var i = 0, n = this.chapters.length; i < n; i++) {
      if(this.chapters[i] != null && this.chapters[i].parentid == chapterId) this.chapters[i].parentid = pId;
      if(this.chapters[i] != null && this.chapters[i].id == chapterId) { 
        this.chapters.splice(i, 1);
        i--; n--;
      }
    }
    // move all tags from deleted chapter to its parent
    if(ch.tagids == null) return;
    for(var i = 0, n = ch.tagids.length; i < n; i++) this.addTagToChapters(this.getTagById(ch.tagids[i]), [pId]);    
    return ch;
	},
	
	addTag: function(tag) {
	  if(tag == null) return null;
    if(this.tags == null) this.tags = new Array();
    if(this.getTagById(tag.id) != null) return this.updateTag(tag);
    return this.tags.push(tag);
	},
	
	updateTag: function(tag) {
	  if(tag == null) return null;
    if(this.tags == null) return this.addTag(tag);
    for(var i = 0, n = this.tags.length; i < n; i++) {
      if(this.tags[i] != null && this.tags[i].id == tag.id) return (this.tags[i] = tag);
    }
    return this.addTag(tag);
	},
	
	deleteTag: function(tagId) {
    if(this.tags == null) return null;
    this.removeTagFromAllChapters(tagId);
    for(var i = 0, n = this.tags.length; i < n; i++) {
      if(this.tags[i] != null && this.tags[i].id == tagId) return (this.tags.splice(i, 1));
    }
    return null;
	},
	
	addTagToChapters: function(tag, chapterIdsList) {
	  if(tag == null) return null;
	  if(this.rootChapter == null) this.rootChapter = new RM3SChapter(); 
	  this.addTag(tag);
	  var ch = null;
	  var tagids = null;
	  var k = 0;
	  if(chapterIdsList == null) chapterIdsList = [null];
    for(var i = 0, n = chapterIdsList.length; i < n; i++) {
      ch = (chapterIdsList[i] == null ? null : this.getChapterById(chapterIdsList[i]));
      tagids = (ch == null ? this.rootChapter.tagids : ch.tagids);
      if(tagids == null) tagids = new Array();
      if(tagids != null) {
        k = tagids.length;
        for(var j = 0; j < k; j++) {
          if(tagids[j] != null && tagids[j] == tag.id) k = -1;
        }
        if(k >= 0) {
          tagids.push(tag.id);
          if(ch == null) this.rootChapter.tagids = tagids;
          else ch.tagids = tagids;
        }
      }
    }
	},
	
	orderTagsByTime: function() {
	  var mm = this;
	  if(this.rootChapter == null) this.rootChapter = new RM3SChapter();
    for(var i = -1, n = (this.chapters == null ? 0 : this.chapters.length); i < n; i++) {
      if(i < 0) {
        if(this.rootChapter.tagids != null) this.rootChapter.tagids.sort(function(a_tagId, b_tagId) {var a_tag = mm.getTagById(a_tagId); var b_tag = mm.getTagById(b_tagId); var a_time = (a_tag == null ? 0 : a_tag.time); var b_time = (b_tag == null ? 0 : b_tag.time); return a_time - b_time; });
      }
      else {
        if(this.chapters[i].tagids != null) this.chapters[i].tagids.sort(function(a_tagId, b_tagId) {var a_tag = mm.getTagById(a_tagId); var b_tag = mm.getTagById(b_tagId); var a_time = (a_tag == null ? 0 : a_tag.time); var b_time = (b_tag == null ? 0 : b_tag.time); return a_time - b_time; });
      }
    }
	},
	
	orderTagsListByTime: function() {
    if(this.tags != null) {
      this.tags.sort(function(a_tag, b_tag) {var a_time = (a_tag == null ? 0 : a_tag.time); var b_time = (b_tag == null ? 0 : b_tag.time); return a_time - b_time; });
    }
	},
	
  removeTagFromAllChapters: function(tagId) {
	  var tagids = null;
    if(this.chapters != null) {
      for(var i = 0, n = this.chapters.length; i < n; i++) {
        tagids = (this.chapters[i] == null ? null : this.chapters[i].tagids);
        if(tagids != null) {
          for(var j = 0, k = tagids.length; j < k; j++) {
            if(tagids[j] != null && tagids[j] == tagId) {
              tagids.splice(j, 1);
              this.chapters[i].tagids = tagids;
              j = k;
            }
          }
        }
      }
    }
    if(this.rootChapter != null) {
      tagids = this.rootChapter.tagids;
      if(tagids != null) {
        for(var j = 0, k = tagids.length; j < k; j++) {
          if(tagids[j] != null && tagids[j] == tagId) {
            tagids.splice(j, 1);
            this.rootChapter.tagids = tagids;
            j = k;
          }
        }
      }
    }
  }

}