var slideDelay = 1;
var           delays = new Array(1,2,5,15,30,60,300);
var           curPicNum = 0;
var           MaxSectionSize = 40;
var           inIndex = 1;
var           inFullSize = 0;
var           bFullIndex = 0;
var           bFullNav = 1;
var           szCurrentDir = "";
var           FirstFileInSection = new Array(0);
var           bShowAllFiles = 0;
var           SectionStart = new Array(0);
var           SectionEnd = new Array(0);
var           timer;
var           allowTimer = false;
var           folderWarning = new Array();
var           timeLeft = delays[slideDelay];
var           previewImages = new Image(FileNames.length);
var           ThumbTD = null;
var           rx = new RegExp();
var           rx2 = new RegExp();
var           oneTimeIndexBackground = -1;
var           buttonPreloads = new Array("rprev.gif","rnext.gif","rindex.gif","gprev.gif","gnext.gif","gindex.gif");
var           navWindowWidth = 360;                        // arbitrary
var           resizeDone = true;
var           oldnavWindowWidth = -1;
var           dirFromNameCache = new Array();
var           previousBackgroundDirectory = '?? junk ??';
var           allowOpen = true;
var           fitFullClient = true;
var           toolStatus = 3;                              // start showing the toolbar in fullclient
var           maximized = false;
var           randomized = false;
var           viewHistory = new Array();
var           debugData = '';
var           oldFileNames = null;
var           wpNames = null;

sec1 = '<a class=nav onclick="clickedPic(';
sec2 = ',true);" style="padding:0px;color:#009000" '+'onmouseover="this.style.backgroundColor=\'#80C0FF\'" '+
'onmouseout="this.style.backgroundColor=\'#FFFFFF\'" >[';
sec3 = ']</a>&nbsp; ';
rx.compile("(gif$)");
rx2.compile("\/","g");
document.onkeydown = indexEventHandler;
//FileNames.sort(randomCompare);
//function randomCompare(a,b){return parseInt(Math.random()*3)-1;}

function say(line)
{
    document.write(line);
} 

var           lineoutArray = new Array();
function set(line)
{
    add(line);
    lineoutArray = new Array();
    lineoutArray[0] = line;
} 

function add(line)
{
    lineoutArray[1+lineoutArray.length] = line;
} 

function fin()
{
    return  lineoutArray.join('');
} 

function StartPage()
{
    document.title = pageTitle;
    document.getElementById("PageHeadingText").innerHTML = heading1;
    t = '';
    for (i = 0; i < buttonPreloads.length; i++)
    {
        t += '<img height=2 width=2 border=0 src="tn_/'+buttonPreloads[i]+'">';
    } 
    document.getElementById("imagePreload").innerHTML = t;
    initStatus();
    setTimeout('hideCopyright(0);', 5300);
    document.onmousedown = swapFullClient;
    document.oncontextmenu = function()
    {
        return  false;
    } 
          
    document.body.style.backgroundColor = "#804020";
    //clickedPic(0, true);
    //collapseToFirstLevel();
    DirectoryEntries[""].closeDirectory();
    DirectoryEntries[""].expandDirectory();
    clickedPic(curPicNum = 0, false);
} 

function ProcessResize()
{
    resizeDone = true;
    FormatPage();
} 

function resizeFullClientPictureToFit()
{
    var           image,newWidth,newHeight;

    image = document.getElementById("fullpicture");
    if (image.width/document.body.clientWidth > image.height/document.body.clientHeight)
    {
        newWidth = Math.min(document.body.clientWidth, image.width);
        newHeight = (newWidth == image.width)?image.height:image.height *newWidth/image.width;
    } 
    else
    {
        newHeight = Math.min(document.body.clientHeight, image.height);
        newWidth = (newHeight == image.height)?image.width:image.width *newHeight/image.height;
    } 
    image.height = newHeight;
    image.width = newWidth;
    image.style.visibility = "visible";
    image.style.top = parseInt((document.body.clientHeight-newHeight)/2)+'px';
    image.style.left = parseInt((document.body.clientWidth-newWidth)/2)+'px';
    document.body.style.overflow = "hidden";
    if (allowTimer)
    {
        timeLeft = delays[slideDelay]+1;
        timer = setTimeout('processTimer();', 100);
    } 
} 

