// $Id: guppy.js,v 1.23 2004/08/11 21:44:06 jornlind Exp $
// =======================================================================
// Guppy by Jorn Lind-Nielsen (C) 2003.
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WithOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// =======================================================================

  // Store active htmlArea editors so we can go through them on submit
var guppyHtmlEditors = {};

var extraStyle;

function guppyHandleOnLoad()
{
    // Set initial focus to field defined by outputed HTML/JavaScript
  var focusField = document.getElementById(guppyInitialFocusElement);
  if (typeof focusField != 'undefined') 
    focusField.focus();
}


function handleOnClickInsertRow(insertImg)
{
  var rowElement    = insertImg.parentNode.parentNode;
  var rowIndex      = rowElement.rowIndex;
  var componentName = getEnclosingComponentName(insertImg);

  doSubmitCommand("insertRow", componentName, rowIndex, null);
}


function handleOnClickDeleteRow(deleteImg)
{
  var rowElement    = deleteImg.parentNode.parentNode;
  var rowIndex      = rowElement.rowIndex;
  var componentName = getEnclosingComponentName(deleteImg);

  var ok = true;
  if (window["confirmDelete"+componentName] != "")
    ok = confirm(window["confirmDelete"+componentName]);

  if (ok)
    doSubmitCommand("deleteRow", componentName, rowIndex-1, null);  // Offset -1 for headings
}


function handleOnClickButton(componentName, buttonName)
{
  doSubmitCommand("button", componentName, null, buttonName);
}


function handleOnClickButtonGroup(componentName, groupID)
{
  var selectElement = document.getElementById(groupID);
  var buttonName = selectElement.value;

  doSubmitCommand("button", componentName, null, buttonName);
}


function handleOnChangeButtonGroup(groupID)
{
  var selectElement = document.getElementById(groupID);
  var buttonElement = document.getElementById("b:" + groupID);

  var optionElement = selectElement.options[selectElement.selectedIndex];

  buttonElement.title = optionElement.title;
}


function handleOnKeyUpInput(inputElement, mandatory)
{
  if (mandatory)
    updateInputFieldDisplay(inputElement, mandatory);
}


function handlOnClickAction(actionName, componentName, rowIndex)
{
  doSubmitCommand("action", componentName, rowIndex, actionName);
}


function handlOnClickHeader(fieldName, componentName)
{
  doSubmitCommand("clickHeader", componentName, null, fieldName);
}


// =======================================================================
// Command submit
// =======================================================================

function doSubmitCommand(action, componentName, rowIndex, param1)
{
  var guppyForm = document.getElementById("guppyForm");

  guppyForm.action.value    = action;
  guppyForm.component.value = componentName;
  guppyForm.rowIndex.value  = rowIndex;
  guppyForm.param1.value    = param1;

    // Make sure all htmlArea 3.0 textareas are updated
    // - htmlArea 3.0 assumes we use a standard submit form, so we need to fake that.
  for (var fieldName in guppyHtmlEditors)
  {
    var html = guppyHtmlEditors[fieldName].getHTML();
    guppyForm[fieldName].value = html;
  }

  guppyForm.submit();
}


// =======================================================================
// Stylesheet handling
// =======================================================================

function updateInputFieldDisplay(inputElement, mandatory)
{
  if (mandatory != "")
  {
    if (inputElement.value.length > 0)
    {
      updateClassName(inputElement, "mdt", true);
      updateClassName(inputElement, "mde", false);
    }
    else
    {
      updateClassName(inputElement, "mdt", false);
      updateClassName(inputElement, "mde", true);
    }
  }
}


function updateClassName(element, className, doAdd)
{
  var pos = element.className.indexOf(className);
  //alert(element.className + "+" + className + (doAdd ? " / add" : " / del"));
  if (doAdd)
  {
    if (pos < 0)
      element.className += " " + className;
  }
  else
  {
    if (pos >= 0)
      element.className = element.className.substr(0,pos) + element.className.substr(pos+className.length);
  }
  //alert(element.className);
}


// =======================================================================
// Menu
// =======================================================================

var mainmenu =
{
};


mainmenu.popup = function(element, evt, id)
{
  evt = (evt ? evt : (event ? event : null));
  if (evt == null)
    return true;

  var pos = getPositionOfElement(element);
  pos.top += 20;

  var menuDivElement = document.getElementById("menu-"+id);
  psmenu.openMenu(mainmenu, menuDivElement, pos);

  return true;
}


