/********************************************************************************

	This script is copyrighted by Orange Street Software

********************************************************************************/

// put all 'global' variables and constants in an object to avoid scoping issues (pass this around explicitly)
// this must be created and setup before th menu can be 'run'
function menu_setup ( 	n_start_x, n_offset_y, 
						n_menu_item_width, n_menu_item_height, 
						s_bgcolour, s_border_colour,
						s_sub_arrow_url, s_item_arrow_url,
						s_title
					 )
{
	// constants defining setup of menu
	this.n_start_x = n_start_x;									// x position on page that root level starts at
	this.n_offset_y = n_offset_y;								// y offset - remainder from y start % n_menu_item_height
	this.n_menu_item_width = n_menu_item_width;					// width of all menu items
	this.n_menu_item_height = n_menu_item_height;				// height of all menu items
	this.s_bgcolour = s_bgcolour;								// the background colour to use for all menu items
	this.s_border_colour = s_border_colour;						// the border colour to use for all menu items
	this.s_sub_arrow_url = s_sub_arrow_url;						// the image url of the left arrow used to open sub-menus
	this.s_item_arrow_url = s_item_arrow_url;					// the image url of the item bullet
	this.s_title = s_title;
	

	// global variables used when menu is running
	this.all_menus = new Array();
	this.open_menus = new Array();
	this.close_timer_running = false;
	this.close_timer = null;
	
	// z-index stuff
	this.n_main_menu_z = 10000;									// this is the z-index for the divs-layers of main-menu level stuff
	this.n_over_main_menu_z = 10001;							// this is the z-index of things that must go over main-menu level stuff, such as the blue lines
	this.n_popup_z = 10002;										// this is the z-index of popup menu's, it is continuosly incremented as they gain focus.

	// Use this method to add menu's to this menu setup
	function add_menu ( menu )
	{
		// add all menus to list of all menus
		this.all_menus [this.all_menus.length] = menu;
		
		// set the root menu as the start of the chain of open menus
		if (menu.depth == 0)
			this.open_menus [0] = menu;
	}
	this.add_menu = add_menu;

	// Use this method to find a menu object from its menu number
	function find_menu ( menu_number )
	{
		for (var i = 0 ; i < this.all_menus.length ; i++)
			if (this.all_menus[i].menu_number == menu_number)
				return this.all_menus[i];
				
		return null;
	}
	this.find_menu = find_menu;

	// Use this method to add menu to the chain of menus, this method will return a list of any menus that need to be closed
	function add_open_menu ( parent_menu, sub_menu )
	{
		var i;
		var menus_to_close;
		
		// find the parent menu of the menu about open in the list of open menus
		for (i = 0; (i < this.open_menus.length) && (this.open_menus[i] != parent_menu) ; i++);
		
		// if the parent was not the last inb the chain then
		if (i < (this.open_menus.length - 1))
		{
			// make an array of menus that must be closed to return
			menus_to_close = this.open_menus.slice (i+1);
			
			// shrink the list of open menus to not include items after the parent menu
			this.open_menus = this.open_menus.slice (0,i+1);
		}
		// parent menu was last menu in chain
		else
		{
			// make an empty list of menus to close to return
			menus_to_close = new Array ();
		}
		
		// add the newly opened menu to the end of the chain
		this.open_menus [this.open_menus.length] = sub_menu;
		
		// return the list of menus to close
		return menus_to_close;
	}
	this.add_open_menu = add_open_menu;
	
	// Use this method to remove all sub menus from the list of open menus, it will return a list of any menus that need to be closed
	function remove_all_sub_open_menu ()
	{
		var menus_to_close;

		// if there are any sub menus open
		if (this.open_menus.length > 1)
		{
			// make an array of menus that must be closed to return
			menus_to_close = this.open_menus.slice (1);

			// shrink the list of open menus to not include items after the root menu
			this.open_menus = this.open_menus.slice (0,1);
		}	
		// if there are no sub menus open	
		else
		{
			// make an empty list of menus to close to return
			menus_to_close = new Array ();
		}
	
		// return the list of menus to close
		return menus_to_close;
	}
	this.remove_all_sub_open_menu = remove_all_sub_open_menu;

}

// define menu object

function menu ( menu_setup, menu_number, menu_name, depth )
{
	this.menu_setup = menu_setup;
	this.menu_number = menu_number;
	this.menu_name = menu_name;
	this.depth = depth;
	
	this.menu_item_array = new Array ();

	this.pos_x = menu_setup.n_start_x - depth * menu_setup.n_menu_item_width;
	this.pos_y = -1;

	if (depth == 0)
		this.is_visible = true;
	else
		this.is_visible = false;
		
	this.menu_setup.add_menu ( this );

	// Use this method to add menu item's to this menu
	function add_menu_item ( menu_item )
	{
		this.menu_item_array [this.menu_item_array.length] = menu_item;
	}
	this.add_menu_item = add_menu_item;
	
	
	
}

// define menu_item object
function menu_item ( menu, menu_item_number, target_name, target_url, target_frame, target_parms, sub_menu )
{
	this.menu = menu;
	this.menu_item_number = menu_item_number;
	this.target_name = target_name;
	this.target_url = target_url;
	this.target_frame = target_frame;
	this.target_parms = target_parms;
	this.sub_menu = sub_menu;
	this.menu_root_index = null;
	
	//This is only used for root items
	this.menu_level = null;
	this.root_item_width = null;

	this.menu.add_menu_item ( this );

	function set_menu_level( menu_level ){
		this.menu_level = menu_level;
	}
	this.set_menu_level = set_menu_level;
	
	function set_root_item_width( root_item_width ){
	
		this.root_item_width = root_item_width;
	}
	this.set_root_item_width = set_root_item_width;

	function setRootIndex(in_index)
	{
	   menu_root_index = in_index;
	}
	this.setRootIndex = setRootIndex;
	
}
