//--------------------
// Table of Contents utility functions
// A Table of contents is a set of hyperlinks, that allow navigation through a web site
// or a series of web pages.  These links can be either with text, images, and/or 
// rollover images.
//
// These functions rely on the following JS files
//      imageutils.js
//      textutils.js
//      tableutils.js
//
// The functions included in this file are:
//      TOCitem()  - the TOC object
//      showTOCDown()
//      showTOCTextHorizontal()
//      showTOCTextHorizontal2()
//      showTOCColumns()
//--------------------

//--------------------
// This function creates the Table of contents object used
// by the other functions in this library
// param: name - the link name
// param: 
//--------------------
function TOCitem(link, text, level, name, img1, img2, alt)
{
  // debug the text
  if (text == null)
  {
    alert("objTOCEntry() : text == null")
    this.text = "defaul text"
  }
  else
    this.text = text

  // debug the link
  if (link == null)
  {
    alert("objTOCEntry() : link == null")
    this.link = ""
  }
  else
    this.link = link

  this.level = (level != null ? level : 1)
  this.name = (name != null ? name : "X" + Math.random())
  this.img1 = (img1 != null ? img1 : "")
  this.img2 = (img2 != null ? img2 : "")
  this.alt = (alt != null ? alt : "")
}

//--------------------
// this TOC displays the images and text straight down with the image above the link
// the TOC is placed within a table of its own
// param: inpercent - true if the table width (twidth) is in percent
// param: twidth - table width
// param: title - TOC title
// param: tocArr - an Array() of objTOCEntry objects
// param: bgcolor - table background color
// param: yes_text - include text - default is true
// param: yes_image - include images - default is true
// param: yes_rollover - include rollover image - default is false, always false if yes_image is false
// returns the number of row created
//--------------------
function showTOCDown(inpercent, twidth, title, bgcolor, tocArr, yes_text, yes_image, yes_rollover)
{
  var idx
  var numrows = 0

  // check optional parameters
  if (yes_text == null)
    yes_text = true

  if (yes_image == null)
    yes_image = true

  // if there is no image displayed do not use the rollover
  if (!yes_image)
    yes_rollover = false
  else if (yes_rollover == null)
    yes_rollover = true


  startTable(inpercent, 0, twidth, bgcolor, "", 0, 0, 2, 0, 0)

  if (title.length > 0)
  {
    startRow()
    startCol(0,0, "", "", "top", "center")
    document.write(title)
    endCol()
    endRow()
    numrows++
  }

  for (idx = 0; idx < tocArr.length; idx++)
  {
    if (yes_image)
    {
      startRow()
      startCol(0,0,"","","top", "center")
      imageLinkEx(tocArr[idx].name, tocArr[idx].link, tocArr[idx].img1, 
		(yes_rollover ? tocArr[idx].img2 : ""), 
		0, 0, tocArr[idx].alt)

      endCol()
      endRow()
      numrows++
    }
       
    if (yes_text)
    {
      startRow()
      startCol(0,0,"","","top", "center")
      textLinkEx(tocArr[idx].name, tocArr[idx].link, tocArr[idx].img1, 
		(yes_rollover ? tocArr[idx].img2 : ""),
		tocArr[idx].text)
      endCol()
      endRow()
      numrows++
    }

    // gap between rows
    startRow()
    startCol(0,0,"","","top", "center")
    document.write("&nbsp;")
    endCol()
    endRow()
    numrows++
  }

  endTable()

  return numrows
}

//--------------------
// This TOC displays the information the following manner:
//   item | item | item | item | item <break>
//            item | item | item
// param: justify - should be a number: 0 = default, 1 = "LEFT", 2 = "CENTER", 3 = "RIGHT"
// param: breakat - the number of items to list before putting in a break
// param: tocArr - an Array() of objTOCEntry
//--------------------
function showTOCTextHorizontal(justify, breakat, fontsize, tocArr)
{
  var idx
  var numitems = 0

  if (justify == 2)
    document.write("<p align=\"CENTER\">")
  else if (justify == 3)
    document.write("<p align=\"RIGHT\">")

  document.write("<FONT SIZE=\"", fontsize, "\">")
  for (idx = 0; idx < tocArr.length; idx++)
  {
    if (numitems > 0)
      document.write(" | ")

    textLinkEx(tocArr[idx].name, tocArr[idx].link, "", "", tocArr[idx].text)

    if (++numitems == breakat)
    {
      document.write("<BR>")
      numitems = 0
    }
  }

  document.write("</FONT>")
}

//--------------------
// This TOC displays the information the following manner:
//   item  item  item  item  item </tr>
//   item  item  item
// param: justify - should be a number: 0 = default, 1 = "LEFT", 2 = "CENTER", 3 = "RIGHT"
// param: breakat - the number of items to list before putting in a break
// param: fontsize - the size of the text
// param: tocArr - an Array() of objTOCEntry
// param: inpercent - identifies that the h and w are in percent
// param: bgcolor - background color of the table
// param: bgimg - background image of the table
// param: border - the border size of the table
//--------------------
function showTOCTextHorizontal2(justify, breakat, fontsize, tocArr, inpercent, h, w, bgcolor, bgimg, border)
{
  var idx
  var numitems = 0
  var tmpstring

  if (justify == 2)
    document.write("<CENTER>")

  startTable(inpercent,h,w,bgcolor, bgimg, border,2,2,0,0)

  // loop through the array  
  for (idx = 0; idx < tocArr.length; idx++)
  {
    // start the row
    if (numitems == 0)
      startRow()

    // start the column
    startCol(0,0,"","","TOP","CENTER", 0,0)

    tmpstring = "<FONT SIZE=\"" + fontsize + "\">&nbsp;" + tocArr[idx].text + "&nbsp;</FONT>"
    textLinkEx(tocArr[idx].name, tocArr[idx].link, "", "", tmpstring)

    endCol()
    if (++numitems == breakat)
    {
      endRow()
      numitems = 0
    }
  }

  endTable()
  if (justify == 2)
    document.write("</CENTER>")
}