function FullClientPictureActualSize()
{
    image = document.getElementById("fullpicture");
    image.style.visibility = "visible";
    image.style.top = Math.max(0, parseInt((document.body.clientHeight-image.height)/2))+'px';
    image.style.left = Math.max(0, parseInt((document.body.clientWidth-image.width)/2))+'px';
    document.body.style.overflow = "auto";
    if (allowTimer)
    {
        timeLeft = delays[slideDelay]+1;
        timer = setTimeout('processTimer();', 100);
    } 
} 

function countInstances(string,word)
{
    var           substrings = string.split(word);
    return  substrings.length-1;
} 

function fIndex()
{
    inIndex = 1;
    bShowAllFiles = 0;
    bFullIndex = 0;
    clickedPic(curPicNum, true);
} 

function fIndexAll(dir)
{
    bShowAllFiles = 1;
    bFullIndex = 1;
    inIndex = 1;
    clickedPic(curPicNum, true);
} 

var           cursorX,cursorY;
function previewOn(id)
{
    if (document.all)
    {
        cursorX = event.x;
        cursorY = event.y;
    } 
    else
    {
        e = arguments.callee.caller.arguments[0];
        cursorX = e.pageX;
        cursorY = e.pageY;
    } 
    if (!previewImages[id])
    {
        previewImages[id] = new Image();
        previewImages[id].src = thumb(FileNames[id]);
    } 
    document.images["preview"].onload = ShowPreview;
    document.images["preview"].src = previewImages[id].src;
    return  true;
} 

function ShowPreview()
{
    previewBox = document.getElementById("previewdiv");
    previewBox.style.top = cursorY+window.document.body.scrollTop-40;
    previewBox.style.left = cursorX-navWindowWidth;
    previewBox.style.visibility = "visible";
} 

function fNav()
{
    bFullNav = 1-bFullNav;
    resizeDone = true;
    FormatPage();
} 

function thumb(id)
{
    var           a,z;

    if ("" == thumbpref)
        return  id;

    a = id.substr(0, 1+id.lastIndexOf('/'));
    if ("" != a)
        a += '/';
    z = id.substr(1+id.lastIndexOf('/'));
    ret = a+thumbpref+'/'+thumbpref+z.replace(rx, "jpg");
    return  ret;
} 

function countFiles()
{
    if (resizeDone || navWindowWidth != oldnavWindowWidth)
    {
        w = getColsPerLine();
        h = Math.max(1, parseInt((document.body.clientHeight-document.getElementById("PageHeading").clientHeight)/160));
        MaxSectionSize = w *h;
        lastDir = "?";
        section = -1;
        filesInSection = 0;
        SectionStart = new Array(0);
        SectionEnd = new Array(0);
        for (a = 0; a < FileNames.length; a++)
        {
            curDir = FileNames[a];
            curDir = curDir.substring(0, curDir.lastIndexOf('/'));
            if (lastDir != curDir || MaxSectionSize == filesInSection)
            {
                if (section >= 0)
                    SectionEnd[section] = a-1;
                SectionStart[++section] = a;
                filesInSection = 1;
            } 
            else
                filesInSection++;
            lastDir = curDir;
        } 
        if (section >= 0)
        {
            SectionEnd[section] = FileNames.length-1;
        } 
        resizeDone = false;
        oldnavWindowWidth = navWindowWidth
    } 
} 

