1/**
2 * Called by picker buttons to insert text and close the picker again
3 *
4 * @author Gabriel Birke <gb@birke-software.de>
5 */
6function indexnumberPickerInsert(text, edid) {
7    var rx = new RegExp('<idxnum\\s+' + text + '\\s+#(\\d+)', 'g'),
8        editor = document.getElementById(edid),
9        maxnumber = 0,
10        result;
11    while ((result = rx.exec(editor.value)) !== null) {
12        maxnumber = Math.max(maxnumber, result[1]);
13    }
14    maxnumber++;
15    insertTags(edid, '<idxnum ' + text + ' #' + maxnumber + ' >', '</idxnum>', '');
16    pickerClose();
17}
18
19/**
20 * Creates a picker window for inserting
21 *
22 * @author Gabriel Birke <gb@birke-software.de>
23 */
24function createIndexnumberPicker(id, list, edid) {
25
26    function getInsertHandler(idxname) {
27        return function () {
28            indexnumberPickerInsert(idxname, edid);
29        };
30    }
31    var $picker, $btn, i;
32    $picker = jQuery('<div class="picker"/>')
33        .attr('id', id);
34
35    for (i = 0; i < list.length; i++) {
36        $btn = jQuery('<button class="pickerbutton" />')
37            .text(list[i])
38            .click(getInsertHandler(list[i]));
39        $picker.append($btn);
40    }
41    jQuery("body").append($picker);
42    return $picker;
43}
44
45
46/**
47 * Add button action for picker buttons and create picker element
48 *
49 * @param  object     $btn  Button element to add the action to (jQuery object)
50 * @param  array      props Associative array of button properties
51 * @param  string     edid  ID of the editor textarea
52 * @return boolean    If button should be appended
53 * @author Gabriel Birke <gb@birke-software.de>
54 */
55function addBtnActionIndexnumberpicker($btn, props, edid) {
56    var pickerid = 'picker' + (pickercounter++);
57    createIndexnumberPicker(pickerid, props.list, edid);
58    $btn.click(function (evt) {
59        pickerToggle(pickerid, $btn);
60        evt.preventDefault();
61        return false;
62    });
63    return true;
64}
65
66
67/*jslint plusplus: true, undef: true, sloppy: true, browser: true */
68