﻿var auth_status = 0;
var auth_statusStr = "";
var auth_changesLog = [];
var auth_changesLogId = -1;

var auth_lastChapterId = "";
var auth_fileMng = new FileMng();

function auth_editTag(divId, playerId, _media, tagId) {
  if(_media == null || _media == "") _media = media;
  var div = document.getElementById(divId);
  if(div == null) return;
  var w_title = (tagId == null ? "Add a tag" : "Edit tag");
  var tag = (_media == null || tagId == null ? null : _media.getTagById(tagId));
	/*if(tag != null) {
		//mediaCommand(playerId, 'goto', tag.time);
		if(menu_tag_onclick_handler != null) menu_tag_onclick_handler(tag.id, tag.time, tag.title, tag.description);
	}*/
  //mediaCommand(playerId, 'pause');
  _player.exec('pause', 0);
  var title = (tag == null || tag.title == null ? "" : rm3s_escapeString(tag.title, ["\""]));
  var desc = (tag == null || tag.description == null ? "" : rm3s_escapeString(tag.description, ["\""]));
  var slide = (tag == null || tag.slide == null ? "" : rm3s_escapeString(tag.slide, ["\""]));
//  var time = (tag == null ? mediaGetCurrentTime(playerId) : tag.time);
  var time = (tag == null || tag.time == null ? _player.getCurrentTime() : tag.time);
  var time_str = rm3s_formatTime(time);
  var ch_opt = '<option value="">[root]</option>';
  
  var curTagId = menu_marked_tag_id;
  
  if(media != null) {
    var chList = (tag == null ? (curTagId == null ? null : _media.getChaptersContainTag(curTagId)) : _media.getChaptersContainTag(tag.id));
    var chId = (chList == null || chList.length == 0 || chList[0] == null ? (curTagId == null ? (auth_lastChapterId == null ? "" : auth_lastChapterId) : "") : chList[0].id);
    for(var i = 0, n = (_media.chapters == null ? 0 : _media.chapters.length); i < n; i++) {
      ch_opt += '<option value="' + _media.chapters[i].id + '"' + (chId == _media.chapters[i].id ? ' selected="selected"' : '') + '>' + _media.chapters[i].title + '</option>';
    }
  }
  div.style.display = "block";
  div.innerHTML = '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed; background-color:#333333;">' +
                  '<tr><td style="width:20%; height:1px;"></td><td style="width:80%; height:1px;"></td></tr>' +                  
                  '<tr><td colspan="2" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-weight:bold;">' + w_title + '</td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Tag Title</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_title" style="width:90%; color:#000000;" value="' + title + '" /></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Description</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><textarea id="auth_desc" style="width:90%; color:#000000;" rows="2">' + desc + '</textarea></td></tr>' +
                  //'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Tag Slide</td>' +
                  //'<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_slide" style="width:90%; color:#000000;" value="' + slide + '" /></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Tag Time</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;">' +
									'<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed;"><tr>' + 	
                  '<td style="text-align:left; vertical-align:middle;"><input type="hidden" id="auth_time_hdn" value="' + time + '" /><input type="text" id="auth_time" style="width:70px; color:#000000;" value="' + time_str + '" onblur="return auth_validateTime(this);" />&nbsp;&nbsp;<a href="" onclick="auth_filltime(\'' + playerId + '\', \'auth_time\'); return false;" style="color:#ffffff; font-weight:bold;">Set Tag Time to Current Time</a></td>' +
									'<td style="width:15px; text-align:right; vertical-align:middle;"><img src="img/skip_back.gif" style="border:none;" onmousedown="skipMediaStart(-1);" onmouseup="skipMediaDo(); auth_filltime(\'' + playerId + '\', \'auth_time\');" onmouseout="skipMediaCancel();" /></td>' +
									'<td style="width:20px; text-align:center; vertical-align:middle; color:#5789cb;" id="skipValueCell">0</td>' +
									'<td style="width:15px; text-align:left; vertical-align:middle;"><img src="img/skip_forward.gif" style="border:none;" onmousedown="skipMediaStart(1);" onmouseup="skipMediaDo(); auth_filltime(\'' + playerId + '\', \'auth_time\');" onmouseout="skipMediaCancel();" /></td>' +
									'</tr></table>' +
									'</td></tr>' +
									'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;"></td><td style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><a href="#" onclick="auth_playcurrenttime(\'' + playerId + '\', \'auth_time_hdn\'); return false;" style="color:#ffffff; font-weight:bold;"><img src="img/play10.gif" style="width:10px; height:10px; border:none;" /> Play from Current Tag Time</a></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Parent Chapter</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><select id="auth_parent" style="width:90%; color:#000000;">' + ch_opt + '</select></td></tr>' +
                  '<tr><td colspan="2" style="width:100%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">' +
                  '<input type="button" value="Save Tag" onclick="auth_applyTag(' + (tagId == null ? 'null' : '\'' + tagId + '\'') + ', \'' + divId + '\');" />&nbsp;&nbsp;&nbsp;<input type="button" value="Cancel" onclick="auth_close(\'' + divId + '\');" /></td></tr>' +
                  '</table>';
  try { document.getElementById("auth_title").focus();
  } catch(e) {}
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceAddControl', false, "auth_desc"); } catch(e) {}
  }
}

