Shadowbox.init({
    // let's skip the automatic setup because we don't have any
    // properly configured link elements on the page
    skipSetup: true
});
// white balloon with default configuration
// (see http://www.wormbase.org/wiki/index.php/Balloon_Tooltips)
var balloon = new Balloon;
balloon.delayTime = 30;
//BalloonConfig(balloon,'GBubble');
/* // plain balloon tooltip
var tooltip = new Balloon;
BalloonConfig(tooltip,'GPlain');

// fading balloon
var fader = new Balloon;
BalloonConfig(fader,'GFade');

// a plainer popup box
var box = new Box;
BalloonConfig(box,'GBox');

// a box that fades in/out
var fadeBox = new Box;
BalloonConfig(fadeBox,'GBox');
fadeBox.bgColor = 'black';
fadeBox.fontColor = 'white';
fadeBox.borderStyle = 'none';
fadeBox.delayTime = 200;
fadeBox.allowFade = true;
fadeBox.fadeIn = 750;
fadeBox.fadeOut = 200;

*/
var tocNumber = [];

function GetSiblingNumber(oElement, selector) {
    return $(oElement).parent().children(selector).index(oElement);
}

$(document).ready(function () {
    if (typeof chapter !== "undefined") {
        var openFunction = "tamchap" + chapter;
        window[openFunction].o(1);
        var tocTarget = "#chap" + chapter;
    }
	
    $toc = $('div#p-Table_of_Contents div.body');
    
   if (typeof tocTarget !== "undefined") {
        $toc.scrollTo(tocTarget);
    }
    if (typeof chapter !== "undefined") {
        $(".tocnumber, .mw-headline").each(function () {
            var rgxp = new RegExp("1([\\d\\.]+)?(.*)");
            var repl = '<span class="numbers">' + chapter + '$1</span> $2';
            this.innerHTML = this.innerHTML.replace(rgxp, repl);
        });
    }
    Shadowbox.setup("a.thumbimage", {});
    $("a.external").attr("title", "Opens in a new window").live('click', function () {
        window.open(this.href); // pop a new window
        return false; // return false to keep the actual link click from actuating
    });
    $("sup.reference a").mouseover(function (event) {
        var footnote = $($(this).attr("href")).clone().contents('a:first').remove().end().html(); // this gets the whole element in text
        // var footnote = $(this).attr("href"); // this gets the href
        // var footnote = footnote.replace("#",""); // this is the id of the citation
        // balloon.showTooltip(event,'load:'+footnote,1,250); // this loads the content of the citation div
        balloon.showTooltip(event, footnote, 1);
    });
    $('.dTreeNode').each(function (rootIndex, root) {
        // select link directly?
        $chapternode = $(this).parents('div[id^="chap"]');
        if (!($chapternode.length)) {
            return;
        }
        var chapterNode = $chapternode[0].id;
        var chapterNodeNum = chapterNode.replace("chap", "");
        var depth = $(this).parents('.clip').length;
        var relativeIndex = GetSiblingNumber(this, ".dTreeNode");
        if ((tocNumber[depth - 1] != relativeIndex + 1) && (tocNumber[depth])) {
            tocNumber.splice(depth, 1);
        }
        tocNumber[depth - 1] = relativeIndex + 1;
        tocNumber[0] = chapterNodeNum;
        var $tocLink = $(this).find('a:last');
        var unnumbered = $tocLink.text();
        $tocLink.html("<span class='number'>" + tocNumber.join('.') + "</span>" + " " + unnumbered);
    });
});
