﻿// File Picker modification for FCK Editor v2.0 - www.fckeditor.net
// by: Pete Forde <pete@unspace.ca> @ Unspace Interactive
// Although the FCK Editor (Fred's Editor) has been upgraded to CK Editor, this utility only works with FCK Editor,
// so FCK Editor remains installed along side CK Editor to support this tool.

// The id of a hidden input that will hold the selected image's URI.
var URI_output_ID = 'Selected_Image_URI';
// The end of the id of an input that holds the selected image's original alternate text.
var Alt_input_ID = '$Original_Alt_Text';
// The id of a hidden input used to return new alternate text to the page.
var Alt_output_ID = 'Selected_Image_Alt_Text';

// Point of entry, called when an image is clicked.
function Browse_Server()
{
  // Allow the user to select a new image.
  Open_Server_Browser(
    '/Freds_Editor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/aspx/connector.aspx',
    screen.width * 0.7,
    screen.height * 0.7 ) ;
}

function Open_Server_Browser( URI, width, height )
{
  var iLeft = (screen.width  - width) / 2 ;
  var iTop  = (screen.height - height) / 2 ;

  var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
  sOptions += ",width=" + width ;
  sOptions += ",height=" + height ;
  sOptions += ",left=" + iLeft ;
  sOptions += ",top=" + iTop ;

  open( URI, "BrowseWindow", sOptions ) ;
}

// Callback function.  Only the URI parameter is used.  alt is not set by the browser.
function SetUrl( URI, width, height, alt )
{
  // Bring the main window to the front, thus hiding the image browser.
  focus();

  // The path to the image that was selected, or empty string if none.
  eval("document.forms[0]." + URI_output_ID).value = URI;
  // If any image was selected, also prompt for alternate text.
  if (URI != "")
  {
    // Locate the alternate text input.
    var Alt_input = null;
    var elems = document.forms[0].elements;
    for (var i in elems)
    {
      try
      {
        if (elems[i].name.substring(elems[i].name.length - Alt_input_ID.length) == Alt_input_ID)
        {
          Alt_input = elems[i];
          break;
        }
      }
      catch(ReferenceError)
      {
        // .name was not found, so just ignore the current input element.
      }
    }
    // Make the alternate text required.
    var Alt_output = document.getElementById(Alt_output_ID);
    var alt_text = null;
    while (true)
    {
      alt_text = prompt("Enter the alternate text for this image, " + URI + ".", Alt_input.value);
      if (alt_text == null || alt_text.length == 0)
      {
        // Explain it to them.
        alert("You must supply some alternate text for people who do not see the image.");
      }
      else if (alt_text.length > 100)
      {
        // Save the first 100 characters and use that as the new default.
        alert("The maximum length of the alternate text is 100 characters.")
        Alt_input.value = alt_text.substring(0, 100);
      }
      else
      {
        Alt_output.value = alt_text;
        break;
      }
    }
  }
  // Send the new image URI and alternate text to the server.
  document.forms[0].submit();
}

