// Menu.js - simple HTML menu
//
//
// The current version of this code is always available at:
// http://www.acme.com/javascript/
//
//
// Copyright © 2009 by Jef Poskanzer <jef@mail.acme.com>.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// For commentary on this license please see http://www.acme.com/license.html


AddCSSRule( '._menuItem:hover', 'color: white; background-color: black' );

var _menuBlock = null;


function AppendMenu( parent, properties )
    {
    if ( _menuBlock == null )
	_menuBlock = AppendElement( document.body, 'div', { style: { position: 'absolute', left: '0', right: '0', top: '0', bottom: '0', zIndex: '9999', backgroundColor: 'white', opacity: '.01', filter: 'alpha(opacity=01)', display: 'none' } } );
    var defaultProperties = { src: 'http://www.acme.com/resources/images/menu.gif', style: { position: 'absolute', top: '2px', right: '2px', width: '12px', height: '16px', cursor: 'pointer' } };
    var mergedProperties = MergeObjects( defaultProperties, properties );
    if ( parent.style.position == null || parent.style.position == '' )
	parent.style.position = 'relative';
    var menu = AppendElement( parent, 'img', mergedProperties );
    menu._popup = AppendElement( document.body, 'div', { style: { position: 'absolute', border: '1px solid black', backgroundColor: 'white', padding: '2px', zIndex: '10000', display: 'none' } } );
    menu.onclick = MakeEventCaller( _MenuClicked, menu );
    _menuBlock.onclick = MakeEventCaller( _MenuBlockClicked, menu );
    return menu;
    }


function AppendMenuItem( menu, callback, properties )
    {
    var defaultProperties = { className: '_menuItem', style: { cursor: 'pointer' } };
    var mergedProperties = MergeObjects( defaultProperties, properties );
    var item = AppendElement( menu._popup, 'div', mergedProperties );
    item._menu = menu;
    item._callback = callback;
    item.onclick = MakeEventCaller( _MenuItemClicked, item );
    return item;
    }


function _MenuClicked( e, menu )
    {
    NoBubble( e );
    _menuBlock.style.display = '';
    var menuXY = GetElementXY( menu );
    var pageWH = GetPageWH();
    menu._popup.style.top = ( menuXY.y + menu.clientHeight ) + 'px';
    menu._popup.style.right = ( pageWH.w - menuXY.x - menu.clientWidth ) + 'px';
    menu._popup.style.display = '';
    }


function _MenuBlockClicked( e, menu )
    {
    NoBubble( e );
    menu._popup.style.display = 'none';
    _menuBlock.style.display = 'none';
    }


function _MenuItemClicked( e, item )
    {
    NoBubble( e );
    item._menu._popup.style.display = 'none';
    _menuBlock.style.display = 'none';
    item._callback( item );
    }