//-[ Listener methods for psmenu ]---------------------------------------------

mainmenu.itemSelected = function(menuId, itemIndex)
{
  doSubmitCommand("menuAction", menuId, itemIndex, null);
}


mainmenu.menuClosed = function()
{
}



function menuTopMouseOver(element)
{
  element.className = "active";
}


function menuTopMouseOut(element)
{
  element.className = "normal";
}


// =======================================================================
// Tree menu
// =======================================================================

var treeMenu =
{
  functions:  // Must match menu sequence in guppy.php
  [
    "handleOnClickTreeEdit", 
    "handleOnClickTreeAdd", 
    "handleOnClickTreeAddSub", 
    "handleOnClickTreeInsert", 
    "handleOnClickTreeDelete", 
    "handleOnClickTreeMoveUp", 
    "handleOnClickTreeMoveDown", 
    "handleOnClickTreeIndent", 
    "handleOnClickTreeUndent", 
    "handleOnClickTreeDeleteRecursive"
  ],
  currentRowIndex: null,
  currentComponentName: null
};


treeMenu.onMouseDown = function(componentName, rowIndex, evt, menuId)
{
  evt = (evt ? evt : (event ? event : null));
  if (evt == null)
    return true;

  var menuDivElement = document.getElementById(menuId);

  var pos = getPositionOfEvent(evt);

  psmenu.openMenu(treeMenu, menuDivElement, pos);

  treeMenu.currentRowIndex = rowIndex;
  treeMenu.currentComponentName = componentName;

  return true;
}


treeMenu.handleOnClickTreeEdit = function(componentName, rowIndex)
{
  doSubmitCommand("treeEdit", componentName, rowIndex);
}


treeMenu.handleOnClickTreeInsert = function(componentName, rowIndex)
{
  doSubmitCommand("treeInsert", componentName, rowIndex);
}


treeMenu.handleOnClickTreeAdd = function(componentName, rowIndex)
{
  doSubmitCommand("treeAdd", componentName, rowIndex);
}


treeMenu.handleOnClickTreeAddSub = function(componentName, rowIndex)
{
  doSubmitCommand("treeAddSub", componentName, rowIndex);
}


treeMenu.handleOnClickTreeDelete = function(componentName, rowIndex)
{
  doSubmitCommand("treeDelete", componentName, rowIndex);
}


treeMenu.handleOnClickTreeMoveUp = function(componentName, rowIndex)
{
  doSubmitCommand("treeMoveUp", componentName, rowIndex);
}


treeMenu.handleOnClickTreeMoveDown = function(componentName, rowIndex)
{
  doSubmitCommand("treeMoveDown", componentName, rowIndex);
}


treeMenu.handleOnClickTreeIndent = function(componentName, rowIndex)
{
  doSubmitCommand("treeIndent", componentName, rowIndex);
}


treeMenu.handleOnClickTreeUndent = function(componentName, rowIndex)
{
  doSubmitCommand("treeUndent", componentName, rowIndex);
}


treeMenu.handleOnClickTreeDeleteRecursive = function(componentName, rowIndex)
{
  doSubmitCommand("treeDeleteRecursive", componentName, rowIndex);
}


function handleOnClickTreeSubmit(componentName, rowIndex, action)
{
  if (action == 'new')
    doSubmitCommand("treeNewItem", componentName, rowIndex);
  else if (action == 'add')
    doSubmitCommand("treeAddItem", componentName, rowIndex);
  else if (action == 'addSub')
    doSubmitCommand("treeAddSubItem", componentName, rowIndex);
  else
    doSubmitCommand("treeChangeItem", componentName, rowIndex);
}


function handleOnClickTreeCancel()
{
  doSubmitCommand("treeCancel", '', '');
}

//-[ Listener methods for psmenu ]---------------------------------------------

treeMenu.itemSelected = function(menuId, itemIndex)
{
  var handlerName = treeMenu.functions[itemIndex];
  treeMenu[handlerName](treeMenu.currentComponentName, treeMenu.currentRowIndex);
}


treeMenu.menuClosed = function()
{
  treeMenu.currentComponentName = treeMenu.currentRowIndex = null;
}


