//--------------------------------------------------------------------
//	doc.js
//--------------------------------------------------------------------


//--------------------------------------------------------------------
//	Definitions
//--------------------------------------------------------------------

    var DETAIL_FRAME_OFFSET = 300;      // Pixels to subtract from browser height for detail frame height.
    var DETAIL_FRAME_MIN    = 100;      // Minimum detail frame height, to avoid trouble.


//--------------------------------------------------
//	Global variables
//--------------------------------------------------

    var giCurrentHit  = 0;            // Current hit number for next/prev navigation. Hits themselves are 1-n; start here with 0.
    var giBestHitNum  = -1;           // Best hit number, < 0 if none. Updated from xsl file scripts.
    var gsNumHits     = "0";          // Number of hits. Updated from xsl file scripts. 1-n.
    var gsShowLoadFullDocument = "0";  // Whether to show link to Load full document. 0=hide 1=show
    var msgDetailOf = " of ";


//--------------------------------------------------
//	Functions
//--------------------------------------------------


//--------------------------------------------------
//	ResizeDetail
//
//	Purpose: Resize detail frame based on height of browser.
//	Inputs:
//		none
//	Returns:
//    none
//
//  Resize detail frame based on height of browser.
//    Use the browser client height, and subtract the offset from the top,
//      and a little for below to make it look good.
//    Check for a minimum value, just to avoid trouble.
//    Check that frame and div exist, in case the page didn't render.
//
//    ASSUME: Use a fixed offset value from the top of the screen, defined above.
//            Should calc position, but involves walking the dom tree, and wasn't worth it.
//
//  Call this inline from the bottom of the html.
//    This makes it occur quick enough to avoid seeing the original size and the resize.
//    Calling this from the onload event shows both in sequence.
//
//--------------------------------------------------
function ResizeDetail()
{
    var elFrame = document.getElementById("awframe");
    var lHeight;

    if (elFrame)
    {
      lHeight = document.body.clientHeight - DETAIL_FRAME_OFFSET;
      lHeight = Math.max(lHeight, DETAIL_FRAME_MIN);          // Minimum height.

      elFrame.style.height = lHeight;
    }
}


//--------------------------------------------------
//	NextHit, PrevHit
//
//	Purpose: Scroll document detail to next/prev hit.
//	Inputs:
//		none
//  Globals:
//    giCurrentHit        - Current (last-navigated) hit number. 1-n
//    gsNumHits
//	Returns:
//    none
//
//  Loop while not at the end of the hits.
//    Try to navigate to next/prev hit.
//    If hit is found, 
//      exit. 
//      (bNavHit updates the current hit number if found.)
//
//  This handles skipping over hits that are not rendered for some reason.
//
//--------------------------------------------------
function NextHit()
{
    var iHit = giCurrentHit;

    while (iHit < gsNumHits)
    {
      iHit++;
      if (bNavHit(iHit))
      {
        break;
      }
    }
}


function PrevHit()
{
    var iHit = giCurrentHit;

    while (iHit > 1)
    {
      --iHit;
      if (bNavHit(iHit))
      {
        break;
      }
    }
}


//--------------------------------------------------
//	BestHit
//
//	Purpose: Navigate to best hit.
//	Inputs:
//    none
//  Globals:
//    giBestHitNum    - Best Hit number, set by xsl.
//	Returns:
//    none
//
//  Check if value is > 0, to make sure it existed.
//    (NavHit would validate, but it's clean to check here).
//  This uses the global, now that it's defined for auto-navigate script.
//  Previously was just a wrapper around NavHit(), just for the url display,
//    since previously all callers knew the hit number (auto-navigate doesn't).
//
//--------------------------------------------------
function goToBestHit()
{
    if (giBestHitNum > 0)
    {
      bNavHit(giBestHitNum);
    }
}


