/**********************************************************************
 * Cirkadia.js  iNavigate v2.2 Free Edition                        May 18 2000
 * 
 * Explorer style dynamic menu structure for MS IE 4.0+
 * Error and Logging Console
 * 
 * The approach is designed to degrade gracefully on other and older 
 * browsers and to be fully search engine compatible.
 * 
 * You may use this code on public or private web sites only if this
 * copyright statement appears intact and you publicly display a link 
 * to http://www.cirkadia.com.
 * 
 * This code is provided "as is" without warranty of any kind.
 * Cirkadia further disclaims all implied warranties including fitness 
 * for any particular purpose. The entire risk arising out of the use 
 * or performace of this code remains with you.
 * 
 * Contact productinfo@cirkadia.com for any other usage.
 * 
 * Copyright ?2000  Cirkadia.  All rights reserved.
 * 
 * http://www.cirkadia.com
 * 
 **********************************************************************/ 

window.onerror=logError;

browserCapable = (document.all);


/**********************************************************************
 * Entry Points
 */ 

function beforeLoad()
{
  if (browserCapable)
  {
    menus_BeforeLoad();
  }
}


function afterLoad()
{
  if (browserCapable)
  {
    menus_AfterLoad();
  }
}


/**********************************************************************
 * Dynamic Menus
 */ 

imgBack  = new Image();        imgBack.src  = "./img/background.gif";
imgBlank = new Image();        imgBlank.src = "./img/blank.gif";

imgPlus  = new Image(22, 15);  imgPlus.src  = "./img/plus.gif";
imgMinus = new Image(22, 15);  imgMinus.src = "./img/minus.gif";

imgLine  = new Image(12, 3);   imgLine.src  = "./img/line.gif";
imgDot   = new Image();        imgDot.src   = "./img/dot.gif";

var thisLink;

var parentLink;

function menus_BeforeLoad()
{
  if (browserCapable)
  {
    //****************************************
    //establish the target link

    var href = typeof parentPage != 'undefined' ? parentPage.length > 0 ? filename(parentPage) 
                                                : ''                    : '' ;
    parentLink = href.length != 0

    if (!parentLink) href = filename(window.location.pathname);


    //****************************************
    //tile vertical dotted lines

    if (document.all.menutable)
    {
      for (i=0; i<document.all.menutable.length; i++)
      {
        table = document.all.menutable[i];
    
        if (table) table.background = imgDot.src;
      }
    }

    //****************************************
    //insert horizontal dotted lines

    if (document.all.menuline)
    {
      for (i=0; i<document.all.menuline.length; i++)
      {
        line = document.all.menuline[i];
     
        if (line) line.src = imgLine.src;
      }
    }

    //****************************************
    //look for the target link in level 1

    if (document.all.menuitem1)
    {
      if (document.all.menuitem1.length)
      {
        for (i=0; i<document.all.menuitem1.length; i++)
        {
          link = document.all.menuitem1[i];

          if (link && link.pathname)
          {
            if (href == filename(link.pathname))
            {
              thisLink = link;
              break;
            }
          }
        }
      }
      else
      {
        link = document.all("menuitem1");

        if (link && link.pathname)
        {
          if (href == filename(link.pathname))
          {
            thisLink = link;
          }
        }
      }
    }

    //****************************************
    //look for the target link in level 2

    if (!thisLink)
    {
      if (document.all.menuitem2)
      {
        for (i=0; i<document.all.menuitem2.length; i++)
        {
          link = document.all.menuitem2[i];
    
          if (link && link.pathname)
          {
            if (href == filename(link.pathname))
            {
              thisLink = link;
              break;
            }
          }
        }
      }
    }

    //****************************************
    //if level 2s previously open or contains target link
    //then open otherwise close

    if (document.all.menu1)
    {
      for (i=0; i<document.all.menu1.length; i++)
      {
        source = document.all.menu1[i];

        if (source)
        {
          source.custom = i;          

          submenu = source.all("menu2");

          if (submenu)
          {
            handle = source.all(0);
  
            handle.style.cursor = "hand";

            openMenu = thisLink ? source.contains(thisLink) 
                                : false;

            if (document.cookie.indexOf("menu" + i + "=1") != -1) openMenu = true;

            if (openMenu)
            {
              document.cookie = "menu" + source.custom + "=1";

              handle.src = imgMinus.src;
              submenu.style.display = "";
            }
            else
            {
              handle.src = imgPlus.src;
              submenu.style.display = "none";
            }
          }
        }
      }
    }
  }
}


function menus_AfterLoad()
{
  //****************************************
  //highlight the current menu item
  //note we do this stuff here for IE4.0 compatibility, in IE5.0 we can do it earlier

  if (browserCapable)
  {
    if (thisLink) 
    {
      //insert pointer graphic to current link

      thisLink.insertAdjacentHTML('AfterEnd', 
                                  (thisLink.id == "menuitem1" ? "<img src=./img/pagetabl.gif alt='Current Page Tab' class=thistabl border=0>"
                                                              : "<img src=./img/pagetabl2.gif alt='Current Page Tab' class=thistabl border=0>"));

      //if the page matches the menu link and is not a child page then kill the link

      if (!parentLink) thisLink.outerHTML = "<em class=thispage>" + thisLink.innerHTML + "</em>";
    }
  }
}


function toggleMenu1(source)
{
  //****************************************
  //toggle menu between open and closed states

  if (browserCapable)
  {
    if (source)
    {
      submenu = source.all("menu2");

      if (submenu)
      {    
        handle = source.all(0);

        if (submenu.style.display == "")
        {
          document.cookie = "menu" + source.custom + "=0"

          handle.src = imgPlus.src;
          submenu.style.display = "none";
        }
        else
        {
          document.cookie = "menu" + source.custom + "=1"

          handle.src = imgMinus.src;
          submenu.style.display = "";
        }
      }
    }
  }
}


function filename(path)
{
  //****************************************
  //extract terminal filename from pathname

  href = path.substr(path.lastIndexOf('\\') + 1);
  href = href.substr(href.lastIndexOf('/') + 1);
  href = unescape(href.toLowerCase());
  if (href.length == 0) href = "index.html"
  return href;
}


/**********************************************************************
 * Error Handler and Logger
 */ 

var firstMessage = true;
var firstError = true;

function logError(message, url, line)
{
  if (firstError)
  {
    firstError = false;

    log('<b>JavaScript Errors</b><hr>', false);

    log('browser: ' + navigator.userAgent)
    log('page: ' + url + '<hr>', false);
  }

  log('line: ' + line);
  log(message + '<hr>', false);
  return false;
}


function log(message)
{
  var JSConsole = window.open("", "CirkadiaJSConsole", "height=400,width=300,scrollbars,resizable");

  JSConsole.focus();

  var d = JSConsole.document;

  if (firstMessage)
  {
    firstMessage = false;

    d.open();
    d.writeln('<title>Cirkadia JavaScript Console</title>');
  }

  d.writeln(message + (arguments.length == 1 ? '<br>' : ''));

  JSConsole.scrollBy(0, 1000);
}

