//These functions are for drawing the left hand side menus

var page_href = parent.location.href;
var site_host = location.host;

//Write out menu - insert arrow image if link is for current page
//var menu;
//menu = "";

function write_menu(text, addr, image, back_class)
{
	var i, line, fullpath;
	for( i = 0; i < text.length; i++ )
	{
		fullpath = 'http://' + site_host +  addr[ i ];
		
		if( fullpath == page_href )
			line = menu_line_arrow( text[ i ], image, back_class );
		else if( addr[ i ].indexOf( "#" ) > -1  )
			line = menu_link( text[ i ], addr[ i ], "main" );
		else
			line = menu_link( text[ i ], addr[ i ], "_top" );

		document.write( line );
//		menu = menu + line
	}
//	alert(menu);
}


//different version for press section because it has expanding section
function write_press_menu()
{
	var args = getargs();
	var line, fullpath, expand;
	for( var i = 0; i < menutext.length; i++ )
	{
		fullpath = 'http://' + site_host +  address[ i ];

		if( fullpath == page_href )
		{
			line = menu_line_arrow( menutext[ i ], arrow, background_class );
			document.write( line );
		}

		else if( menutext[i] == "Featured Articles:" )
		{
			line = menu_line( menutext[ i ] );
			document.write( line );
			write_menu( sheettext, sheetadd, arrow, background_class );
		}
		else
		{
			line = menu_link( menutext[ i ], address[ i ], "_top" );
			document.write( line );
		}

	}
}



//different version for technology section because it has expanding section
function write_tech_menu()
{
	var args = getargs();
	var line, fullpath, expand;
	for( var i = 0; i < menutext.length; i++ )
	{
		fullpath = 'http://' + site_host +  address[ i ];
		
		if( fullpath == page_href )
		{
			line = menu_line_arrow( menutext[ i ], arrow, background_class );
			document.write( line );
			if( menutext[i] == "Programming Languages" )
				write_menu( progtext, progadd, arrow, background_class );
			if( menutext[i] == "databases" )
				write_menu( dbtext, dbadd, arrow, background_class );
			if( menutext[i] == "database connection" )
				write_menu( dctext, dcadd, arrow, background_class );
		}
		else if( menutext[i] == "Internet" )
		{
			if( args["expand"] == "int" || in_submenu( intadd ) )
			{

				line = menu_link( menutext[ i ], address[ i ], "_self" );
				document.write( line );
				write_menu( inttext, intadd, arrow, background_class );					
			}
			else
			{
				line = menu_link( menutext[ i ], address[ i ] + "?expand=int", "_self" );
				document.write( line );
			}
		}
		else if( menutext[i] == "Featured Articles:" )
		{
			line = menu_line( menutext[ i ] );
			document.write( line );
			write_menu( sheettext, sheetadd, arrow, background_class );
		}
		else
		{
			line = menu_link( menutext[ i ], address[ i ], "_top" );
			document.write( line );
		}
	}
}

function write_home_menu()
{
	var args = getargs();
	var line, fullpath;
	for( var i = 0; i < menutext.length; i++ )
	{
		fullpath = 'http://' + site_host +  address[ i ];
		
		if( menutext[ i ] == "Special Section for:" )
		{
//don't show this anymore
//			line = menu_line( menutext[ 2 ] );
//			document.write( line );
			write_menu( proftext, profadd, arrow, background_class );
		}
		else
		{
			if( fullpath == page_href )
				line = menu_line_arrow( menutext[ i ], arrow, background_class );
			else
				line = menu_link( menutext[ i ], address[ i ], "_top" );
			document.write( line );
		}

	}
}


function in_submenu( subaddress )
{
	var j;
	for( j=0; j < subaddress.length; j++ )
		if( 'http://' + site_host + subaddress[ j ] == page_href )
			return 1;
	return 0;
}		

function menu_line_arrow( text, image, back_class )
{
	var line;
	line = '<TR><TD CLASS=MENUTEXT WIDTH="166px">' + text + '</TD>';
	line += '<TD CLASS=' + back_class + '><IMG SRC="' + image + '" WIDTH="12px" HEIGHT="12px"></TD></TR>';
	return line;
}

function menu_line( text )
{
	var line;
		line = '<TR><TD CLASS=MENUTEXT WIDTH="183px" COLSPAN=2>' + text + '</TD></TR>';
	return line;
}

function menu_link( text, address, target )
{
	var line;
		line = '<TR><TD CLASS=MENUTEXT WIDTH="183px" COLSPAN=2>';
		line += '<A HREF="' + address + '" TARGET="' + target + '">';
		line +=  text;
		line += '</A></TD></TR>';
	return line;
}

function getargs_for_window( obj )
{
	var args = new Object();
	var query;
	
	if( obj != null )
		query = obj.location.search.substring( 1 );
	else
		query = location.search.substring( 1 );
		
	var pairs = query.split("&");
	for( var i = 0; i < pairs.length; i++ )
	{
		var pos = pairs[i].indexOf('=');
		if( pos == -1 ) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

function getargs()
{
	return getargs_for_window(window);
}

function query_string( obj, str )
{
	return getargs_for_window(obj)[str];
}