function auth_filltime(playerId, inputId) {
//  var time = mediaGetCurrentTime(playerId);
  var time = _player.getCurrentTime();
  var input = document.getElementById(inputId);
  if(input != null) input.value = rm3s_formatTime(time);
  input = document.getElementById(inputId + "_hdn");
  if(input != null) input.value = time;
}

function auth_playcurrenttime(playerId, hiddenId) {
  var hidden = document.getElementById(hiddenId);
  if(hidden != null) { 
		_player.exec('goto', hidden.value);
		_player.exec('play', 0);
		//mediaCommand(playerId, 'goto', hidden.value);
		//mediaCommand(playerId, 'play', 0);
	}
}

function auth_applyTag(tagId, divId) {
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceRemoveControl', false, "auth_desc"); } catch(e) {}
  }
  var i_title = document.getElementById("auth_title");
  var i_desc = document.getElementById("auth_desc");
  var i_slide = document.getElementById("auth_slide");
  var h_time = document.getElementById("auth_time_hdn");
  var s_parent = document.getElementById("auth_parent");
  var chId = (s_parent == null || s_parent.value == "" ? null : s_parent.value);
  if(tagId != null && tagId != "") media.removeTagFromAllChapters(tagId);
  else auth_lastChapterId = chId;
  if(tagId == null || tagId == "") tagId = auth_getNewId();
  media.addTagToChapters(new RM3STag(tagId, (h_time == null ? 0 : h_time.value), (i_title == null ? "" : i_title.value), (i_desc == null ? "" : i_desc.value), 0, (i_slide == null ? "" : i_slide.value)), [chId]);
  //media.orderTagsByTime();
  media.calcDurations(process_media_duration);
  media.calcChaptersTime("");
  var menuDiv = document.getElementById("menuDiv");
  if(menuDiv != null) menuDiv.innerHTML = menu_create_html(media);
  auth_close(divId);
  
/*  // make this tag active
  var tag = (tagId == null ? null : media.getTagById(tagId));
	if(tag != null) {
		var st = _player.getState();
		_player.exec('goto', tag.time);
		_player.exec('pause', 0);
		//if(menu_tag_onclick_handler != null) menu_tag_onclick_handler(tag.id, tag.time, tag.title, tag.description);
	}
*/  
	auth_saveChanges();	
}

function auth_deleteTag(_media, tagId) {
  if(_media == null || _media == "") _media = media;
  var tag = (_media == null || tagId == null ? null : _media.getTagById(tagId));
  if(tag == null) return;
  if(confirm("Delete '" + tag.title + "' tag?")) {
    _media.deleteTag(tagId);
    _media.calcDurations(process_media_duration);
    _media.calcChaptersTime("");
    var menuDiv = document.getElementById("menuDiv");
    if(menuDiv != null) menuDiv.innerHTML = menu_create_html(_media);
		auth_saveChanges();	
  }
}

function auth_close(divId) {
  var div = document.getElementById(divId);
  if(div != null) {
		div.innerHTML = "";
	  div.style.display = "none";
	}
}

function auth_getNewId() {
  return "id" + Math.random();
}

