/**
 * C4U Javascript library for functions of form elements.
 * 
 * Copyright (c) 1999-2010 by Community4you GmbH
 */

/**
 * Changes a tab in a form.
 * 
 * @param theFormID the id of the form
 * @param theTabBarID the id of the tab bar
 * @param theTabItemID the id of the tab item
 */
function changeTabInForm(theFormID, theTabBarID, theTabItemID)
{
  var _element = jQuery('#' + theTabBarID);
  if (_element.size() == 0)
    return;
  
  _element[0].value = theTabItemID;
  
  var _form = jQuery('#' + theFormID)[0];
  if (_form.onsubmit())
    _form.submit();
}

/**
 * Changes a tab in an old form.
 * 
 * @param theTabPrefixID the prefix of the hidden tab element
 * @param theTabItemID the id of the tab item
 */
function changeTabInOldForm(theTabPrefixID, theTabItemID)
{
  var _element = jQuery('#' + theTabItemID);
  if (_element.size() == 0)
    return;
  
  var _form = _element.parents().find('form');
  if (_form.size() == 0)
    return; 
  
  var _hidden = jQuery('#' + theTabPrefixID + _form.attr('id'));
  if (_hidden.size() == 0)
  {
    // fallback to "tabpane"
    _hidden = jQuery('#' + theTabPrefixID + "tabpane");
    if (_hidden.size() == 0)
    {
      // find the first hidden element which starts width "_tabpane"
      _hidden = _form.find("input[id^='_tabpane_']").first();
      if (_hidden.size() == 0)
        return;
    }
  }
  
  _hidden[0].value = theTabItemID;
  
  if (_form[0].onsubmit)
    _form[0].onsubmit();
    
  _form[0].submit();
}

/* the opened menus */
var _openedMenus = new Array();

/* the menus in the body */
var _bodyMenus = new Array();

/* menu close function added to body */
var _menuCloseAdded = false;

/**
 * Shows a menu.
 */
function showMenu(theParentID, theMenuID)
{
  addOnClickMenuClose();
  
  var _element = jQuery('#' + theMenuID);
  
  // close all other menus
  for (var _menuID in _openedMenus)
  {
    if (_menuID != theMenuID)
      hideMenu(_openedMenus[_menuID], _menuID);
  }
  
  // close the own menu?
  if (_element.css('display') == 'block')
  {
    hideMenu(theParentID, theMenuID);
    return;
  }
  
  _openedMenus[theMenuID] = theParentID;

  // open the menu
  var _parent = jQuery('#' + theParentID);

  // position absolute below the parent element
  var _left = _parent.offset().left;
  var _top = _parent.offset().top + _parent.height();
  
  var _window = jQuery('body');
  
  if ((_left + _element.outerWidth() ) > _window.innerWidth())
    _left = _window.innerWidth() - _element.outerWidth();
  
  _element.css('left', _left);
  _element.css('top', _top );
  
  // move to body, if not already done
  if (!_element.parent().is('body') && !_bodyMenus[theMenuID])
  {
    _element.detach();
    _element.appendTo('body');
    _bodyMenus[theMenuID] = true;
  }
  
  // show
  _element.show();
}

/**
 * Hides a menu.
 */
function hideMenu(theParentID, theMenuID)
{
  // move back to the parent
  var _element = jQuery('#' + theMenuID);
  _element.hide();
  
  var _parentID = _openedMenus[theMenuID];
  if (_parentID)
  {
    var _parent = jQuery('#' + _parentID);
    if (_parent.find('#' + theMenuID).size() == 0)
    {
      _element.detach();
      _element.appendTo(_parent);
    }
    else
      _element.remove();
    
    _bodyMenus[theMenuID] = null;
  }
  
  _openedMenus[theMenuID] = null;
}

/**
 * Hides all opened menus.
 * @param theEvent if not null, the event (to check, if the mouse is currently inside the parent or
 * the menu).
 */
function hideAllOpenedMenus(theEvent)
{
  for (var _menuID in _openedMenus)
  {
    var _hide = true;
    if (theEvent)
    {
      // do only check the parent; the menu itself must close when clicking
      if (elementClicked(_openedMenus[_menuID], theEvent))
        _hide = false;
    }
    
    if (_hide)
      hideMenu(_openedMenus[_menuID], _menuID);
  }
}

/**
 * Adds an onclick function to the body to hide all opened menus.
 */
function addOnClickMenuClose()
{
  if (_menuCloseAdded)
    return;
  
  jQuery('body').click(function(theEvent)
      {
        hideAllOpenedMenus(theEvent);
      });
  
  _menuCloseAdded = true;
}