var maxDepthAvailable = 0;
function showNav()
{
    var           a,row,n;

    n = 0;
    row = 0;
    set('<table class=navtable id=navtable>');
    add('<tr class="NavTitleBar"><td nowrap onClick="javascript:fNav()">');
    add('Folders ('+FileNames.length+' picture'+((1 != FileNames.length)?'s':'')+')&nbsp;&nbsp;&nbsp;');
    add('<td align=right nowarp>');
    if (bFullNav)
    {
        add('<img onClick="javascript:collapse()" border=0 src="tn_/collapse.gif">');
        add('<img onClick="javascript:expand()" border=0 src="tn_/expand.gif">&nbsp;&nbsp;');
        add('<img onClick="javascript:sync()" border=0 src="tn_/sync.gif">&nbsp;&nbsp;');
    } 
    add('<img onClick="javascript:fNav()" border=0 src="tn_/');
    add(bFullNav?'minpixel.gif':'maxpixel.gif');
    add('">');
    var           verts = new Array();
    var           newDirLevels = new Array();
    var           startcol = 0;

    verts[-1] = "vert";
    var           oldDirLevels = new Array();
    var           newDirLevels = new Array();

    newDirLevels[0] = '';
    if (bFullNav)
    {
        printDirectoryLevel(DirectoryEntries[""], 0);
    } 
    add('</table>');
    win = document.getElementById("navBox");
    win.innerHTML = fin();
    navWindowWidth = document.getElementById("navtable").offsetWidth+30;
    if (!inFullSize)
        document.getElementById("rightcol").style.left = navWindowWidth;
} 

function initStatus()
{
    var           dir,a,i;
   // FileNames.sort(compareFileNames);

    DirectoryEntries = new Array();
    i = 0;
    root = new DirectoryEntry('', null);
    DirectoryEntries[''] = root;
    root.open = true;
    for (a = 0; a < FileNames.length; a++)
    {
        dir = setDirFromFileName(FileNames[a]);
        dirParts = dir.split('/');
        part = '';
        for (b = 0; b < dirParts.length; b++)
        {
            partParent = part;
            if (dir != '')
                part += '/';
            part += dirParts[b];
            if (typeof(DirectoryEntries[part]) == "undefined")
            {
                newDir = new DirectoryEntry(part, DirectoryEntries[partParent]);
                newDir.firstPicture = a;
                DirectoryEntries[part] = newDir;
            } 
        } 
        maxDepthAvailable = Math.max(maxDepthAvailable, dirParts.length);
        //DirectoryEntries[part].addPicture(FileNames[a].substring(1+FileNames[a].lastIndexOf('/')));
        DirectoryEntries[part].piccount++;
    } 
    root.closeDirectory();
    resizeDone = true;
    countFiles();
} 

function pictureCount()
{
    return  this.piccount;
    //return  this.pictures.length;
} 

function DirectoryEntry(inPath,inParent)
{
    this.path = inPath;
    this.parent = inParent;
    this.children = new Array();
    this.pictures = new Array();
    this.nextSibling = null;
    this.open = true;
    this.firstPicture = 0;
    this.piccount = 0;
    if (inParent)
    {
        if (0 < inParent.children.length)
            inParent.children[inParent.children.length-1].nextSibling = this;
        inParent.children[inParent.children.length] = this;
    } 
    this.printConnector = printConnector;
    this.closeDirectory = closeDirectory;
    this.expandDirectory = expandDirectory;
    this.pictureCount = pictureCount;
//    this.addPicture = addPicture;
    this.collapseToDepth = collapseToDepth;
    this.findMaxOpenDepth = findMaxOpenDepth;
} 

function addPicture(filename)
{
    return  this.piccount;
    return  this.pictures[this.pictures.length] = filename;
} 

function getFileFromFileName(fname)
{
    var           i,f;

    f = fname.substring(fname.lastIndexOf('/')+1);
    i = f.lastIndexOf('.');
    if (i > -1)
        f = f.substring(0, i);
    return  f.replace(/[-_]/g, ' ');
} 

function setDirFromFileName(fname)
{
    var           i;

    i = fname.lastIndexOf('/');
    if (i < 1)
        i = dirFromNameCache[fname] = '';
    else
        i = dirFromNameCache[fname] = fname.substring(0, i);
    return  i;
} 

function getDirFromFileName(fname)
{
    return  dirFromNameCache[fname];
} 

function isNextDirectorySibling(dir)
{
    return (null != DirectoryEntries['/'+dir].nextSibling);
} 

