var siteBaseUrl = 'http://thinker.netrunnersystems.com/';

DictionarySearch = (function() {

  var selection, selectionText, selectionButton, newRange;

  function handleClick(event) {
    try {
      if (selectionButton) {
        cleanUp();
      }
      selection = getSelection();
      selectionText = selection && selection.toString();
      if (selectionText) {
        window.setTimeout(insertButton, 0);
        event.stop();
      }
    }
    catch (err) {
    }  
  }

  function getSelection() {
    try {
      return Try.these(
        function() { return window.getSelection() },
        function() { return document.getSelection() },
        function() {
          var selection = document.selection && document.selection.createRange();
          selection.toString = function() { return this.text };
          return selection;
        }
      ) || false;
    }
    catch (err) {
    }  
  }

  function insertButton() {
    try {
      selectionButton = new Element(
          'span', {
            'className': 'word_selection_button',
            'id': 'word_selection_button',
            'title': 'Lookup Word',
            'style': 'margin:-25px 0 0 -29px; position:absolute; background:url(' + siteBaseUrl + 'images/word_search_bubble.png);width:30px;height:31px;cursor:pointer;_background-image: none;filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + siteBaseUrl + 'images/word_search_bubble.png", sizingMethod="image");'
          }
      )
      if (Prototype.Browser.IE) {
        var tmp = new Element('div');
        tmp.appendChild(selectionButton);
        newRange = selection.duplicate();
        newRange.setEndPoint("StartToEnd", selection);
        newRange.pasteHTML(tmp.innerHTML);
        selectionButton = $('word_selection_button');
      }
      else {
        var range = selection.getRangeAt(0);
        newRange = document.createRange();
        newRange.setStart(selection.focusNode, range.endOffset);
        newRange.insertNode(selectionButton);
      }
      Element.observe(selectionButton, 'mouseup', exportSelection, true);
    }
    catch (err) {
    }  
  }

  function cleanUp() {
    try {
      selection = null;
      selectionButton.stopObserving('mouseup', exportSelection);
      newRange && newRange.pasteHTML && newRange.pasteHTML('');
      newRange = null;
      selectionButton.remove();
      selectionButton = null;
      selectionText = '';
    }
    catch (err) {
    }  
  }

  function exportSelection(event) {
    try {
      var url = siteBaseUrl + 'search_word.aspx?text=' + encodeURIComponent(selectionText);
      var newwin = window.open(url, 'answersWindow', 'height=400,width=600,location=false,menubar=false,toolbar=false,status=false,resizable, scrollbars');
      if (newwin) newwin.focus();
      event.stop();
    }
    catch (err) {
    }  
  }
  return {
    initialize: function() {
      document.observe('mouseup', handleClick, false);
    }
  };
})();