function auth_editChapter(divId, playerId, _media, chapterId) {
  if(_media == null || _media == "") _media = media;
//  mediaCommand(playerId, 'pause');
  _player.exec('pause', 0);
  var div = document.getElementById(divId);
  if(div == null) return;
  var w_title = (chapterId == null ? "Add a chapter" : "Edit chapter");
  var chapter = (_media == null || chapterId == null ? null : _media.getChapterById(chapterId));
  var title = (chapter == null || chapter.title == null ? "" : rm3s_escapeString(chapter.title, ["\""]));
  var desc = (chapter == null || chapter.description == null ? "" : rm3s_escapeString(chapter.description, ["\""]));
  var ch_opt = '<option value="">[root]</option>';
  
  var pId = (chapter == null ? (auth_lastChapterId == null ? "" : auth_lastChapterId) : chapter.parentid);
  var curTagId = menu_marked_tag_id;
  var assignHTML = "";
  if(chapter == null && curTagId != null && _media != null) {
		// set parent chapter for new chapter as a chapter of current tag
		var chList = _media.getChaptersContainTag(curTagId);
		if(chList != null && chList.length > 0 && chList[0] != null) pId = chList[0].id;
		else pId = "";
		// if there's a selected tag and new chapter, add checkboxes to assign current tag and/or all underneaths to this new chapter
		assignHTML = '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="checkbox" id="auth_assignCurrentTag" checked="checked" /></td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><label for="auth_assignCurrentTag">Place current tag into this new chapter</label></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="checkbox" id="auth_assignUnderneathTags" checked="checked" /></td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><label for="auth_assignUnderneathTags">Place all tags underneath current into this new chapter</label></td></tr>';
  }
  if(_media != null) {
    for(var i = 0, n = (_media.chapters == null ? 0 : _media.chapters.length); i < n; i++) {
      if(_media.chapters[i].id != chapterId && !media.isSubChapter(chapter, _media.chapters[i])) ch_opt += '<option value="' + _media.chapters[i].id + '"' + (pId == _media.chapters[i].id ? ' selected="selected"' : '') + '>' + _media.chapters[i].title + '</option>';
    }
  }
  div.style.display = "block";
  div.innerHTML = '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed; background-color:#333333;">' +
                  '<tr><td style="width:20%; height:1px;"></td><td style="width:80%; height:1px;"></td></tr>' +                  
                  '<tr><td colspan="2" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-weight:bold;">' + w_title + '</td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Chapter Title</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_title" style="width:90%; color:#000000;" value="' + title + '" /></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Description</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><textarea id="auth_desc" style="width:90%; color:#000000;" rows="2">' + desc + '</textarea></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Parent Chapter</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><select id="auth_parent" style="width:90%; color:#000000;">' + ch_opt + '</select></td></tr>' +
                  assignHTML +
                  '<tr><td colspan="2" style="width:100%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">' +
                  '<input type="button" value="Save Chapter" onclick="auth_applyChapter(' + (chapterId == null ? 'null' : '\'' + chapterId + '\'') + ', \'' + divId + '\');" />&nbsp;&nbsp;&nbsp;<input type="button" value="Cancel" onclick="auth_close(\'' + divId + '\');" /></td></tr>' +
                  '</table>';
  try { document.getElementById("auth_title").focus();
  } catch(e) {}
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceAddControl', false, "auth_desc"); } catch(e) {}
  }
}