function FormatPage()
{
    if (inFullSize)
    {
        setBigAss();
        return ;
    } 
    document.getElementById("index").style.display = "none";
    showNav();
    countFiles();
    for (section = 0; section < SectionStart.length && (curPicNum < SectionStart[section] || curPicNum > SectionEnd[section]); section++)
        ;
    setBackgroundPicture();
    document.getElementById("rightcol").style.left = navWindowWidth;
    szCurrentDir = FileNames[curPicNum];
    szCurrentDir = szCurrentDir.substring(0, szCurrentDir.lastIndexOf('/'));
    set('<table id="IndexTable" border="0" align=left cellspacing="');
    add(inIndex?'3" style="border: 0px solid #000"':'4"');
    add(' cellpadding="0" >');
    add('<tr><td colspan=13 class=titleBack id=titleback>');
    add('<div id=tbardir style="float:left;width:75%;overflow:hidden">');
    add('</div>');
    if (bShowAllFiles == 0)
    {
        add('<div style="float:right">');
        add('&nbsp;&nbsp<small>(</small>');
        if (SectionStart[section]-MaxSectionSize >= getFirstFileInSection(szCurrentDir))
        {
            add('<a style="padding:0px;border-width:0px;background:transparent"');
            add(' onclick="clickedPic('+(SectionStart[section]-MaxSectionSize)+
                ',true)"><img onmouseover="iSet(this,0)" onmouseout="iSet(this,3)" border="0" src="tn_/gprev.gif" width="16" height="16"></a>&nbsp;&nbsp;');
        } 
        else
            add('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
        add('<small>Group '+(1+parseInt((curPicNum-getFirstFileInSection(szCurrentDir))/MaxSectionSize))+' of '+(1+parseInt(filesPerDir(szCurrentDir)/
            MaxSectionSize))+'</small>');
        if (SectionEnd[section]+1 < getFirstFileInSection(szCurrentDir)+filesPerDir(szCurrentDir))
        {
            add('&nbsp;&nbsp;<a style="padding:0px;border-width:0px;background:transparent"');
            add(' onclick="clickedPic('+(SectionEnd[section]+1)+
                ',true)"><img onmouseover="iSet(this,1)" onmouseout="iSet(this,4)" border="0" src="tn_/gnext.gif" width="16" height="16"></a>');
        } 
        else
            add('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
        add('<small> )</small>');
        add('</div>');
    } 
    add('<tr class=indx>');
    n = 0;
    row = 0;
    idNumbersPerLine = getColsPerLine();
    itemsLeft = idNumbersPerLine;
    dir = FileNames[curPicNum];
    dir = dir.substring(0, dir.lastIndexOf('/'));
    if (bShowAllFiles)
    {
        start = getFirstFileInSection(dir);
        end = start+filesPerDir(dir)-1;
    } 
    else
    {
        start = SectionStart[section];
        end = SectionEnd[section];
    } 
    for (a = start; a <= end; a++)
    {
        if (!previewImages[a])
        {
            previewImages[a] = new Image();
            previewImages[a].src = thumb(FileNames[a]);
        } 
        add('<td class="tdindex'+useGrayScale+'" id="cell'+a+'" onmousedown="curPicNum='+a+'"');
        add(' onmouseover="hilite('+a+')"');
        add(' onmouseout="window.status=\'\'"');
        add('>');
        add('<a class=nb onclick="fullsize1('+a+')"');
        add('>');
        add('<img border=0 src="'+thumb(FileNames[a])+'"');
        if (curPicNum == a)
            add(' onload="reposition('+a+')"');
 //add('style="filter:progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)"');

        add('></a>');
        n++;
        if (0 == --itemsLeft && a < FileNames.length-1)
        {
            add('<tr class=indx>');
            itemsLeft = idNumbersPerLine;
        } 
    } 
    add('</table>');
    win = document.getElementById("index");
    win.innerHTML = fin();
    win.style.display = "inline";
    document.getElementById("tbardir").style.width = document.getElementById("titleback").offsetWidth *.7;
    document.getElementById("tbardir").innerHTML = szCurrentDir.replace(rx2, '<b style="color:red"> / </b>');
    document.getElementById("index").style.visibility = "visible";
} 

function setBackgroundPicture()
{
    switch (eval(useRandomIndexBackgroundEachTime))
    {
        case 0 :
            document.body.background = indexBackgroundImage;
            document.body.style.backgroundImage = "url("+escape(indexBackgroundImage)+")";
            break;
        case 1 :
            if (-1 == oneTimeIndexBackground)
                oneTimeIndexBackground = getRandom();
            document.body.style.backgroundImage = "url("+escape(FileNames[oneTimeIndexBackground])+")";
            break;
        case 2 :
            curDir = getDirFromFileName(FileNames[curPicNum]);
            if (curDir != '')
                curDir = '/'+curDir;
            if (previousBackgroundDirectory != curDir)
            {
                start = DirectoryEntries[curDir].firstPicture;
                pics = DirectoryEntries[curDir].pictureCount();
                oneTimeIndexBackground = start+parseInt(Math.random()*pics);
            } 
            document.body.style.backgroundImage = "url("+escape(FileNames[oneTimeIndexBackground])+")";
            previousBackgroundDirectory = curDir;
            break;
    } 
} 

function hilite(id)
{
    reposition(id);
    //window.status = '';
    window.status=getFileFromFileName(FileNames[id]);
} 

function fullsize1(id)
{
    curPicNum = id;
    SwitchToFullClient();
    return  true;
} 

function setBigAss()
{
    var           inner;

    document.onkeydown = fullClientEventHandler;
    inFullSize = 2;
    if (curPicNum < 0)
        curPicNum = FileNames.length-1;
    if (curPicNum >= FileNames.length)
        curPicNum = 0;
    inner = '<img id="fullpicture" onclick="ExitFullClient()" src  class="fullClientPicture">';
    //inner += '<div id=debug class="debug" ondblclick="debugData=\'\';this.innerHTML=\'\'"></div>';
    inner += '<div id=toolbar2 class="toolbar">';
    inner += '<table border=0 cellspacing=0 cellpadding=0 class=toolbartable>';
    inner += '<tr><td class=tbsep><img border=0 src="tn_/'+(maximized?"min":"max")+'pixel.gif" onclick="sizewindow()">';
    inner += '<tr><td><img class=t1 src="tn_/gnext.gif" onclick="smartNextPic(1);setBigAss()" onmouseover="iSet(this,1)" onmouseout="iSet(this,4)">';
    inner += '<tr><td><img class=t1 src="tn_/gindex.gif" onclick="ExitFullClient()" onmouseover="iSet(this,2)" onmouseout="iSet(this,5)">';
    inner += 
        '<tr><td class=tbsep><img class=t1 src="tn_/gprev.gif" onclick="smartNextPic(-1);setBigAss()" onmouseover="iSet(this,0)" onmouseout="iSet(this,3)">';
    inner += '<tr><td><img class=t1 src="tn_/'+(fitFullClient?"expand":"collapse")+'.gif" onclick="toggleSize()">';
    inner += '<tr><td><img class=t'+(randomized?"0":"1")+' src="tn_/random.gif" onclick="toggleRandom()">';
    inner += '<tr><td class=tbsep><img class=t'+(allowTimer?'0':"1")+' src="tn_/timer.gif" onclick="toggleTimer();setBigAss()">';
    inner += '<tr><td><img class=t'+((toolStatus == 3)?"0":"1")+'  src="tn_/'+((toolStatus == 3)?"locked":"unlocked")+'.gif" onclick="toolStatus='+((toolStatus 
        == 3)?2:3)+';setBigAss()">';
    inner += '</table></div>';
    document.body.innerHTML = inner;
    moveToolBar();
    setTimeout('moveToolBar()', 100);
    document.onmousemove = checkToolBar;
    if (fitFullClient)
        document.getElementById("fullpicture").onload = resizeFullClientPictureToFit;
    else
        document.getElementById("fullpicture").onload = FullClientPictureActualSize;
    addToHistory(curPicNum);
    document.getElementById("fullpicture").src = escape(FileNames[curPicNum]);
    window.status=getFileFromFileName(FileNames[curPicNum]);
} 

function smartNextPic(direction)
{
    if (!randomized)
        curPicNum += direction;
    else
        if (direction == 1)
            curPicNum = getRandom();
        else
            if (viewHistory.length > 1)
            {
                var           n = viewHistory[viewHistory.length-2];
          
                viewHistory.splice(viewHistory.length-2, 2);
                curPicNum = n;
            } 
} 

function toggleRandom()
{
    randomized = !randomized;
    fitFullClient = true;
    setBigAss();
} 

function newrandpic()
{
    clickedPic(getRandom(), true);
} 

function sizewindow()
{
    if (maximized)
    {
        self.resizeTo(screen.width/2, screen.height/2);
        self.moveTo(screen.width/2, screen.height/2);
    } 
    else
    {
        self.moveTo(0, 0);
        self.resizeTo(screen.width, screen.height);
    } 
    maximized = !maximized;
    setBigAss();
} 

function minwindow()
{
} 

function toggleSize()
{
    fitFullClient = !fitFullClient;
    setBigAss();
} 

function toggleTimer()
{
    clearTimeout(timer);
    allowTimer = !allowTimer;
    if (allowTimer)
    {
        timeLeft = delays[slideDelay]+1;
        timer = setTimeout('processTimer();', 400);
    } 
    else
        window.status = '';
} 

function processTimer()
{
    clearTimeout(timer);
    if (allowTimer)
    {
        if (0 >= --timeLeft)
        {
            inIndex = 0;
            if (randomized)
                curPicNum = getRandom();
            else
            {
                ++curPicNum;
                curPicNum = (curPicNum >= FileNames.length)?0:curPicNum;
            } 
            FormatPage();
            timeLeft = delays[slideDelay]+1;
            return ;
        } 
        timer = setTimeout('processTimer();', 1000);
        window.status = timeLeft+" second"+((timeLeft > 1)?"s":"")+" until next picture";
    } 
    else
        window.status = "";
} 

function getRandom()
{
    return  Math.round(Math.random()*FileNames.length);
} 

function clickedPic(id,allowOpen)
{
    allowTimer = false;
    clearTimeout(timer);
    id = (id >= FileNames.length)?0:id;
    id = (0 > id)?FileNames.length-1:id;
    curPicNum = id;
    addToHistory(id);
    if (allowOpen != false)
        openDirectory(curPicNum);
    FormatPage();
    bShowAllFiles = 0;
} 

function clickedPicWithoutLocate(id)
{
    allowTimer = false;
    clearTimeout(timer);
    id = (id >= FileNames.length)?0:id;
    id = (0 > id)?FileNames.length-1:id;
    curPicNum = id;
    FormatPage();
    bShowAllFiles = 0;
} 

function addToHistory(id)
{
    if (inFullSize > 0)
        if (viewHistory.length == 0 || viewHistory[viewHistory.length-1] != id)
            viewHistory[viewHistory.length] = id;
} 

function indexEventHandler(e)
{
    if (!e)
        e = window.event;
    keycode = e.keyCode;
    if (keycode == 192)
    {
        document.location.href = "about:blank";
        return  false;
    } 
          
    szCurrentDir = FileNames[curPicNum];
    szCurrentDir = szCurrentDir.substring(0, szCurrentDir.lastIndexOf('/'));
    for (section = 0; section < SectionStart.length && (curPicNum < SectionStart[section] || curPicNum > SectionEnd[section]); section++)
        ;
    if (keycode == 82 || keycode == 191)                   // r
    {
        curPicNum = Math.round(Math.random()*FileNames.length);
        if (e.shiftKey || keycode == 191)
            SwitchToFullClient();
        else
        {
            DirectoryEntries[""].closeDirectory();
            DirectoryEntries[""].open = true;
            bFullIndex?fIndexAll():fIndex();
        } 
        return  false;
    } 

    if (bFullIndex)
    {
        start = getFirstFileInSection(szCurrentDir);
        end = start+filesPerDir(szCurrentDir)-1;
    } 
    else
    {
        start = SectionStart[section];
        end = SectionEnd[section];
    } 
    document.getElementById('config').style.visibility = 'hidden';
    switch (keycode)
    {
        case 33 :
        case 34 :
        case 32 :
        case 8 :
        case 9 :
            if ((keycode == 9 && e.shiftKey) || keycode == 8 || keycode == 33)
            {
                if (--section < 0)
                    section = SectionStart.length-1;
            } 
            else
            {
                if ((++section) > SectionStart.length-1)
                    section = 0;
            } 
            curPicNum = SectionStart[section];
            bFullIndex?fIndexAll():fIndex();
            break;
        case 13 :
            if (e.shiftKey)
            {
                bFullIndex = 1-bFullIndex;
                bFullIndex?fIndexAll():fIndex();
            } 
            else
                SwitchToFullClient();
            break;
        case 37 :
            if (--curPicNum >= start)
                reposition(curPicNum);
            else
                bFullIndex?fIndexAll():fIndex();
            break;
        case 38 :
            curPicNum -= idNumbersPerLine;
            if (curPicNum >= start)
                reposition(curPicNum);
            else
                bFullIndex?fIndexAll():fIndex();
            break;
        case 39 :
            if (++curPicNum <= end)
                reposition(curPicNum);
            else
                bFullIndex?fIndexAll():fIndex();
            break;
        case 40 :
            curPicNum += idNumbersPerLine;
            if (curPicNum > FileNames.length)
            {
                curPicNum -= FileNames.length;
                bFullIndex?fIndexAll():fIndex();
            } 
            else
                if (curPicNum <= end)
                    reposition(curPicNum);
                else
                    bFullIndex?fIndexAll():fIndex();
            break;
        case 36 :
            curPicNum = 0;
            clickedPic(curPicNum, true);
            return  false;

        case 35 :
            curPicNum = FileNames.length-1;
            clickedPic(curPicNum, true);
            return  false;

        case 66 :                                          // b find background
            if (e.shiftKey)
            {
                if (2 < ++useRandomIndexBackgroundEachTime)
                    useRandomIndexBackgroundEachTime = 0;
                setBackgroundPicture();
            } 
            else
                if (useRandomIndexBackgroundEachTime > 0)
                {
                    curPicNum = oneTimeIndexBackground;
                    SwitchToFullClient();
                } 
            break;
        case 67 :                                          // c collapse
            (e.shiftKey)?collapseToFirstLevel():collapse();
            break;
        case 68 :                                          // d change delay
            if (delays.length <= ++slideDelay)
                slideDelay = 0;
            return  false;
            break;

        case 69 :                                          // e expand
            if (e.shiftKey)
            {
                DirectoryEntries[""].expandDirectory();
                clickedPic(curPicNum, false);
            } 
            else
                expand();
            break;
        case 71 :                                          // g
            useGrayScale = 1-useGrayScale;
            clickedPic(curPicNum, false);
            return ;
        case 113 :
            toggleSettingsDialog();
            return  false;
          
        case 83 :                                          // s start timer
            SwitchToFullClient();
            toggleTimer();
            setBigAss();
            return  false;
          
        case 87 :                                          // w
            toSaveWallpaper = (e.shiftKey && e.ctrlKey);
            wallpaperCandidates();
            return  false;
        default  :
            return  true;
    } 
    return  false;
} 

function fullClientEventHandler(e)
{
    if (document.all)
        e = window.event;
    keycode = e.keyCode;
    if (keycode == 9)
    {
        sub = 1;
        keycode = (e.shiftKey)?37:39;
    } 
    else
        sub = 0;
    // alert("fullClientEventHandler("+ keycode+')')
    switch (keycode)
    {
        case 8 :
        case 37 :
        case 33 :
            smartNextPic(-1);
            setBigAss();
            return  false;

        case 32 :
        case 34 :
        case 39 :
            smartNextPic(1);
            setBigAss();
            return  false;

        case 82 :                                          // r random
        case 191 :                                         // slash or question on us keyboard
            if ((e.ctrlKey || e.shiftKey) && keycode == 82)
            {
                toggleRandom();
            } 
            else
            {
                curPicNum = getRandom();
                setBigAss();
            } 
            return  false;

        case 83 :                                          // s (slide show)
            toggleTimer();
            setBigAss();
            return  false;

        case 36 :
            curPicNum = 0;
            setBigAss();
            return  false;

        case 35 :
            curPicNum = FileNames.length-1;
            setBigAss();
            return  false;
          
        case 65 :                                          // a actual
            fitFullClient = false;
            setBigAss();
            return  false;
          
        case 70 :                                          // f fit
            fitFullClient = true;
            setBigAss();
            return  false;
          
        case 66 :                                          // b
        case 13 :                                          // enter
        case 27 :                                          // esc
            ExitFullClient();
            return  false;
          
        case 17 :
        case 16 :
        case 38 :
        case 40 :
        case 122 :
            return  true;
          
        case 71 :                                          // g
            return  false;
        default  :
          
            ExitFullClient();
            document.onkeydown(e);
    } 
} 

function findPosY(obj)
{
    var           curtop = 0;

    if (typeof(obj.offsetParent) != "undefined")
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        } 
    } 
    else
        if (obj.y)
            curtop = obj.y;
    return  curtop;
} 

