﻿var starttimeWidth = 58;
var durationWidth = 50;
var editWidth = 25;
var delWidth = 20;

function menu_hl_chapter(el, hl, chapter_id) {
  if(el == null) return;
  if(el.className == null) el = document.getElementById(el);
  if(el == null || el.className == null) return;
  var m = (el.className.indexOf("_mark") > 0 ? "_mark" : "");
  el.className = (hl ? "menu_chapter_hl" : "menu_chapter") + m;
  // highlight inner tags
  var ch_el = document.getElementById("menu_chapter_" + chapter_id + "_children");
  if(ch_el == null) return;
  ch_el.className = (hl ? "menu_chapter_children_hl" : "menu_chapter_children") + m;
}

function menu_hl_tag(el, hl) {
  if(el == null) return;
  if(el.className == null) el = document.getElementById(el);
  if(el == null || el.className == null) return;
  var m = (el.className.indexOf("_mark") > 0 ? "_mark" : "");
  el.className = (hl ? "menu_tag_hl" : "menu_tag") + m;
}

function menu_mark_chapter(el, mark) {
  if(el == null) return;
  if(el.className == null) el = document.getElementById(el);
  if(el == null || el.className == null) return;
  var h = (el.className.indexOf("_hl") > 0 ? "_hl" : "");
  el.className = "menu_chapter" + h + (mark ? "_mark" : "");
}

function menu_mark_tag(el, mark) {
  if(el == null) return;
  if(el.className == null) el = document.getElementById(el);
  if(el == null || el.className == null) return;
  var h = (el.className.indexOf("_hl") > 0 ? "_hl" : "");
  el.className = "menu_tag" + h + (mark ? "_mark" : "");
}

function menu_show_tag(el, show) {
  // opens all closed Chapters to show a tag and scroll menu to make this tag visible
  if(el == null) return;
  if(el.className == null) el = document.getElementById(el);
  if(el == null || el.className == null) return;
  var ch = null;
  var children = null;
  var closed = false;
  var ch_id = el.getAttribute("chapter_id");
  while(ch_id != null && ch_id != "") {
    children = document.getElementById("menu_chapter_" + ch_id + "_children");
    closed = (children != null && children.style.display == "none");
    if(closed) {
      menu_chapter_open("menu_chapter_" + ch_id);
      ch = document.getElementById("menu_chapter_" + ch_id + "_title");
      ch_id = (ch == null ? null : ch.getAttribute("parent_id"));
    } else {
      ch_id = null;
    }
  }
  //el.scrollIntoView(false);
  menu_scroll_into_view(el);
}

function menu_scroll_into_view(el) {
	if(el == null) return;
	var el_t = el.offsetTop;
	var d = el.offsetParent;
	while(d != null && d.offsetHeight == d.scrollHeight) {
		if(d.tagName.toLowerCase() == "body" || d.tagName.toLowerCase() == "html" || d.tagName.toLowerCase() == "undefined") d = null;
		else {		
			if(d.tagName.toLowerCase() != "tr") el_t += d.offsetTop;
			d = d.parentNode;
		}
	}
	if(d == null) return;
	var el_b = el_t + el.offsetHeight;
	var dv_t = d.scrollTop;
	var dv_b = dv_t + d.offsetHeight;

	if(el_t < dv_t) {
		// scroll up
		d.scrollTop = (el_t > 8 ? el_t - 8 : 0);
	} else if(el_b > dv_b) {
		// scroll down
		d.scrollTop = el_b - d.offsetHeight + 8;
	}
}


function menu_chapter_open(el) {
  if(el == null) return;
  if(el.id == null) el = document.getElementById(el);
  if(el == null || el.id == null) return;
  var children = document.getElementById(el.id + "_children");
  if(children == null) return;
  var closed = (children.style.display == "none");
  children.style.display = (closed ? "block" : "none");
  el.src = (closed ? "img/chapter_opened.gif" : "img/chapter_closed.gif");
  var timeSpan = document.getElementById(el.id + "_timeSpan");
  if(timeSpan == null) return;
  timeSpan.style.display = (closed ? "none" : "inline");
}