function auth_applyChapter(chapterId, divId) {
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceRemoveControl', false, "auth_desc"); } catch(e) {}
  }
  var i_title = document.getElementById("auth_title");
  var i_desc = document.getElementById("auth_desc");
  var s_parent = document.getElementById("auth_parent");
  var c_assignCurrentTag = document.getElementById("auth_assignCurrentTag");
  var c_assignUnderneathTags = document.getElementById("auth_assignUnderneathTags");
    
  var chId = (s_parent == null || s_parent.value == "" ? "" : s_parent.value);
  var chapter = media.getChapterById(chapterId);
  var nId = "";
  media.addChapter(new RM3SChapter((chapter == null ? nId = auth_getNewId() : chapter.id), (i_title == null ? 0 : i_title.value), (i_desc == null ? 0 : i_desc.value), chId, (chapter == null ? new Array() : chapter.tagids)));
  if(chapter == null) auth_lastChapterId = nId;
  var curTagId = menu_marked_tag_id;
	if(curTagId != null && c_assignUnderneathTags != null && c_assignUnderneathTags.checked) {
		var tag = media.getTagById(curTagId);
		if(tag != null) {
			// move all underneath tags to the new chapter
			var chList = media.getChaptersContainTag(tag.id);
			var tagIds = (chList == null || chList.length < 1 || chList[0] == null ? (media.rootChapter == null ? null : media.rootChapter.tagids) : chList[0].tagids);
			var b = false;
			var i = 0;
			var n = (tagIds == null ? 0 : tagIds.length);
			for(i = 0; i < n && !b; i++) {
				b = (tagIds[i] == tag.id);
			}
			var tt = null;
			for(;i < n; n--) {
				tt = tagIds[i];
				media.removeTagFromAllChapters(tt);
				media.addTagToChapters(media.getTagById(tt), [(chapter == null ? nId : chapter.id)]);
			}

		}
	}

	if(curTagId != null && c_assignCurrentTag != null && c_assignCurrentTag.checked) {
		var tag = media.getTagById(curTagId);
		if(tag != null) {
			media.removeTagFromAllChapters(tag.id);
			media.addTagToChapters(tag, [(chapter == null ? nId : chapter.id)]);
		}
	}

  //media.orderTagsByTime();
  media.calcDurations(process_media_duration);
  media.calcChaptersTime("");
  var menuDiv = document.getElementById("menuDiv");
  if(menuDiv != null) menuDiv.innerHTML = menu_create_html(media);
  auth_close(divId)
	
	auth_saveChanges();
}

function auth_deleteChapter(_media, chapterId) {
  if(_media == null || _media == "") _media = media;
  var chapter = (_media == null || chapterId == null ? null : _media.getChapterById(chapterId));
  if(chapter == null) return;
  if(confirm("Delete '" + chapter.title + "' chapter?")) {
    _media.deleteChapter(chapterId);
    _media.calcDurations(process_media_duration);
    _media.calcChaptersTime("");
    var menuDiv = document.getElementById("menuDiv");
    if(menuDiv != null) menuDiv.innerHTML = menu_create_html(_media);
		auth_saveChanges();	
  }
}

