/********************************************************************************
	This script is copyrighted by Orange Street Softare
		www.orangestreetsoftware.com
	
	It requires: 
		util.js

	The following <td> styles msut be defined:
		rootmenu
		rootmenuexp
		submenu
		submenuexp
		
********************************************************************************/

// ================================== GLOBAL VARIABLES ====================================================
// gloabl variable containg object with all menu setup information, 
// needs to be global within this script for event handlers (sorry)
var container;

// these constants are indexes into the spatial_metrics array in the container
var CI_MENU_TOP = 0;
var CI_MENU_LEFT = 1;
var CI_MENU_WIDTH = 2;
var CI_TREE_DEPTH_SPACER = 3;
var CI_ROOT_HEIGHT = 4;
var CI_NORMAL_HEIGHT = 5;
var CI_TITLE_HEIGHT = 6;

// these constants are indexes into the image_urls array in the container
var CI_TITLE_IMAGE = 0;
var CI_EXPAND_IMAGE = 1;
var CI_EXPANDED_IMAGE = 2;
var CI_BOTTOM_IMAGE = 3;

// ================================== UTITLITY FUNCTIONS ==================================================

// --------------------------------------------------------------------------------------------------------
// single point of call for all document writes (usefull for debuging etc.)
function menu_write ( s_text )
{

/*	debung stuff to show all tags as text
	s_text = s_text.replace ( /</g, "&lt;");
	s_text = s_text.replace ( />/g, "&gt;");
	document.writeln ( s_text + "<br>" );
*/
	if (container.allow_render_caching)
	{
		if (!container.is_cached)
			container.render_cache = container.render_cache + s_text;
	}
	else
		doc_write ( s_text );
}

// --------------------------------------------------------------------------------------------------------
// this is used to flush ot the cached menu_writes, 
// should be called afeter all otherrendering code has finished, but before setup code
function menu_flush ()
{
	if (container.allow_render_caching)
	{
		doc_write ( container.render_cache );
		container.is_cached = true;
	}
}

// ================================== MENU RENDERING FUNCTIONS ============================================

// --------------------------------------------------------------------------------------------------------
// use this to activate the menus (draw them), this will need to be called for each content page refresh
function render_all_menus ( menu_container )
{
var n_item_index;
var root_menu;

	// storing the menu setup object global to ease accessing metrics and urls
	//    er, sorry.
	container = menu_container;

	// first dump title image
	make_title ();

	// get the root menu in the active container 	
	root_menu = container.get_root_menu ();

	// start generating stuff from the root menu down (like recursively invlolutedly, shoowow man)	
	render_menu ( root_menu );

	// lastly dump the end image
	make_end ();
	
	// flush the cached document writes if caching used
	menu_flush ();
	
	// exapand the root menu, this will also show all sub-menus that are open in a recursive manner
	root_menu.show_menu ();
/*	for ( n_item_index = 0 ; n_item_index < root_menu.menu_items.length ; n_item_index++ )
		if (root_menu.menu_items[n_item_index].sub_menu)
			root_menu.menu_items[n_item_index].sub_menu.show_menu ();
*/
}

// --------------------------------------------------------------------------------------------------------
// render out the title box
function make_title ()
{
	if ( document.layers )
		menu_write ( '<layer name="title" pageY=' + (container.metrics[CI_MENU_TOP] - container.metrics[CI_TITLE_HEIGHT]) + ' pageX=0 height=' + container.metrics[CI_TITLE_HEIGHT] + ' width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="show">' );
	else
		menu_write ( '<div id="title" style="position: absolute; top: ' + (container.metrics[CI_MENU_TOP] - container.metrics[CI_TITLE_HEIGHT]) + 'px; left: 0px; height: ' + container.metrics[CI_TITLE_HEIGHT] + 'px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: visible;">' );

	menu_write ( '<table width="5" height="' + container.metrics[CI_TITLE_HEIGHT]  + '" border="0" cellspacing="0" cellpadding="0">' );
	menu_write ( '<tr><td width="5" height="' + container.metrics[CI_TITLE_HEIGHT]  + '"></td></tr></table>' );

	if ( document.layers )
		menu_write ( '</layer>' );
	else
		menu_write ( '</div>' );
}

// --------------------------------------------------------------------------------------------------------
// render out the title box
function make_end  ()
{
	if ( document.layers )
		menu_write ( '<layer name="menuend" pageY=0 pageX=0 height=5 width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="hide">' );
	else
		menu_write ( '<div id="menuend" style="position: absolute; top: 0px; left: 0px; height: 5px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: hide;">' );

	menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_TITLE_HEIGHT]  + '" border="0" cellspacing="0" cellpadding="0">' );
	menu_write ( '<tr><td width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_TITLE_HEIGHT]  + '"><img onmouseover=this.style.cursor="hand"; onclick=window.open("subscribe.html","subscribe","width=300,height=300"); src="' + container.image_urls[CI_BOTTOM_IMAGE] + '" border="0"></td></tr></table>' );

	if ( document.layers )
		menu_write ( '</layer>' );
	else
		menu_write ( '</div>' );

	container.active_window = window;
	container.bottom_tag = "menuend";
}

