var m_LastHitId = -1;
var m_nHitId = 0;
var m_oPrevHit = null;
var m_oTextRange = null;
var m_oHitIds = null;

function gotoTop()
{
    m_nHitId = 0;
    if (m_oPrevHit != null)
    {
        m_oPrevHit.className = "cssHit";
        m_oPrevHit = null;
    }
    
    if (m_oTextRange != null) // Support in IE
    {
     m_oTextRange = null;
     document.selection.empty();
    }

    var oCtlNextHit = document.getElementById("cmdNextHit");
    oCtlNextHit.disabled = false;
    oCtlNextHit.className = "whit-link";

    scrollTo(0,0);
}

function findLink(links, name)
{
    for (i = 0; i < links.length; ++i)
    {
        if (links[i])
        {
            if (links[i].id == name)
                return links[i];
        }
    }
    
    return null;
}

function IsNext ()
{
     var bReturn = false;
 
     if (m_nHitId >= m_LastHitId)
     {
         var nChapterId = "";
         var oCtlNextHit = document.getElementById("nextChapter");
         if (oCtlNextHit != null)
            nChapterId = oCtlNextHit.innerHTML;

         if (nChapterId.length > 0)
            bReturn = true;
     }
     else
     {
         bReturn = true;
     }
         
    return bReturn;
}

function IsPrev ()
{
     var bReturn = false;
 
     if (m_nHitId <= 1)
     {
         var nChapterId = "";
         var oCtlNextHit = document.getElementById("prevChapter");
         if (oCtlNextHit != null)
            nChapterId = oCtlNextHit.innerHTML;

         if (nChapterId.length > 0)
            bReturn = true;
     }
     else
     {
         bReturn = true;
     }
         
    return bReturn;
}

function gotoHit(pCtl)
{ 
    if (m_oPrevHit != null)
    {
        m_oPrevHit.className = "cssHit";
    }
        
    m_oPrevHit = pCtl;

    var yPos = findPosY(pCtl);
    window.scrollTo(0, yPos);
        
    pCtl.className = "cssHitSelected";

//    m_oTextRange = document.body.createTextRange();
//    if (m_oTextRange != null) // Support in IE
//    {
//        m_oTextRange.moveToElementText(pCtl);
//        m_oTextRange.moveEnd("word");
//        m_oTextRange.select();
//        m_oTextRange.scrollIntoView();
//    }

    var oCtlNextHit = document.getElementById("cmdNextHit");
    var oCtlPrevHit = document.getElementById("cmdPrevHit");
    
      if (IsNext())
      {
          oCtlNextHit.disabled = false;
          oCtlNextHit.className = "whit-link";
      }
      else
      {
          oCtlNextHit.disabled = true;
          oCtlNextHit.className = "whit-link2";
      }
    
     if (IsPrev())
     {
         oCtlPrevHit.disabled = false;
         oCtlPrevHit.className = "whit-link";
     }
     else
     {
         oCtlPrevHit.disabled = true;
         oCtlPrevHit.className = "whit-link2";
     }
}

function gotoNthHit(nId)
{
 if (nId > 0 && nId <= m_LastHitId)
 {
  m_nHitId = nId;
  var oCtl = m_oHitIds[nId - 1];
  
  gotoHit(oCtl);
 }
}
 
function nextHit() {
    if (m_nHitId < m_LastHitId)
    {
      gotoNthHit(m_nHitId + 1);
    }
    else
    {
     var nChapterId = "";
     var oCtlNextHit = document.getElementById("nextChapter");
     if (oCtlNextHit != null)
        nChapterId = oCtlNextHit.innerHTML;
     
     if (nChapterId.length > 0)
     {
         var oCtlUrl = document.getElementById("parmurl");
         var sUrl = oCtlUrl.innerHTML;

         sUrl = sUrl.replace(/&amp;/g, "&");
         sUrl += "&chapter=" + nChapterId + "&d=N";;
         
         document.location.href = sUrl;
     }
    }
}

function prevHit() {
    if (m_nHitId > 1)
    {
     gotoNthHit (m_nHitId - 1);
    }
    else
    {
     var nChapterId = "";
     var oCtlPrevHit = document.getElementById("prevChapter");
     if (oCtlPrevHit != null)
        nChapterId = oCtlPrevHit.innerHTML;
     
     if (nChapterId.length > 0)
     {
         var oCtlUrl = document.getElementById("parmurl");
         var sUrl = oCtlUrl.innerHTML;
         sUrl = sUrl.replace(/&amp;/g, "&");
         sUrl += "&chapter=" + nChapterId + "&d=P";
         
         document.location.href = sUrl;
     }
    }
}