// =======================================================================
// Photoshare stuff
// =======================================================================

  // Used for guppy image field
function guppyFindImage(formFieldName, photoshareFindImageURL, thumbnailSize)
{
  if (typeof photoshareFindImage == "undefined")
    alert("You have apparently not installed the Photoshare image gallery");
  else
    photoshareFindImage(formFieldName, photoshareFindImageURL, thumbnailSize);
}


// =======================================================================
// htmlArea
// =======================================================================

  // Create a new htmlArea instance (one for each original textarea)
function guppyHtmlAreaInit(textareaID, themeBaseURL, width, height, undoEnabled, wordKillEnabled)
{
    // get a default configuration
  var config = new HTMLArea.Config();

  config.width  = width;
  config.height = height;
  config.killWordOnPaste = wordKillEnabled;

  if (undoEnabled)
    config.statusBar = false;

  config.toolbar[config.toolbar.length-1].push("separator");

  config.registerButton({
    id        : "photoshare",
    tooltip   : "Insert Photoshare image",
    image     : "modules/pagesetter/guppy/HTMLArea30beta/images/photoshare.gif",
    textMode  : false,
    action: 
      function(editor, id)
      {
        if (typeof photoshareFindImage == "undefined")
          alert("You have apparently not installed the Photoshare image gallery");
        else
        {
            // Check for base URL which is used to make this run in fullscreen mode
          url = (typeof postnukeBaseURL == "undefined" ? "" : postnukeBaseURL+"/" /* defined by html output */);
          url += "index.php?module=photoshare&func=findimage&url=absolute&html=img&targetID=" + textareaID;
          photoshareFindImageHtmlArea30(editor, url, photoshareThumbnailSize /* defined by html output */);
        }
      }
  });

  config.toolbar[config.toolbar.length-1].push("photoshare");

  config.registerButton({
    id        : "pagesetter",
    tooltip   : "Insert Pagesetter link",
    image     : "modules/pagesetter/guppy/HTMLArea30beta/images/pagesetter.gif",
    textMode  : false,
    action: 
      function(editor, id)
      {
          // Check for base URL which is used to make this run in fullscreen mode
        url = (typeof postnukeBaseURL == "undefined" ? "" : postnukeBaseURL+"/" /* define by html output */);
        url += "index.php?module=pagesetter&func=pubfind&url=relative&html=a&targetID=" + textareaID;
        pagesetterFindPubHtmlArea30(editor, url);
      }
  });

  config.toolbar[config.toolbar.length-1].push("pagesetter");

  config.registerButton({
    id        : "pagebreak",
    tooltip   : "Insert pagebreak",
    image     : "modules/pagesetter/guppy/HTMLArea30beta/images/pagebreak.gif",
    textMode  : false,
    action: 
      function(editor, id)
      {
        editor.insertHTML("<hr class=\"pagebreak\" />");
      }
  });

  config.toolbar[config.toolbar.length-1].push("pagebreak");

  if (typeof themeBaseURL != "undefined")
    extraStyle =   "<link rel=\"StyleSheet\" href=\"" + themeBaseURL + "/style/style.css\" type=\"text/css\">"
                 + "<link rel=\"StyleSheet\" href=\"" + themeBaseURL + "/style/styleNN.css\" type=\"text/css\">";
  else
    extraStyle = "";

  extraStyle += "<link rel=\"StyleSheet\" href=\"modules/pagesetter/guppy/HTMLArea30beta/htmlarea.css\" type=\"text/css\">"

    // Make it possible to modify the config before using it
  if (typeof HTMLAreaConfigSetup != "undefined")
    HTMLAreaConfigSetup(config);

	var ta = HTMLArea.getElementById("textarea", textareaID);
	var editor = new HTMLArea(ta, config);

    // Make it possible to modify the editor before using it
  if (typeof HTMLAreaEditorSetup != "undefined")
    HTMLAreaEditorSetup(editor);

  editor.registerPlugin(TableOperations);
  editor.generate(extraStyle);

    // Store editor for access when pasting
  guppyHtmlEditors[textareaID] = editor;
}


// =======================================================================
// DOM traversal helpers
// =======================================================================

function getEnclosingComponentName(element)
{
  while (element.tagName != "DIV")
    element = element.parentNode;

  return element.id;
}