//--------------------
// This function displays a TOC in columns of (hopefully) even distribution.
//   Each column in the table holds N / Columns items.
//   The left column holds the first N items, the next column holds the next N items
//   until all of the columns are full.  The left most columns may have 1 more item
//   than the right most columns if the number of items is not evenly divisable by the
//   number of columns
//
// param: justify - 1 for LEFT, 2 for CENTER, 3 for RIGHT
// param: numcol - The number of colums in the TOC table
// param: fontsize - Size of the fonts for the links
// param: fontname - the name of the font for the link text
// param: tocArr - an array of objTOCEntry objects
// param: inpercent - determines if the height and width are in percent or pixels
// param: h - height of the table
// param: w - width of the table
// param: bgcolor - background color for the table
// param: bgimg - background image for the table
// param: border - the size of the border
// param: yes_image - present the images for the link - default is false
// param: yes_text - present the text for the link - default is true
// param: spacing - table's column cell spacing - default is 2
//--------------------
function showTOCColumns(justify, numcol, fontsize, fontname, tocArr, inpercent, h, w, bgcolor, bgimg, 
			border, yes_image, yes_text, spacing, yes_link)
{
  var idx
  var numitems = 0
  var tmpstring
  var numrows
  var currow

//alert("Starting showTOCColumns")

  // check for optional parameters
  if (yes_image == null)
    yes_image = false

  if (yes_text == null)
    yes_text = true

  if (spacing == null)
    spacing = 2

  if (yes_link == null)
    yes_link = true

//alert("Finished checking for optional parameters")

  // determine the number of rows
  // if the remainder is greater than 0 add another row
  numrows = (tocArr.length % numcol == 0 ? tocArr.length / numcol :  Math.floor((tocArr.length / numcol) + 1) )

//alert("Determine the number of row = " + numrows)

  // add the justification
  if (justify == 2)
    document.write("<CENTER>")

//alert("Set justify ")


  startTable(inpercent,h,w,bgcolor, bgimg, border, spacing,2,0,0)

//alert("Table started")

  // loop through the number of rows
  for (currow = 0; currow < numrows; currow++)
  {
//    alert("Starting loop 1 at row " + currow)

    startRow()

    // loop for the number of columns
    for (idx = currow, curcol = 0;
	 curcol < numcol && idx < tocArr.length;
	 curcol++, idx += numrows)
    {
//      alert("Starting loop 2 at idx " + idx)

      // start the column
      // if there is an image then center the text
      startCol(0,0,"","","TOP",
		(yes_image ? "CENTER" : "LEFT"),
		0,0)

//      alert("Starting column " + curcol)

      if (yes_image)
      {
//        alert("Creating image link")
        imageLinkEx(tocArr[idx].name, tocArr[idx].link, tocArr[idx].img1, tocArr[idx].img2, 0, 0, tocArr[idx].alt)
      }
      
      if (yes_text)
      {
//        alert("Creating Text Link")

        if (yes_image)
          document.write("<br>")

        // build the font description
        if (fontsize > 0 || fontname != null)
        {
          tmpstring = "<font "
          if (fontname != null)
            tmpstring += "face=\"" + fontname + "\" "

          if (fontsize > 0)
            tmpstring += "SIZE=\"" + fontsize + "\" "

          tmpstring += ">&nbsp;" + tocArr[idx].text + "&nbsp;</FONT>"
        }
        else
          tmpstring = tocArr[idx].text

//        alert ("font info built")

        // determine whether to show the link or just the text
        if (yes_link)
        {
//          alert("Adding text link")
          textLinkEx(tocArr[idx].name, tocArr[idx].link, "", "", tmpstring)
        }
        else
        {
//          alert("Adding plain text")
          document.write(tmpstring)
        }
      }

      endCol()
    }
    endRow()
  }

  endTable()
  if (justify == 2)
    document.write("</CENTER>")

//  alert("Ending showTOCColumns()")
}

//--------------------
// When there is more than one table of contents displayed in a web page and they both
// use the same array, and both have rollover images, the browser
// may get mixed up to which link to update. Consequently, the wrong image link may be
// rolledover.  newName() helps with this by changing the name of the link to a <prefix> + a number
// This function will return the TOC Array sent in
// param: prefix - The prefix of the name - must be text
// param: tocArr - the Table of contents array
// return: tocArr
//--------------------
function newLinkName(prefix, tocArr)
{
  var idx
  for (idx = 0; idx < tocArr.length; idx++)
    tocArr[idx].name = prefix + idx

  return tocArr
}


