﻿dojo.require("dojo.fx");
dojo.require("dojo.NodeList-fx");

/*
      { { <int, 1-6, vertex number, 1 is at 1 o'clock, 2 is at 3o'clock etc>, 0 is title, vertexDef }
*/

function gorel( relurl ) {
    if ( location.pathname == '/' + relurl ) return;
    location.href = relurl;
}

// Prefix MUST be 2 chars
var hsysStruct =
    [ { prefix:"01",
        hexdef:[
          { node:0, fn:function(){ console.log('hexnode 01/0 home'); location.href='index.html'; } },
          { node:1 },
          { node:4,
            hexdef:[
              { node:0, fn:function(){ console.log('hexnode 01/4 bios'); } },
              /*{ node:3, fn:function(){console.log('hexnode 01/4/3 bios nadine'); gorel('bio-nadine.html');} },*/
              { node:3, fn:function(){console.log('hexnode 01/4/3 bios nadine'); gorel('bio-more.html');} },
              { node:4, fn:function(){console.log('hexnode 01/4/4 bios charlene'); gorel('bio-charlene.html');} },
              { node:5, fn:function(){console.log('hexnode 01/4/5 bios allison'); gorel('bio-allison.html');} }
            ]
          },
          { node:5,
            hexdef:[ 
              { node:0, fn:function(){ console.log('hexnode 01/4/5 salon'); } },
              { node:1, fn:function(){console.log('hexnode 01/5/1 salon/products'); gorel('salon-products.html'); } },
              { node:4, 
                hexdef:[
                  { node:0, fn:function(){ console.log('hexnode 01/4/5 salon/services'); } },
                  { node:3, fn:function(){console.log('hexnode 01/5/4/3 salon/services nadine'); gorel('services-more.html');} },
                  { node:4, fn:function(){console.log('hexnode 01/5/4/4 salon/services charlene'); gorel('services-charlene.html');} },
                  { node:5, fn:function(){console.log('hexnode 01/5/4/5 salon/services allison'); gorel('services-allison.html');} } 
                ]
              },
              { node:5, fn:function(){console.log('hexnode 01/5/5 salon/our work'); gorel('salon-our-work.html'); } },
              { node:6, fn:function(){console.log('hexnode 01/5/6 salon/space'); gorel('salon-space.html'); } }
            ]
          },
          //{ node:6, fn:function(){console.log('hexnode 01/6 space'); } }
        ]
      },
      { prefix:"02",
        background:false, // ignored at present
        hexdef:[
          //{ node:0, fn:function(){console.log('hexnode 02 title contact us'); }, no_mouseover:true },
          { node:0, fn:function(){console.log('hexnode 02 title contact us'); gorel('contact-us.html'); }, no_mouseover:true },
          { node:1 },
          { node:2 },
          //{ node:3 }
        ]
      }
    ];

var dbgwatch;
var visibleHexPfx = "";
var visibleTopNavPfx = "";
var lastActiveNode = "";

function substr( str, start, len ) {
    return str.substring( start, start + len ); 
}

function resetActiveNode( prefix, node ) {
    return function(){
        //console.log('Reset Active Node: ' + prefix + ',' + node.node);
        //setActiveNode( (prefix + node.node + ''), true );
    };
}