function reposition(ind)
{
    obj = document.getElementById("cell"+ind);
    curPicNum = ind;
    if (obj)
    {
        oldY = newY = document.body.scrollTop;
        maxY = document.body.scrollTop+document.body.clientHeight-150;
        minY = document.body.scrollTop;
        objTop = findPosY(obj);
        if (objTop < minY)
            newY = objTop-100;
        else
            if (objTop > maxY)
                newY = objTop-document.body.clientHeight+150;
        if (oldY != newY)
            window.scroll(0, newY);
        if (ThumbTD != null)
            ThumbTD.className = "tdindex"+useGrayScale;
        ThumbTD = obj;
        obj.className = "tdindex-active";
    } 
} 

function wheel()
{
    if (allowTimer)
        toggleTimer();
    switch (inFullSize)
    {
        case 2 :
            if (event.wheelDelta <= -120)
                smartNextPic(1);
            else
                if (event.wheelDelta >= 120)
                    smartNextPic(-1);
            setBigAss();
            break;
        default  :
            if (event.wheelDelta >= 120)
            {
                if (--section < 0)
                    section = SectionStart.length-1;
            } 
            else
                if (event.wheelDelta <= -120)
                {
                    if ((++section) > SectionStart.length-1)
                        section = 0;
                } 
            clickedPic(SectionStart[section], true);
    } 
} 

