// Que List 
var QueSize = 0;
var QueHead = 0;
var QueTail = 0;

// Global Direction Variables
  r  = "<img src='../../img/right.gif' alt='Right' width='28' height='24' border='0'>"
  l  = "<img src='../../img/left.gif' alt='Left' width='28' height='24' border='0'>"
  br = "<img src='../../img/br.gif' alt='Bear Right' width='28' height='24' border='0'>"
  bl = "<img src='../../img/bl.gif' alt='Bear Left' width='28' height='24' border='0'>"
  rl = "<img src='../../img/rl.gif' alt='right then left' width='28' height='24' border='0'>"
  lr = "<img src='../../img/lr.gif' alt='left then right' width='28' height='24' border='0'>"
  s  = "<img src='../../img/str.gif' alt='Straight' width='28' height='24' border='0'>"
  p  = "<img src='../../img/str.gif' alt='Pass' width='28' height='24' border='0'>"
  x  = "<img src='../../img/x.gif' alt='Cross' width='28' height='24' border='0'>"
  xo = "<img src='../../img/xo.gif' alt='Cross Over' width='28' height='24' border='0'>"
  xu = "<img src='../../img/xu.gif' alt='Cross Under' width='28' height='24' border='0'>"
  u  = "<img src='../../img/u.gif' alt='U-Turn' width='28' height='24' border='0'>"
  n  = " ";
  ss  = "<img src='../../img/ss.gif' width='17' height='16' border='0'>";
  tl  = "<img src='../../img/tl.gif' width='33' height='13' border='0'>";
  tee = "<img src='../../img/tee.gif' width='17' height='16' border='0'>";
  why = "<img src='../../img/why.gif' width='17' height='16' border='0'>";
  
// Global settings that can be overridden
  var g_documentName = "que.htm";
  var g_loopRide = 1;
  var g_pageLength = 20;

function displayWarning()
{
    document.write("<table border='1' align='center' bgcolor='#FFFF00' style='font-size: 10pt;'>");
    document.write("<tr align='center'><td><font color='red'><b>WARNING!</b></font>: ");
    document.write("<b>Follow at own risk</b>!  Bicycling can be a dangerous activity. ");
    document.write("Directions may contain errors and do not identify all hazards.");
    document.write("The author can not be held responsible for any death, injury, or property loss ");
    document.write("should you choose to follow this route. </td></tr></table>");
}

// This function displays a float number with one decimal point
function formatFloat(param)
{
  var number  = Math.round(param*10)/10;
  var intNum  = Math.floor(number);
  var fracNum = (number-intNum)*10;
  var string =  parseInt(intNum) + "." + parseInt(fracNum + 0.05);
  return string;
}

// This function defines a segment entry in the list
// When created, it adds
function queSeg(len, dir, street) 
{
  this.len    = len;
  this.dir    = dir;
  this.street = street;
  this.idx    = QueSize;
  QueSize     = QueSize + 1;
  this.next   = QueHead;
  if (0 == QueHead) { QueHead      = this; }
  else              { QueTail.next = this; }
  QueTail = this;
}

// This function displays the entire list
function displayQue1(current, start, totalLines)
{
  document.write("<p align='center'><b>" + document.title + "</b></p>");
  displayWarning();

  document.write("<p align='center'><b>NOTE</b>: A newer browser will display this list with fewer pages </p>");
  document.write("<br clear='all'><hr>");

  // Setup a default start index, assuming Northbound route
  var dist   = 0.0;
  var seglen = 0.0;
  document.write("<table width='100%'><tr align='center'><th>Dist.<\/th><th>Leg<\/th><th>Dir<\/th><th align='left'>Street<\/th><\/tr>");
  var lines = 0;
  do
  {
    document.write("<tr align='center'><td><a href='./" + g_documentName);
    document.write("?&start=" + current.idx +"'>" + formatFloat(dist) + "<\/a><\/td>");
    document.write("<td>" + formatFloat(seglen) + "<\/td>");
    document.write("<td><b><i>" + current.dir + "</i><\/b><\/td>");
    document.write("<td align='left'>" + current.street + "<\/td><\/tr>");
    document.write("<tr><td colspan='4'><hr><\/td><\/tr>");
    seglen  = current.len;
    current = current.next;

    // Update the running distance from the next segment
    dist  = (Math.round((dist + seglen)*100))/100;

    // Increment the number of lines displayed to determine if
    // a new column is required.
    lines = lines + 1;

  } while ( (current.idx != start) && (lines < totalLines) )

  // If we reached the end, put a finishing touch on things
  document.write("<tr class='QueText'><td><a href=\"./" + g_documentName);
  document.write("?&start=" + current.idx +"\">" + formatFloat(dist) + "<\/a><\/td>");
  document.write("<td>" + formatFloat(seglen) + "<\/td>");
  document.write("<td align='center'><b>Finish<\/b><\/td><\/tr><\/table>");
}

