/********************************************************************************
	This script is copyrighted by Orange Street Softare
		www.orangestreetsoftware.com
	
	It requires: 
		util.js

	The following <td> styles msut be defined:
		topmenu
********************************************************************************/

// ================================== GLOBAL VARIABLES ====================================================
// gloabl variable containg object with all menu setup information, 
// needs to be global within this script for event handlers (sorry)
var this_top_bar_container;

// these constants are indexes into the image_urls array in the container
var CI_TOP_BG_IMAGE = 0;

// ================================== UTITLITY FUNCTIONS ==================================================

// --------------------------------------------------------------------------------------------------------
// single point of call for all document writes (usefull for debuging etc.)
function top_bar_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 (this_top_bar_container.allow_render_caching)
	{
		if (!this_top_bar_container.is_cached)
			this_top_bar_container.render_cache = this_top_bar_container.render_cache + s_text;
	}
	else
		doc_write ( s_text );
}

// --------------------------------------------------------------------------------------------------------
// this is used to flush ot the cached top_bar_writes, 
// should be called afeter all other rendering code has finished, but before setup code
function top_bar_flush ()
{
	if (this_top_bar_container.allow_render_caching)
	{
		doc_write ( this_top_bar_container.render_cache );
		this_top_bar_container.is_cached = true;
	}
}

// ===================================== RENDERING FUNCTIONS ==============================================

// --------------------------------------------------------------------------------------------------------
// use this to draw the top bar, this will need to be called for each content page refresh
function render_top_bar ( the_container )
{
	// storing the menu setup object global to ease accessing metrics and urls
	//    er, sorry.
	
	//window.alert(the_container + ' : ' + window.the_container + ' : ' + window.parent.the_container);
	if (typeof(window.parent.the_container) == "undefined" || typeof(window.parent.the_container) != "object"){
	  return;
	}
	
	this_top_bar_container = window.parent.the_container;

	// make the rest of the menu bar	
	make_top_bar ();

	// flush the cached document writes if caching used
	top_bar_flush ();
}

// --------------------------------------------------------------------------------------------------------
// render out menu part of top bar
function make_top_bar ()
{
       var max_width_px = 850;
       var char_width   = 10;
       var total_width_used = 0;       
       var draw_devider = true;
       var n_item_index;
	// open div / layer
	// background:transparent; absolute
	if ( document.layers )
		top_bar_write ( '<layer name="topmenu" pageY=105 pageX=10 height=25 width=780 z-index=1 visibility="show">' );
	else
		top_bar_write ( '<div id="topmenu" style="position: absolute; top: 25px; left: 900px; height: 25px; width: ' + max_width_px + 'px; z-index: 3000; visibility: visible;">' );
		
	// make a table to display the image
	top_bar_write ( '<table border="0" cellspacing="0" cellpadding="0" class=topmenu>' );
	//top_bar_write ( '<form name="rightbarform">' );
	top_bar_write ( '<tr><td align="middle">' );

	// loop thru the items in the top bar and make appropriate item tags
	for ( n_item_index = 0 ; n_item_index < this_top_bar_container.item_count ; n_item_index++ )
	{
		// dump out the hyperlinked item
		if (this_top_bar_container.item_urls[n_item_index] != ''){
		   top_bar_write ( '<a href="javascript: this_top_bar_container.item_click (' + n_item_index.toString() + ');" >' + this_top_bar_container.item_names[n_item_index] + '</a>' );
		}else{
		   top_bar_write ( '<span class ="blackbig">' + this_top_bar_container.item_names[n_item_index] + '</span>');
		}
		
		total_width_used += this_top_bar_container.item_names[n_item_index].length * char_width;		
		if (total_width_used > max_width_px){
		   total_width_used = 0;
		   top_bar_write ( '<BR>' );
		}
		if (n_item_index < this_top_bar_container.item_count - 1 && total_width_used > 0){
		   top_bar_write ( '|&nbsp;' );
		   total_width_used += 3 * char_width;
		}   
	}

	//top_bar_write ( '</td></tr></form></table>' );
        top_bar_write ( '</td></tr></table>' );
        
	// close div / layer
	if ( document.layers )
		top_bar_write ( '</layer>' );
	else
		top_bar_write ( '</div>' );
}

// --------------------------------------------------------------------------------------------------------

