// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// Useful way to have a confirm_destroy button in unobstrusive javascript
function confirm_destroy(element, action) {
    if (confirm("Are you sure?")) {
        var f = document.createElement('form');
        f.style.display = 'none';
        element.parentNode.appendChild(f);
        f.method = 'POST';
        f.action = action;
        var m = document.createElement('input');
        m.setAttribute('type', 'hidden');
        m.setAttribute('name', '_method');
        m.setAttribute('value', 'delete');
        f.appendChild(m);
        
        // Authentification token, used agains CSFR
        var m = document.createElement('input');
        m.setAttribute('type', 'hidden');
        m.setAttribute('name', AUTH_TOKEN_NAME);
        m.setAttribute('value', AUTH_TOKEN_VALUE);
        f.appendChild(m);
        
        
        f.submit();
    }
    return false;
}

document.observe("dom:loaded", function() {
    // This hides all the elements with class .antiJSBlock calling CSS hide
    $$('.antiJSBlock').invoke('hide');
    
    // This is used in case Ajax is not activated
    if (!Ajax.getTransport()) {
        $$('form').each(function(s){
            if (s.hasAttribute("onsubmit")) {
                s.onsubmit = null;
            };
        });
    };
});

// This is only used in Admin::Users
document.observe("dom:loaded", function() {
    var ldapCheckbox = $('user_is_ldap_user');
    if (ldapCheckbox != null && ldapCheckbox.checked) {
        $('password_fields').hide();
    };
});

// This method is only used in absence views
function changeReason(current) {
    $$('.antiJSBlock').invoke('hide');
    if (current.selectedIndex != 0) {
        $(current.options[current.selectedIndex].value).show();
    };
};

// This as well
document.observe("dom:loaded", function() {
    var current = $('absence_reason');
    if (current != null && current.selectedIndex != 0) {
        $$('#' + current.options[current.selectedIndex].value).invoke('show');
    };
});

// This is only used in Inventory::Workstations
document.observe("dom:loaded", function() {
    // Is there a list of plug? If not, we're probably not on the right page...
    var plugList = $('workstation_plug_id');
    // ... so we do nothing.
    if (plugList == null) { return; }
    
    // We need a new "Room" select
    var roomListLabel = document.createElement('p');
    var label = new Element('label', { 'for' : 'workstation_room_id' }).update('Room');
    roomListLabel.appendChild(label);

    var roomList = document.createElement('select');
    roomList.writeAttribute({'id' : 'workstation_room_id'});
    var emptyEntry = new Element('option', { 'value' : 0 }).update('    ');
    roomList.appendChild(emptyEntry);
    
    roomListLabel.appendChild(roomList);
    
    // We need a new "Plug" select
    var newPlugList = document.createElement('select');
    newPlugList.writeAttribute({'name' : 'workstation[plug_id]'});
    newPlugList.writeAttribute({'id' : 'workstation_plug_id'});
    emptyEntry = document.createElement('option');
    emptyEntry.appendChild(document.createTextNode('Choose room first'));
    
    // We will keep the Plugs in this Array, first index is the room
    var plugArray = new Array();
    plugArray[0] = new Array();
    plugArray[0].push(emptyEntry);
    
    plugList.getElementsBySelector('optgroup').each(function(optgroup, id) {
        var roomId = id + 1;
        
        var room = document.createElement('option');
        room.appendChild(document.createTextNode(optgroup.label));
        room.value = roomId;
        roomList.appendChild(room);
        
        plugArray[roomId] = new Array();
        
        optgroup.childElements().each(function(option) {
            var plug = document.createElement('option');
            plug.appendChild(document.createTextNode(option.textContent));
            plug.value = option.value
            
            // If a plug is selected (probably because we're editing the workspace,
            // and not creating a new one), we need to select the room to which it
            // belongs
            if (option.selected) {
                plug.selected = true;
                room.selected = true;
            }
            // Store the plug for displaying later
            plugArray[roomId].push(plug);
        });
        
        
        if (room.selected) {
            // If a plug was selected, we display all the other plugs in that room
            plugArray[roomId].each(function(plug) {
                newPlugList.appendChild(plug);
            });
        }
    });
    
    if (newPlugList.down() == null) {
        newPlugList.appendChild(emptyEntry);
    }
    
    // Register the event listener
    roomList.observe('change', function() {
        newPlugList.update('');
        plugArray[$F(roomList)].each(function(plug) {
            newPlugList.appendChild(plug);
        });
    });
    
    var containingParagraph = plugList.parentNode;
    if (containingParagraph.nodeName == 'SPAN') {
        containingParagraph = containingParagraph.parentNode;
    }
    
    containingParagraph.parentNode.insertBefore(roomListLabel, containingParagraph);
    plugList.parentNode.replaceChild(newPlugList, plugList);

});
