function show_random_quote(target_frame,prefix)
{
// grabs a random file from a predefined directory and dumps it to the frame specified in the parameters. 

var quote_count=Get_var('sci_fi_quote_count',0);

var prefix="fic";
var file_path=Get_var('base_url',0)+"quotes/";
var file_extension=".htm";

target_frame.location.href=file_path+prefix+getindex(quote_count)+file_extension;
}
//****************

function main_menu(requote)
{				// sets up the images for the quote-o-matic in the menu frame, displays the main page then displays a random quote in the summary frame if requote equals 1.

var href="javascript:show_random_quote(top.summary,'fic')";
var lmnt="top.menu.document.getElementById('summary_cell_0')";
var quoteover="'"+"images/scifipipequoteover.jpg"+"'";
var quoteunover="'"+"images/scifipipestrplugcont.jpg"+"'";
top.content.location.href="scifi_mainpage.htm";


menutable=top.menu.document.getElementById("summary_table");
cells=menutable.getElementsByTagName("TD");

cells[0].innerHTML='<IMG SRC="images/scifipipestrplugcont.jpg" Border=0 ID="summary_cell_0">';
cells[1].innerHTML='<A HREF="'+href+'"Border=0><IMG SRC="images/scifinewquote.jpg" ALT="Click here for a fresh random quote" width="72" height="28" Border=0 onMouseOver="pipe_change('+lmnt+','+quoteover+')" onMouseOut="pipe_change('+lmnt+','+quoteunover+')"></A>';

if(requote==1){show_random_quote(top.summary,'fic');}
}

//****************
function pipe_change(image,image_url){image.src=image_url;}

//****************

function add_image_to_object(obj,id)
{
var newimg=document.createElement("IMG"); 
obj.appendChild(newimg);
newimg.id=id;	 
return newimg;
}

//****************

function build_menu(selected_item)
{
  var width=Get_var('scifi_menu_width',0); var height=Get_var('scifi_menu_height',0); 
  var newtable1=do_table(top.menu.document.body,width,1,0,'summary_table'); tidy_table(newtable1); 
  var newtable2=do_table(top.menu.document.body,width,height,0,'menu_table'); tidy_table(newtable2);
  do_menu(width,height,selected_item);

}

//****************


function do_menu(width,height,selected_item)
{
  if(selected_item=="none"){main_menu(1);}
  else{
    if(selected_item=="planet_maker"){planet();}
      else{
        if(selected_item=="influences"){influences();}
          else{
            summary(selected_item);
              }
          }
      }
  fill_menu(width,height,selected_item);
}

//****************

function fill_menu(width,height,selected)
{
var base_url=Get_var('base_url',0);
var id_str=" ";
var cellcount=0;
mtable=top.menu.document.getElementById('menu_table');
mcells=mtable.getElementsByTagName('TD');
var content;
  for(i=0;i<height;i++)
  {
  content=Get_var('scifi_menu_item',i)
    for(j=0;j<width;j++)
    {
    if(j%2==0)	    					//even numbered (left hand) cells
      {
	id_str="menu_cell_"+String(cellcount);

        if (!mcells[cellcount].hasChildNodes())				// If the cell is empty...
           {var newimg=add_image_to_object(mcells[cellcount],id_str);}	// Create an image within the cell.
            
        type1="str";type2="plug";type3="cont";			//by default pipe cells are straight, plugged, continuation cells.
	if(selected==content){type1="link";}			//If this item selected, it becomes a linked cell.
	if(content=='null'){type2="blnk";}	 		//If cell to the right is blank, it gets unplugged.
	if(i==height-1){type3="term";}				//If bottom cell, it gets a "terminator" cell.

        top.menu.document.getElementById(id_str).src=base_url+"images/scifipipe"+type1+type2+type3+".jpg";	
      }//end left-handed cell


    if(j%2!=0)						//odd numbered (right hand) cells
      {

      if (!mcells[cellcount].hasChildNodes())			// Only change right-side cells if cell is empty (ie new).
      {
           var newhyplink=document.createElement("A");     	// first add a hyperlink...
	   var newhref=document.createAttribute('href');	// then create an href 
	   newhyplink.setAttributeNode(newhref);               	// Attach the href to the hyperlink 
           newhref.value="javascript:do_story('"+content+"',0);";	// Give the hyperlink a url.
           var newimg=add_image_to_object(newhyplink,id_str);	// then add an image to the hyperlink.
           mcells[cellcount].appendChild(newhyplink);  		// then attach it all to the cell


           newimg.src="images/"+content+".jpg";			// build image source from cellurls array
           newimg.border=0; newimg.alt=content;			// Get rid of border, add alt tag etc.
    
      }//end empty cell
      }//end right-handed cell.
    cellcount++; 
    }//end for j
  }//end for i
}

