1/**
2 * Page behaviours
3 *
4 * This class adds various behaviours to the rendered page
5 */
6dw_bibtex = {
7    /**
8     * initialize page behaviours
9     */
10    init: function(){
11        jQuery('span.bibtex_citekey').mouseover(dw_bibtex.bibtexReferenceDisplay);
12    },
13
14    /**
15     * Create/get a insitu popup used by the footnotes
16     *
17     * @param target - the DOM element at which the popup should be aligned at
18     * @param popup_id - the ID of the (new) DOM popup
19     * @return the Popup jQuery object
20     */
21    insituPopup: function(target, popup_id) {
22        // get or create the popup div
23        var $bibtexdiv = jQuery('#' + popup_id);
24
25        // popup doesn't exist, yet -> create it
26        if($bibtexdiv.length === 0){
27            $bibtexdiv = jQuery(document.createElement('div'))
28                .attr('id', popup_id)
29                .addClass('insitu-bibtexref JSbibtexref')
30                .mouseleave(function () {jQuery(this).hide();});
31            jQuery('.dokuwiki:first').append($bibtexdiv);
32        }
33
34        // position() does not support hidden elements
35        $bibtexdiv.show().position({
36            my: 'left top',
37            at: 'left center',
38            of: target
39        }).hide();
40
41        return $bibtexdiv;
42    },
43
44    /**
45     * Display an insitu bibtex reference popup
46     *
47     * @author Andreas Gohr <andi@splitbrain.org>
48     * @author Chris Smith <chris@jalakai.co.uk>
49     * @author Till Biskup <till@till-biskup.de>
50     */
51    bibtexReferenceDisplay: function () {
52        var content = jQuery(jQuery(this)).html();
53
54        if (content === null){
55            return;
56        }
57
58        // strip the leading content anchors and their comma separators
59        content = content.replace(/(^.*<span>)+\s*/gi, '');
60
61        // prefix ids on any elements with "insitu__" to ensure they remain unique
62        content = content.replace(/\bid=(['"])([^"']+)\1/gi,'id="insitu__$2');
63
64        // now put the content into the wrapper
65        dw_bibtex.insituPopup(this, 'insitu__bibtex').html(content).show();
66    }
67};
68
69jQuery(dw_bibtex.init);
70