function auth_editMedia(divId, playerId, _media) {
  if(_media == null || _media == "") _media = media;
  _player.exec('pause', 0);
//  mediaCommand(playerId, 'pause');
  var div = document.getElementById(divId);
  if(div == null || _media == null) return;
  var w_title = (_media.id == null || _media.id == "" ? "Add a program" : "Edit program");
  var title = (_media.title == null ? "" : rm3s_escapeString(_media.title, ["\""]));
  var desc = (_media.description == null ? "" : rm3s_escapeString(_media.description, ["\""]));
  var surl = (_media.streamingURLs == null || _media.streamingURLs.length < 1 || _media.streamingURLs[0] == null ? "" : rm3s_escapeString(_media.streamingURLs[0].publicURL, ["\""]));
  var surl_local = (_media.streamingURLs == null || _media.streamingURLs.length < 1 || _media.streamingURLs[0] == null ? "" : rm3s_escapeString(_media.streamingURLs[0].localURL, ["\""]));
  var password = (_media.password == null ? "" : rm3s_escapeString(_media.password, ["\""]));
  var slidesFolder = rm3s_escapeString(_media.slidesFolder, ["\""]);
  var width = (_media.dimensions == null || isNaN(_media.dimensions.width) ? 0 : _media.dimensions.width);
  var height = (_media.dimensions == null || isNaN(_media.dimensions.height) ? 0 : _media.dimensions.height);
  var v_back = (_media.styles == null || _media.styles.v_background == null ? "" : rm3s_escapeString(_media.styles.v_background, ["\""]));
  var h_back = (_media.styles == null || _media.styles.h_background == null ? "" : rm3s_escapeString(_media.styles.h_background, ["\""]));
  
  div.style.display = "block";
  div.innerHTML = '<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed; background-color:#333333;">' +
                  '<tr><td style="width:20%; height:1px;"></td><td style="height:1px;"></td><td style="width:20px; height:1px;"></td></tr>' +                  
                  '<tr><td colspan="3" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-weight:bold;">' + w_title + '</td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Title</td>' +
                  '<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_title" style="width:90%; color:#000000;" value="' + title + '" /></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Description</td>' +
                  '<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><textarea id="auth_desc" style="width:90%; color:#000000;" rows="2">' + desc + '</textarea></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Streaming URL</td>' +
                  '<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_surl" style="width:90%; color:#000000;" value="' + surl + '" /></td></tr>' +
                  //'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Local authoring URL</td>' +
                  //'<td style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_surl_local" style="width:90%; color:#000000; z-index:1000;" value="' + surl_local + '" /></td>' +
                  //'<td style="width:100%; background-image:url(\'img/open.gif\'); background-position:left center; background-repeat:no-repeat; text-align:left; vertical-align:middle;">' + 
                  //'<input type="file" id="auth_browse" onchange="auth_getBrowseFile(\'auth_browse\', \'auth_surl_local\');" class="browse_hidden" /></td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Video</td>' +
                  '<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;">width <input type="text" id="auth_width" style="width:20%; color:#000000;" value="' + width + '" />' +
                  '&nbsp;&nbsp;height <input type="text" id="auth_height" style="width:20%; color:#000000;" value="' + height + '" /></td></tr>' +
                  //'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Slides Folder</td>' +
                  //'<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_slidesFolder" style="width:90%; color:#000000;" value="' + slidesFolder + '" /></td></tr>' +
                  //'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Password</td>' +
                  //'<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_password" style="width:90%; color:#000000;" value="' + password + '" /></td></tr>' +
                  //'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;"></td>' +
                  //'<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-size:9px;">Specify password to protect this program from being viewed.<br />Left password blank to make this program public</td></tr>' +
									(_media.id == null || _media.id == "" ? '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="checkbox" id="auth_createStartTag" checked="checked" /></td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><label for="auth_createStartTag">Create Start tag at 00:00:00</label></td></tr>' : '') +                  
									//'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Vertical Mode Background</td>' +
                  //'<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_v_background" style="width:90%; color:#000000;" value="' + v_back + '" /></td></tr>' +
									//'<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">Horizontal Mode Background</td>' +
                  //'<td colspan="2" style="text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="text" id="auth_h_background" style="width:90%; color:#000000;" value="' + h_back + '" /></td></tr>' +
                  '<tr><td colspan="3" style="width:100%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">' +
                  '<input type="button" value="Apply" onclick="auth_applyMedia(\'' + divId + '\');" />&nbsp;&nbsp;&nbsp;<input type="button" value="Cancel" onclick="auth_close(\'' + divId + '\');" /></td></tr>' +
                  '</table>';
  try { document.getElementById("auth_title").focus();
  } catch(e) {}
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceAddControl', false, "auth_desc"); } catch(e) {}
  }
}

function auth_getBrowseFile(browse_id, input_id) {
	var browse = document.getElementById(browse_id);
	var input = document.getElementById(input_id);
	if(browse == null || input == null) return;
	var s = browse.value; 
	input.value = "file://" + s.replace(/\\/ig, "/");
}