function setActiveNode( node, fromMouseOver ) {
    //return;
    console.log('Set Active Node: ' + node + ',' + lastActiveNode);
    
    /*
    var q = "";
    // Append 'act' to background-image property of stb and txt leading to and including 'node' (eg, "0145")
    for ( var i = 2; i < node.length - 1; ++i ) {
        q += '#hex-' + substr( node, 0, i ) + '-stb-' + substr( node, i, 1 ) + '_,' +
             '#hex-' + substr( node, 0, i ) + '-txt-' + substr( node, i, 1 ) + '_,';
    }
    
    console.log( "Stuff to set act: " + q );
    
    dojo.query( q ).forEach( 
        function( item, index, array ){
            var b = dojo.style( item, "backgroundImage" );
            var s = substr( b, 0, b.length - 5 ) + '-act.gif)';
            console.log( 'setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
            dojo.style( item, "backgroundImage", s ); 
         } );
    */
    
    var q = "";
    if(node.length == 4 && lastActiveNode == "" && fromMouseOver == false) {
        console.log('path A');
        q += '#hex-' + substr( node, 0, 3 ) + '-stb-' + substr( node, 3, 1 ) + '_,' +
             '#hex-' + substr( node, 0, 3 ) + '-txt-' + substr( node, 3, 1 ) + '_,';
        console.log( "Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = substr( b, 0, b.length - 5 ) + '-act.gif)';
                console.log( 'A setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
        
        var t;
        var fx;
        
        //q = '[id^=topnav-*-act_],[id^=topnav-*-act-link_],';
        //t = dojo.query( q );
        //t.style( {display:"none"} );
        
        q = '[id^=topnav-' + node + '_],[id^=topnav-' + node + '-link_],';
        q += '#topnav-' + node + '_,' +
             '#topnav-' + node + '-link_,';
        t = dojo.query( q );
        t.style( {display:""} );
        console.log('A stuff to show:');

        var newFx = t.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
        lastActiveNode = node;
    /*
    }else if(node.length == 2) {
        console.log('path AA');
        q += '#hex-' + substr( node, 0, 2 ) + '-bg_,' +
             '#hex-' + substr( node, 0, 2 ) + '-title_';
        console.log( "AA Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = substr( b, 0, b.length - 5 ) + '-act.gif)';
                console.log( 'AA setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
        
        var t;
        var fx;
        
        //q = '[id^=topnav-*-act_],[id^=topnav-*-act-link_],';
        //t = dojo.query( q );
        //t.style( {display:"none"} );
        
        q = '[id^=topnav-' + node + '_],[id^=topnav-' + node + '-link_],';
        q += '#topnav-' + node + '_,' +
             '#topnav-' + node + '-link_,';
        t = dojo.query( q );
        t.style( {display:""} );
        console.log('AA stuff to show:');

        var newFx = t.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
        lastActiveNode = node;
    */    
    }else if(node.length == 4 && lastActiveNode != node && fromMouseOver) {
        console.log('path B');
        q += '#hex-' + substr( node, 0, 3 ) + '-stb-' + substr( node, 3, 1 ) + '_,' +
             '#hex-' + substr( node, 0, 3 ) + '-txt-' + substr( node, 3, 1 ) + '_,';
        console.log( "B Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = substr( b, 0, b.length - 5 ) + '.gif)';
                console.log( 'B setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
        
        var t;
        var fx;
                
        q = '[id^=topnav-' + node + '_],[id^=topnav-' + node + '-link_],';
        q += '#topnav-' + node + '_,' +
             '#topnav-' + node + '-link_,';
        t = dojo.query( q );
        t.style( {display:""} );
        console.log('B stuff to show:');

        var newFx = t.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
        lastActiveNode = node;
    }else if(node.length == 4 || lastActiveNode != "" && fromMouseOver == false) {
        console.log('path C');
        var p;
        var t;
        var fx;
                
        q = '[id^=topnav-' + lastActiveNode + '_],[id^=topnav-' + lastActiveNode + '-link_],';
        q += '#topnav-' + lastActiveNode + '_,' +
             '#topnav-' + lastActiveNode + '-link_,';
        t = dojo.query( q );
        t.style( {display:"none"} );
        console.log('C stuff to hide:');

        //var newFx = t.fadeIn( { duration:300, delay:0 } );
        //if ( fx ) {
        //    fx = dojo.fx.combine( fx, newFx )
        //}else {
        //    fx = newFx;
        //}
        //lastActiveNode = node;
    }else if(node.length == 5  && lastActiveNode == "" && fromMouseOver == false) {
        console.log('path D');

        q += '#hex-' + substr( node, 0, 4 ) + '-stb-' + substr( node, 4, 1 ) + '_,' +
             '#hex-' + substr( node, 0, 4 ) + '-txt-' + substr( node, 4, 1 ) + '_,';
        console.log( "D Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = substr( b, 0, b.length - 5 ) + '-act.gif)';
                console.log( 'D setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
        
        var t;
        var fx;
                
        q = '[id^=topnav-' + node + '_],[id^=topnav-' + node + '-link_],';
        q += '#topnav-' + node + '_,' +
             '#topnav-' + node + '-link_,';
        t = dojo.query( q );
        t.style( {display:""} );
        console.log('D stuff to show:');

        var newFx = t.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
        lastActiveNode = node;
    }
}

function revealTopNav( prefix ) {
    var stuffToHide = "";
    var stuffToShow = "";
    
    console.log( 'revealTopNav:' + prefix);
        
    if ( (( visibleTopNavPfx == prefix ) || ( visibleTopNavPfx.length <= 2 && prefix.length <= 2 )) && prefix != "02" ) {
        console.log( 'revealTopNav - prefix is the same as visible' );
        return;
    }

    if ( substr( prefix, 0, visibleTopNavPfx.length ) == visibleTopNavPfx ) {
        // we are revealing a deeper level below the current visible hex
        // NB: this also handles the case visibleHexPrefix = ""
        
        console.log('path A: ' + (visibleTopNavPfx.length + 1) + ',' + prefix.length);
        
        // show all backgrounds and titles in chain
        for ( var i = Math.max( visibleTopNavPfx.length + 1, 3 ); i <= prefix.length; ++i ) {
            stuffToShow += '#topnav-' + substr( prefix, 0, i ) + '_,' + 
                           '#topnav-' + substr( prefix, 0, i ) + '-link_,';
        }
        
        // hide the stb and txt which is currently showing which leads to prefix
        //var vertex = substr( prefix, prefix.length - 1, 1 );
        //stuffToHide += '#topnav-' + substr( prefix, 0, prefix.length - 1 ) + '_,' + 
                         //'#topnav-' + substr( prefix, 0, prefix.length - 1 ) + '-_,';
    } else if ( substr( visibleTopNavPfx, 0, prefix.length ) == prefix ) {
        console.log('path B');
        // we are going back up one or more levels. 
        // we must hide the deeper levels which should no longer be shown
        // NB: this also handles the case prefix = ""
        
        // Hide deeper backgrounds and titles
        for ( var i = Math.max( prefix.length + 1, 3 ); i <= visibleTopNavPfx.length; ++i ) {
            stuffToHide += '#topnav-' + substr( visibleTopNavPfx, 0, i ) + '_,' +
                           '#topnav-' + substr( visibleTopNavPfx, 0, i ) + '-link_,';
        }
        
        // Hide txt, stb and title elements of deepest hex
        stuffToHide += '[id^=topnav-' + visibleTopNavPfx + '_],[id^=topnav-' + visibleTopNavPfx + '-link_],';
        
        // Show the txt and stb leading to the deeper part of the chain that we're hiding
        var vertex = substr( visibleTopNavPfx, Math.max( prefix.length, 2 ), 1 );
        var hex = substr( visibleTopNavPfx, 0, Math.max( prefix.length, 2 ) );
        
        //stuffToShow += '#topnav-' + hex + '-stb-' + vertex + '_,#hex-' + hex + '-txt-' + vertex + '_,';
        
    } else {
        console.log('path C');
        // Assume that we need to hide the whole chain represented by visibleHexPfx and show the whole chain
        // represented by prefix. NOTE: this will probably make a mess of the case where you go, eg, from
        // hex-01432 to 0142 - ie, some common 'root' to the chain, but then changing to a different
        // branch. This is not at present an expected navigation step.
        
        // show all backgrounds in chain
        for ( var i = 3; i <= prefix.length; ++i ) {
            stuffToShow += '#topnav-' + substr( prefix, 0, i ) + '_,';
        }

        // Hide all backgrounds and titles
        for ( var i = 3; i <= visibleTopNavPfx.length; ++i ) {
            stuffToHide += '#topnav-' + substr( visibleTopNavPfx, 0, i ) + '_,' +
                           '#topnav-' + substr( visibleTopNavPfx, 0, i ) + '-link_,'
        }
        
        // Hide txt, stb and title elements of deepest hex
        stuffToHide += '[id^=topnav-' + visibleTopNavPfx + '-txt],[id^=topnav-' + visibleTopNavPfx + '-stb],';

        // hide the stb and txt which is currently showing which leads to prefix
        //var vertex = substr( prefix, 2, 1 );
        //stuffToHide += '#topnav-' + substr( prefix, 0, 2 ) + '-txt-' + vertex + '_,' +
        //               '#topnav-' + substr( prefix, 0, 2 ) + '-stb-' + vertex + '_,';

        // show the stb and txt which is currently hidden which leads to visibleHexPfx
        //var vertex = substr( visibleHexPfx, 2, 1 );
        //stuffToShow += '#hex-' + substr( visibleHexPfx, 0, 2 ) + '-txt-' + vertex + '_,' +
        //               '#hex-' + substr( visibleHexPfx, 0, 2 ) + '-stb-' + vertex + '_,';
        
    }
        
    // Stuff we'll always want to show:
    // show all txt and stb elements for deepest hex and title
    if ( prefix != "" ) {
        stuffToShow += '[id^=topnav-' + prefix + '_],[id^=topnav-' + prefix + '-link_]';
        //stuffToShow += '#topnav-01_';
    }
    
    var fx;
    
    if ( stuffToShow != "" ) {
        var q = dojo.query( stuffToShow );
        
        console.log('stuff to show:');
        q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
        
        //q.style( { opacity:0, display:"" }  );
        q.style( {display:""} );
        var newFx = q.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
    }
        
    if ( stuffToHide != "" ) {
        var q = dojo.query( stuffToHide );

        console.log('stuff to hide:');
        q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
        
        q.style( { display:"none" } );
        
        var newFx = q.fadeOut( { duration:500, delay:0 } );
        dojo.connect( newFx, "onEnd", function(){ q.style( {display:"none"} ); } );
        if ( fx )
            fx = dojo.fx.combine( fx, newFx )
        else
            fx = newFx;
    }
    
    if ( false && fx ) {
        fx.play();
    }
        
    visibleTopNavPfx = prefix;
}

function revealHex( prefix ) {
    var stuffToHide = "";
    var stuffToShow = "";
    
    console.log( 'revealHex current prefix: ' +  visibleHexPfx + ', new prefix: ' + prefix);
    
    if(prefix == "02") {
        var q = "";
        q += '#hex-01-bg_,' +
             '#hex-02-title_,' +
             '#hex-02-title_,' +
             '#hex-01-txt-1_,' +
             '#hex-02-txt-1_,' +
             '#hex-02-txt-2_,' +
             '#hex-02-stb-1_,' +
             '#hex-02-stb-2_,' +
             '#hex-01-stb-1_';
        console.log( "AA Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = substr( b, 0, b.length - 5 ) + '-act.gif)';
                console.log( 'AA setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
    }else{
        var q = "";
        q += '#hex-01-bg_,' +
             '#hex-02-title_' +
             '#hex-01-txt-1_,' +
             '#hex-02-txt-1_,' +
             '#hex-02-txt-2_,' +
             '#hex-02-stb-1_,' +
             '#hex-02-stb-2_,' +
             '#hex-01-stb-1_';
        console.log( "AA Stuff to set act: " + q );
        
        dojo.query( q ).forEach( 
            function( item, index, array ){
                var b = dojo.style( item, "backgroundImage" );
                var s = b + '.gif';
                console.log( 'AA setActiveNode: ' + item.getAttribute( 'id' ) + ' ' + b + ' -> ' + s );
                dojo.style( item, "backgroundImage", s ); 
            } 
        );
    }
    
    
    if ( ( visibleHexPfx == prefix ) || ( visibleHexPfx.length <= 2 && prefix.length <= 2 ) ) {
        console.log( 'revealHex - prefix is the same as visible' );
        return;
    }
    
    if ( substr( prefix, 0, visibleHexPfx.length ) == visibleHexPfx ) {
        console.log('path A');
        // we are revealing a deeper level below the current visible hex
        // NB: this also handles the case visibleHexPrefix = ""
        
        // show all backgrounds and titles in chain
        for ( var i = Math.max( visibleHexPfx.length + 1, 3 ); i <= prefix.length; ++i ) {
            stuffToShow += '#hex-' + substr( prefix, 0, i ) + '-bg_,' +
                           '#hex-' + substr( prefix, 0, i ) + '-title_,';
        }
        
        // hide the stb and txt which is currently showing which leads to prefix
        var vertex = substr( prefix, prefix.length - 1, 1 );
        stuffToHide += '#hex-' + substr( prefix, 0, prefix.length - 1 ) + '-txt-' + vertex + '_,' + 
                       '#hex-' + substr( prefix, 0, prefix.length - 1 ) + '-stb-' + vertex + '_,';
        
    } else if ( substr( visibleHexPfx, 0, prefix.length ) == prefix ) {
        console.log('path B');
        // we are going back up one or more levels. 
        // we must hide the deeper levels which should no longer be shown
        // NB: this also handles the case prefix = ""
        
        // Hide deeper backgrounds and titles
        for ( var i = Math.max( prefix.length + 1, 3 ); i <= visibleHexPfx.length; ++i ) {
            stuffToHide += '#hex-' + substr( visibleHexPfx, 0, i ) + '-bg_,' +
                           '#hex-' + substr( visibleHexPfx, 0, i ) + '-title_,';
        }
        
        // Hide txt, stb and title elements of deepest hex
        stuffToHide += '[id^=hex-' + visibleHexPfx + '-txt],[id^=hex-' + visibleHexPfx + '-stb],#hex-' + visibleHexPfx + '-title_,';
        
        // Show the txt and stb leading to the deeper part of the chain that we're hiding
        var vertex = substr( visibleHexPfx, Math.max( prefix.length, 2 ), 1 );
        var hex = substr( visibleHexPfx, 0, Math.max( prefix.length, 2 ) );
        
        stuffToShow += '#hex-' + hex + '-stb-' + vertex + '_,#hex-' + hex + '-txt-' + vertex + '_,';
        
    } else {
        console.log('path C');
        // Assume that we need to hide the whole chain represented by visibleHexPfx and show the whole chain
        // represented by prefix. NOTE: this will probably make a mess of the case where you go, eg, from
        // hex-01432 to 0142 - ie, some common 'root' to the chain, but then changing to a different
        // branch. This is not at present an expected navigation step.
        
        // show all backgrounds in chain
        for ( var i = 3; i <= prefix.length; ++i ) {
            stuffToShow += '#hex-' + substr( prefix, 0, i ) + '-bg_,';
        }

        // Hide all backgrounds and titles
        for ( var i = 3; i <= visibleHexPfx.length; ++i ) {
            stuffToHide += '#hex-' + substr( visibleHexPfx, 0, i ) + '-bg_,' +
                           '#hex-' + substr( visibleHexPfx, 0, i ) + '-title_,';
        }
        
        // Hide txt, stb and title elements of deepest hex
        stuffToHide += '[id^=hex-' + visibleHexPfx + '-txt],[id^=hex-' + visibleHexPfx + '-stb],#hex-' + visibleHexPfx + '-title_,';

        // hide the stb and txt which is currently showing which leads to prefix
        var vertex = substr( prefix, 2, 1 );
        stuffToHide += '#hex-' + substr( prefix, 0, 2 ) + '-txt-' + vertex + '_,' +
                       '#hex-' + substr( prefix, 0, 2 ) + '-stb-' + vertex + '_,';

        // show the stb and txt which is currently hidden which leads to visibleHexPfx
        var vertex = substr( visibleHexPfx, 2, 1 );
        stuffToShow += '#hex-' + substr( visibleHexPfx, 0, 2 ) + '-txt-' + vertex + '_,' +
                       '#hex-' + substr( visibleHexPfx, 0, 2 ) + '-stb-' + vertex + '_,';
    }
    
    // Stuff we'll always want to show:
    // show all txt and stb elements for deepest hex and title
    if ( prefix != "" ) {
        stuffToShow += '[id^=hex-' + prefix + '-txt],[id^=hex-' + prefix + '-stb],#hex-' + prefix + '-title_';
    }
    
    var fx;
    
    if ( stuffToShow != "" ) {
        var q = dojo.query( stuffToShow );
        
        console.log('stuff to show:');
        q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
        
        
        
        //q.style( { opacity:0, display:"" }  );
        q.style( {display:""} );
        var newFx = q.fadeIn( { duration:300, delay:0 } );
        if ( fx ) {
            fx = dojo.fx.combine( fx, newFx )
        }else {
            fx = newFx;
        }
    }
        
    if ( stuffToHide != "" ) {
        stuffToHide = safetyCheck(stuffToHide);
        console.log(stuffToHide);
        var q = dojo.query( stuffToHide );

        console.log('stuff to hide:');
        q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
        
        q.style( { display:"none" } );
        
        var newFx = q.fadeOut( { duration:500, delay:0 } );
        dojo.connect( newFx, "onEnd", function(){ q.style( {display:"none"} ); } );
        if ( fx )
            fx = dojo.fx.combine( fx, newFx )
        else
            fx = newFx;
    }
    
    if ( false && fx ) {
        fx.play();
    }
    
    visibleHexPfx = prefix;
}

function safetyCheck( stuffToHide ) {
    var t = stuffToHide
    var q;
    if(t.indexOf("#hex-014-bg_") != -1) {
        if(t.indexOf("#hex-014-txt-3_") == -1) {
            t += "#hex-014-txt-3_,";
        }
        
        if(t.indexOf("#hex-014-stb-3_") == -1) {
            t += "#hex-014-stb-3_,";
        }
        
        if(t.indexOf("#hex-014-txt-4_") == -1) {
            t += "#hex-014-txt-4_,";
        }
        
        if(t.indexOf("#hex-014-stb-4_") == -1) {
            t += "#hex-014-stb-4_,";
        }
        
        if(t.indexOf("#hex-014-txt-5_") == -1) {
            t += "#hex-014-txt-5_,";
        }
        
        if(t.indexOf("#hex-014-stb-5_") == -1) {
            t += "#hex-014-stb-5_,";
        }
    }else if(t.indexOf("#hex-015-bg_") != -1) {
        if(t.indexOf("#hex-015-txt-1_") == -1) {
            t += "#hex-015-txt-1_,";
        }
        
        if(t.indexOf("#hex-015-stb-1_") == -1) {
            t += "#hex-015-stb-1_,";
        }
        
        if(t.indexOf("#hex-015-txt-4_") == -1) {
            t += "#hex-015-txt-4_,";
        }
        
        if(t.indexOf("#hex-015-stb-4_") == -1) {
            t += "#hex-015-stb-4_,";
        }
        
        if(t.indexOf("#hex-015-txt-5_") == -1) {
            t += "#hex-015-txt-5_,";
        }
        
        if(t.indexOf("#hex-015-stb-5_") == -1) {
            t += "#hex-015-stb-5_,";
        }
        
        if(t.indexOf("#hex-015-stb-6_") == -1) {
            t += "#hex-015-stb-6_,";
        }
        
        if(t.indexOf("#hex-015-txt-6_") == -1) {
            t += "#hex-015-txt-6_,";
        }
    }else if(t.indexOf("#hex-0154-bg_") != -1) {
        if(t.indexOf("#hex-0154-stb-3_") == -1) {
            t += "#hex-0154-stb-3_,";
        }
    
        if(t.indexOf("#hex-0154-txt-3_") == -1) {
            t += "#hex-0154-txt-3_,";
        }
        
        if(t.indexOf("#hex-0154-stb-4_") == -1) {
            t += "#hex-0154-stb-4_,";
        }
        
        if(t.indexOf("#hex-0154-txt-4_") == -1) {
            t += "#hex-0154-txt-4_,";
        }
        
        if(t.indexOf("#hex-0154-stb-5_") == -1) {
            t += "#hex-0154-stb-5_,";
        }
        
        if(t.indexOf("#hex-0154-txt-5_") == -1) {
            t += "#hex-0154-txt-5_,";
        }
    }
    
    console.log(t);
    return t;
}

function addDiv( parent, id ) {
    //return dojo.byId( id );
    var domNode = document.createElement( 'div' );
    domNode.setAttribute( 'id', id );
    dojo.byId( parent ).appendChild( domNode );
    return domNode;
}

function ahd( id ) {
    //return dojo.byId( id );
    return addDiv( "hexnav_", id );
}

/*
function removeDiv( parent, id ) {
    var domNode = document.getElement( id );
    dojo.byId( parent ).removeChild( domNode );
    return domNode;
}

function rtd( id ) {
    return removeDiv("topnav_", id );
}
*/

function atd( id ) {
    //return dojo.byId( id );
    return addDiv( "topnav_", id );
}

function buildIntermediateNodeFn( prefix, node ) {
    return function(){  
        console.log( 'onmouseover showing ' + prefix + node.node );
        console.log( 'buildIntermediateNodeFn');
        revealHex( prefix + node.node );
        //revealTopNav( prefix + node.node );
        //setActiveNode( prefix, false );
    };
}

function buildTitleFn( prefix ) {
    return function(){  
        console.log( 'onmouseover showing ' + prefix ); 
        console.log( 'buildTitleFn');
        revealHex( prefix ); 
        //revealTopNav( prefix );
        //setActiveNode( prefix, false );
    };
}

function buildHexNavRecurse( prefix, hexdef ) {
    ahd( 'hex-' + prefix + '-bg_' );
    for ( var i = 0; i < hexdef.length; ++i ) {
        var node = hexdef[i];
        if ( typeof(node)=='undefined' || typeof(node.node)=='undefined' ) {
            //dbgwatch= hexdef;
            console.log('node ' + prefix + '-' + i + ' is missing element <node>');
        }
        else {
            var selectionNodes;
            
            if ( node.node == 0 ) {
                var id = 'hex-' + prefix + '-title_';
                ahd( id  )
                selectionNodes = dojo.query( '#' + id );
                
                if ( typeof(node.no_mouseover)=='undefined' ) node.no_mouseover = false;
                
                if ( node.no_mouseover==false )
                {
                    selectionNodes.connect( "onmouseover", buildTitleFn( prefix ) );
                }
            }
            else {
                var id1 = 'hex-' + prefix + '-stb-' + node.node + '_';
                var id2 = 'hex-' + prefix + '-txt-' + node.node + '_'; 
                ahd( id1 );
                ahd( id2 );
                selectionNodes = dojo.query( '#' + id1 + ',#' + id2 );
            }
            
            selectionNodes.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
            if ( typeof(node.fn)!='undefined' ) {
                // leaf node
                selectionNodes.connect( "onmousedown", node.fn );
                
                //selectionNodes.connect( "onmouseover", function(){console.log("onMouseOver");});
                selectionNodes.connect( "onmouseover", resetActiveNode( prefix, node ) );
            }
            else if ( typeof(node.hexdef)!='undefined' ) {
                // intermediate node
                selectionNodes.connect( "onmouseover", buildIntermediateNodeFn( prefix, node ) );
                buildHexNavRecurse( prefix + node.node, node.hexdef );
            }
            else {
                console.log('note: node ' + prefix + i + ' has no <fn> or <hexdef> (this is probably ok)');
            }
        }
    }
}

function buildHexNav() {
    /*
    var nText = dojo.byId('text_');
    
    var domNode = document.createElement( 'div' );
    domNode.setAttribute( 'id', 'hexnav_' );
    
    nText.parentNode.insertBefore( domNode, nText ); //appendChild( domNode );
    */
    ahd( 'logo_' );
    var stuffToHide = '#fallback_,[id^="hex-"]';
    var s = hsysStruct;
    console.log('struct has length ' + s.length );
    for ( var i = 0; i < s.length; ++i )
    {
        var rootHex = s[i];
        console.log('Processing root idx ' + i + ' prefix:' + rootHex.prefx);
        if ( typeof(rootHex.prefix)=='undefined' ) {
            console.log('root entry ' + i + ' is missing element <prefix>');
        }
        else {
            if ( typeof(rootHex.hexdef)=='undefined' ) {
                console.log('root entry ' + i + ' is missing element <hexdef>');
            }
            else {
                stuffToHide += ':not([id^=hex-' + rootHex.prefix + '-])';
                buildHexNavRecurse( rootHex.prefix, rootHex.hexdef );
            }
        }
    }
    var q = dojo.query( stuffToHide );
    console.log('hiding non-root items:');
    q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
    q.style( { display:"none" } );
}

function buildTopNav() {
    var stuffToHide = '#fallback_,[id^="topnav-"]';
    var q;
    atd( 'topnav-01_' );
    //q = dojo.query( '#topnav-01_' );
    //q.connect( "onmousedown", gorel('index.html'));
    
    atd( 'topnav-02-link_' );
    
    atd( 'topnav-014-link_' );
    atd( 'topnav-0143-link_' );
    atd( 'topnav-0144-link_' );
    atd( 'topnav-0145-link_' );
    
    atd( 'topnav-015-link_' );
    atd( 'topnav-0151-link_' );
    atd( 'topnav-0154-link_' );
    atd( 'topnav-0155-link_' );
    atd( 'topnav-0156-link_' );
    
    atd( 'topnav-01543-link_' );
    atd( 'topnav-01544-link_' );
    atd( 'topnav-01545-link_' );
    
    atd( 'topnav-02_' );
    
    atd( 'topnav-014_' );
    atd( 'topnav-0143_' );
    atd( 'topnav-0144_' );
    atd( 'topnav-0145_' );
    
    atd( 'topnav-015_' );
    atd( 'topnav-0151_' );
    atd( 'topnav-0154_' );
    atd( 'topnav-0155_' );
    atd( 'topnav-0156_' );
    
    atd( 'topnav-01543_' );
    atd( 'topnav-01544_' );
    atd( 'topnav-01545_' );
    
    stuffToHide += ':not([id^=topnav-01_])';
    
    q = dojo.query( stuffToHide );
    console.log('hiding non-root items:');
    q.forEach( function( item, index, array ){ console.log( item.getAttribute( 'id' ) ); } );
    q.style( { display:"none" } );
}

function ol( node ) {
    console.log('ol: node value: ' + node);
    lastActiveNode = "";
    buildHexNav();
    buildTopNav();
    
    if ( node.length > 2 ) {
        revealHex( substr( node, 0, node.length - 1 ) );
        revealTopNav( substr( node, 0, node.length - 1 ) );
        console.log('ol: node value: ' + node);
        setActiveNode(node, false);
    } else {
        revealHex( node );
        revealTopNav( node );
    }
    
    //fade in text
    var q = dojo.query('#text_');
    q.style( { opacity:0, display:"block" } );
    q.fadeIn( 300, 0 ).play();
    
    /*
    //fade in top nav
    q = dojo.query('#topnav_');
    q.style( { opacity:0, display:"block" } );
    q.fadeIn( 300, 0 ).play();
    */
}