// ==UserScript==
// @name Citation announcer
// @namespace http://www.mindtrove.info/
// @description Speaks the number of citations on a Wikipedia article
// @include http://*.wikipedia.org/wiki/*
// @require http://www.json.org/json2.js
// @require http://outfox.googlecode.com/svn/tags/release-0.3.5/js/outfox.js
// ==/UserScript==
    
// number of major sections
var sections = 0;

function onOutfoxAudioInit(response) {
    // say the number of main sections
    outfox.audio.say(sections + ' main sections');
    // return the parameter for other outfox deferred callbacks
    return response;
}

function onOutfoxInit(version) {
    var content = document.getElementById('bodyContent');
    // count the number of main sections
    sections = content.getElementsByTagName('h2').length;
    // take one back for the TOC heading if it's present
    if(document.getElementById('toc')) {
        --sections;
    }
    // start the outfox audio service
    var def = outfox.startService('audio');
    def.addCallback(onOutfoxAudioInit);
    // return the parameter for other outfox deferred callbacks
    return version;
}

function onDOMContentLoaded() {
    // create a node for outfox use
    var div = document.createElement('div');
    document.body.appendChild(div);
    // initialize outfox
    var def = outfox.init(div, JSON.stringify, JSON.parse);
    def.addCallback(onOutfoxInit);
}

// this event triggers execution of the GM script
onDOMContentLoaded();