<div class="entry-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('Pages Permissions') ?></h4>
    </div>
    <input type="hidden" name="pages" id="store_permissions" value="" style="width:100%;" />
    <fieldset>
        <span class="field-row" id="permissions_container">
            <label><?php echo $this->__('Pages') ?></label>
            <div class="f-left">
                <div class="tree x-tree" id="content-tree"></div>
            </div>
        </span>
    </fieldset>
</div>
<?php $this->getResTreeJson() ?>
<!-- Draw Resources Tree -->
<script type="text/javascript">
<?php if($this->getEverythingAllowed()): ?>
    $('permissions_container').hide();
<?php endif; ?>
Ext.EventManager.onDocumentReady(function() {
    var tree = new Ext.tree.TreePanel('content-tree', {
        animate:false,
        loader: false,
        enableDD:false,
        containerScroll: true,
        rootUIProvider: Ext.tree.CheckboxNodeUI,
        selModel: new Ext.tree.CheckNodeMultiSelectionModel(),
        rootVisible: false
    });

    tree.on('check', checkHandler, tree);

    // set the root node
    var root = new Ext.tree.TreeNode({
        text: 'root',
        draggable: false,
        checked: false,
        id: '0',
        uiProvider: Ext.tree.CheckboxNodeUI
    });

    tree.setRootNode(root);
    bildResourcesTree(root, <?php echo $this->getResTreeJson() ?>);
    tree.addListener('click', resourceClick.createDelegate(this));

    // render the tree
    tree.render();
    // root.expand();
    tree.expandAll();

    $('store_permissions').value = tree.getChecked().join(',');
});

function resourceClick(node, e){
    node.getUI().check(!node.getUI().checked());
    varienElementMethods.setHasChanges(Event.element(e), e);
};

function bildResourcesTree(parent, config){
    if (!config) return null;

    if (parent && config && config.length){
        for (var i = 0; i < config.length; i++){
            config[i].uiProvider = Ext.tree.CheckboxNodeUI;
            var node = new Ext.tree.TreeNode(config[i]);
            parent.appendChild(node);
            if(config[i].children){
                bildResourcesTree(node, config[i].children);
            }
        }
    }
}

function checkHandler(node)
{
    if ( node.attributes.checked && node.parentNode ) {
        var n = node.parentNode;
        this.removeListener('check', checkHandler);
        do {
            if (!n || n.attributes.id == 'admin' || n.attributes.id == '0') {
                break;
            } else {
                n.ui.check(true);
            }
        } while (n = n.parentNode );
        this.on('check', checkHandler);
    }
    if ( !node.isLeaf() && node.hasChildNodes() ) {
        this.removeListener('check', checkHandler);
        processChildren(node, node.attributes.checked);
        this.on('check', checkHandler);
    }
    $('store_permissions').value = this.getChecked().join(',');
}

function processChildren(node, state)
{
    if ( !node.hasChildNodes() ) return false;
    for(var i = 0; i < node.childNodes.length; i++ ) {
        node.childNodes[i].ui.check(state);
        if ( node.childNodes[i].hasChildNodes() ) {
            processChildren(node.childNodes[i], state);
        }
    }
    return true;
}
</script>
