jQuery(function($)
{

    if ($('body').hasClass("country_page")) {
        var sparkline_init = function()
        {
            $('#main_body .key_indicators .sparkline').sparkline('html', {
                'width': '25px',
                'height': '15px',
                'lineColor': 'black',
                'fillColor': '',
                'spotColor': '#df0002',
                'minSpotColor': '',
                'maxSpotColor': ''
                // ,'spotRadius': '2' // causes spots not to render in IE 7, but the default (1.5?) is okay
            });
        }();
    } 
    var briefing_tabs_init = function()
    {
        // add #notabs to the url to view w/o tabs
        if (location.href.match(/#notabs\b/))
            return;
        var tabs = $('#main_body h2.featured-content, div.briefing-modules h2');
        var panes = $('div.featured-content-area, div.briefing-modules > div');
        // redundancy / sanity check
        if (tabs.length < 2 || tabs.length != panes.length) {
            return;
        }

        // this can be removed when all the " for [country]" in module titles are removed
        tabs.each(function()
        {
            var self = $(this);
            self.html(self.html().replace(/ for .*/, ''));
            self.html().match(/Featured Content/i) && self.html('Featured');
        });
        
        // make a reference in each tab to its corresponding pane
        for (var i=0; i < tabs.length; i++) {
            tabs.eq(i).data('my_pane', panes.eq(i));
        }

        // put all the tabs in a new div above the lists
        $('.briefing-modules').prepend(panes).prepend($('<div class="tabs"></div>').prepend(tabs));

        // style our stuff taberifically
        $('#main_body').addClass('briefing-tabify');

        // add the "critical issues" header
        $('.briefing-modules').before('<h2 class="critical-issues">Critical Issues</h2>');

        // activate the tabs
        tabs.bind('click', function()
        {
            tabs.removeClass('active');
            $(this).addClass('active');
            panes.hide();
            $(this).data('my_pane').show();
        });
        // click the first tab
        tabs.eq(0).click();
    }();

    // fix an IE8 rendering bug
    $('body').hide().show();

});