function menu_create_html(media) {
  if(media == null) return;
  menu_marked_tag_id = null;
  menu_marked_tags = new Array();

  // chapters tree
  var html = "";
  if(media.chapters != null && media.rootChapter != null) {
    html += menu_create_menu_tree_html(media, media.chapters, "", media.rootChapter.tagids);
  }
/*  if(media.chapters != null) {
    html += menu_create_chapters_tree_html(media, media.chapters, "");
  }
  // root chapter tags
  if(media.rootChapter != null) {
    html += menu_create_tags_list_html(media, media.rootChapter.tagids, "");
  }
*/
  return html;
}

function menu_create_menu_tree_html(media, chapters, parentid, tagids) {
  var chapter = null;
  var tag = null;
  var html = "";
  if(media == null) return "";
  var a = new Array();
  // fill array a with tags and chapters of the current level
  if(chapters != null) {
    for(var i = 0, n = chapters.length; i < n; i++) {
      chapter = chapters[i];
      if(chapter != null && chapter.parentid == parentid) {
        a.push(({"type":"chapter", "id":chapter.id, "time":chapter.time}));
      }
    }
  }
  if(tagids != null) {
    for(var i = 0, n = tagids.length; i < n; i++) {
      tag = media.getTagById(tagids[i]);
      if(tag != null) {
        a.push(({"type":"tag", "id":tag.id, "time":tag.time}));
      }
    }
  }
  a.sort(function(a, b) {var a_time = (a == null ? 0 : a.time); var b_time = (b == null ? 0 : b.time); return a_time - b_time; });
  for(var i = 0, n = a.length; i < n; i++) {
    if(a[i] != null) {
      if(a[i].type == "chapter") {
        html += menu_create_chapter_html(media, chapters, media.getChapterById(a[i].id), parentid);
      } else {
        html += menu_create_tag_html(media, media.getTagById(a[i].id), parentid);
      }
    }
  }
  return html;
}

function menu_create_chapters_tree_html(media, chapters, parentid) {
  var chapter = null;
  var html = "";
  if(chapters != null) {
    for(var i = 0, n = chapters.length; i < n; i++) {
      chapter = chapters[i];
      if(chapter != null && chapter.parentid == parentid) {
        html += menu_create_chapter_html(media, chapters, chapter, parentid);
/*        html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;" id="menu_chapter_' + chapter.id + '_table">' +
                '<tr><td style="width:16px; height:16px; vertical-align:middle; text-align:center;"><img id="menu_chapter_' + chapter.id + '" src="img/chapter_closed.gif" onclick="menu_chapter_open(this);" style="border:none; cursor:pointer;" /></td>' +
                '<td style="height:16px; vertical-align:middle; text-align:left;" id="menu_chapter_' + chapter.id + '_title" parent_id="' + parentid + '" class="menu_chapter" onmouseover="menu_hl_chapter(this, true, \'' + chapter.id + '\');" onmouseout="menu_hl_chapter(this, false, \'' + chapter.id + '\');" onclick="menu_chapter_onclick(\'' + chapter.id + '\', \'' + rm3s_escapeString(chapter.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(chapter.description, ["'", "\""]) + '\', \'' + chapter.parentid + '\')">' + 
                  '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">' +
                  '<tr><td style="height:16px; vertical-align:middle; text-align:left;">' +  chapter.title + '</td>' + 
                  (editMode ? '' : '<td style="width:55px; text-align:left; vertical-align:middle; padding:0px 4px 0px 4px;">' + (chapter.time >= 0 ? rm3s_formatTime(chapter.time) : "--:--:--") + '</td>') + 
                  '<td style="width:55px; text-align:left; vertical-align:middle; padding:0px 4px 0px 4px;">' + (chapter.duration > 0 ? rm3s_formatTime(chapter.duration) : "--:--") + '</td>' + 
                  (editMode ? '<td style="width:34px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="" onclick="auth_editChapter(\'authoringDiv\', \'player\', media, \'' + chapter.id + '\'); return false;"><img src="img/Edit12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a>&nbsp;<a href="" onclick="auth_deleteChapter(media, \'' + chapter.id + '\'); return false;"><img src="img/Delete12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td>' : '') + '</tr></table>' +
                '</td></tr>' +
                '<tr><td style="padding:0px 0px 0px 16px; vertical-align:top; text-align:left;" colspan="2">' +
                '<div id="menu_chapter_' + chapter.id + '_children" style="width:100%; display:none;">';
        if(chapter.hasChildren(chapters)) html += menu_create_chapters_tree_html(media, chapters, chapter.id);
        html += menu_create_tags_list_html(media, chapter.tagids, chapter.id);
        html += '</div></td></tr></table>';
*/
      }
    }
  }
  return html;
}

