//
// Hit navigation JavaScript for the dtSearch 6 ASP sample
//
// Copyright 2000 dtSearch Corp.
//
// This JavaScript is included in both retrieved documents and in
// the search results list, to enable hit navigation using the
// button bar in the sample search form.  The button bar
// uses JavaScript to call these functions in the search results frame
// or in the retrieved document frame.

// The first two variables in this file may need to be changed
// if you modify dtsearch.asp to change the layout of search results.
//
// nFirstLink is the offset in search results of the first
//            link to a retrieved item.
// nLinksPerItem is the number of links for each search results item.
//            nextDoc() and prevDoc() use these values to navigate through the
//            list of links on a search results page.
//
// http://localhost/AudioAndText/Village_Chapel/AAT_Search.asp?cmd=getdoc&maxSize=300000&DocId=18&Index=C%3a%5cAudioAndText%5cWebsite%5cVillage%5fChapel%5cIndexes%5cVC&HitCount=73&hits=2e+3f+42+53+6b+7b+e6+16b+171+181+1a8+1bd+1ce+1d1+1d8+1e3+1e5+1f2+20f+218+289+309+32d+335+345+366+37a+38c+390+3a3+3b6+3c1+3c9+3d2+3e8+3e9+3ed+3f1+3f6+403+41a+429+431+49a+4ac+540+544+566+578+589+597+700+72e+740+770+7c3+7d7+7ed+7fa+801+809+810+818+820+839+842+883+898+8a8+8ad+8ba+8d2+8ec+&hc=611&req=god


nFirstLink = 1;
nLinksPerItem = 4;
nDoc = -1;

nHit = 1;
maxHits = 1
fAutoNextDoc = 0;

var simpleNav = 1;
var browser = navigator.appName;
var version = parseInt(navigator.appVersion);

if ((browser.indexOf("Microsoft") != -1) && (version >= 4))
    simpleNav = 0;

var Querystring =  "";
URLstring = window.location.search;
//alert("hitnav.js: URLstring="+URLstring)

function gotoAATNthHit(n) {
    nHit = n;
    if ((n > 1) && (n == maxHits))
        gotoHit('hit_last');
    else if (n == 0)
        a = "nop"
    else
        gotoHit('hit' + n);
 }

function getParameter ( queryString, parameterName ) {
   var parameterName = parameterName + "=";       // Add "=" to the parameter name (i.e. parameterName=value)
   if ( queryString.length > 0 ) {
      begin = queryString.indexOf ( parameterName );     // Find the beginning of the string
      if ( begin != -1 ) {      // If the parameter name is not found, skip it, otherwise return the value
         begin += parameterName.length;     // Add the length (integer) to the beginning
         end = queryString.indexOf ( "&" , begin );     // Multiple parameters are separated by the "&" sign
         if ( end == -1 ) {
            end = queryString.length
         }
         return unescape ( queryString.substring ( begin, end ) );    // Return the string
      }
      return "null";    // Return "null" if no parameter has been found
   }
}

maxHits = getParameter ( URLstring, 'HitCount' );
//alert("hitnav.js: URLstring="+URLstring+", maxHits="+maxHits)

function old_gotoHit(where) {
    //alert("hitnav.js: function old_gotoHit, where="+where)
    window.location.hash = where;
    //parent.ABC_Body_Left.location.hash = where;
    //parent.location.hash='#top'
    //parent.ABC_Body_Left.transcript_content.location.hash = where;
    }

function findLink(links, name)
{   if (links.namedItem)
        return links.namedItem(name);
    for (i = 0; i < links.length; ++i) {
        if (links[i]) {
            if (links[i].name == name)
                return links[i];
            }
        }
}

function gotoHit(where) {
    //alert("hitnav.js: function gotoHit, where="+where)
    // *** Initialize hit point to correct location (if user clicked on highlighted keyword link), where is format Hit10 (need to strip off Hit to get integer)
    //*** nHit = n;     3=strips off "Hit" 5 just high number, i.e. means get rest of string
          if (where == "hit_last") {
             nHit = maxHits
             //alert("hitnav.js: function gotoHit *** hit_last found *** where="+where+", nHit="+nHit+", maxHits="+maxHits)
             }
          else {
             ss = where.substring(3,7)
             //alert("hitnav.js: function gotoHit where="+where+", ss="+ss)
             nHit = parseInt(ss)
             }

    if (simpleNav) {
        old_gotoHit(where);
        return;
        }

    var a = findLink(document.anchors, where);
    if (a == null) return;
    if (a.length > 1) return;

    if (a.y) {
        // Netscape
        window.scrollTo(0, a.y);
        }
    else {
        // IE
        window.scrollTo(0, a.offsetTop - a.offsetHeight + a.offsetParent.offsetTop);
        }

    var s = document.body.createTextRange();
    if (s != null) {
        s.moveToElementText(a);
        s.moveEnd("word");
        s.select();
        s.scrollIntoView();
        }
}

