/*
   Copyright (c) Cristalink Limited, 2004-2006. All rights reserved.
*/

var hhsplit = document.getElementById("hhsplit");
hhsplit.onmousedown     = hhsplitBeginResize;
hhsplit.onmouseup       = hhsplitEndResize;
hhsplit.onlosecapture   = hhsplitEndResize;
hhsplit.onmousemove     = hhsplitOnResize;
//hhsplit.style.cursor    = "col-resize";
hhsplit.setAttribute("UNSELECTABLE", true);
var hhsplit_isResize = false;
var hhtoc = document.getElementById("hhtoc");
var hhsplit_nWidth = 0;

function hhsplitBeginResize(e)
{
   if(!e) e = event;
   if(!e) return;
   hhsplit_isResize = true;
   hhsplit_nClientX = e.clientX;
   hhsplit_nWidth = parseInt(hhtoc.style.width);
   hhsplit.setCapture();
}

function hhsplitEndResize()
{
   hhsplit_isResize = false;
   hhsplit.releaseCapture();
}

function hhsplitOnResize(e)
{
   if(!e) e = event;
   if(!e || !hhsplit_isResize) return;
   var nDelta = e.clientX - hhsplit_nClientX;
   var nWidth = hhsplit_nWidth + nDelta;
   if( nWidth >= 50
      && nWidth <= 1000 )
   {
      hhtoc.style.width = nWidth + "px";
   }
}

/* Finds a parent row for a given id. level<0 finds the nearest parent. */ 
function hhtocGetParentId( id, level )
{
   var lpos = id.lastIndexOf( "-" );
   if( lpos < 1 ) return null;
   if( level < 0 ) return id.substr( 0, lpos );

   level++;
   var pos = -1;
   for( var i = 0; i <= level; i++ )
   {
      pos = id.indexOf( "-", pos + 1 );
      if(   pos < 1
         || pos > lpos )
      { 
         return null;
      }
   }
   
   return id.substr( 0, pos );
}

/* Checks for a child row */
function hhtocIsChild( parentId, id, immediate )
{
   if( !id ) return false;
   
   parentId += "-";
   var len = parentId.length;
   if(   id.length <= len
      || id.substr( 0, len ) != parentId )
   { 
      return false;
   }
      
   if( immediate )
   {
      var pos = id.indexOf( "-", len + 1 );
      if( pos >= 0 ) return false;
   }
   
   return true;
}

/* Updates image for a parent row */
function hhtocUpdateImage( parentId, open )
{
   el = document.getElementById( parentId + "-img" );
   if( !el ) return;
   el.src = "/images/" + ( open ? "hhobook.gif" : "hhcbook.gif" );
}

/* Expands or toggles rows for a given parent id */
function hhtocExpandChildren( parentId, expand )
{
   /* Enumerate child rows */
   var operation = 0;
   var trs = document.getElementsByTagName( "tr" );
   for( var i = 0; i < trs.length; i++ )
   {
      var tr = trs.item( i );
      
      if( tr.style.display == "block" )
      {
         if( expand ) continue;
         if( !hhtocIsChild( parentId, tr.id, false ) ) continue;
         tr.style.display = "none";
         operation = -1;
      }
      else
      {
         if( !hhtocIsChild( parentId, tr.id, true ) ) continue;
         tr.style.display = "block";
         operation = 1;
      }
   }
   
   /* Update image */
   if( operation != 0 )
   {
      hhtocUpdateImage( parentId, operation > 0 );
   }
}

/* Toggles a book when clicked on the book's link */
function hhtocToggle( el )
{
   /* The book row to toggle */
   var parentId = hhtocGetParentId( el.id, -1 );
   if( parentId != null )
   {
      hhtocExpandChildren( parentId, false );
   }
}

/* Synchronizes TOC with the current help page */
function hhtocSync( file )
{
   var i, el, els;
   
   /* Find the link in TOC */
   file = "=" + file.toLowerCase();
   var rowId = null;
   els = document.getElementsByTagName("a");
   for( i = 0; i < els.length; i++ )
   {
      el = els.item(i);
      var href = el.getAttribute( "href", 2 );
      if(   href
         && href.toLowerCase().indexOf( file ) >= 0 )
      {
         rowId = hhtocGetParentId( el.id, -1 );
         break;
      }
   }

   if( rowId == null ) return;
   rowId += "-a";

   /* Expand parents */
   var parentId = null;
   for( var level = 0; level < 20; level++ )
   {
      var pid = hhtocGetParentId( rowId, level );
      if( pid == null ) break;
      parentId = pid;
      hhtocExpandChildren( parentId, true );
   }

   if( parentId == null ) return;
   
   /* Unselect the previous item */
   els = document.getElementsByTagName("td");
   for( i = 0; i < els.length; i++ )
   {
      el = els.item(i);
      if(   el.className
         && el.className == "hhtocsel" )
      {
         el.className = "hhtoctxt";
         break;
      }
   }
   
   /* Select the new item */
   el = document.getElementById( parentId + "-td" );
   if( el )
   {
      el.className = "hhtocsel";
      //el.scrollIntoView( true );
      //el.focus(); // affects bookmarks
   }
}

/* Synchronize TOC */
hhtocSync( g_ustrHhPage );