function auth_applyMedia(divId) {
  if(tinyMCE) {
		try { tinyMCE.execCommand('mceRemoveControl', false, "auth_desc"); } catch(e) {}
  }
  var i_title = document.getElementById("auth_title");
  var i_desc = document.getElementById("auth_desc");
  var i_surl = document.getElementById("auth_surl");
  var i_surl_local = document.getElementById("auth_surl_local");
  var i_password = document.getElementById("auth_password");
  var i_width = document.getElementById("auth_width");
  var i_height = document.getElementById("auth_height");
  var i_slidesFolder = document.getElementById("auth_slidesFolder");

  var i_v_back = document.getElementById("auth_v_background");
  var i_h_back = document.getElementById("auth_h_background");

  var c_createStartTag = document.getElementById("auth_createStartTag");

  var s_title = (i_title == null ? "" : i_title.value);
  var s_description = (i_desc == null ? "" : i_desc.value);
  var s_streamingURL = (i_surl == null ? "" : i_surl.value);
  var s_streamingURL_local = (i_surl_local == null ? "" : i_surl_local.value);
  var s_password = (i_password == null ? "" : i_password.value);
  var s_width = (i_width == null ? "0" : parseInt(i_width.value));
  var s_height = (i_height == null ? "0" : parseInt(i_height.value));
  var s_slidesFolder = (i_slidesFolder == null ? "" : i_slidesFolder.value);
  
  var s_v_back = (i_v_back == null ? "" : i_v_back.value);
  var s_h_back = (i_h_back == null ? "" : i_h_back.value);

  if(s_title != media.title) {
    media.title = s_title;
    var titleDiv = document.getElementById("titleDiv");
    if(titleDiv != null) titleDiv.innerHTML = media.title;
    document.title = media.title + " :: Raptive Media";
  }
  if(s_description != media.description) {
    media.description = s_description;
  }
	
  if(s_password != media.password) {
    media.password = s_password;
  }
	
  if(s_slidesFolder != media.slidesFolder) {
    media.slidesFolder = s_slidesFolder;
  }
	
	media.dimensions = {width: (isNaN(s_width) ? 0 : s_width), height: (isNaN(s_height) ? 0 : s_height)};
	updateDimensions();
	
	var needUpdateStyles = false;
	if(media.styles == null || s_v_back != media.styles.v_background) {
		if(media.styles == null) media.styles = {h_background: null, v_background: null};
		media.styles.v_background = s_v_back;
		needUpdateStyles = true;
	}
	if(media.styles == null || s_h_back != media.styles.h_background) {
		if(media.styles == null) media.styles = {h_background: null, v_background: null};
		media.styles.h_background = s_h_back;
		needUpdateStyles = true;
	}
	if(needUpdateStyles) updateStyles();
	
  var oldURL =(media.streamingURLs == null || media.streamingURLs.length < 1 || media.streamingURLs[0] == null ? "" :  (useLocalURL ? media.streamingURLs[0].localURL : media.streamingURLs[0].publicURL));
  var newURL = (useLocalURL ? s_streamingURL_local : s_streamingURL);
	
	media.streamingURLs = [{id:0, publicURL:s_streamingURL, localURL:s_streamingURL_local}];
	
	if(oldURL != newURL) {
		_player.changeVideoURL(newURL, "playerDiv", "playerControlsDiv");
		//mediaCommand('player', 'open', newURL);
	}

	if(c_createStartTag != null && c_createStartTag.checked) {
		var tId = auth_getNewId();
		media.addTagToChapters(new RM3STag(tId, 0, "Start", "Start"), [null]);
		media.calcDurations(process_media_duration);
		media.calcChaptersTime("");
		var menuDiv = document.getElementById("menuDiv");
		if(menuDiv != null) menuDiv.innerHTML = menu_create_html(media);
	}
	
	media.id = auth_getNewId();
	
  auth_close(divId)
	auth_saveChanges();	
}

function auth_saveChanges() {
  // saving changed XML to Log and to cookies
  var media_xml = media.saveToXML();
	if(auth_changesLogId < -1) auth_changesLogId = -1;
	auth_changesLogId++;
	if(auth_changesLog == null) {
		auth_changesLog = [];
		auth_changesLogId = 0;
	}
	auth_changesLog.length = auth_changesLogId + 1;
	auth_changesLog[auth_changesLogId] = media_xml;
  if(mediaId > 0) {
		setCookie("rm3sLocalMedia" + mediaId, media_xml, new Date('02/02/2020'), "", "", false);
	}
}

function auth_saveMedia(_media, mediaId, needEvalAnswer) {
	if(_media == null || _media == "") _media = media;
	if(_media == null) return;
  if(mediaId > 0) {
		setCookie("rm3sLocalMedia" + mediaId, _media.saveToXML(), new Date('02/02/2020'), "", "", false);
	}
}

function auth_undoChanges() {
	if(auth_changesLogId > 0 && auth_changesLog != null && auth_changesLog.length > auth_changesLogId) {
		auth_changesLogId--;
		auth_applyChanges();
	} else {
		alert("No changes for Undo");
	}
}

function auth_redoChanges() {
	if(auth_changesLogId >= 0 && auth_changesLog != null && auth_changesLog.length > auth_changesLogId + 1) {
		auth_changesLogId++;
		auth_applyChanges();
	} else {
		alert("No changes for Redo");
	}
}