function HitsMarked() {
   if (parent.frames.length == 0) {
      var ssearch = window.location.search
      }
   else {
      var ssearch = new String(parent.transcript_content.location.search);
      }
   if (ssearch.indexOf("cmd=getdoc") == 0) {
      return 0
      }
   else {
      return 1
      }
}  //*** end function    cmd=search cmd=getdoc

function gotoNthHit(n) {
    nHit = n;
      //alert("hitnav.js: function gotoNthHit, n="+n+", maxHits="+maxHits+".")
    if ((n > 1) && (n == maxHits))
        gotoHit('hit_last');
    else if (n == 0)
        old_gotoHit('hit0');
    else
        gotoHit('hit' + n);
 }
function nextHit() {
    //alert("hitnav.js: function nextHit, nHit="+nHit+", maxHits="+maxHits+".")
    if (HitsMarked()) {
       if (nHit < maxHits) {
           gotoNthHit(nHit+1);
           }
       else {
           nHit = maxHits
           alert("You are at the last Hit")
           }
       }
    else {
      alert("Search Result Hit Navigation only works when viewing a single transcript and Hits are highlighted.");
      }
}   //*** end function

function prevHit() {
    //alert("hitnav.js: function prevHit, nHit="+nHit+", maxHits="+maxHits+".")
    if (HitsMarked()) {
       if (nHit > 1) {
           gotoNthHit(nHit-1);
           }
       else {
           if (nHit == "") {
              //*** take care of unusual case, search, then click on main link, then click on Prev, so instead just message "you are at First Link", it also takes you there
              gotoHit("hit1")
              }
           nHit = 1
           alert("You are at the first Hit")
           }
       }
    else {

       alert("Search Result Hit Navigation only works when viewing a single transcript and Hits are highlighted.");
       }
    }   //*** end function


function firstHit() {
    //alert("hitnav.js: function prevHit, nHit="+nHit+", maxHits="+maxHits+".")
    if (HitsMarked()) {
       if (nHit > 0) {
           gotoNthHit(1);
           }
       }
    else {
       alert("Search Result Hit Navigation only works when viewing a single transcript and Hits are highlighted.");
       }
    }    //*** end function


function lastHit() {
      //alert("Start lastHit")
      //*** note: this function has to take care of many different results from search engine
      n = maxHits
      nn = String(window.location).replace(/\#.*$/, "") + "#hit"+n;
      hitname = "hit"+maxHits
      //alert("hitnav.js: function lastHit, nHit="+nHit+", maxHits="+maxHits+", nn="+nn+", hitname="+hitname)

      //*** sometime maxhits is off by 1, code below checks anchors array for maxhit anchor, if not found, subtract 1 from it
      var link;
      var links = document.getElementsByTagName('a');
      found_hit = "N"
      found_last_hit = "N"
      for (var i = 0; i < links.length; i++) {
          link = links[i];
          if (link.name == "hit_last") {
             //alert("Found hit_last="+hitname+", i="+i+", maxhits="+maxHits)
             found_last_hit = "Y"
             }  // end if
          if (link.name == "hitname") {
             //alert("Found hitname="+hitname+", i="+i+", maxhits="+maxHits)
             found_hit = "Y"
             }  // end if
         }  // end for loop


      if (found_last_hit  == "Y") {
         hitname = "last_hit"
         //alert("found last_hit, starting additional code")

         var a = findLink(document.anchors, "hit_last");
         if (a == null) return;
         if (a.length > 1) return;

         if (a.y) {
             // Netscape
             window.scrollTo(0, a.y);
             }
         else {
             // IE
             window.scrollTo(0, a.offsetTop - a.offsetHeight + a.offsetParent.offsetTop);
             }

         var s = document.body.createTextRange();
         if (s != null) {
             s.moveToElementText(a);
             s.moveEnd("word");
             s.select();
             s.scrollIntoView();
             }
         }
      else if (found_hit == "N") {
         n = maxHits - 1
         if (n < 1) {
            hitname = "hit1"
            }
         hitname = "hit"+n
         //alert("subtract 1 from hitname")
         }
      else {
         n = maxHits
         hitname = "hit"+n
         //alert("use maxHits for hitname")
         }
      //n = maxHits - 1
      if (hitname == "hit0") {
         hitname = "hit1"
         }
      //alert("going to hitname="+hitname)
      gotoHit(hitname);

    }    //*** end function