function GotoFirstHit() {
    gotoNthHit(1);
}

function GotoLastHit() {
    gotoNthHit(m_LastHitId);
}

function HideBack2Top ()
{
 var oCtl = document.getElementById('bttl');
 if (oCtl != null)
 {
  if (m_b2bTimer != null)
     clearTimeout(m_b2bTimer);

  oCtl.style.display = "none";
 }
}

function InitHit ()
{
  if (m_LastHitId == -1)
  {
    m_LastHitId = 0;
    m_nHitId = 0;  
    var links = document.getElementsByTagName("span");
    m_oHitIds = new Array();
    
    for (i = 0; i < links.length; ++i)
    {
        if (links[i])
        {
            if (links[i].id.indexOf("hit") == 0)
            {
             m_oHitIds[m_LastHitId] = links[i];
             
             ++m_LastHitId;
            }
        }
    }
    
    if (m_LastHitId > 0)
    {
     HideBack2Top ();
     
     var nDebugFlag = 0;
     var oCtlDebugFlag = document.getElementById("DebugFlag");
     if (oCtlDebugFlag != null)
        nDebugFlag = oCtlDebugFlag.innerHTML;

     if (nDebugFlag > 0)
     {
         var oCtlHitCount = document.getElementById("CountHIT");
         if (oCtlHitCount != null)
         {
           var sHitCount = "(" + m_LastHitId + ")";
           oCtlHitCount.innerHTML = sHitCount;
         }
     }

    if (m_LastHitId == 1)
    {
         var oCtlPrevHit = document.getElementById("cmdPrevHit");
         oCtlPrevHit.disabled = true;
         oCtlPrevHit.className = "whit-link2";
    }

//     var sDirection = GetQueryParameter("d");
     
//     if (sDirection == "P")
//        GotoLastHit();
//     else
//        GotoFirstHit();
    }
    else
    {
     var oCtl = document.getElementById('divFloatMenu');
     if (oCtl != null)
         oCtl.style.display = "none";
    }
  }
}


/* Script by: www.jtricks.com */
var floatingMenuId = 'divFloatMenu';
var floatingMenu;