function toggleSettingsDialog()
{
    if (document.getElementById('config').style.visibility != 'visible')
    {
        document.getElementById('config').style.left = parseInt((browserWindowWidth()-200)/2);
        document.getElementById('config').style.top = parseInt(browserWindowHeight()/5);
        document.getElementById('config').style.visibility = 'visible';
        document.getElementById('configOK').focus();
        document.getElementById('configSB').selectedIndex = useRandomIndexBackgroundEachTime;
        document.getElementById('configEF').selectedIndex = hidePagesIfOnlyOne;
        document.getElementById('configGS').selectedIndex = useGrayScale;
        document.getElementById('configSS').selectedIndex = slideDelay;
    } 
    else
        document.getElementById('config').style.visibility = 'hidden';
} 

function showhelp(act)
{
    document.getElementById("keyshelp").style.visibility = (act?"visible":"hidden");
} 

function iSet(obj,id)
{
    obj.src = 'tn_/'+buttonPreloads[id];
} 

function idxSet(id)
{
    iSet(document.getElementById("indexImg"), id)
} 

function hideCopyright(p)
{
    try
    {
        document.getElementById("copyright").style.right = p;
        if (p > -500)
            setTimeout('hideCopyright('+(p-20)+')', 50);
        else
            document.getElementById("copyright").style.display = "none";
    } 
    catch(e)
    {
        setTimeout('hideCopyright('+(p)+')', 150);
    } 
} 

