
var allDocuments = 
{
    'OmixedOverview':'Platform Overview',
    'OmixedInstallGuide':'Installation',
    'OmixedAdminConsole':'The Admin Console',
    'OmixedServerAPI':'The Server API',
    'OmixedQuerySyntax':'The Query Syntax'
};

function buildNavigation( currentDocument )
{
    document.getElementById( "indexContainer" ).innerHTML = scanPageAndMakeNavigationLinks( currentDocument );
}

function scanPageAndMakeNavigationLinks( currentDocument )
{
  var documentPath = currentDocument + '.html';

  var htmlBuffer = '<DIV CLASS="listMetaHeader">All Documents</DIV>';

  // firstly we figure out how many items are in each menu cateory

  // starting with the number of top-level documents

  var nDocuments = 0;

  for( var d in allDocuments )
  {
      nDocuments++;
  }

  // anything in a H2 element is a 'topic'

  var topicNodes = document.getElementsByTagName( 'H2' );

  var nTopics = topicNodes.length;


  // now detect any 'concept' SPANs

  var childSpans = document.getElementsByTagName( "A" );

  var conceptNameList = new Array();
			
  for( var j = 0 ; j < childSpans.length; j++ )
  {
     if( childSpans[ j ].className == "concept" )
     {
         var conceptName = childSpans[ j ].getAttribute( "name" );

         conceptNameList.push( conceptName );
     }
  }

  conceptNameList.sort();

  var nConcepts = conceptNameList.length;

  // finally, any methodHeading DIVs

  var childDivs = document.getElementsByTagName( "DIV" );

  var methodNameList = new Array();

  for( var j = 0 ; j < childDivs.length; j++ )
  {
     if( childDivs[ j ].className == "methodHeading" )
     {
         var actualMethodName = childDivs[ j ].firstChild.nodeValue;

         if( ( actualMethodName == null ) || ( actualMethodName == null ) )
         {
            var lastGood = methodNameList.length > 0 ? methodNameList[ 1 ] : null;

	    if( lastGood == null )       
		alert( "First methodHeading class has no method name" );
            else
	      alert( "methodHeading class has no method name (after '" + lastGood + "')" );
         }
         else
	 {
           methodNameList.push( actualMethodName );
	 }
     }
  }

  methodNameList.sort();

  var nMethods = methodNameList.length;
  
  // now allocate heights
  
  var dimensions = getWindowSize();

  //alert( dimensions.width + "x" + dimensions.height + ": nDocuments=" + nDocuments + " nTopics=" + nTopics + " nConcepts=" + nConcepts + " nMethods=" + nMethods );

  // we allocate the total browser height proportionately

  var availableHeight = dimensions.height * 0.8;

  var nSpacers = 12;

  var totalThings = ( 3 * nDocuments ) + ( 2 * nTopics ) + ( 2 * nConcepts ) + ( 1 * nMethods ) + nSpacers;

  var spacePerThing = availableHeight / totalThings;

  var hDocuments = 3 * nDocuments * spacePerThing;
  var hTopics    = 2 * nTopics    * spacePerThing;
  var hConcepts  = 2 * nConcepts  * spacePerThing;
  var hMethods   = 1 * nMethods   * spacePerThing;
  

  // and finally, generate some HTMLs

  // 1. documents 

  htmlBuffer += '<DIV ' + makeStyle( hDocuments ) + ' ID="indexDocumentList" CLASS="list">';

  for( var d in allDocuments )
  {
      var documentUrl = d + '.html';
      var documentName = allDocuments[ d ];

      if( d == currentDocument )
      {
 	  htmlBuffer += '<B CLASS="menuItem">' + documentName + '</B>';
      }
      else
      {
	  htmlBuffer += '<A CLASS="menuItem" HREF="' + documentUrl + '">' + documentName + '</A>';
      }
  } 

  htmlBuffer += "</DIV>"; // end of 'indexDocumentList'

  htmlBuffer += '<DIV CLASS="listMetaHeader">In This Document...</DIV>';

  htmlBuffer += '<DIV ID="indexDocumentContents">';
      
  htmlBuffer += '<DIV CLASS="listHeader">Topics</DIV><DIV ' + makeStyle( hTopics ) + ' CLASS="list">';

  // 2. topics

  for( var t  = 0 ; t < topicNodes.length; t++ )
  {
    var topicName = topicNodes[ t ].firstChild.nodeValue;

    htmlBuffer += ( '<A CLASS="menuItem" HREF="' + documentPath + '#' + topicName + '">' + topicName + '</A>' );
   }

  htmlBuffer += '</DIV>';  // end of 'Topics'
			
  // 3. concepts

  if( conceptNameList.length > 0 )
  {
    htmlBuffer += '<DIV CLASS="listHeader">Concepts</DIV><DIV ' + makeStyle( hConcepts ) + ' CLASS="list">';

    for( var j = 0 ; j < conceptNameList.length; j++ )
    {
       htmlBuffer += ( '<A CLASS="menuItem" HREF="' + documentPath + '#' + conceptNameList[ j ] + '">' + conceptNameList[ j ] + '</A>' );
    }

    htmlBuffer += '</DIV>';
  }  

  // 4. methods

  // now detect any 'methodHeading' DIVs

  if( methodNameList.length > 0 )
  { 		  
      htmlBuffer += '<DIV CLASS="listHeader">Methods</DIV><DIV ' + makeStyle( hMethods ) + ' CLASS="list">';

     for( var j = 0; j < methodNameList.length; j++ )
      {	       
	  var fragmentName = getFragmentName( methodNameList[ j ] );
	       
          htmlBuffer += ( '<A CLASS="menuItem" HREF="' + documentPath + '#' + fragmentName + '">' + methodNameList[ j ] + '</A>' );
      }

      htmlBuffer += '</DIV>';
  }

  htmlBuffer += '</DIV>';   // end of  "indexDocumentContents"

  //alert( htmlBuffer );

  return htmlBuffer;
}

function makeStyle( height )
{
    return '';
    // return 'STYLE="max-height:' + height + 'px;overflow:auto"';
}

function getWindowSize() 
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
    } 
    else 
    {
	if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	} else
	{
	    if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	    {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	    }
	}
    }
    return { height:myHeight, width:myWidth };
}


function getFragmentName( actualMethodName )
{
  var name = actualMethodName.replace( /(\w+)\(\)/, "$1" );
 
  var upperCase= name.toUpperCase();

  return "method" + upperCase.substring( 0, 1 ) + name.substring( 1 );
}


function sizeNavigatorWindow()
{
    var windowSize = getWindowSize();

    document.getElementById("indexContainer").style.minHeight = (windowSize.height * 0.99 ) + "px";
    document.getElementById("indexContainer").style.height = (windowSize.height * 0.99 ) + "px";

    //document.getElementById("indexFrame").style.minHeight = (windowSize.height * 0.9 ) + "px";
    //document.getElementById("indexFrame").style.height = (windowSize.height * 0.9 ) + "px";

    //top.frames.indexFrame.style.minHeight = (windowSize.height * 0.9 ) + "px";
    //top.frames.indexFrame.style.height    = (windowSize.height * 0.9 ) + "px";

    //top.frames.indexFrame.document.getElementById("indexContainer").style.minHeight = (windowSize.height * 0.9 ) + "px";
    //top.frames.indexFrame.document.getElementById("indexContainer").style.height    = (windowSize.height * 0.9 ) + "px";
}