//****************

function do_story(selected,chapter)
{		//Rebuilds the menu frame & places the appropriate files in the summary and story frames.

var base_url=Get_var('base_url',0);
var width=Get_var('scifi_menu_width',0); 
var height=Get_var('scifi_menu_height',0);
do_menu(width,height,selected);
top.summary.location.href=base_url+"stories/summ_"+selected+String(chapter)+".htm"; 
top.content.location.href=base_url+"stories/story_"+selected+String(chapter)+".htm";
}

//****************

function summary(selected_item)
{	 			// sets up the summary images in the menu frame

var width=Get_var('scifi_menu_width',0); var height=Get_var('scifi_menu_height',0);

var href="javascript:do_story('"+selected_item+"',0)";
var lmnt="top.menu.document.getElementById('summary_cell_0')";
var summover="'"+"images/scifipipesummover.jpg"+"'";
var summunover="'"+"images/scifipipeplugtop.jpg"+"'";

summarytable=top.menu.document.getElementById("summary_table");
cells=summarytable.getElementsByTagName("TD");

cells[0].innerHTML='<IMG SRC="images/scifipipeplugtop.jpg" Border=0 ID="summary_cell_0">';
cells[1].innerHTML='<A HREF="'+href+'"Border=0><IMG SRC="images/scifisummary.jpg" ALT="Re-display story summary" width="72" height="28" Border=0 onMouseOver="pipe_change('+lmnt+','+summover+')" onMouseOut="pipe_change('+lmnt+','+summunover+')"></A>';
}
//****************

function influences()
{
summarytable=top.menu.document.getElementById("summary_table");
cells=summarytable.getElementsByTagName("TD");

var base_url=Get_var('base_url',0);
var imgsrc="'images/scifiinfluences.jpg' ";
var imgoversrc="'images/scifiinfluencesover.jpg' ";
var mseover="this.src="+imgoversrc;
var mseout="this.src="+imgsrc;
var href=base_url+"index.htm";

cells[0].innerHTML='<IMG SRC="images/scifipipelinkplugcont.jpg" Border=0 ID="summary_cell_0">';
cells[1].innerHTML='<A HREF="'+href+'" Border=0 target="_top"><IMG SRC='+imgsrc+' ALT="return to main page" width="72" height="28" Border=0 onMouseover="'+mseover+'" onMouseout="'+mseout+'"></A>';
}
//****************
function planet()
{
summarytable=top.menu.document.getElementById("summary_table");
cells=summarytable.getElementsByTagName("TD");

var base_url=Get_var('base_url',0);

var imgsrc="'images/scifiplanet.jpg' ";
var imgoversrc="'images/scifiplanetover.jpg' ";
var mseover="this.src="+imgoversrc;
var mseout="this.src="+imgsrc;
var href=base_url+"index.htm";

cells[0].innerHTML='<IMG SRC="images/scifipipeplugtop" Border=0 ID="summary_cell_0">';
cells[1].innerHTML='<A HREF="'+href+'" Border=0 target="_top"><IMG SRC='+imgsrc+' ALT="return to main page" width="72" height="28" Border=0 onMouseover="'+mseover+'" onMouseout="'+mseout+'"></A>';
}