//--------------------------------------------------
//	bNavHit
//
//	Purpose: Navigate to given hit: update hit number and text, scroll display.
//	Inputs:
//		iHitNum             - Hit number to navigate.
//  Globals:
//    msgDetailOf         - Localized string for 'of'.
//  Outputs:
//    giCurrentHit        - Updated with new hit number, if found.
//	Returns:
//    Whether hit was found, true/false.
//
//  Look for an element named Hit<n>, where <n> = hit number.
//  If found,
//    Update hit number, navigation arrows, and text display.
//    Scroll hit into view. 
//    Set current hit number for navigation.
//
//--------------------------------------------------
function bNavHit(iHitNum)
{
    var bResult = false;
    var elDoc, elHit, elHitScroll;

    if (document.frames)                            // IE
    {
      elDoc = document.frames("awframe").document;
    }
    else if (document.getElementById("awframe"))   // NS
    {
      elDoc = document.getElementById("awframe").contentDocument;
    }

    elHit = elDoc.getElementById("hit" + iHitNum);
    if (elHit)
    {
      UpdateHitText(iHitNum);     // Text.

      /*
      Netscape 6.2 does not natively support dynamic scrolling.  Ignore this
      feature except when using a browser that explicitly supports it.
	  Also, now a separate empty element due to Netscape not scrolling hidden elements.
      Check for element and feature. If hit is unreachable (e.g. in a select box option),
        there won't be an element, even though we have the hit text above.
      */
	  elHitScroll = elDoc.getElementById("hita" + iHitNum);
      if (elHitScroll && elHitScroll.scrollIntoView) {
        elHitScroll.scrollIntoView();     // Scroll.
      }
      giCurrentHit = iHitNum;     // Set current number.
      bResult = true;
    }

    return bResult;
}


//--------------------------------------------------
//	CheckHitKey
//
//	Purpose: Check keypress on hit, and run ClickHit if Enter key.
//	Inputs:
//    iHitNum               - Hit number pressed. ASSUME: Caller passes a number, not a string, for current hit math.
//    e                     - Keypress event that called this. Passed in here because the event object is down in the iframe.
//  Globals:
//    none
//  Outputs:
//    e.returnValue     - If enter key, set false to cancel the event.
//	Returns:
//    none
//
//  Called by hit span's onkeypress event.
//  If enter key,
//    Run ClickHit() function.
//    Cancel the event, to cancel key on the span (avoids a bell); 
//      and to cancel navigating to a containing link in the document, if any.
//
//  The hit markup was changed from a link to a span, to avoid conflicting with a hit within a document's link.
//  This required replicating the link's behavior: cursor hand pointer, tab stop, enter key.
//
//--------------------------------------------------
function CheckHitKey(iHitNum, e)
{
    if (e.keyCode == 13)
    {
      ClickHit(iHitNum);

      e.returnValue = false;
    }
}


//--------------------------------------------------
//	ClickHit
//
//	Purpose: Handle clicking a hit text or xml document: hit text.
//	Inputs:
//    iHitNum           - Hit number clicked. ASSUME: Caller passes a number, not a string, for current hit math.
//  Globals:
//    none
//  Outputs:
//    parent.giCurrentHit         - Updated with new hit number, if found.
//	Returns:
//    false, to cancel caller navigating to any enclosing link. 
//      Calling html must also 'return' the call to this function.
//
//  Look for an element named Hit<n>, where <n> = hit number.
//  If found,
//    Update hit text.
//
//--------------------------------------------------
function ClickHit(iHitNum)
{
    var elDoc, elHit;
    if (document.frames)                            // IE
    {
      elDoc = document.frames("awframe").document;
    }
    else if (document.getElementById("awframe"))   // NS
    {
      elDoc = document.getElementById("awframe").contentDocument;
    }
    elHit = elDoc.getElementById("hit" + iHitNum);
    if (elHit)
    {
      UpdateHitText(iHitNum);

    // Update globals with current numbers.
      giCurrentHit  = iHitNum;
    }

    return false;
}


//--------------------------------------------------
//	UpdateHitText
//
//	Purpose: Update hit text and navigation arrows in the pane.
//	Inputs:
//	  iHitNum             - Hit number to update, 1-n.
//  Globals:
//    msgDetailOf         - Localized string for 'of'.
//    gsNumHits           - Total number of hits.
//	Returns:
//    none.
//
//  Look for an element named Hit<n>, where <n> = hit number.
//  If found,
//    Update hit number and text display.
//    Update the navigation arrows whether at first hit, last hit, or between.
//      Needed to do this with independent elements that we show/hide.
//      Just updating the html of the link to spaces still showed the 'empty' link.
//    Include the trailing 'of', since we omit it when entering the document.
//
//--------------------------------------------------
function UpdateHitText(iHitNum)
{
    var elDoc, elHit;
    var sText;

    if (document.frames)                            // IE
    {
      elDoc = document.frames("awframe").document;
    }
    else if (document.getElementById("awframe"))   // NS
    {
      elDoc = document.getElementById("awframe").contentDocument;
    }

    elHit = elDoc.getElementById("hit" + iHitNum);
    if (elHit)
    {
      sText = iHitNum + " " + msgDetailOf + " ";

      if (gsNumHits)      // Check, in case xsl didn't render.
      {
        sText += gsNumHits;
      }

      document.getElementById("spanHitNum").innerHTML = sText;
      document.getElementById("spanHitText").innerHTML = elHit.innerHTML;

    // Navigation arrows.
      UpdateHitNavArrows(iHitNum);
    }
}