function getColsPerLine()
{
    var           w;

    w = Math.max(2, parseInt((browserWindowWidth()-navWindowWidth)/160));
    return  w;
} 

function browserWindowWidth()
{
    var           w;

    if (window.innerWidth)
        w = window.innerWidth;
    else
        if (document.documentElement.clientWidth)
            w = document.documentElement.clientWidth;
        else
            w = document.body.clientWidth;
    return  w;
} 

function browserWindowHeight()
{
    var           w;

    if (window.innerHeight)
        w = window.innerHeight;
    else
        if (document.documentElement.clientHeight)
            w = document.documentElement.clientHeight;
        else
            w = document.body.clientHeight;
    return  w;
} 

function swapFullClient(e)
{
    var           button = 1;

    if (navigator.appName == "Netscape")
        button = e.which;
    else
        button = event.button;
    if (navigator.appName == "Netscape")
        switch (button)
        {
            case 3 :
                button = 2;
                break;
            case 2 :
                button = 4;
                break;
        } 
    switch (button)
    {
        case 1 :
            return  true;

        case 2 :
            if (!inFullSize)
            {
                toggleSettingsDialog();
                return  false;
            } 
            else
                return  true;
            break;
        default  :
          
            if (inFullSize)
            {
                toggleTimer();
                return  false;
            } 
            else
                return  true;
    } 
} 

