/**
* nicExample
* @description: An example button plugin for nicEdit
* @requires: nicCore, nicPane, nicAdvancedButton
* @author: Brian Kirchoff
* @version: 0.9.0
*/

/* START CONFIG */
var nicExampleOptions = {
    buttons : {
        'example' : {name : __('Some alt text for the button'), type : 'nicEditorExampleButton'}
    }/* NICEDIT_REMOVE_START */,iconFiles : {'example' : '/fiktivkod/js/nicEdit/nicCode/icons/code.png'}/* NICEDIT_REMOVE_END */
};
/* END CONFIG */



function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    } else if (myField.selectionStart || myField.selectionStart == 0) {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + myValue
    + myField.value.substring(endPos, myField.value.length);
    } else {
    myField.value += myValue;
    }
}


var nicEditorExampleButton = nicEditorButton.extend({
  mouseClick : function() {
    var editor = nicEditors.findEditor('content');
    var lang = prompt('Language?');

    var userSelection;
    if (window.getSelection) {
      userSelection = window.getSelection();
    } else if (document.selection) { // should come last; Opera!
      userSelection = document.selection.createRange();
    }

    var pos = userSelection.anchorOffset;

    var content = editor.getContent();

    //editor.setContent(content + '<code lang="' + lang + '"></code><p></p>');

    // calling the function
    insertAtCursor(document.getElementById('content'), 'test');

  }
});

nicEditors.registerPlugin(nicPlugin,nicExampleOptions);