// --------------------------------------------------------------------------------------------------------
// render out all tags / layers that comprise the menu
function render_menu ( menu )
{
var n_item_index;

	if ( menu.tree_depth == 0 )
	{
		menu.contractable = false;
		menu.top = container.metrics[CI_MENU_TOP];
		menu.item_height = container.metrics[CI_ROOT_HEIGHT];
	}
	else
	{
		menu.contractable = true;
		menu.item_height = container.metrics[CI_NORMAL_HEIGHT];
	}
	menu.total_height = 0;
	menu.visible = false;
	
	// loop thru the items in the menu and make appropriate item tags
	for ( n_item_index = 0 ; n_item_index < menu.menu_items.length ; n_item_index++ )
	{
		if ( menu.tree_depth == 0 )
			render_root_item ( menu.menu_items[n_item_index] );
		else
			render_normal_item ( menu.menu_items[n_item_index] );
		
		// if this item has a sub-menu render it recursively
		if (menu.menu_items[n_item_index].sub_menu)
			render_menu ( menu.menu_items[n_item_index].sub_menu );
	}
}

// --------------------------------------------------------------------------------------------------------
// this renders out div / layer for a root menu item 
// positioned by initial expantion, then always open 
function render_root_item ( menu_item )
{
	if ( document.layers )
		menu_write ( '<layer name="' + menu_item.id + '" pageY=0 pageX=' + container.metrics[CI_MENU_LEFT] + ' height=' + (container.metrics[CI_ROOT_HEIGHT] - 1) + ' width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="hide">' );
	else
		menu_write ( '<div id="' + menu_item.id + '" style="position: absolute; top: 0px; left: ' + container.metrics[CI_MENU_LEFT] + 'px; height: ' + (container.metrics[CI_ROOT_HEIGHT] - 1) + 'px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: hidden;">' );

	menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_ROOT_HEIGHT] + '" border="1" cellspacing="0" cellpadding="0" bgcolor="#FFE69B" style="border: thin dotted;">' );
	menu_write ( '<tr>' );

	

	menu_write ( '<td width="' + (container.metrics[CI_MENU_WIDTH] - 11) + '">' );
	menu_write ( '<a href="javascript:window.top.' + menu_item.id + '.item_click ();" class="rootmenuG">' + escape_string ( menu_item.name ) + '&nbsp;</a>' );	
	menu_write ( '</td>' );
	menu_write ( '</tr>' );
	menu_write ( '</table>' );
	
	if ( document.layers )
		menu_write ( '</layer>' );
	else
		menu_write ( '</div>' );

	// start expanded tag
	if (menu_item.sub_menu)
	{
		if ( document.layers )
			menu_write ( '<layer name="' + menu_item.id + 'exp" pageY=0 pageX=' + container.metrics[CI_MENU_LEFT] + ' height=' + (container.metrics[CI_ROOT_HEIGHT] - 1) + ' width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="hide">' );
		else
			menu_write ( '<div id="' + menu_item.id + 'exp" style="position: absolute; top: 0px; left: ' + container.metrics[CI_MENU_LEFT] + 'px; height: ' + (container.metrics[CI_ROOT_HEIGHT] - 1) + 'px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: hidden;">' );
	
		menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_ROOT_HEIGHT] + '" border="0" cellspacing="0" cellpadding="0">' );
		menu_write ( '<tr>' );
	
		if (menu_item.sub_menu)
			menu_write ( '<td width=11><a href="javascript:top.' + menu_item.id + '.contract_sub ();"><img src="' + container.image_urls[CI_EXPANDED_IMAGE] + '" border=0></a></td>' );
		else
			menu_write ( '<td width=11>&nbsp;</td>' );
	
		menu_write ( '<td width="' + (container.metrics[CI_MENU_WIDTH] - 11) + '" align="right">' );
		menu_write ( '<a href="javascript:window.top.' + menu_item.id + '.item_click ();" class="rootmenuexpG">' + escape_string ( menu_item.name ) + '&nbsp;</a>' );	
		menu_write ( '</td>' );
		menu_write ( '</tr>' );
		menu_write ( '</table>' );

		if ( document.layers )
			menu_write ( '</layer>' );
		else
			menu_write ( '</div>' );
	}
	
	// register div tag setup in the menu_item object
	menu_item.active_window = window;
	menu_item.show_normal = true;
	if (menu_item.sub_menu)
		menu_item.expanded_name = menu_item.id + 'exp';
	menu_item.contracted_name = menu_item.id;
	
}