function ExitFullClient()
{
    document.onmousemove = null;
    if (toolStatus != 3)
        toolStatus = 0;
    inFullSize = 0;
    document.body.style.overflow = "auto";
    document.onkeydown = indexEventHandler;
    document.body.innerHTML = oldpage;
    document.body.style.background = oldColor;
    document.body.style.backgroundImage = oldImage;
    document.oncontextmenu = function()
    {
        return  false;
    } 
          
    clickedPic(curPicNum, true);
} 

function SwitchToFullClient()
{
    oldpage = document.body.innerHTML;
    oldImage = document.body.style.backgroundImage;
    oldColor = document.body.style.background;
    document.body.style.backgroundImage = "";
    document.body.style.background = "black";
    document.oncontextmenu = null;
    setBigAss();
    return  false;
} 

function checkToolBar(e)
{
    if (document.all)
    {
        x = event.x;
        y = event.y;
    } 
    else
    {
        x = e.pageX;
        y = e.pageY;
    } 
    tb = document.getElementById("toolbar2");
    if ((toolStatus == -2) && (x > document.body.clientWidth-30))
    {
        clearTimeout(toolTimer);
        toolStatus = 0;
    } 
    else
        if ((toolStatus == -1) && (x < document.body.clientWidth-33))
        {
            clearTimeout(toolTimer);
            toolStatus = 0;
        } 
    switch (toolStatus)
    {
        case 0 :
            if (x > document.body.clientWidth-30)
            {
                toolStatus = -1;                           // waiting to expand
                toolTimer = setTimeout('expandTool()', 100);
            } 
            else
                tb.style.left = document.body.clientWidth-3;
            break;
        case 2 :
            if (x < document.body.clientWidth-33)
            {
                toolStatus = -2;                           // waiting to contract
                toolTimer = setTimeout('contractTool()', 500);
            } 
            else
                tb.style.left = document.body.clientWidth-30;
        case 3 :
            break;
    } 
} 

