/**
 * tinymce a51editor_plugin
 */
(function() {
  var plugin;
    
  tinymce.create('tinymce.plugins.a51editor_mark', {
    // block formats
    'block_formats' : {
      'a51editor_mark_note_block'       : { block   : 'div', wrapper : true, remove  : 'all', classes : 'note' },      
      'a51editor_mark_updated_block'    : { block   : 'div', wrapper : true, remove  : 'all', classes : 'updated' },
      'a51editor_mark_important_block'  : { block   : 'div', wrapper : true, remove  : 'all', classes : 'important' }
    },
    
    // inline formats
    'inline_formats' : {
      'a51editor_mark_note_inline'      : { inline : 'span', classes : 'note' },
      'a51editor_mark_updated_inline'   : { inline : 'span', classes : 'updated' },
      'a51editor_mark_important_inline' : { inline : 'span', classes : 'important' }      
    },
    
    init : function(ed, url) {
      plugin = this;
      
      // register commands
      ed.addCommand('a51editor_mark', this.command_a51editor_mark);
      
      // register buttons
      ed.addButton('a51editor_mark_note', { title : App.lang('Mark as Note'), cmd : 'a51editor_mark', value : 'note' });
      ed.addButton('a51editor_mark_updated', { title : App.lang('Mark as Updated'), cmd : 'a51editor_mark', value : 'updated' });
      ed.addButton('a51editor_mark_important', { title : App.lang('Mark as Important'), cmd : 'a51editor_mark', value : 'important' });
      
      // after editor initializes
      ed.onInit.add(function() {
        // register block formats
        $.each(plugin.block_formats, function (format_name) {
          ed.formatter.register(format_name, this);
        });
                
        // register inline formats
        $.each(plugin.inline_formats, function (format_name) {
          ed.formatter.register(format_name, this);
        });
      });
      
      // toggle state of buttons
      ed.onNodeChange.add(function(editor, control_manager, current_node, selection_is_collapsed, object) {        
        if (control = control_manager.get('a51editor_mark_note')) {
          control.setActive(this.formatter.matchAll(['a51editor_mark_note_block', 'a51editor_mark_note_inline']).length);
        } // if
        
        if (control = control_manager.get('a51editor_mark_updated')) {
          control.setActive(this.formatter.matchAll(['a51editor_mark_updated_block', 'a51editor_mark_updated_inline']).length);
        } // if

        if (control = control_manager.get('a51editor_mark_important')) {
          control.setActive(this.formatter.matchAll(['a51editor_mark_important_block', 'a51editor_mark_important_inline']).length);
        } // if
      });
    },
    
    /**
     * Remove all block formats from selection excluding skip one
     */
    removeBlockMarkings : function (skip) {
      var editor = this;
      $.each(plugin.block_formats, function (format_name) {
        if (format_name != skip) {
          editor.formatter.remove(format_name);
        } // if
      });
    },

    /**
     * Remove all inline formats from selection excluding skip one
     */
    removeInlineMarkings : function (skip) {
      var editor = this;
      $.each(plugin.inline_formats, function (format_name) {
        if (format_name != skip) {
          editor.formatter.remove(format_name);
        } // if
      });
    },
    
    /**
     * Check if current selection matches one of markings
     * 
     * @return boolean
     */
    matchBlockMarkings : function () {
      var names = [];
      
      $.each(plugin.block_formats, function (format_name) {
        names.push(format_name);
      });
      
      return this.formatter.matchAll(names);
    },
    
    /**
     * Execute mark command
     * 
     * @param ui
     * @param value
     */
    command_a51editor_mark : function (ui, value) {
      var block_format_name = 'a51editor_mark_' + value + '_block';
      var inline_format_name = 'a51editor_mark_' + value + '_inline';
      var start_node = $(this.selection.getStart());
      var end_node = $(this.selection.getEnd());
      var start_node_parent = start_node.parent().is('body') ? start_node : start_node.parentsUntil('body', ':last');
      var end_node_parent = end_node.parent().is('body') ? end_node : end_node.parentsUntil('body', ':last');

      if (this.selection.isCollapsed()) {
        // if selection is collapsed
        plugin.removeBlockMarkings.apply(this, [block_format_name]);
        this.formatter.toggle(block_format_name);
        
      } else if (start_node_parent[0] != end_node_parent[0]) {
        // if they dont have same parent
        plugin.removeBlockMarkings.apply(this, [block_format_name]);
        this.formatter.toggle(block_format_name);
        
      } else if (start_node){
        // if they have same parent
        if (this.formatter.matchAll(plugin.block_formats).length) {
          plugin.removeBlockMarkings.apply(this, [block_format_name]);
          this.formatter.toggle(block_format_name);          
        } else {
          plugin.removeInlineMarkings.apply(this, [inline_format_name]);
          this.formatter.toggle(inline_format_name);          
        } // if
      } // if
    },
    
    /**
     * Get plugin information
     * 
     * @returns Object
     */
    getInfo : function() {
      return {
        longname : 'Mark',
        author : 'A51 D.O.O.',
        authorurl : 'http://www.a51dev.com/',
        infourl : 'http://www.a51dev.com/',
        version : tinymce.majorVersion + "." + tinymce.minorVersion
      };
    }
  });

  // Register plugin
  tinymce.PluginManager.add('a51editor_mark', tinymce.plugins.a51editor_mark);
})();