function auth_applyChanges() {
	if(auth_changesLog && auth_changesLogId >= 0) {
//		var streamingURL = (media.streamingURLs == null || media.streamingURLs.length < 1 ? "" : media.streamingURLs[0]);
	  var oldURL = (media.streamingURLs == null || media.streamingURLs.length < 1 || media.streamingURLs[0] == null ? "" :  (useLocalURL ? media.streamingURLs[0].localURL : media.streamingURLs[0].publicURL));
		media.loadFromXML(auth_changesLog[auth_changesLogId]);
		var newURL = (media.streamingURLs == null || media.streamingURLs.length < 1 || media.streamingURLs[0] == null ? "" :  (useLocalURL ? media.streamingURLs[0].localURL : media.streamingURLs[0].publicURL));
		if(oldURL != newURL) {
			if(newURL == "" && useLocalURL) switchLocalURL();
			else window.setTimeout("_player.exec('open', '" + newURL + "');", 500); 
			//else window.setTimeout("mediaCommand('player', 'open', '" + newURL + "');", 500); 
    }
    var menuDiv = document.getElementById("menuDiv");
    if(menuDiv != null) menuDiv.innerHTML = menu_create_html(media);

    var titleDiv = document.getElementById("titleDiv");
    if(titleDiv != null) titleDiv.innerHTML = media.title;
    document.title = media.title + " :: Raptive Media";
    
		updateDimensions();
	}
}

function auth_import(divId) {
  var div = document.getElementById(divId);
  if(div == null) return;
  div.style.display = "block";
  div.innerHTML = '<iframe id="auth_uploadFrame" name="auth_uploadFrame" src="about:blank" style="display:none;"></iframe>' +
									'<form action="Actions/ReadLocalFile.aspx" method="post" enctype="multipart/form-data" target="auth_uploadFrame" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;" onsubmit="auth_startImport(\'' + divId + '\'); return true;">' +
									'<table border="0" cellpadding="0" cellspacing="0" style="width:100%; table-layout:fixed; background-color:#333333;">' +
                  '<tr><td style="width:20%; height:1px;"></td><td style="width:80%; height:1px;"></td></tr>' +                  
                  '<tr><td colspan="2" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-weight:bold;">Open Program from XML or RMX.JS</td></tr>' +
                  '<tr><td style="width:20%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">File</td>' +
                  '<td style="width:80%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="file" id="auth_file" name="auth_file" style="width:90%; color:#000000;" /></td></tr>' +
                  '<tr><td colspan="2" style="width:100%; text-align:right; vertical-align:middle; padding:4px 4px 4px 4px;">' +
                  '<input type="hidden" name="callback" value="window.top.window.auth_importComplete" />' +
                  '<input type="submit" value="Open" />&nbsp;&nbsp;&nbsp;<input type="button" value="Cancel" onclick="auth_close(\'' + divId + '\');" /></td></tr>' +
                  //'<tr><td colspan="2" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px;"><a href="#" onclick="auth_importPP(\'' + divId + '\'); return false;">Import Program from ProwerPoint presentation</a></td></tr>' +
                  '</table></form>';
  try { document.getElementById("auth_file").focus();
  } catch(e) {}
}

function auth_startImport(divId) {
	var authStatusDiv = document.getElementById("authStatusDiv");
	if(authStatusDiv != null) authStatusDiv.innerHTML = "Opening...";
	var div = document.getElementById(divId);
	if(div != null) div.style.display = "none";
}

function auth_importComplete(xml) {
	var authStatusDiv = document.getElementById("authStatusDiv");
	if(authStatusDiv != null) authStatusDiv.innerHTML = "";
	//alert(xml);
	if(auth_changesLogId < -1) auth_changesLogId = -1;
	auth_changesLogId++;
	if(auth_changesLog == null) {
		auth_changesLog = [];
		auth_changesLogId = 0;
	}
	auth_changesLog.length = auth_changesLogId + 1;
	
	try {
		// veotag import support
		var tags = new Array();
		xml = xml.replace(/veotag.data.Tag/gi, "Array");
		// 
		eval(xml);
		if(window.rmxj_xml) xml = window.rmxj_xml;
		else if(tags != null && tags.length > 0) {
			// import tags from veotag
			auth_importVeotagTags(tags);
			xml = media.saveToXML();
		}
	} catch(e) {}
	
	auth_changesLog[auth_changesLogId] = xml;
	auth_applyChanges();

	mediaId = media.id;
	auth_saveMedia(media, mediaId, false);
	
	auth_close(divId);
}