function printDirectoryLevel(dirEntry,depth)
{
    var           linkstyle;

    add('<tr><td class=dir colspan=2>');
    if (null != dirEntry.parent)
    {
        dirEntry.parent.printConnector();
    } 
    var           p,a,i;

    a = dirEntry.firstPicture;
    p = dirEntry.path;
    if (0 == p.indexOf('/'))
        p = p.substring(1);
    lastSegment = p.substring(p.lastIndexOf('/')+1);
    if (dirEntry.children.length > 0)
    {
        prefix = (dirEntry.open)?"minus":"plus";
        clickAction = ' onclick="javascript:toggleDir(\''+dirEntry.path+'\')"';
    } 
    else
    {
        prefix = "tee";
        clickAction = ' ondblclick="javascript:clickedPic('+dirEntry.firstPicture+',true)"'
    } 
    ;
    isCurrDirectory = (p == getDirFromFileName(FileNames[curPicNum]));
    if (isCurrDirectory)
        linkstyle = ' style="color:white;background:blue;cursor:pointer;height:16" ';
    else
        linkstyle = ' style="color:blue;background:white;cursor:pointer;height:16" ';
    add('<img src="tn_/'+prefix+(dirEntry.nextSibling?"":"term")+'.gif" border=0 height=16 width=37 ');
    add(clickAction);
    add('>&nbsp;&nbsp;');
    add('<span id=navtd'+a+linkstyle);
    if (!isCurrDirectory)
    {
        add(' onmouseover="javascript:this.style.backgroundColor=\'#80C0FF\'" ');
        add(' onmouseout="javascript:this.style.backgroundColor=\'#FFFFFF\'" ');
    } 
    add(' onclick="javascript:clickedPic('+a+',true)">');
    add(lastSegment);
    if (dirEntry.pictureCount() > 0)
   //     add(' ('+dirEntry.pictureCount()+' pic'+((dirEntry.pictureCount() > 1)?"s)":")"));
        add(' (<small>'+dirEntry.pictureCount()+'</small>)');
    add('</span></td></tr>');
    if (isCurrDirectory && dirEntry.open)
        printSections(dirEntry);
    if (dirEntry.open)
        for (a = 0; a < dirEntry.children.length; a++)
            printDirectoryLevel(dirEntry.children[a], depth+1);
} 

function printConnector()
{
    if (this.parent)
    {
        this.parent.printConnector();
    } 
    add('<img src="tn_/'+(this.nextSibling?"vert":"white")+'.gif" border=0 height=16 width=18>');
} 

function toggleDir(id)
{
    obj = DirectoryEntries[id];
    obj.open = !obj.open;
    if (!obj.open)
        obj.closeDirectory();
    clickedPicWithoutLocate(curPicNum);
} 

function openDirectory(fileNumber)
{
    var           dirEntry,fn;

    fn = getDirFromFileName(FileNames[fileNumber]);
    if (fn != '')
        fn = '/'+fn;
    dirEntry = DirectoryEntries[fn];
    while (dirEntry)
    {
        dirEntry.open = true;
        dirEntry = dirEntry.parent;
    } 
} 

function closeDirectory()
{
    var           a;

    this.open = false;
    for (a = 0; a < this.children.length; a++)
        this.children[a].closeDirectory();
} 

function expandDirectory()
{
    var           a;

    this.open = true;
    for (a = 0; a < this.children.length; a++)
        this.children[a].expandDirectory();
} 

function findMaxOpenDepth(depthSoFar)
{
    var           a,m;

    m = 0;
    if (this.open)
    {
        m = depthSoFar;
        for (a = 0; a < this.children.length; a++)
            m = Math.max(m, this.children[a].findMaxOpenDepth(depthSoFar+1));
    } 
    return  m;
} 

function collapse()
{
    var           maxDepthAllowed = Math.max(0, DirectoryEntries[""].findMaxOpenDepth(0));
          
    DirectoryEntries[""].collapseToDepth(0, maxDepthAllowed);
    clickedPic(curPicNum, false);
} 

function collapseToFirstLevel()
{

    DirectoryEntries[""].closeDirectory();
    DirectoryEntries[""].open = true;
    clickedPic(curPicNum, false);
} 

function expand()
{
    maxDepthAllowed = Math.min((DirectoryEntries[""].findMaxOpenDepth(0)+2), maxDepthAvailable);
    DirectoryEntries[""].expandDirectory();
    DirectoryEntries[""].collapseToDepth(0, maxDepthAllowed);
    clickedPic(curPicNum, false);
} 

function sync()
{
    DirectoryEntries[""].closeDirectory();
    clickedPic(curPicNum, true);
} 

function collapseToDepth(depth,target)
{
    var           a;

    if (depth >= target)
        this.closeDirectory();
    else
        for (a = 0; a < this.children.length; a++)
            this.children[a].collapseToDepth(depth+1, target);
} 

function printSections(dirEntry)
{
    var           a,n;
          
    for (a = 0; a < SectionStart.length && dirEntry.firstPicture > SectionStart[a]; a++)
        ;
    if (hidePagesIfOnlyOne == false || (hidePagesIfOnlyOne && (dirEntry.pictureCount()/MaxSectionSize > 1)))
        for (n = 0; n < ((dirEntry.pictureCount())/MaxSectionSize); n++)
        {
            if (0 == n%4)
            {
                add('<tr><td class=dir colspan=2>');
                dirEntry.printConnector();
                add('<img src="tn_/'+((dirEntry.children.length > 0)?"vert":"white")+'.gif" border=0 height=16 width=18>');
                add('&nbsp;&nbsp;');
            } 
   //alert(curPicNum+'  '+  SectionStart[a] +'  '+ curPicNum +'  '+ SectionEnd[a]+' : '+MaxSectionSize+' : '+dirEntry.pictureCount());
            thisSection = (curPicNum >= SectionStart[a] && curPicNum <= SectionEnd[a]);
            if (thisSection)
            {
                add('<span class=currentSection>['+(n+1)+']</span>&nbsp; ');
            } 
            else
            {
                add(sec1+SectionStart[a]+sec2+(n+1)+sec3);
            } 
            a++
        } 
} 

function filesPerDir(dir)
{
    return  DirectoryEntries[((dir != '')?'/':'')+dir].pictureCount();
} 

function getFirstFileInSection(dir)
{
    return  DirectoryEntries[((dir != '')?'/':'')+dir].firstPicture;
} 

function compareFileNames(a,b)
{
    p1 = ((-1 == a.indexOf('/'))?"/":"")+a;
    p2 = ((-1 == b.indexOf('/'))?"/":"")+b;
    return (p1 < p2)?-1:((p1 > p2)?1:0);
} 

