phpedit = {};

phpedit.dependency = {
    remove: function( item ) {
        while( item.nodeName != 'LI' ) {
            if ( item.nodeName == 'BODY' || !item.parentNode ) { return; }
            item = item.parentNode;
        }

        var parent = item.parentNode;
        parent.removeChild( item );

        var inputs = parent.getElementsByTagName('input');
        if ( inputs.length == 0 ) { parent.getElementsByTagName('li')[0].style.display = 'list-item'; }
    },

    add: function( id, identifier, name ) {
        if ( !identifier ) { return; }
        var element = document.getElementById( id );
        if ( !element ) { throw 'Element with id ' + id + ' not found'; }

        var inputs = element.getElementsByTagName( 'input' );
        for( var y = 0; y < inputs.length; y++ ) {
            var input = inputs[ y ];
            if ( input.type != 'hidden' ) { continue; }
            if ( input.value == identifier ) { return; }
        }
        if ( inputs.length == 0 ) { element.getElementsByTagName('li')[0].style.display = 'none'; }

        var container = document.createElement('li');

        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'dependency[]';
        input.value = identifier;
        container.appendChild(input);

        container.appendChild( document.createTextNode( name + ' (' ) );

        var removeLink = document.createElement('a');
        removeLink.href = 'javascript:void(0)';
        removeLink.onclick = function() { phpedit.dependency.remove( this ); }
        removeLink.appendChild( document.createTextNode( 'remove' ) );

        container.appendChild( removeLink );
        container.appendChild( document.createTextNode( ')' ) );

        element.appendChild( container );
    },

    display: function( container, id ) {
        var pool = document.getElementById( container );
        if ( !pool ) { throw 'Container ' + container + ' not found'; }

        var divElements = pool.getElementsByTagName('div');
        for( var y = 0; y < divElements.length; y++ ) {
            var item = divElements[ y ];
            if ( item.className != 'snippetDependencyVersion' ) { continue; }
            item.style.display = 'none';
        }

        var activeItem = document.getElementById( 'dependenciesVersionsFor-' + id );
        if ( !activeItem ) { return; }
        activeItem.style.display = 'block';
    }
}