  // Naming:
  //   Opener window - the window in which we find the button that
  //                   activates the opening of the selector.
  //   Selector window - the window in which we do the actual
  //                     selection of the items

  // These variables are used in both opener and new window to
  // communicate parameters from folderOpenSelector
var folderDestinationId = null;
var folderPasteMode = null;
var folderHTMLArea3Editor = null;

/*===========================================================================
  Stuff used in the opener window
===========================================================================*/

  // The main public function for opening the selector
function folderOpenSelector(destinationId, pasteMode)
{
  var url = "index.php?module=folder&func=select";
  var win = window.open(url, "_blank", "width=600,height=400");

  folderDestinationId = destinationId;
  folderPasteMode = pasteMode;
}


  // Opening the selector window for HTMLArea3
function folderOpenSelectorForHTMLArea3(destId, pasteMode, editor)
{
  HTMLArea3Editor = editor;
  folderOpenSelector(destId, pasteMode);
}


/*===========================================================================
  Stuff used in the selector window
===========================================================================*/

  // Called when clicking on "cancel" button
function folderCancelSelectItem()
{
  window.opener.focus();
  window.close();
}


  // called when clicking on "ok" button
function folderSelectItem()
{
  if (typeof folderModuleSpecificSelect == "undefined")
    return;

  var element = window.opener.document.getElementById(window.opener.folderDestinationId);
  var selector =
  {
    destinationId: window.opener.folderDestinationId,
    element: element,
    mode: window.opener.folderPasteMode,
    insert: folderPasteResult
  };

    // Call module specific handler (supplied by the HTML from the module)
  folderModuleSpecificSelect(selector);

  window.opener.focus();
  window.close();
}


  // Handling of insertion of text/html from the selected item
function folderPasteResult(text)
{
  if (this.mode == 'key')
  {
    this.element.value = text;
    var keyElement = window.opener.document.getElementById(this.destinationId+'_key');
    if (keyElement == null)
      alert("Selection could not be pasted into parent window. The key input element '" + this.destinationId+'_key' + "' is missing");

    keyElement.value = folderItemModule + "/" + folderItemType + "/" + folderItemKey;
  }
  else
  {
    if (this.element.tagName == 'INPUT')
    {
        // Simply overwrite value of input elements
      this.element.value = text;
    }
    else if (this.element.tagName == 'TEXTAREA')
    {
        // Try to paste into textarea - technique depends on environment
      if (window.opener.HTMLArea3Editor != null)
      {
        window.opener.HTMLArea3Editor.focusEditor(); 
        window.opener.HTMLArea3Editor.insertHTML(text);
        return;
      }

      if (typeof document.selection != "undefined")
      {
        if (this.element.style.display == 'none' &&
            typeof window.opener.editor_insertHTML != "undefined")
        {
            // IE: using htmlArea 2.0(hopefully) for editing
          window.opener.editor_insertHTML(targetInputID, text);
        }
        else
        {
            // IE: Move focus to textarea (which fortunately keeps its current selection) and overwrite selection
          this.element.focus();
          window.opener.document.selection.createRange().text = text;
        }
      }
      else if (typeof this.element.selectionStart != "undefined")
      {
          // Mozilla: Get start and end points of selection and create new value based on old value
        var startPos = this.element.selectionStart;
        var endPos = this.element.selectionEnd;
        this.element.value = this.element.value.substring(0, startPos)
                                   + text
                                   + this.element.value.substring(endPos, this.element.value.length);
      } 
      else 
      {
          // Others: just append to the current value
        this.element.value += text;
      }
    }
  }
}

