$(function() {
	var _src_open = $( '#js-menu-open' ).attr( 'src' );
	var _src_closed = $( '#js-menu-closed' ).attr( 'src' );

	var _item_height = 18;
	var _item_left = 18;

	var _menu_status = {};

	var _itemId = 0 , _menuId = 0;
	var _browseMenu = function ( _menu , _depth ) {
		var _entries = _menu.children( );
		for ( var i = 0 ; i < _entries.length ; i ++ ) {

			var _e = $( _entries[ i ] );
			_e.attr( 'id' , 'menu-item-id-' + _itemId );
			_itemId ++;

			var _echildren = _e.children( );
			var _cheight = $( _echildren[ 0 ] ).height( );
			var _pad , _h;
			if ( _cheight <= _item_height ) {
				_pad = Math.floor( ( _item_height - _cheight ) / 2 );
				_h = _item_height - _pad;
			} else {
				_pad = 0;
				_h = _cheight;
			}
			$( _echildren[ 0 ] ).css({
				'display' : 'block' ,
				'height' : _h + 'px' ,
				'margin' : '0px 0px 0px ' + _item_left + 'px' ,
				'padding' : _pad + 'px 0px 0px 0px'
			});

			if ( _echildren.length > 1 ) {
				var _is_open = _e.hasClass( 'open' );
				var _sub = $( _echildren[ 1 ] );
				_sub.attr( 'id' , 'sub-menu-' + _menuId );
				_menuId ++;

				_browseMenu( _sub , _depth + 1 );

				_e.css({
					'background-image' : 'url('
						+ ( _is_open
							? _src_open
							: _src_closed )
						+ ')' ,
					'background-repeat' : 'no-repeat'
				});
				_menu_status[ _sub.attr( 'id' ) ] = [
						_e.attr( 'id' ) , _is_open
					];
			}
		}
		_menu.css({
			'margin' : '0px 0px 0px 0px' ,
			'padding' : '0px 0px 0px '
					+ ( ( _depth > 1 ? 1 : 0 ) * _item_left )
					+ 'px'
		});
	};

	_browseMenu( $('.top-level-menu') , 1 );


	var _addClickHandler = function ( _mid , _pid ) {
		var _m = $( '#' + _mid );
		var _p = $( '#' + _pid );

		if ( !_menu_status[ _mid ][ 1 ] ) {
			_m.hide( );
		}

		_p.click( function (e) {
			if ( ! ( $( e.target ).is( '#' + _pid ) || $( e.target ).is( '#' + _pid + ' > strong' ) ) ) {
				return;
			}

			if ( _menu_status[ _mid ][ 1 ] ) {
				_m.hide('fast');
			} else {
				_m.show('fast');
			}

			_menu_status[ _mid ][ 1 ] = ! _menu_status[ _mid ][ 1 ];
			$(this).css( 'background-image' ,
				'url(' + ( _menu_status[ _mid ][ 1 ]
						? _src_open
						: _src_closed )
					+ ')' );
		} );
	};

	for ( var mid in _menu_status ) {
		_addClickHandler( mid , _menu_status[ mid ][ 0 ] );
	}
});

/*
		if ( _e.hasClass( 'menu-level-1' ) ) {
			_prev_parent = _e;

		} else {

			if ( ! _nodes[ _prev_parent.attr( 'id' ) ] ) {
				var _pe = _prev_parent.hasClass( 'open' );
				_nodes[ _prev_parent.attr( 'id' ) ] = [];
				_status[ _prev_parent.attr( 'id' ) ] = _pe;
				_prev_parent.css( 'background-image' , 'url(' + ( _pe ? _src_open : _src_closed ) + ')' );

				_prev_parent.click( function (e) {
					if ( ! $( e.target ).is( 'span' ) ) {
						return;
					}

					var _id = $(this).attr( 'id' );
					for ( var x in _nodes[ _id ] ) {
						if ( _status[ _id ] ) {
							_nodes[ _id ][ x ].hide('fast');
						} else {
							_nodes[ _id ][ x ].show('fast');
						}
					}
					_status[ _id ] = ! _status[ _id ];
					$(this).css( 'background-image' , 'url(' + ( _status[ _id ] ? _src_open : _src_closed ) + ')' );
				} );
			}

			_nodes[ _prev_parent.attr( 'id' ) ].push( _e );

			if ( _prev_parent.hasClass( 'closed' ) ) {
				_e.hide( );
			}

		}
	}
});
*/
