// create a sortable out of our doc-ordering-list
function createSortableSpotlightList (fieldName) {
    MochiKit.Sortable.Sortable.create(fieldName + '-spotlight-sorting-list',
                                      {ghosting:true,constraint:'vertical',hoverclass:'over',
                                       onUpdate:function (element) {
                                           saveSpotlightReferences(element, fieldName)
                                       }
                                      });
}

function createSpotlightList(fieldName) {
    var spotlight_list = function (item) {
        var removal_id = fieldName + '-' + item[0] + '-removal';
        return LI({id:item[0]}, item[1] + ' ', A({id:removal_id},'X'));
    }
    // create a list with the current spotlights
    var sortableSpotlightList = UL({id:fieldName + '-spotlight-sorting-list'},map(spotlight_list, spotlightList));
    //log('the field name is',fieldName + '-spotlight-sorting', 'and the object', sortableSpotlightList)
    swapDOM($(fieldName + '-spotlight-sorting'),sortableSpotlightList);
    // setup listeners for the removal buttons
    var listChildren = $(fieldName + '-spotlight-sorting-list').childNodes;
    for (var i=0;i<listChildren.length;i++) {
        var removal_id = listChildren[i].childNodes[1].id;
        // setup listener for the 'X'
        connect(removal_id, 'onclick', rejectItem);
    }
    // set up listeners for the buttons
    connect(fieldName + '-add-button', 'onclick', addPendingItems);
    connect(fieldName + '-reject-button', 'onclick', rejectPendingItems);
    connect(fieldName + '-reject-add-button', 'onclick', addRejectedItems);
    connect(fieldName + '-pending-button', 'onclick', pendingRejectedItems);
    var in_out = function (item) {
        return OPTION({value:item[0], id:item[0]}, item[1]);
    }
    // setup the 'in' widget
    var in_widget = map(in_out, pendingList)
    replaceChildNodes(fieldName + '-inbox', in_widget);
    // now make the UL sortable
    createSortableSpotlightList(fieldName);
    // set the hidden input upon edit
    saveSpotlightReferences($(fieldName + '-spotlight-sorting-list'), fieldName);
}

function saveSpotlightReferences(referenceList, fieldName) {
    // get the li's from our sorted list
    var listChildren = referenceList.childNodes;
    
    var uid_list = function (list_item) {
        return list_item.id;
    }
    // get the uids so we can populate the options
    var spotlight_uids = map(uid_list,listChildren);
    var option_creator = function (option) {
        return OPTION({selected:"selected",value:option}, option);
    }
    // create the list of options
    var new_options = map(option_creator, spotlight_uids);
    // add the new list of options to the select box
    replaceChildNodes(fieldName + ':list', new_options);
}

function addPendingItems(event) {
    // generate the fieldName
    var fieldName = event.src().id.split('-')[0];
    // get all the options and then only act if 'selected'
    var inbox_options = $(fieldName + '-inbox').childNodes;
    var options_to_delete = new Array();
    for (var i=0;i<inbox_options.length;i++) {
        if (inbox_options[i].selected) {
            // create a unique id for the 'X'
            var removal_id = fieldName + '-' + inbox_options[i].value + '-removal';
            var new_li = LI({'id':inbox_options[i].value},inbox_options[i].text + ' ', A({id:removal_id},'X'));
            // add this 'option' to the ordering list
            appendChildNodes(fieldName + '-spotlight-sorting-list', [new_li]);
            // setup listener for the 'X'
            connect(removal_id, 'onclick', rejectItem);
            // add 'id' to the list to delete
            options_to_delete.push(inbox_options[i].id);
        } else {}
    }
    // remove the nodes from the select box
    for (var i=0;i<options_to_delete.length;i++) {
        removeElement(options_to_delete[i]);
    }
    // save the reference field upon adding the new items
    saveSpotlightReferences($(fieldName + '-spotlight-sorting-list'), fieldName);
    // make sure everything is still sortable
    createSortableSpotlightList(fieldName);
}

function rejectPendingItems(event) {
    // generate the fieldName
    var fieldName = event.src().id.split('-')[0];
    // get all the options and then only act if 'selected'
    var inbox_options = $(fieldName + '-inbox').childNodes;
    var options_to_move = new Array();
    for (var i=0;i<inbox_options.length;i++) {
        if (inbox_options[i].selected) {
            // add this element to the list to move
            options_to_move.push(inbox_options[i]);
        } else {}
    }
    // add this 'option' to the 'rejected' box
    for (var i=0;i<options_to_move.length;i++) {
        appendChildNodes(fieldName + '-outbox', [options_to_move[i]]);
    }
}

function addRejectedItems(event) {
    // generate the fieldName
    var fieldName = event.src().id.split('-')[0];
    // get all the options and then only act if 'selected'
    var inbox_options = $(fieldName + '-outbox').childNodes;
    var options_to_delete = new Array();
    for (var i=0;i<inbox_options.length;i++) {
        if (inbox_options[i].selected) {
            // create a unique id for the 'X'
            var removal_id = fieldName + '-' + inbox_options[i].value + '-removal';
            var new_li = LI({'id':inbox_options[i].value},inbox_options[i].text + ' ', A({id:removal_id},'X'));
            // add this 'option' to the ordering list
            appendChildNodes(fieldName + '-spotlight-sorting-list', [new_li]);
            // setup listener for the 'X'
            connect(removal_id, 'onclick', rejectItem);
            // add 'id' to the list to delete
            options_to_delete.push(inbox_options[i].id);
        } else {}
    }
    // remove the nodes from the select box
    for (var i=0;i<options_to_delete.length;i++) {
        removeElement(options_to_delete[i]);
    }
    // save the reference field upon adding the new items
    saveSpotlightReferences($(fieldName + '-spotlight-sorting-list'), fieldName);
    // make sure everything is still sortable
    createSortableSpotlightList(fieldName);
}

function pendingRejectedItems(event) {
    // generate the fieldName
    var fieldName = event.src().id.split('-')[0];
    // get all the options and then only act if 'selected'
    var inbox_options = $(fieldName + '-outbox').childNodes;
    var options_to_move = new Array();
    for (var i=0;i<inbox_options.length;i++) {
        if (inbox_options[i].selected) {
            // add this element to the list to move
            options_to_move.push(inbox_options[i]);
        } else {}
    }
    // add this 'option' to the 'rejected' box
    for (var i=0;i<options_to_move.length;i++) {
        appendChildNodes(fieldName + '-inbox', [options_to_move[i]]);
    }
}

function rejectItem(event) {
    var deletion_id = event.src().id.split('-');
    // generate the fieldName
    var fieldName = deletion_id[0];
    var object_id = deletion_id[1];
    // get the title from the parent text
    var object_title = event.src().parentNode.childNodes[0];
    // remove this item from the orderable list
    removeElement(object_id);
    // save the reference field upon adding the items
    saveSpotlightReferences($(fieldName + '-spotlight-sorting-list'), fieldName);
    // put the item back into the 'rejected' box
    appendChildNodes(fieldName + '-outbox', [OPTION({value:object_id, id:object_id, selected:"selected"}, object_title)]);
}