//--------------------------------------------------
//	UpdateHitNavArrows
//
//	Purpose: Update hit navigation arrows in the pane.
//	Inputs:
//	  iHitNum             - Hit number to update, 1-n.
//  Globals:
//    gsNumHits           - Total number of hits.
//	Returns:
//    none.
//
//  Update the navigation arrows whether at first hit, last hit, or between.
//      Needed to do this with independent elements that we show/hide.
//      Just updating the html of the link to spaces still showed the 'empty' link.
//
//  ASSUME: Caller verified if a hit exists (to update this only if nav occurred), or doesn't care (init).
//
//--------------------------------------------------
function UpdateHitNavArrows(iHitNum)
{
    var iNumHits = -1;                  // Flag value to know if we have this number is available.
    var bShowPrev = true;               // Navigation arrows. Default to showing both, just to be safe.
    var bShowNext = true;

    if (gsNumHits)      // Check, in case xsl didn't render.
    {
        iNumHits = parseInt(gsNumHits, 10);         // Just for the math below.
    }

    if (iHitNum == 1)
    {
        bShowPrev = false;
    }

    if ((iNumHits >= 0) && (iHitNum >= iNumHits))     // Show if count is unavailable, or if not at the last hit.
    {
        bShowNext = false;
    }

    document.getElementById("spanHitNavPrev").style.display = (bShowPrev) ? "inline" : "none";
    document.getElementById("spanHitNavPrevHide").style.display = (bShowPrev) ? "none" : "inline";

    document.getElementById("spanHitNavNext").style.display = (bShowNext) ? "inline" : "none";
    document.getElementById("spanHitNavNextHide").style.display = (bShowNext) ? "none" : "inline";
}


//--------------------------------------------------
//	InitHitNav
//
//	Purpose: Initialize the hit nav bar display.
//	Inputs:
//		none
//  Globals:
//		gsNumHits           - Number of hits
//    giBestHitNum        - Best Hit number, set by xsl.
//	Returns:
//    none.
//
//  Initialize hit number display:
//  Centered number of hits
//    Pad around it so the arrows don't jump on the first click.
//    i.e., change the former << 12 >> to <<   12   >>
//  Update hit nav arrows.
//  Hide best hit link if not available.
//  Display the nav bar text (initially hidden until this text settles).
//  If no hits, display the no-hit bar.
//    The text in this bar now indicates there are hits in the document, just not available here.
//    ASSUME: This text assumes if no hits in our count here, 
//      the document really has them, but something failed in the transformation.
//
//  Called by detail view to sync when value is available.
//  This was all formerly rendered in the html from xsl,
//    but the separate iframe makes this data unavailable
//    until after the detail frame loads.
//
//--------------------------------------------------
function InitHitNav(){
 if (parseInt(gsNumHits, 10) > 0){
    UpdateHitText(1);
 if (giBestHitNum == -1)
      {
        document.getElementById("spanBestHit").style.display = "none";
      }    
 }else{
    document.getElementById("spanHitNavPrev").style.display = "none";
    document.getElementById("spanHitNavPrevHide").style.display = "inline";

    document.getElementById("spanHitNavNext").style.display = "none";
    document.getElementById("spanHitNavNextHide").style.display = "inline";
    
    document.getElementById("spanBestHit").style.display = "none";
 }
}

function InitHitNav1()
{
    var sText;

  // If hits, display nav bar text.
    if (parseInt(gsNumHits, 10) > 0)
    {
      sText = "&#160;&#160;&#160;&#160;" + gsNumHits + "&#160;&#160;&#160;";
      document.getElementById("spanHitNum").innerHTML = sText;

    // Navigation arrows.
      UpdateHitNavArrows(1);                // Update as if we're on the first hit. Best hit auto-jump will follow as needed.

    // If no best hit, hide the link.
      /*if (giBestHitNum == -1)
      {
        document.getElementById("spanBestHit").style.display = "none";
      }*/

    // Display the nav bar.
      document.getElementById("spanHitNavBar").style.display = "inline";
    }


  // If no hits, display the no-hit nav bar text.
    /*else
    {
      document.getElementById("spanNoHitNavBar").style.display = "inline";
      document.getElementById("spanBestHit").style.display = "none";
    }*/

    //if (parseInt(gsShowLoadFullDocument, 10) == 1)
    //{
    //  document.getElementById("spanLoadFullDocument").style.display = "inline";
    //}
}
