flyout_manager = new Array();
flyout_manager[ 'colors' ] = new Array();

function mainNavigationFlyoutInit( )
{		
	
	$('#main_navigation_menu').find( 'li' ).each( function( i ) {
		
		//Does ul#main_navigation_menu >* li contain a ul element?
		if( $(this).has('ul') )
		{
			//Yes!
			flyout_manager[ $(this).children().last().attr('id') ] = new Array();
			$(this).bind( 'mouseover', function() {
				
				var ul_node = $(this).children().last();
				var ul_id = ul_node.attr('id');
				
				//  cancel pending link mouseout, if set
				if( flyout_manager[ul_id]['link_mouseout_timer'])
				{
					delete flyout_manager[ ul_id ][ 'link_mouseout_timer' ];					
				}
				
				//  cancel pending hide ul, if set
				if ( flyout_manager[ ul_id ][ 'hide_ul_timer' ] )
				{
					window.clearTimeout( flyout_manager[ ul_id ][ 'hide_ul_timer' ] );
					delete flyout_manager[ ul_id ][ 'hide_ul_timer' ];
				}
				else
				{
					var a_node_key = $(this).children().first().attr('class') + ':hover';
					if ( flyout_manager[ 'colors' ][ a_node_key ] )
					{
						for ( var prop in flyout_manager[ 'colors' ][ a_node_key ] )
						{
							if( prop !== 'clear')
							{
								$('#' + ul_id ).parent().children().first().css(prop, flyout_manager[ 'colors' ][ a_node_key ][ prop ] );					
							}
						}
					}
					ul_node.css('display', 'block');
					var list_viewport_offset = ul_node.offset().top - $(window).scrollTop();
					var list_height = ul_node.height();
					var window_height = $(window).height();
					
					/*STEP 1: IF THE BOTTOM OF THE UL DOES NOT FALL BELOW THE PAGE, THEN SIMPLY PLACE LIST AT STARTING POINT*/
					if( (list_viewport_offset + list_height + 30 ) < window_height )
					{
						ul_node.css( 'marginTop', '0px' );
					}
					/*STEP TWO: OTHERWISE, ALIGN THE BOTTOM OF THE UL WITH THE BOTTOM OF THE VIEWPORT	*/
					else
					{
						ul_node.css( 'marginTop', ( window_height - list_viewport_offset - list_height - 30 ) + 'px' );
					}					
				}
				
			});
		
			$(this).bind( 'mouseout', function() {
				var ul_out_id = $(this).children().last().attr('id');
				flyout_manager[ ul_out_id ]['hide_ul_timer'] = setTimeout( 'hide_submenu( "' + ul_out_id + '" )', 20 );
				flyout_manager[ ul_out_id ][ 'link_mouseout_timer' ] = 1;				
			});
		}
	} );
}



function hide_submenu( submenu_id )
{
	if( submenu_id !== '' )
	{
			var submenu_node = $( '#' + submenu_id );
			submenu_node.css( 'display', 'none' );
			submenu_node.css('marginTop', '0px');

			delete flyout_manager[ submenu_id ][ 'hide_ul_timer' ];

			if ( flyout_manager[ submenu_id ][ 'link_mouseout_timer' ] )
			{
				var a_node = submenu_node.parent().children().first();
				var a_node_key = a_node.className;

				if ( flyout_manager[ 'colors' ][ a_node_key ] )
				{
					for ( var prop in flyout_manager[ 'colors' ][ a_node_key ] )
					{
						if( prop !== 'clear')
						{
							a_node.css(prop, flyout_manager[ 'colors' ][ a_node_key ][ prop ] );					
						}
					}
				}
			}		
	}

}
