function getSelectedText() {
  var _txt, _selected_text, _full_text, _selection_start, _selection_end
  if (window.getSelection) {
    _txt = window.getSelection();
    _selected_text = _txt.toString();
    _full_text = _txt.anchorNode.textContent;
    _selection_start = _txt.anchorOffset;
    _selection_end = _txt.focusOffset;
  } else if (document.getSelection) {
    _txt = document.getSelection();
    _selected_text = _txt.toString();
    _full_text = _txt.anchorNode.textContent;
    _selection_start = _txt.anchorOffset;
    _selection_end = _txt.focusOffset;
  } else if (document.selection) {
    _txt = document.selection.createRange();
    _selected_text = _txt.text;
    _full_text = _txt.parentElement().innerText;

    var _stored_range = _txt.duplicate();
    _stored_range.moveToElementText(_txt.parentElement());
    _stored_range.setEndPoint('EndToEnd', _txt);
    _selection_start = _stored_range.text.length - _txt.text.length;
    _selection_end = _selection_start + _selected_text.length;
  } else return;

  if(_selection_start > _selection_end) {
    var _tmp = _selection_start;
    _selection_start = _selection_end;
    _selection_end = _tmp;
  }

  var _context_pre_start, _context_pre, _context_post_stop, _context_post;

  _context_pre_start = _selection_start-50;
  _context_pre_start = _context_pre_start>=0 ? _context_pre_start : 0;
  _context_pre = _full_text.substring(_context_pre_start, _selection_start);

  _context_post_stop = _selection_end+50;
  _context_post_stop = _context_post_stop<=_full_text.length ? _context_post_stop : _full_text.length;
  _context_post = _full_text.substring(_selection_end, _context_post_stop);

  var _selection = {
    pre: _context_pre,
    text: _selected_text,
    post: _context_post
  };

  return _selection;
}

function typo_report_window() {
  /*
  var _selected_text = '';
  if(window.getSelection){
    _selected_text = window.getSelection();
  }else if(document.getSelection){
    _selected_text = document.getSelection();
  }else if(document.selection){
    _selected_text = document.selection.createRange().text;
  }
  _selected_text += ''; */
  var _selected = getSelectedText();
  if(_selected.text.length > 50) {
    alert('Нужно выбрать текст длиной не более 50 символов');
  } else if(_selected.text.length > 0) {
    $('form','#reportbug-dialog')
      .find('input[name=context_pre]').val(_selected.pre).end()
      .find('input[name=buggytext]').val(_selected.text).end()
      .find('input[name=context_post]').val(_selected.post).end()
    ;
    $("#buggy-context-pre").html(_selected.pre);
    $("#buggytext").html(_selected.text);
    $("#buggy-context-post").html(_selected.post);
    $("#reportbug-dialog").dialog({
      modal: true,
      width: '70%'
    })
  }
}

$(document)
  .keydown(function(e) {
    if (e.ctrlKey && e.keyCode == 13) {
      typo_report_window()
      return false;
    }
  })
  .ready(function(){
    $('input.cancel','#reportbug-dialog').click(function(){
      $("#reportbug-dialog").dialog('close');
    })
  })

