$(document).ready(function(){
    
    $('.js-hide').remove();
    
    $('select#newsfilter_month, select#newsfilter_year, select#newsfilter_issue').change( function() {
          location.href = $(this).val();
    });
    
    if( $('div#scorecard_wrapper ul.navigation_tabs li.ui-tabs-selected').length < 1 ){
        $('div#scorecard_wrapper ul.navigation_tabs li:eq(0)').addClass('ui-tabs-selected');
    }

    
    // SMOOTH SCROLLING
    $('a.scrollto').live('click',function(){
        var trgt = $(this).attr('href')
       $('html, body').animate({scrollTop: $(trgt).offset().top - 65}, 500);
       return false;
    });
    
    // DATACENTER CROP NAVIGATION
    $('div#datacenter_scorecard ul.navigation_tabs li a').live('click', function(){
        $('div#scorecard').fadeTo("slow", 0.33).height($('div#scorecard').height()).html('Loading...');
        $('div#datacenter_scorecard ul.navigation_tabs li.ui-tabs-selected').removeClass('ui-tabs-selected');
        $(this).parent('li').addClass('ui-tabs-selected');
        var new_scorecard = $('<div id="scorecard" style="display:none;"></div>');
        var target = $(this).attr('href').split('#');
        $('div#datacenter_scorecard').height($('div#datacenter_scorecard').height());
        new_scorecard.load(target[0]+ ' div#scorecard_content', function(){
            $('div#scorecard').replaceWith(new_scorecard);
            $('div#scorecard').fadeIn('slow');
            $('div#datacenter_scorecard').css({height:''});
        });
        return false;
    });
    
    
    
// BITS AND BOBS

    $('body').addClass('js-enabled');
    $('ul#navigation_glossary li:last').addClass('last');
    $('div#related_content ul li:last').addClass('last');
    $('div#spotlight_wrapper ul#blog_listing li:last, div#spotlight_wrapper ul#video_listing li:last, div#spotlight_wrapper ul#news_listing li:last, div#spotlight_wrapper ul#document_listing li:last, div#spotlight_wrapper ul#photo_listing li:last, div#spotlight_wrapper ul#audio_listing li:last').addClass('last');
    $('div#datacenter_popup tr:odd').addClass('alt');
    // $('div#spotlight_overview, div#spotlight_blog, div#spotlight_videos, div#spotlight_news, div#spotlight_documents, div#spotlight_photos, div#spotlight_audio').jScrollPane();
        
// CALL FUNCTIONS

    issue_intros();
    news_commentary();
    $('div.sidebar_block#share ul').print_link();
    $('div#datacenter_popup').datacenter_popup_position();

    
});

// DEALS WITH THE INTRO PARAGRAPHS ON ISSUE PAGES
function issue_intros(){
    
    $('div#issue_intro_intro div.more').hide();
    $('div#issue_intro_intro div.continued p:last').append('<a href="#" class="more_toggle">Continue reading</a>');
    $('a.more_toggle').click(function(){
        $(this).toggleClass('down');
        $('div#issue_intro_intro div.more').slideToggle('medium');
        return false;
    });
    
}



// DROPDOWN COMMENTARY OF NEWS LISTINGS
function news_commentary(){
    
    $('div.news_commentary').hide();
    $('ul#news_listing li p.meta.commentary').append('| <a href="#" class="commentary_toggle">Read commentary</a>');
    $('a.commentary_toggle').click(function(){
        $(this).toggleClass('down');
        $(this).parent('p').next('div').slideToggle('medium');
        return false;
    });
    
}



// PRINT LINK
$.fn.print_link = function(){
    this.each(function(){
       $(this).append('<li class="print"><a href="#">Print this page</a></li>');
       $('li.print a').click(function(){
           window.print();
           return false;
       });
    });
}

function onCountryClick( url_title )
{
    if( $.isArray(url_title) ){ 
        url_title = url_title[1];
        $('select[name="country_select"]').unbind('change').val(url_title).datacenter_country_select();
    }

    // fix the body height to prevent any jumping about
    $('body').height($('body').height());
    $('div#datacenter_controls').addClass('loading');
    $('div#datacenter_instructions').html('<p>Loading...</p>').show();
    
    if( $('div#datacenter_popup').length ) {
        $('div#datacenter_popup').fadeOut('fast', function(){
            $.ajax({
                url: '/country-snapshots/'+url_title,
                async: true,
                success:function(new_html){
                    // replace 3 items: stats, scorecard, sidebar
                    $('div#datacenter_popup').replaceWith($('div#datacenter_popup', new_html).hide().fadeIn('slow'));
                    $('div#datacenter_popup tr:odd').addClass('alt');
                    $('div#datacenter_scorecard').replaceWith($('div#datacenter_scorecard', new_html));
                    $('div#datacenter_countryinfo').replaceWith($('div#datacenter_countryinfo', new_html));
                    $('body').css({height:''});
                    $('div#datacenter_controls').removeClass('loading');
                    $('div#datacenter_popup').datacenter_popup_position();
                    if( $('div#scorecard_wrapper ul.navigation_tabs li.ui-tabs-selected').length < 1 ){
                        $('div#scorecard_wrapper ul.navigation_tabs li:eq(0)').addClass('ui-tabs-selected');
                    }
                    $('div.sidebar_block#share ul').print_link();
                    $('div#datacenter_instructions').hide();
                    $('a.modal').colorbox({innerWidth:'500px'});
                }
            });
        });
    } else {
         $.ajax({
            url: '/country-snapshots/'+url_title,
            async: true,
            success:function(new_html){
                $('div#datacenter_interface').append($('div#datacenter_popup', new_html));
                $('div#datacenter_popup tr:odd').addClass('alt');
                $('div#datacenter_scorecard', new_html).appendTo($('div#datacenter_content'));
                $('div#datacenter_countryinfo', new_html).appendTo($('div#datacenter_content'));
                $('body').css({height:''});
                $('div#datacenter_controls').removeClass('loading');
                $('div#datacenter_popup').datacenter_popup_position();
                if( $('div#scorecard_wrapper ul.navigation_tabs li.ui-tabs-selected').length < 1 ){
                    $('div#scorecard_wrapper ul.navigation_tabs li:eq(0)').addClass('ui-tabs-selected');
                }
                $('div.sidebar_block#share ul').print_link();
                $('div#datacenter_instructions').hide();
                $('a.modal').colorbox({innerWidth:'500px'});
            }
       });
   }
}

// NAVIGATE TO A SPECIFIC COUNTRY WHEN THE COUNTRY SELECT CHANGES
$.fn.datacenter_country_select = function ( )
{
    $(this).change(function() {
        url_title = $(this).val();
        onCountryClick(url_title);
        changeCountry($('option[value="'+url_title+'"]',this).html());
        return $(this);
     });
}

// VERTICALLY POSITIONS THE DATA CENTER POPUP WITHIN THE GLOBE AREA
$.fn.datacenter_popup_position = function() {
    var popup_height        = $(this).height() + 30;
    var popup_difference    = 375 - popup_height;
    var popup_margin        = popup_difference / 2;
    return $(this).css({top: popup_margin});     
}