function menu_create_chapter_html(media, chapters, chapter, parentid) {
  var html = "";
  if(media != null && chapter != null && chapter.parentid == parentid) {
/*        html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;" id="menu_chapter_' + chapter.id + '_table">' +
            '<tr><td style="width:16px; height:16px; vertical-align:middle; text-align:center;"><img id="menu_chapter_' + chapter.id + '" src="img/chapter_closed.gif" onclick="menu_chapter_open(this);" style="border:none; cursor:pointer;" /></td>' +
            '<td style="height:16px; vertical-align:middle; text-align:left;" id="menu_chapter_' + chapter.id + '_title" parent_id="' + parentid + '" class="menu_chapter" onmouseover="menu_hl_chapter(this, true, \'' + chapter.id + '\');" onmouseout="menu_hl_chapter(this, false, \'' + chapter.id + '\');" onclick="menu_chapter_onclick(\'' + chapter.id + '\', \'' + rm3s_escapeString(chapter.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(chapter.description, ["'", "\""]) + '\', \'' + chapter.parentid + '\')">' + chapter.title + '</td></tr>' +
            '<tr><td style="padding:0px 0px 0px 16px; vertical-align:top; text-align:left;" colspan="2">' +
            '<div id="menu_chapter_' + chapter.id + '_children" style="width:100%; display:none;">';
*/
    html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;" id="menu_chapter_' + chapter.id + '_table">' +
            '<tr><td style="width:16px; height:16px; vertical-align:top; text-align:center;"><img id="menu_chapter_' + chapter.id + '" src="img/chapter_closed.gif" onclick="menu_chapter_open(this);" style="border:none; cursor:pointer; vertical-align:top;" /></td>' +
            '<td style="height:16px; vertical-align:middle; text-align:left;" id="menu_chapter_' + chapter.id + '_title" parent_id="' + parentid + '" class="menu_chapter" onmouseover="menu_hl_chapter(this, true, \'' + chapter.id + '\');" onmouseout="menu_hl_chapter(this, false, \'' + chapter.id + '\');" onclick="menu_chapter_onclick(\'' + chapter.id + '\', \'' + rm3s_escapeString(chapter.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(chapter.description, ["'", "\""]) + '\', \'' + chapter.parentid + '\')">' + 
              '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">' +
              '<tr><td style="height:16px; vertical-align:middle; text-align:left;">' +  chapter.title + '</td>' + 
              '<td style="width:' + starttimeWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><span id="menu_chapter_' + chapter.id + '_timeSpan">' + (chapter.time >= 0 ? rm3s_formatTime(chapter.time, true, true) : "--:--:--") + '</span></td>' + 
              (editMode ? '' : '<td style="width:' + durationWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;">' + (chapter.duration > 0 ? rm3s_formatTime(chapter.duration, true, true) : "--:--:--") + '</td>') + 
              (editMode ? '<td style="width:' + editWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="#" onclick="auth_editChapter(\'authoringDiv\', \'player\', media, \'' + chapter.id + '\'); event.cancelBubble = true; if(event.stopPropagation) event.stopPropagation(); return false;"><img src="img/Edit12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td><td style="width:' + delWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="#" onclick="auth_deleteChapter(media, \'' + chapter.id + '\'); event.cancelBubble = true; if(event.stopPropagation) event.stopPropagation(); return false;"><img src="img/Delete12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td>' : '') + '</tr></table>' +
            '</td></tr>' +
            '<tr><td style="padding:0px 0px 0px 16px; vertical-align:top; text-align:left;" colspan="2">' +
            '<div id="menu_chapter_' + chapter.id + '_children" style="width:100%; display:none;">';
    html += menu_create_menu_tree_html(media, chapters, chapter.id, chapter.tagids);
/*    if(chapter.hasChildren(chapters)) html += menu_create_chapters_tree_html(media, chapters, chapter.id);
    html += menu_create_tags_list_html(media, chapter.tagids, chapter.id);
*/
    html += '</div></td></tr></table>';
  }
  return html;
}

function menu_create_tags_list_html(media, tagids, chapter_id) {
  var html = "";
  var tag = null;
  if(media != null && tagids != null) {
    for(var i = 0, n = tagids.length; i < n; i++) {
      tag = media.getTagById(tagids[i]);
      html += menu_create_tag_html(media, tag, chapter_id);      
/*      if(tag != null) {
        id = "menu_tag_" + chapter_id + "_" + tag.id;
        html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">'+
                 '<tr><td style="height:16px; vertical-align:middle; text-align:left;" class="menu_tag" id="' + id + '" chapter_id="' + chapter_id + '" onmouseover="menu_hl_tag(this, true);" onmouseout="menu_hl_tag(this, false);" title="' + rm3s_formatTime(tag.time) + '" onclick="menu_tag_onclick(\'' + tag.id + '\', \'' + tag.time + '\', \'' + rm3s_escapeString(tag.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(tag.description, ["'", "\""]) + '\');">' + 
                 '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">' +
                 '<tr><td style="height:16px; vertical-align:middle; text-align:left;">' + tag.title + '</td>' + 
                  (editMode ? '' : '<td style="width:50px; text-align:left; vertical-align:middle; padding:0px 4px 0px 4px;">' + rm3s_formatTime(tag.time) + '</td>') +
                 '<td style="width:55px; text-align:left; vertical-align:middle; padding:0px 4px 0px 4px;">' + rm3s_formatTime(tag.duration) + '</td>' + 
                  (editMode ? '<td style="width:34px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="JavaScript:auth_editTag(\'authoringDiv\', \'player\', media, \'' + tag.id + '\');"><img src="img/Edit12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a>&nbsp;<a href="JavaScript:auth_deleteTag(media, \'' + tag.id + '\');"><img src="img/Delete12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td>' : '') + '</tr></table>' +
                 '</td></tr></table>';
      }
*/
    }
  }
  return html;
}

function menu_create_tag_html(media, tag, chapter_id) {
  var html = "";
  var id = "";
  if(tag != null) {
    id = "menu_tag_" + chapter_id + "_" + tag.id;
/*        html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">'+
             '<tr><td style="height:16px; vertical-align:middle; text-align:left;" class="menu_tag" id="' + id + '" chapter_id="' + chapter_id + '" onmouseover="menu_hl_tag(this, true);" onmouseout="menu_hl_tag(this, false);" title="' + rm3s_formatTime(tag.time) + '" onclick="menu_tag_onclick(\'' + tag.id + '\', \'' + tag.time + '\', \'' + rm3s_escapeString(tag.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(tag.description, ["'", "\""]) + '\');">' + tag.title + '</td>' + 
             '<td style="width:50px; text-align:left; vertical-align:middle; padding:0px 4px 0px 4px;">' + rm3s_formatTime(tag.duration) + '</td>' + 
             '<td style="width:34px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="JavaScript:auth_editTag(\'authoringDiv\', \'player\', media, \'' + tag.id + '\');"><img src="img/Edit12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a>&nbsp;<a href="JavaScript:auth_deleteTag(media, \'' + tag.id + '\');"><img src="img/Delete12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td></tr></table>';
*/
    html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">'+
             '<tr><td style="height:16px; vertical-align:middle; text-align:left;" class="menu_tag" id="' + id + '" chapter_id="' + chapter_id + '" onmouseover="menu_hl_tag(this, true);" onmouseout="menu_hl_tag(this, false);" title="' + rm3s_formatTime(tag.time) + '" onclick="menu_tag_onclick(\'' + tag.id + '\', \'' + tag.time + '\', \'' + rm3s_escapeString(tag.title, ["'", "\""]) + '\', \'' + rm3s_escapeString(tag.description, ["'", "\""]) + '\');">' + 
             '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;">' +
             '<tr><td style="width:16px; height:16px; vertical-align:top; text-align:center;"><img src="img/tag.gif" style="border:none; cursor:pointer; vertical-align:top;" /></td>' + 
             '<td style="height:16px; vertical-align:middle; text-align:left;">' + tag.title + '</td>' + 
             '<td style="width:' + starttimeWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;">' + rm3s_formatTime(tag.time, true, true) + '</td>' +
             (editMode ? '' : '<td style="width:' + durationWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;">' + (tag.duration > 0 ? rm3s_formatTime(tag.duration, true, true) : "") + '</td>') + 
             (editMode ? '<td style="width:' + editWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="#" onclick="auth_editTag(\'authoringDiv\', \'player\', media, \'' + tag.id + '\'); event.cancelBubble = true; if(event.stopPropagation) event.stopPropagation(); return false;"><img src="img/Edit12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td><td style="width:' + delWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;"><a href="#" onclick="auth_deleteTag(media, \'' + tag.id + '\'); event.cancelBubble = true; if(event.stopPropagation) event.stopPropagation(); return false;"><img src="img/Delete12.gif" style="width:12px; height:12px; border:none; vertical-align:middle;" /></a></td>' : '') + '</tr></table>' +
             '</td></tr></table>';
  }
  return html;
}

function menu_header_create_html() {
  var html = "";
  html += '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;" class="menu_header">'+
           '<tr><td style="height:20px; vertical-align:middle; text-align:left; padding:0px 4px 0px 4px;" class="menu_header">Chapters/Tags</td>' + 
           '<td style="width:' + (starttimeWidth + 4) + 'px; text-align:left; vertical-align:middle; padding:0px 4px 0px 0px;" class="menu_header_small">Start&nbsp;Time</td>' +
           (editMode ? '' : '<td style="width:' + (durationWidth + 4) + 'px; text-align:left; vertical-align:middle; padding:0px 4px 0px 0px;" class="menu_header_small">Duration</td>') + 
           (editMode ? '<td style="width:' + editWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;" class="menu_header_small">Edit</td><td style="width:' + delWidth + 'px; text-align:right; vertical-align:middle; padding:0px 4px 0px 4px;" class="menu_header_small">Del</td>' : '') + 
           '</tr></table>';
  return html;
}

var menu_chapter_onclick_handler = null;
var menu_tag_onclick_handler = null;

function menu_chapter_onclick(chapter_id, chapter_title, chapter_description, chapter_parentid) {
  if(menu_chapter_onclick_handler != null) {
    try {
      menu_chapter_onclick_handler(chapter_id, chapter_title, chapter_description, chapter_parentid);
    } catch(e) {}
  } else {
    menu_chapter_open("menu_chapter_" + chapter_id);
  }
	log_event(mediaId, '', chapter_id, 3, 'Click on chapter "' + chapter_title + '"');
}

function menu_tag_onclick(tag_id, tag_time, tag_title, tag_description) {
  if(menu_tag_onclick_handler != null) {
    try {
      menu_tag_onclick_handler(tag_id, tag_time, tag_title, tag_description);
    } catch(e) {}
  } else {
//    mediaCommand('player', 'goto', tag_time); 
//    mediaCommand('player', 'play');  
  }
	log_event(mediaId, tag_id, '', 4, 'Click on tag "' + tag_title + '" (' + rm3s_formatTime(tag_time) + ')');
}
var menu_marked_tag_id = null;
var menu_marked_tags = new Array();

function menu_mark_tag_by_id(tag_id, chapters_list) {
  if(tag_id == menu_marked_tag_id) return;
  // clear current marked
  if(menu_marked_tag_id != null) {
    menu_marked_tag_id = null;
    while(menu_marked_tags != null && menu_marked_tags.length > 0) menu_mark_tag(menu_marked_tags.pop(), false);
  }
  // mark new
  if(tag_id != null) {
    menu_marked_tag_id = tag_id;
    // all marked items should be visible (open Chapters and make visible using scroll)
    var ch = null;
    var tag = null;
    for(var i = -1, n = (chapters_list == null ? 0 : chapters_list.length); i < n; i++) {
      ch = (i < 0 ? "" : (chapters_list[i] == null ? null : chapters_list[i].id));
      if(ch != null) {
        tag = document.getElementById("menu_tag_" + ch + "_" + tag_id);
        if(tag != null) {
            menu_mark_tag(tag, true);
            menu_show_tag(tag, true);
            menu_marked_tags.push(tag);
        }
      }
    }
  }
}