function DoFloatTopDiv ()
{
    floatingMenu =
        {
            targetX: -214,
            targetY: 500,

            hasInner: typeof(window.innerWidth) == 'number',
            hasElement: document.documentElement
                && document.documentElement.clientWidth,

            menu:
                document.getElementById
                ? document.getElementById(floatingMenuId)
                : document.all
                  ? document.all[floatingMenuId]
                  : document.layers[floatingMenuId]
        };


    floatingMenu.move = function ()
    {
        if (document.layers)
        {
            floatingMenu.menu.left = floatingMenu.nextX;
            floatingMenu.menu.top = floatingMenu.nextY;
        }
        else
        {
            floatingMenu.menu.style.left = floatingMenu.nextX + 'px';
            floatingMenu.menu.style.top = floatingMenu.nextY + 'px';
        }
    }

    floatingMenu.computeShifts = function ()
    {
        var de = document.documentElement;

        floatingMenu.shiftX =
            floatingMenu.hasInner
            ? pageXOffset
            : floatingMenu.hasElement
              ? de.scrollLeft
              : document.body.scrollLeft;
        if (floatingMenu.targetX < 0)
        {
            if (floatingMenu.hasElement && floatingMenu.hasInner)
            {
                // Handle Opera 8 problems
                floatingMenu.shiftX +=
                    de.clientWidth > window.innerWidth
                    ? window.innerWidth
                    : de.clientWidth
            }
            else
            {
                floatingMenu.shiftX +=
                    floatingMenu.hasElement
                    ? de.clientWidth
                    : floatingMenu.hasInner
                      ? window.innerWidth
                      : document.body.clientWidth;
            }
        }

        floatingMenu.shiftY = 
            floatingMenu.hasInner
            ? pageYOffset
            : floatingMenu.hasElement
              ? de.scrollTop
              : document.body.scrollTop;
        if (floatingMenu.targetY < 0)
        {
            if (floatingMenu.hasElement && floatingMenu.hasInner)
            {
                // Handle Opera 8 problems
                floatingMenu.shiftY +=
                    de.clientHeight > window.innerHeight
                    ? window.innerHeight
                    : de.clientHeight
            }
            else
            {
                floatingMenu.shiftY +=
                    floatingMenu.hasElement
                    ? document.documentElement.clientHeight
                    : floatingMenu.hasInner
                      ? window.innerHeight
                      : document.body.clientHeight;
            }
        }
    }

    floatingMenu.doFloat = function()
    {
        var stepX, stepY;

        floatingMenu.computeShifts();

        stepX = (floatingMenu.shiftX + 
            floatingMenu.targetX - floatingMenu.nextX) * .07;
        if (Math.abs(stepX) < .5)
        {
            stepX = floatingMenu.shiftX +
                floatingMenu.targetX - floatingMenu.nextX;
        }

        stepY = (floatingMenu.shiftY + 
            floatingMenu.targetY - floatingMenu.nextY) * .07;
        if (Math.abs(stepY) < .5)
        {
            stepY = floatingMenu.shiftY + 
                floatingMenu.targetY - floatingMenu.nextY;
        }

        if (Math.abs(stepX) > 0 ||
            Math.abs(stepY) > 0)
        {
            floatingMenu.nextX += stepX;
            floatingMenu.nextY += stepY;
            floatingMenu.move();
        }

        setTimeout('floatingMenu.doFloat()', 10);
    };

    // addEvent designed by Aaron Moore
    floatingMenu.addEvent = function(element, listener, handler)
    {
        if(typeof element[listener] != 'function' || 
           typeof element[listener + '_num'] == 'undefined')
        {
            element[listener + '_num'] = 0;
            if (typeof element[listener] == 'function')
            {
                element[listener + 0] = element[listener];
                element[listener + '_num']++;
            }
            element[listener] = function(e)
            {
                var r = true;
                e = (e) ? e : window.event;
                for(var i = element[listener + '_num'] -1; i >= 0; i--)
                {
                    if(element[listener + i](e) == false)
                        r = false;
                }
                return r;
            }
        }

        //if handler is not already stored, assign it
        for(var i = 0; i < element[listener + '_num']; i++)
            if(element[listener + i] == handler)
                return;
        element[listener + element[listener + '_num']] = handler;
        element[listener + '_num']++;
    };

    floatingMenu.init = function()
    {
        floatingMenu.initSecondary();
        floatingMenu.doFloat();
    };

    // Some browsers init scrollbars only after
    // full document load.
    floatingMenu.initSecondary = function()
    {
        floatingMenu.computeShifts();
        floatingMenu.nextX = floatingMenu.shiftX +
            floatingMenu.targetX;
        floatingMenu.nextY = floatingMenu.shiftY +
            floatingMenu.targetY;
        floatingMenu.move();
    }



    floatingMenu.init();
}

function DoClientInit()
{
 var oFixedMenuB = ReadCookie ("FIXEDMENU_B");
 if (oFixedMenuB != null)
     return;
    
 var oCtl = document.getElementById("imgGetAdobeReader");
 if (oCtl != null)
 {
//     var pos = object_pos (oCtl);
     
//     var xPos = pos.left;
//     var yPos = pos.top;

     var xPos =  findPosX(oCtl);
     var yPos = findPosY(oCtl);
     
     var nWidth, nHeight;
     var nScreenWidth = screen.width;
     var nScreenHeight = screen.height;
     
    if (window.innerWidth)
    {
        nWidth = window.innerWidth;
        nHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
        nWidth = document.documentElement.clientWidth;
        nHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        nWidth = document.body.clientWidth;
        nHeight = document.body.clientHeight;
    }
    
    var nRight, nBottom;
    
//    alert ('(W,H)' + nWidth + ' ' + nHeight);
//    alert ('(x,y)' + xPos + ' ' + yPos);
    
    
    nRight = nWidth - xPos - 112 - 38;
    nBottom = nHeight - yPos - 33 - 90;

    if (nRight < 0)
        nRight = 0;
    
    if (nBottom < 0)
        nBottom = 0;
    
     SetCookie ("FIXEDMENU_R", nRight);
     SetCookie ("FIXEDMENU_B", nBottom);
 }
   
}

function GetQueryParameter(piKey)
{
 var sUrl = window.document.location.href;
 var nPos, nLen;
 var sId = "";
 var sKey = "&"+piKey +"=";

 nLen = sUrl.length;
 nPos = sUrl.indexOf(sKey)
 if (nPos > 0)
    sId = sUrl.substr (nPos+sKey.length,nLen-nPos-sKey.length);

 return sId;
}
