//=============================================================================
// Stand alone file selector for Mediashare
// (C) Jorn Wildt 2006
//=============================================================================

//=============================================================================
// External interface functions
//=============================================================================

function mediashareFindItem(targetId, mediashareURL)
{
  currentMediashareInput = document.getElementById(targetId);
  if (currentMediashareInput == null)
    alert("Unknown input element '" + targetId + "'");

  window.open(mediashareURL, "", "width=500,height=300,resizable");
}


function mediashareFindItemHtmlArea30(editor, mediashareURL)
{
    // Save editor for access in selector window
  currentMediashareEditor = editor;

  window.open(mediashareURL, "", "width=500,height=300,resizable");
}


//=============================================================================
// Internal stuff
//=============================================================================

  // htmlArea 3.0 editor for access in selector window
var currentMediashareEditor = null;
var currentMediashareInput = null;

var mediashare = {}

mediashare.find = {}

mediashare.find.onFolderChanged = function(selectElement)
{
  var selectedValue = selectElement.value;
  var albumIdInput = document.getElementById('albumIdInput');
  albumIdInput.value = selectedValue;

  var commandInput = document.getElementById('commandInput');
  commandInput.value = "selectAlbum";

  var form = document.getElementById('selectorForm');
  form.submit();
}


mediashare.find.onItemChanged = function(selectElement)
{
  var selectedValue = selectElement.value;
  var mediaIdInput = document.getElementById('mediaIdInput');
  mediaIdInput.value = selectedValue;

  var commandInput = document.getElementById('commandInput');
  commandInput.value = "selectMedia";

  var form = document.getElementById('selectorForm');
  form.submit();
}


mediashare.find.handleCancel = function()
{
  var w = parent.window;
  window.close();
  w.focus();
}


  // User clicks on "select item" button
mediashare.find.selectItem = function()
{
  if (window.opener.currentMediashareEditor != null)
  {
    var html = mediashare_paste_getHtml('html');

    window.opener.currentMediashareEditor.focusEditor();
    window.opener.currentMediashareEditor.insertHTML(html);
  }
  else
  {
    var html = mediashare_paste_getHtml('url');
    var currentInput = window.opener.currentMediashareInput;

    if (currentInput.tagName == 'INPUT')
    {
        // Simply overwrite value of input elements
      currentInput.value = html;
    }
    else if (currentInput.tagName == 'TEXTAREA')
    {
        // Try to paste into textarea - technique depends on environment
        
      if (typeof document.selection != "undefined")
      {
          // IE: Move focus to textarea (which fortunately keeps its current selection) and overwrite selection
        currentInput.focus();
        window.opener.document.selection.createRange().text = html;
      }
      else if (typeof currentInput.selectionStart != "undefined")
      {
          // Firefox: Get start and end points of selection and create new value based on old value
        var startPos = currentInput.selectionStart;
        var endPos = currentInput.selectionEnd;
        currentInput.value = currentInput.value.substring(0, startPos)
                                                     + html
                                                     + currentInput.value.substring(endPos, currentInput.value.length);
      } 
      else 
      {
          // Others: just append to the current value
        currentInput.value += html;
      }
    }
  }

  window.opener.focus();
  window.close();
}


function handleOnClickCancel()
{
  window.opener.focus();
  window.close();
}