function expandTool()
{
    tb = document.getElementById("toolbar2");
    if (tb && toolStatus != 3)
        if (tb.offsetLeft > document.body.clientWidth-26)
        {
            toolStatus = 1;
            tb.style.left = tb.offsetLeft-4;
            toolTimer = setTimeout('expandTool()', 50);
        } 
        else
        {
            toolStatus = 2;
            tb.style.left = document.body.clientWidth-30;
        } 
} 

function contractTool()
{
    tb = document.getElementById("toolbar2");
    if (tb && toolStatus != 3)
        if (tb.offsetLeft < document.body.clientWidth-7)
        {
            toolStatus = -1;
            tb.style.left = tb.offsetLeft+4;
            toolTimer = setTimeout('contractTool()', 50);
        } 
        else
        {
            toolStatus = 0;
            tb.style.left = document.body.clientWidth-3;
        } 
} 

function moveToolBar()
{
    var           tb = document.getElementById("toolbar2");
          
    tb.style.left = document.body.clientWidth-((toolStatus >= 2)?30:3);
} 

function debug(line)
{
    var           d = document.getElementById('debug');

    if ('' != line)
        debugData += line+'<br>';
    if (d)
        d.innerHTML = debugData;
} 

function hideConfig()
{
    document.getElementById('config').style.visibility = 'hidden';
} 

function wallpaperCandidates()
{
    var           i,w,h,ratio,scrRatio;
          
    document.getElementById("wpProgress").style.visibility = "hidden";
    if (oldFileNames != null)
    {
        FileNames = oldFileNames;
        oldFileNames = null;
        StartPage();
    } 
    else
    {
        if (wpNames == null)
        {
            document.getElementById("wpProgress").style.visibility = "visible";
            document.getElementById("wpProgress").style.width = '5%';
            wpNames = new Array();
            wpImages = new Array();
            setTimeout('loadThumbForRatio()', 1);
        } 
        else
        {
            oldFileNames = FileNames;
            FileNames = wpNames;
            StartPage();
        } 
    } 
} 

var           imgp = 0,ximg = null;
function loadThumbForRatio()
{
    ximg = new Image();
    ximg.onload = getRatio;
    ximg.srcname = FileNames[++imgp];
    ximg.src = thumb(FileNames[imgp]);
} 

function getRatio()
{
    var           scrRatio = screen.height/screen.width;
    var           imgRatio = ximg.height/ximg.width;

    if (Math.abs(imgRatio-scrRatio) < 0.1)
        wpNames[wpNames.length] = ximg.srcname;
    if (imgp == FileNames.length-1)
    {
        document.getElementById("wpProgress").style.visibility = "hidden";
        wpImages = null;
        oldFileNames = FileNames;
        FileNames = wpNames;
        StartPage();
        if (toSaveWallpaper)
        {
            var           a = 'var wpNames = new Array(';
          
            for (i = 0; i < wpNames.length-1; i++)
                a += '\n\t"'+wpNames[i]+'",';
            if (wpNames.length > 0)
                a += '\n\t"'+wpNames[wpNames.length-1]+'"';
            a += ');';
            w = window.open('', 'name', 'resizable=yes');
            w.document.write('<html><head><title>Wallpaper Candidates</title></head>');
            w.document.write('<body bgcolor=#FFFFC0><pre>'+a+'</pre></body></html>');
            w.document.close();
        } 
    } 
    else
    {
        if (imgp%10 == 0)
            document.getElementById("wpProgress").style.width = Math.max(1, parseInt(100*imgp/FileNames.length))+'%';
        setTimeout('loadThumbForRatio()', 1);
    } 
} 