function auth_export(divId) {
  var div = document.getElementById(divId);
  if(div == null) return;
  div.style.display = "block";
  div.innerHTML = '<form action="Actions/ExportXML.aspx" method="post" enctype="multipart/form-data" target="_blank" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;" onsubmit="auth_startExport(\'' + divId + '\'); return true;">' +
									'<table border="0" cellpadding="0" cellspacing="0" style="width:100%; background-color:#333333;">' +
                  '<tr><td colspan="4" style="width:100%; text-align:left; vertical-align:middle; padding:4px 4px 4px 4px; font-weight:bold;">Save Program to XML</td></tr>' +
                  '<tr><td style="text-align:center; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="submit" name="file" id="auth_file" value="Save To XML" style="width:95%;" /></td>' +
                  '<td style="text-align:center; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="submit" name="rmxjs" id="auth_rmxjs" value="Save To RMX.JS" style="width:95%;" /></td>' +
                  '<td style="text-align:center; vertical-align:middle; padding:4px 4px 4px 4px;"><input type="submit" name="open" id="auth_open" value="Open In Browser" style="width:95%;" /></td>' +
                  '<td style="text-align:center; vertical-align:middle; padding:4px 4px 4px 4px;"><a href="#" onclick="auth_close(\'' + divId + '\'); return false;">Cancel</a></td></tr>' +
                  '</table>' +
                  '<input type="hidden" name="title" value="' + rm3s_escapeString(media.title, ["\""]) + '" />' +
                  '<textarea name="xml" style="display:none;">' + media.saveToXML() + '</textarea>' +
                  '</form>';
  try { document.getElementById("auth_file").focus();
  } catch(e) {}
}

function auth_startExport(divId) {
	window.setTimeout('auth_close(\'' + divId + '\');', 1000);
}

function auth_saveToFile() {
	var d = new Date();
	var filename = rm3s_escapeString(media.title).replace(/\s+/ig, "_") + "_" + d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + ".xml";
	try {
		filename = auth_fileMng.saveDialog("", filename, "XML Files (*.xml)|*.xml", "Save Program");
		if(filename == null || filename == "") return;
		auth_fileMng.saveFile(filename, media.saveToXML());
	} catch(e) {
		//alert("Save file is not supported or permission denied");
		 auth_export("authoringDiv");
	}
}

function auth_importVeotagTags(tags) {
	if(tags == null) return;
	media.tags = new Array();
  media.rootChapter = new RM3SChapter();
  media.chapters = new Array();
  var tag = null;
  var rm3stag = null;
  var rm3schapter = null;
  var _t = null;
  for(var i = 0, n = tags.length; i < n; i++) {
		tag = tags[i];
		if(tag != null && tag.length > 4) {
			media.addTag(rm3stag = new RM3STag(tag[0], tag[2], tag[3], tag[4], null, null));
			rm3schapter = media.getChapterById("c" + tag[1]);
			
			if(rm3schapter != null) {
				// add to this chapter
				media.addTagToChapters(rm3stag, [rm3schapter.id]);
			} else {
				_t = media.getTagById(tag[1]);
				if(_t != null) {
					// create new chapter and place it's tag into it
					media.addChapter(rm3schapter = new RM3SChapter("c" + _t.id, _t.title, _t.description, null, [], null, null));
					media.removeTagFromAllChapters( _t.id);
					media.addTagToChapters(_t, [rm3schapter.id]);
					media.addTagToChapters(rm3stag, [rm3schapter.id]);
				} else {
					// add to root
					media.addTagToChapters(rm3stag, [null]);
				}
			}
		}
  }
  media.calcDurations(process_media_duration);
  media.calcChaptersTime("");
}

function auth_validateTime(input) {
	if(input == null || input.value == null) return false;
	var s = input.value.replace(/(\s)+/g, "");
	var r = /^((([0-9]*)[:])?([0-5]?[0-9])[:])?([0-5]?[0-9])([.]([0-9]*))?$/;
	var m = s.match(r);
	if(m == null) {
		alert("Please input time in 'HH:MM:SS.XXX' format");
		input.focus();
		return false;
	} else {
		var t = (m[3] == null || m[3] == "" ? 0 : parseInt(m[3])) * 3600 + (m[4] == null || m[4] == "" ? 0 : parseInt(m[4])) * 60 + (m[5] == null || m[5] == "" ? 0 : parseInt(m[5])) + (m[7] == null || m[7] == "" ? 0 : eval("0." + parseInt(m[7]) + "0"));
		var auth_time_hdn = document.getElementById("auth_time_hdn");
		if(auth_time_hdn != null) auth_time_hdn.value = t;
		input.value = rm3s_formatTime(t);
		return true;
	}
}