// This function displays the entire list
function displayQue2(current, start, totalLines)
{
  // Setup a default start index, assuming Northbound route
  var dist   = 0.0;
  var seglen = 0.0;

  // Add one line for Finish Result
  totalLines = totalLines + 1;
  totalRows  = 1;

  var aIdx  = new Array(totalLines);
  var aDst  = new Array(totalLines);
  var aLeg  = new Array(totalLines);
  var aDir  = new Array(totalLines);
  var aMsg  = new Array(totalLines);
  var aLns  = new Array(totalLines);
  
  // Build array staring from desired start location
  var page = 1;
  var idx = 0;
  var lns = 0;
  var startIdx = current.idx;
  do
  {
    aIdx[idx]  = current.idx;
    aDst[idx]  = dist;
    aLeg[idx]  = seglen;
    aDir[idx]  = current.dir;
    aMsg[idx]  = current.street;
    regPat     = /(<[^>]*>)/g ;
    striped    = current.street.replace(regPat,"");
    aLns[idx]  = Math.ceil(striped.length / 36);
    totalRows  = totalRows + aLns[idx];

    // Update the running distance from the next segment
    seglen  = current.len;
    dist    = (Math.round((dist + seglen)*100))/100;
  
    current = current.next;
    idx = idx + 1;
  
  } while (idx < (totalLines - 1))

  aIdx[idx]  = startIdx;
  aDst[idx]  = dist;
  aLeg[idx]  = seglen;
  aDir[idx]  = "";
  aMsg[idx]  = "<b>End of Route</b>";
  aLns[idx]  = 1;
  totalRows  = totalRows + aLns[idx] + 1;

  // Determine how many pages are required for this ride
  // Since there are two columns per page, take number of
  // lines/2 and then divide that by pageLength
  var pageLength = 32;

  // If an alternative page count was specified, use that instead
  pagePattern = /.*pageCount=(\d*).*/g ;
  var pageCount = parseInt(location.search.replace(pagePattern,"$1"));
  if (isNaN(pageCount))
  {
      // Determine how many pages are required for this ride
      // Since there are two columns per page, take number of
      // lines/2 and then divide that by pageLength
      pageCount = Math.ceil( ((totalRows/2) / pageLength));
  }

  // Once the number of pages have been determined, 
  // we can calculate the number of lines per column
  var columnLines = Math.round((totalRows / (pageCount * 2)) + 0.45);

  idx = 0;

  do
  {
    document.write("<table width='100%'><tr>");
    document.write("<td><b>" + document.title + "</b> - " +  formatFloat(dist) + " Miles</td>" );
    document.write("<td align='right'>Page " + page + " of " + (pageCount) + "</td>" );
    document.write("</tr></table>");
    displayWarning();
    page = page + 1;
    var colCount  = 0;
    var colSide   = "left";
    do
    {
      var lines   = 0;
      document.write("<table width='49%' align='" + colSide +"' frame='box' rules='rows'><tr class=QueHead>");
      document.write("<td align='right'>Mile&nbsp;<\/td><td align='center'>Turn<\/td><td align='right'>Leg&nbsp;<\/td><td>Description<\/td><\/tr>");
      document.write("<tr><td colspan='4'><img src='../../img/line.gif' alt='' width='320' height='1' border='0'><\/td></tr>");
      while ((lines + aLns[idx]) <= columnLines)
      {
        document.write("<tr class='QueText'><td align='right'><a href='./" + g_documentName);
        document.write("?&start=" + aIdx[idx] +"'>" + formatFloat(aDst[idx]) + "<\/a>&nbsp;<\/td>");
        document.write("<td align='center'><b><i>" + aDir[idx] + "</i><\/b><\/td>");
        document.write("<td align='right'>&nbsp;" + formatFloat(aLeg[idx]) + "&nbsp;<\/td>");
        document.write("<td>" + aMsg[idx] + "<\/td><\/tr>");
        lines = lines + aLns[idx];
        idx = idx + 1;
        if (idx >= totalLines) break;
      }
      document.write("<\/table>");
      colSide = "right";
      colCount = colCount + 1;
      if (idx >= totalLines) break;
    } while (colCount < 2);

    // There are more cues, insert a page break
    document.write("<br clear='all'><p align='center' style='font-size: 10pt;'>");
    document.write("NOTE: This cue sheet can be customized to fit your specific needs. Just click on ");
    document.write("any line to automatically recalculate the route starting from that location.</p>");
    document.write("<DIV STYLE=\"page-break-before:always\"></DIV>");

  } while (idx < totalLines)
}


function displayQue()
{
  // Search this Page's URL for Alternative Start Location
  startPattern = /.*start=(\d*).*/g ;
  var start = parseInt(location.search.replace(startPattern,"$1"));
  if (isNaN(start)) {start = 0;}

  // Verify that the desired start point is in the list
  if (start < QueSize)
  {
    // Search the List for the desired start location
    var current = QueHead;
    while (start != current.idx)
    {
      current = current.next;
    }

    // If this is not a loop ride, then force the list to stop at the end
    var totalLines = QueSize;
    if (0 == g_loopRide)
    {
      totalLines = QueSize - start;
      start = 0;
    }

    // check for Netscape 4.x
    var bSimple = 0;
    if (navigator.appName == "Netscape")
    {
      if (4 == parseInt(navigator.appVersion))
      {
         bSimple = 1;
      }
    }
    if (1 == bSimple)
    { 
      displayQue1(current, start, totalLines);
    }
    else
    {
      displayQue2(current, start, totalLines);
    }

  }
}