// --------------------------------------------------------------------------------------------------------
// this renders out div / layer for a normal menu item
// positioned when expanded, may be open or closed, sub-menus can expand and contract
function render_normal_item ( menu_item )
{
var n_indent_width;
var n_remaining_width;

	// calculate the width for indent and remaining width for menu item
	n_indent_width = container.metrics[CI_TREE_DEPTH_SPACER] * menu_item.menu.tree_depth;
	n_remaining_width = container.metrics[CI_MENU_WIDTH] - n_indent_width;
	
	n_x_pos = 20;
	
	if ( document.layers )
		menu_write ( '<layer name="' + menu_item.id + '" pageY=0 pageX=' + container.metrics[CI_MENU_LEFT] + ' height=' + (container.metrics[CI_NORMAL_HEIGHT] - 1) + ' width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="hide">' );
	else
		menu_write ( '<div id="' + menu_item.id + '" style="position: absolute; top: 0px; left: ' + container.metrics[CI_MENU_LEFT] + 'px; height: ' + (container.metrics[CI_NORMAL_HEIGHT] - 1) + 'px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: hidden;">' );

	menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_NORMAL_HEIGHT] + '" border="0" cellspacing="0" cellpadding="0">' );
	menu_write ( '<tr>' );
	
	if (menu_item.sub_menu)
		menu_write ( '<td width=10><a href="javascript:top.' + menu_item.id + '.expand_sub ();"><img src="' + container.image_urls[CI_EXPAND_IMAGE] + '" width=9 height=9 border=0></a></td>' );
	else
		menu_write ( '<td width=10>&nbsp;</td>' );
	
	menu_write ( '<td align="left">' );
		menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_NORMAL_HEIGHT] + '" border="0" cellspacing="0" cellpadding="0">' );
		menu_write ( '<tr><td width="' + n_indent_width + '">&nbsp;</td><td width="' + n_remaining_width + '" align="right">' );
		menu_write ( '<a href="javascript:top.' + menu_item.id + '.item_click ();" class="submenuG">' + escape_string ( menu_item.name ) + '&nbsp;</a>' );	
		menu_write ( '</td></tr></table>' );
	menu_write ( '</td>' );
	menu_write ( '</tr>' );
	menu_write ( '</table>' );

	if ( document.layers )
		menu_write ( '</layer>' );
	else
		menu_write ( '</div>' );

	if (menu_item.sub_menu)
	{
		if ( document.layers )
			menu_write ( '<layer name="' + menu_item.id + 'exp" pageY=0 pageX=' + container.metrics[CI_MENU_LEFT] + ' height=' + (container.metrics[CI_NORMAL_HEIGHT] - 1) + ' width=' + container.metrics[CI_MENU_WIDTH] + ' z-index=1000 visibility="hide">' );
		else
			menu_write ( '<div id="' + menu_item.id + 'exp" style="position: absolute; top: 0px; left: ' + container.metrics[CI_MENU_LEFT] + 'px; height: ' + (container.metrics[CI_NORMAL_HEIGHT] - 1) + 'px; width: ' + container.metrics[CI_MENU_WIDTH] + 'px; z-index: 1000; visibility: hidden;">' );
	
	
		menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_NORMAL_HEIGHT] + '" border="0" cellspacing="0" cellpadding="0">' );
		menu_write ( '<tr>' );
		
		if (menu_item.sub_menu)
			menu_write ( '<td width=10><a href="javascript:top.' + menu_item.id + '.contract_sub ();"><img src="' + container.image_urls[CI_EXPANDED_IMAGE] + '" width=9 height=9 border=0></a></td>' );
		else
			menu_write ( '<td width=10>&nbsp;</td>' );
		
		menu_write ( '<td align="left">' );
			menu_write ( '<table width="' + container.metrics[CI_MENU_WIDTH] + '" height="' + container.metrics[CI_NORMAL_HEIGHT] + '" border="0" cellspacing="0" cellpadding="0">' );
			menu_write ( '<tr><td width="' + n_indent_width + '">&nbsp;</td><td width="' + n_remaining_width + '" align="right">' );
			menu_write ( '<a href="javascript:top.' + menu_item.id + '.item_click ();" class="submenuexpG">' + escape_string ( menu_item.name ) + '&nbsp;</a>' );	
			menu_write ( '</td></tr></table>' );
		menu_write ( '</td>' );
		menu_write ( '</tr>' );
		menu_write ( '</table>' );
		
		if ( document.layers )
			menu_write ( '</layer>' );
		else
			menu_write ( '</div>' );
	}

	// register div tag setup in the menu_item object
	menu_item.active_window = window;
	menu_item.show_normal = true;
	if (menu_item.sub_menu)
		menu_item.expanded_name = menu_item.id + "exp";
	menu_item.contracted_name = menu_item.id;
}

// --------------------------------------------------------------------------------------------------------
