/*
 * This activates the social links to certain pages on our site, depending on a URL format (/issue/)
 * Email this, print (v2), tweet, share on facebook, comment
*/

google.setOnLoadCallback( function() {
    if ( window.location.pathname.startsWith( '/issue' ) ) {
        var url = window.location.href;
        var req = new Ajax.Request(
            '/comments/query_num?url=' + encodeURIComponent(url),
            {
                method: 'get',
                onSuccess: function( transport ) {
                    var json = transport.headerJSON;
                    $('comment_url').writeAttribute( 'href', '/comments/view?url=' + escape(json['url']) );
                    var comment_num = json['num'];
                    if ( comment_num > 0 ) {
                        $('comment_num').update( ' (' + comment_num + ')' );
                    }
                    var subheads = $$('.subhead-top');
                    var links_div = $('social_links');
                    if ( subheads.length >= 1 ) {
                        $$('.atclear').invoke( 'remove' );
                        links_div.remove();
                        subheads[0].insert( links_div );
                    }
                    links_div.show();
                }
            }
        );
    }
} );
