/**
 * Initializes custom TinyMCE plugins.
 * 
 * @param string currentChapterTitle  Title of the button for the current chapter plugin
 * @param string popUpTitle           Title of the button for the popup plugin
 */
function initTinyMCE(currentChapterTitle, popUpTitle)
{
  //creates plugins
  createCurrentChapterPlugin(currentChapterTitle);
  createPopupPlugin(popUpTitle);
  
  //registers plugins
  if(tinymce.PluginManager.get("currentChapter") == null)
    tinymce.PluginManager.add("currentChapter", tinymce.plugins.CurrentChapterPlugin);
  
  if(tinymce.PluginManager.get("popUp") == null)
    tinymce.PluginManager.add("popUp", tinymce.plugins.PopUpPlugin);
}

/**
 * Create a new plugin class used to insert the current chapter.
 * 
 * @param string title  Button's title
 */
function createCurrentChapterPlugin(title)
{
  //avoid create the plugin more than once
  if(tinymce.plugins != undefined && tinymce.plugins.CurrentChapterPlugin != undefined)
    return;
  
  tinymce.create("tinymce.plugins.CurrentChapterPlugin", {
     init : function(ed, url) {
      ed.addButton("currentChapter", {
        onclick : currentChapter_onClick,
        "class" : "currentChapterIcon",
        title : title
      });
    }
  });
}

/**
 * Create a new plugin class used to pop up the take a note.
 * 
 * @param string title  Button's title
 */
function createPopupPlugin(title)
{
  //avoid create the plugin more than once
  if(tinymce.plugins != undefined && tinymce.plugins.PopUpPlugin != undefined)
    return;
  
  tinymce.create("tinymce.plugins.PopUpPlugin", {
    init : function(ed, url) {
      ed.addButton("popUp", {
        title : title,
        onclick : popUp_onClick,        
        "class" : "popUpIcon"
      });
    }
  });
}

/**
 * Handles click event on the current chapter plugin button.
 */
function currentChapter_onClick() 
{
  if (document.location.href.slice(document.location.href.lastIndexOf("/") + 1) == document.getElementById("timecodeAtStart").value) 
  { 
    var chapterUrl = document.location.href.slice(0, document.location.href.lastIndexOf("/")); 
  } 
  else 
  { 
    var chapterUrl = document.location.href; 
  }

  if (theColibri.components.chapters.currentChapter != null)
  {
    chapterUrl += theColibri.components.chapters.content[theColibri.components.chapters.currentChapter].timecode;
    document.getElementById("noteDetails_note_ifr").contentWindow.document.getElementById("tinymce").innerHTML += "<a href= " +chapterUrl + ">" + theColibri.components.chapters.content[theColibri.components.chapters.currentChapter].title + "</a>";
  }      
}

/**
 * Handles click event on the pop up plugin button
 */
function popUp_onClick()
{
  window.parent.open("'.sfContext::getInstance()->getRequest()->getRelativeUrlRoot().'/index.php/user/takeANote","Take_a_Note","width=620,height=550,location=no,menubar=no,toolbar=no")
}

/**
 * Sets the content of a TinyMCE instance.
 * 
 * @param string id       Identifier of the TinyMCE intance
 * @param string content  Content in HTML format
 */
function setEditorContent(id, content)
{
  editor = tinyMCE.activeEditor;
  if(id == null)
    editor = tinyMCE.getInstanceById(id);
     
  // Sets the HTML contents of the activeEditor editor
  editor.setContent(content, {format : 'html'});
}

/**
 * Gets the content of a TinyMCE instance in HTML format.
 * 
 * @param string id   Identifier of the TinyMCE instance
 * @return Returns the content of the editor.
 */
function getEditorContent(id)
{
  editor = tinyMCE.activeEditor;
  if(id == null)
    editor = tinyMCE.getInstanceById(id);
    
  if(editor != null)
    return editor.getContent({format : 'html'});
  else
    return "";
}
