$(document).ready(function( )
{
  var emptyBorderNormal = emptyBorderHover = emptyBorderSelected = 4;
  var arrowBorderNormal = arrowBorderHover = arrowBorderSelected = 18;

  $('#settings').LayerButton({
    'groupID' : 'toolbar-wo-arrow',
    'normal' : {
      'imageBorders' : {
        'right' : emptyBorderNormal,
        'left' : emptyBorderNormal
      }
    },
    'hover' : {
      'imageSource' : {
        'prefix' : 'setting-hover_',
        'suffix' : '.png'
      },
      'imageBorders' : {
        'right' : emptyBorderHover,
        'left' : emptyBorderHover
      }
    },
    'selected' : {
      'imageSource' : {
        'prefix' : 'setting-pressed_',
        'suffix' : '.png'
      },
      'imageBorders' : {
        'right' : emptyBorderSelected,
        'left' : emptyBorderSelected
      },
      'onShow' : function(element)
      {
        tuDispatcher.trigger("popupmenu", jQuery(element), 1);
      }
    }

  });

  $('#help').LayerButton({
    'groupID' : 'toolbar-help-w-arrow',
    'normal' : {
      'imageSource' : {
        'prefix' : 'subnav-normal_',
        'suffix' : '.png'
      },
      'imageBorders' : {
        'right' : arrowBorderNormal,
        'left' : emptyBorderNormal
      }
    },
    'hover' : {
      'imageSource' : {
        'prefix' : 'subnav-hover_',
        'suffix' : '.png'
      },
      'imageBorders' : {
        'right' : arrowBorderHover,
        'left' : emptyBorderHover
      }
    },
    'selected' : {
      'imageSource' : {
        'prefix' : 'subnav-pressed_',
        'suffix' : '.png'
      },
      'imageBorders' : {
        'right' : arrowBorderSelected,
        'left' : emptyBorderSelected
      },
      'onShow' : function(element)
      {
        tuDispatcher.trigger("popupmenu", jQuery(element), 3);
        // extra case: another menu opens, a mouseup can occurs there and not at the document.body
        // mouseDownPressed = false;
      }
    }
  });

  $('#help').mousedown(function(e)
  { // NAB 07.06.2011: added e.button==0
    if (( e.button == 1 ) || ( e.button == 0 )) {
      mouseDownPressed = getClientRect(this);
      $(this).click( );
    }
  }).keydown(function(e)
  {
    if (e.keyCode == 13) {
      $(this).click( );
    }
  });

  $('#settings').mousedown(function(e)
  {
    // NAB 08.06.2011: extended with e.button==0 and skipOnShow
    if (( e.button == 1 ) || ( e.button == 0 )) {
      mouseDownPressed = getClientRect(this);
      // show visual change but dont start menu
      $(this).trigger('click', 'skipOnShow');
    }
  }).mouseup(function(e)
  {
    if (mouseDownPressed && !isMouseInRect(e)) {
      return;
    }
    $(this).click( );
  }).mouseleave(function(e)
  {
    $.LayerButton.hideLayer(this, 'selected');
  }).mouseenter(function(e)
  {
    if (mouseDownPressed && !isMouseInRect(e)) {
      return;
    }
    if (mouseDownPressed && isMouseInRect(e)) {
      $(this).trigger('click', 'skipOnShow');
    }
  }

  ).keydown(function(e)
  {
    if (e.keyCode == 13) {
      $(this).click( );
    }
  });
});
