/*global-block-begin*/


barrique.base.syndication.SyndicateDomain.prototype.onContextMenu = function(context, menu) {
    menu.addEntry('Syndicated Server...', dojo.hitch(this, 'addSyndicatedServer'), 'Add');
    menu.addEntry('Syndicated User...', dojo.hitch(this, 'addSyndicatedUser'), 'Add');
};

barrique.base.syndication.SyndicateDomain.prototype.addSyndicatedServer = function() {
    windowManager.openModalWindow(barrique.contextPath+'/sx/ArtifactHtmlPage?type=Server&class='+this.getJavaClass()+'&view=additem', 
            'AddSyndicatedItemWindow');
}
barrique.base.syndication.SyndicateDomain.prototype.addSyndicatedUser = function() {
    windowManager.openModalWindow(barrique.contextPath+'/sx/ArtifactHtmlPage?type=User&class='+this.getJavaClass()+'&view=additem', 
            'AddSyndicatedItemWindow');
}

barrique.base.syndication.SyndicatedUser.prototype.onContextMenu = function(context, menu) {
    if (artifactManager.isOwner(this)) {
        menu.addEntry('Syndicated User', dojo.hitch(this, 'removeSyndicatedUser'), 'Remove');
    }
}

barrique.base.syndication.SyndicatedUser.prototype.removeSyndicatedUser = function() {
    var syndicationId = this.getTitle();
    serverManager.asyncRequest('syndication/RemoveSyndicatedUser', null,
        new AsyncRequestCallback(function() {
            window.location.reload();
        }, function() {
            alert('Error while removing syndicated user');
        }), {syndicationId:syndicationId});
}


/*global-block-end*/
/*function-begin (getHeadHtml2l) */
function getHeadHtml2l() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml2l) */
/*function-begin (getBodyHtml2l) */
function getBodyHtml2l() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml2l) */
/*global-block-begin*/


barrique.module.docs.DocumentCategory.prototype.onContextMenu = function(context, menu) {
    var artifact = this;
    
    if (artifactManager.hasLinkRight(artifact)) {
        menu.addEntry('Document', dojo.hitch(this, 'onCreateDocument'), 'New');
        menu.addEntry('Category', dojo.hitch(this, 'onCreateSubCategory'), 'New');
    }
}
barrique.module.docs.DocumentCategory.prototype.onCreateSubCategory = function() {
    this.createNewSubItem('barrique.module.docs.DocumentCategory', 'barrique.base.artifact.HasChildItem', 'SubCategories');
}
barrique.module.docs.DocumentCategory.prototype.onCreateDocument = function() {
    this.createNewSubItem('barrique.module.docs.MultiPartDocument', 'barrique.module.docs.HasDocument', 'Documents');
}

barrique.module.docs.MultiPartDocument.prototype.onContextMenu = function(context, menu) {
    if (!artifactManager.hasLinkRight(this) && browserContext.isEditable()) { return; }
    var artifact = this, isManaged = commonEditPage && commonEditPage.isManaged(this);
    if (artifactManager.hasWriteRight(artifact) && browserContext.isEditable() && isManaged) {
        if (barrique.base.artifact.hasPresentationHint(artifact, 'featured')) { 
            menu.addEntry('Show in Featured', function() { barrique.base.artifact.removePresentationHint(artifact, 'featured'); }, 'Presentation', 'actions/apply'); 
        } else { 
            menu.addEntry('Show in Featured', function() { barrique.base.artifact.addPresentationHint(artifact, 'featured'); }, 'Presentation'); 
        }
        if (barrique.base.artifact.hasPresentationHint(artifact, 'context')) { 
            menu.addEntry('Show in Context', function() { barrique.base.artifact.removePresentationHint(artifact, 'context'); }, 'Presentation', 'actions/apply'); 
        } else { 
            menu.addEntry('Show in Context', function() { barrique.base.artifact.addPresentationHint(artifact, 'context'); }, 'Presentation'); 
        }
        if (barrique.base.artifact.hasPresentationHint(artifact, 'slides')) { 
            menu.addEntry('Enable Slides', function() { barrique.base.artifact.removePresentationHint(artifact, 'slides'); }, 'Presentation', 'actions/apply'); 
        } else { 
            menu.addEntry('Enable Slides', function() { barrique.base.artifact.addPresentationHint(artifact, 'slides'); }, 'Presentation'); 
        }
    }
}

barrique.module.docs.DocumentPart.prototype.onContextMenuPart = function(context, menu) {
    if (context.relation) { context.relation.onContextMenuPart(context, menu); }
}
barrique.module.docs.HasDocumentPart.prototype.onContextMenuPart = function(context, menu) {
    if (artifactManager.hasWriteRight(context.baseArtifact) && browserContext.isEditable()) {
        var hasDocumentPart = this;
        if (barrique.base.artifact.hasPresentationHint(hasDocumentPart, 'teaser')) { 
            menu.addEntry('Show in Teaser', function() { barrique.base.artifact.removePresentationHint(hasDocumentPart, 'teaser'); }, 'Presentation', 'actions/apply'); 
        } else { 
            menu.addEntry('Show in Teaser', function() { barrique.base.artifact.addPresentationHint(hasDocumentPart, 'teaser'); }, 'Presentation'); 
        }
        if (barrique.base.artifact.hasPresentationHint(hasDocumentPart, 'context')) { 
            menu.addEntry('Show in Document View', function() { barrique.base.artifact.removePresentationHint(hasDocumentPart, 'context'); }, 'Presentation', 'actions/apply'); 
        } else { 
            menu.addEntry('Show in Document View', function() { barrique.base.artifact.addPresentationHint(hasDocumentPart, 'context'); }, 'Presentation'); 
        }
    }
}

/*global-block-end*/
/*function-begin (getHeadHtml1t) */
function getHeadHtml1t() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml1t) */
/*function-begin (getBodyHtml1t) */
function getBodyHtml1t() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml1t) */
/*global-block-begin*/


dojo.require("dijit.Menu");

function ContextInfo(baseArtifact) {
    this.baseArtifact = baseArtifact;
    this.getBaseArtifact = function() { return this.baseArtifact; };
    this.add = function(key, value) { this[key] = value; return this; }
}
var contextMenuManager = {
    contextMenuList: [],
    contextMenuMixins: {},
    addContextMenu: function(artifact, context) {
        var id = this.contextMenuList.length;
        this.contextMenuList.push({artifact: artifact, context: context});
        return id;
    },
    addContextMenuMixin: function(javaClass, menuHandler, aspect) {
        if (!this.contextMenuMixins[javaClass]) { this.contextMenuMixins[javaClass] = {}; }
        var mixins = this.contextMenuMixins[javaClass],
            aspectName = 'aspect-'+(aspect ? aspect : '');
        if (!mixins[aspectName]) { mixins[aspectName] = []; }
        mixins[aspectName].push(menuHandler);
    },
    hasMenuEntries: function(artifact, context) {
        var aspect = context.aspect ? context.aspect : '';
            methods = this.collectMenuHandlers(artifact, context, aspect);
        for (var i = methods.length-1; i >= 0; i--) {
            var menuStub = new ContextMenuStub();
            try {
                methods[i].call(artifact, context, menuStub);
            } catch(e) {
                console.debug('Exception in hasMenuEntries', e);
            }
            if (menuStub.hasEntries()) { return true; }
        }
        return false;
    },
    onContextMenu: function(target, event, id) {
        if (this.currentMenu) { this.currentMenu(); this.currentMenu = null; }
        
        var entry = this.contextMenuList[id],
            menuWidget = new dijit.Menu({id:'dijit-menu-'+id});
            menu = new ContextMenu(),
            aspect = entry.context.aspect ? entry.context.aspect : '';
        this.collectEntries(entry.artifact, entry.context, menu, aspect);
        menu.processMenuWidget(menuWidget);
        menuWidget.bindDomNode(target);
        menuWidget.startup();
        barrique.dijit.popupMenu(menuWidget, {around:target,orient:{'BR':'TR', 'TR':'BR'}});
    },
    collectEntries: function(artifact, context, menu, aspect) {
        var methods = this.collectMenuHandlers(artifact, context, aspect);
        for (var i = 0; i < methods.length; i++) {
            menu.beginEntries();
            methods[i].call(artifact, context, menu);
            menu.endEntries();
        }
    },
    collectMenuHandlers: function(artifact, context, aspect) {
        var result = [];
            members = barrique.collectInherited(artifact, 'onContextMenu'+aspect);
        for (var i = members.length-1; i >= 0; i--) {
            result.push(members[i].member);
        }
        var classes = barrique.collectInherited(artifact);
        for (var j = 0; j < classes.length; j++) {
            var target = classes[j].target,
                javaClass = target.getJavaClass ? target.getJavaClass() : null;        
            if (javaClass != null && this.contextMenuMixins[javaClass]) {
                var mixins = this.contextMenuMixins[javaClass],
                    aspectName = 'aspect-'+aspect;
                if (mixins[aspectName]) {
                    methods = mixins[aspectName];
                    for (var i = 0; i < methods.length; i++) {
                        result.push(methods[i]);
                    }
                }
            }
        }
        return result;
    }
};

function ContextMenu() {
    this.rootEntries = [];
    this.currentEntries = this.rootEntries;
    this.groupings = {};
    this.mergePoint = null;
    
    this.beginEntries = function() {
        this.mergePoint = null;
    }
    this.endEntries = function() {
        if (this.mergePoint != null) {
            this.currentEntries = this.mergePoint.entries;
        }
    }
    this.addEntry = function(caption, callback, groupName, iconName, isDisabled) {
        var entry = {caption:caption,callback:callback,iconName:iconName,disabled:isDisabled?true:false};
        if (groupName != null) {
            if (typeof(this.groupings[groupName]) == 'undefined') { this.addGrouping(groupName); }
            this.groupings[groupName].entries.push(entry);
        } else {
            this.currentEntries.push(entry);
        }
    }
    this.addGrouping = function(groupName) {
        if (typeof(this.groupings[groupName]) == 'undefined') {
            this.groupings[groupName] = { groupName: groupName, entries: []};
            this.currentEntries.push(this.groupings[groupName]);
        } 
    }
    this.addSeparator = function(groupName) {
        if (groupName) {
            if (this.groupings[groupName]) {
                this.groupings[groupName].entries.push('separator');
            }
        } else {
            this.currentEntries.push('separator');
        }
    }
    this.addMergePoint = function() {
        this.mergePoint = { mergePoint: true, entries: [] };
        this.currentEntries.push(this.mergePoint);
    }
    this.processMenuWidget = function(menuWidget, entries, prefix) {
        if (entries == null) { entries = this.rootEntries; }
        for (var i = 0; i < entries.length; i++) {
            var entry = entries[i];
            if (typeof(entry.groupName) != 'undefined') {
                //console.debug('ContextMenu.processMenuWidget', 'grouping', entry.groupName);
                if (entry.entries.length > 1) { 
                    var groupMenuWidget = new dijit.Menu({parentMenu:menuWidget});
                    menuWidget.addChild(new dijit.PopupMenuItem({label:entry.groupName, popup:groupMenuWidget}));
                    this.processMenuWidget(groupMenuWidget, entry.entries);
                } else {
                    this.processMenuWidget(menuWidget, entry.entries, entry.groupName+' ');
                }
            } else if (typeof(entry.mergePoint) != 'undefined') {
                //console.debug('ContextMenu.processMenuWidget', 'merge point');
                this.processMenuWidget(menuWidget, entry.entries);
            } else if (entry == 'separator') {
                if (i > 0 && i < entries.length-1 && entries[i-1] != 'separator') {
                    //console.debug('ContextMenu.processMenuWidget', 'separator');
                   // menuWidget.addChild(dojo.widget.createWidget('MenuSeparator2'));
                   menuWidget.addChild(new dijit.MenuSeparator());
                }
            } else {
                var caption = prefix != null? prefix+entry.caption : entry.caption,
                    iconClass = entry.iconName ? 'icon-'+entry.iconName.replace('/', '-')+'-16'  : null;
                //console.debug('ContextMenu.processMenuWidget', caption, iconSrc);
                menuWidget.addChild(new dijit.MenuItem(
                    {label: caption, onClick: entry.callback, iconClass: iconClass, disabled: entry.disabled}));
            }
        }
    }
}

function ContextMenuStub() {
    this.entryCount = 0;
    this.beginEntries = this.endEntries = this.addGrouping =
    this.addSeparator = this.addMergePoint = function() {}
    this.addEntry = function() { this.entryCount++; }
    this.hasEntries = function() { return this.entryCount > 0; }
}

function tagContextMenu(args, nestedHtmlGetter) {
    var artifact = args.artifact,
        context = typeof(args.context) != 'undefined' && args.context != null ? args.context : {},
        tag = typeof(args.tag) != 'undefined' ? args.tag : 'span',
        isButton = args.isButton ? true : false;

    if (typeof(args.aspect) != 'undefined') { context.aspect = args.aspect; }
    
    if (!browserContext.isEditable() || !browserContext.isNavigationEnabled()) { return nestedHtmlGetter(); }
    if (!contextMenuManager.hasMenuEntries(artifact, context)) { return nestedHtmlGetter(); }
    
    var html = '', id = contextMenuManager.addContextMenu(artifact, context);
    if (isButton) {
        var onClick = 'contextMenuManager.onContextMenu(dojo.byId(\'ContextMenuButton'+id+'\'), null, '+id+')',
            dropDownTemplate = new dijit.form.DropDownButton().templateString,
            buttonHtml = tagIconButton({id:'ContextMenuButton'+id,onClick:onClick}, function() {
                return '<div style="line-height:16px;vertical-align:middle;">'+nestedHtmlGetter()+'&thinsp;&#9660;</div>'});
        html += buttonHtml;
    } else {
        html += '<'+tag+' class="context-menu">';
        html += nestedHtmlGetter();
        html += '<a onMouseOver="dojo.addClass(this.parentNode, \'context-menu-hover\')" '+
                'onMouseOut="dojo.removeClass(this.parentNode, \'context-menu-hover\')" '+
                'onclick="contextMenuManager.onContextMenu(this, event, '+id+')" '+
                'class="context-menu-handle" title="Click to open the context menu">&nabla;</a></'+tag+'>';
    }
    return html;
}

barrique.base.artifact.Item.prototype.hasContextMenu = function(context) {
    if (artifactManager.hasUnlinkRight(this)) return true;
    if (this.getHref() != null) return true;
    return false;
}
barrique.base.artifact.Item.prototype.openItem = function() {
    serverManager.setUrl(this.getHref()); 
}
barrique.base.artifact.Item.prototype.deleteItem = function() {
    var title = typeof(this.getTitle) != 'undefined' ? '"'+this.getTitle()+'"' : 'this '+this.getClassName();
    if (confirm('You are about to delete '+title+'.\nAre you sure?')) {
        var artifactId = this.getArtifactId();
        serverManager.asyncRequest('DeleteArtifact', null, new AsyncRequestCallback(onItemDeleted, onItemDeletedError), {aid:artifactId});
    }
}
barrique.base.artifact.Item.prototype.onEditRights = function() {
    var isManaged = commonEditPage && commonEditPage.isManaged(this);
    //console.debug('onEditRights', commonEditPage, isManaged);
    editAcl.editAcl(this, !isManaged);
}
barrique.base.artifact.Item.prototype.onContextMenu = function(context, menu) {
    if (this.getHref() != null && !artifactManager.isMainArtifact(this)) {
        menu.addEntry('Open', dojo.hitch(this, 'openItem'), null, 'actions/forward');
        menu.addSeparator();
    }
    menu.addGrouping('New');
    menu.addMergePoint();
    if (artifactManager.hasUnlinkRight(this)) {
        menu.addEntry('Delete', dojo.hitch(this, 'deleteItem'), null, 'actions/editdelete');
    }
    if (artifactManager.hasWriteRight(this) || browserContext.isSystemUser()) {
        menu.addSeparator();
        menu.addEntry('Edit Rights...', dojo.hitch(this, 'onEditRights'), null, 'actions/edit_user');
    }
}
barrique.base.artifact.Item.prototype.createNewSubItem = function(targetClass, relationClass, attributeName) {
    windowManager.openModalWindow(this.getHref()+'?view=create-subitem&targetClass='+targetClass+'&relationClass='+relationClass+'&attributeName='+attributeName, 'NewRelationWindow');
}

function onItemDeleted(artifactId) {
    var mainArtifact = artifactManager.getMainArtifact();
    if (mainArtifact != null && 
        mainArtifact.getArtifactId() == artifactId) {
        var relationPath = mainArtifact.getRelationPath();
        if (relationPath != null) {
            var segments = relationPath.getSegments();
            if (segments.length > 1) {
                relationPath = new RelationPath(segments.slice(0, segments.length - 1));
                serverManager.setUrl(relationPath.getHref());
            } else {
                serverManager.setUrl(barrique.contextPath+'/');
            }
        } else { console.debug('Main artifact has no relation path'); }
    } else {
        window.location.reload();
    }
}
function onItemDeletedError(exceptions) {
    console.debug(exceptions);
    alert('Cannot delete this item.');
}

var editAcl = {
    editAclList: [],
    applyRecursively: {},
    window: window,
    setString: function(index, aclAsString, save) {
        //console.debug('editAcl.setString', aclAsString, save);
        //if (this.editAclList[index].getAcl() == aclAsString) { return; }
        this.editAclList[index].setAcl(aclAsString);
        if (save) { this.window.setTimeout(dojo.hitch(this, 'save', index), 0); }
        else { commonEditPage.addSaveRequestParameter(this.getSaveRequestParameters(index)); }
    },
    setApplyRecursively: function(index, applyRecursively) {
        this.applyRecursively[index] = applyRecursively;
    },
    save: function(index, noarg) {
        serverManager.asyncArtifactRequest(this.editAclList[index], 'SaveArtifact', 
            new AsyncRequestCallback(dojo.hitch(this, 'onSaved'), dojo.hitch(this, 'onSavedError')),
            this.getSaveRequestParameters(index));
    },
    getSaveRequestParameters: function(index) {
        var applyRecursively = this.applyRecursively[index];
        if (applyRecursively) {
            artifactId = this.editAclList[index].getArtifactId();
            return {applyAclRecursively:artifactId};
        }
        return null;
    },
    onSaved: function() {
        commonHeader.displayInfo('Successfully saved');
    },
    onSavedError: function(exceptions) {
        console.debug('Cannot save the an artifact', exceptions);
        commonHeader.displayError('Error while saving rights');
    },
    getString: function(index) { 
        return this.editAclList[index].getAcl();
    },
    getArtifactId: function(index) {
        return this.editAclList[index].getArtifactId();
    },
    editAcl: function(artifact, save, publish) {
        var index = this.editAclList.length;
        this.editAclList.push(artifact);
        
        // collect all users
        var aclAsString = artifact.getAcl(),
            subjectIds = '';
        if (aclAsString) {
            var acl = eval(aclAsString),
                ownerId = acl[0],
                subjectMatrix = acl[1],
                subjectIdMap = {};
            subjectIdMap[ownerId] = null;
            for (var rightIndex = 0; rightIndex < subjectMatrix.length; rightIndex++) {
                var subjectTypeVector = subjectMatrix[rightIndex];
                for (var subjectTypeIndex = 0; subjectTypeIndex < subjectTypeVector.length; subjectTypeIndex++) {
                    var subjectArray = subjectTypeVector[subjectTypeIndex];
                    for (var subjectIndex = 0; subjectIndex < subjectArray.length; subjectIndex++) {
                        var subjectId = subjectArray[subjectIndex];
                        subjectIdMap[subjectId] = null;
                    }
                }
            }
            for (x in subjectIdMap) { subjectIds += '&sid='+x; }
        }

        windowManager.openModalWindow(barrique.contextPath+
                '/sx/security/EditAcl?class=barrique.base.security.DirectoryDomain&view=edit-acl-dialog&'+
                'index='+index+subjectIds+(save ? '&save=true' : '')+(save ? '&save=true' : ''), 'EditAclDialog');
    }
}



/*global-block-end*/
/*function-begin (getHeadHtmlx) */
function getHeadHtmlx() {
    var html = '';
    html += '\n\n\n\n\<style type=\"text\/css\"\>\n\<\/style\>\n';
    return html;
}
/*function-end (getHeadHtmlx) */
/*function-begin (getBodyHtmlx) */
function getBodyHtmlx() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtmlx) */
/*global-block-begin*/


contextMenuManager.addContextMenuMixin('barrique.base.artifact.Item', function(context, menu) {
    if (browserContext.isSystemUser()) {
        menu.addSeparator('Admin');
        menu.addEntry('Backup...', dojo.hitch(this, 'synchronizationBackup'), 'Admin');
        menu.addEntry('Restore...', dojo.hitch(this, 'synchronizationRestore'), 'Admin');
    }
});

barrique.base.artifact.Item.prototype.synchronizationBackup = function() {
    windowManager.openModalWindow(barrique.contextPath+'/sx/synchronization/BackupDialog?aid='+this.getArtifactId(), 'SynchronizationBackupWindow');
}
barrique.base.artifact.Item.prototype.synchronizationRestore = function() {
    windowManager.openModalWindow(barrique.contextPath+'/sx/synchronization/RestoreDialog?aid='+this.getArtifactId(), 'SynchronizationRestoreWindow');
}


/*global-block-end*/
/*function-begin (getHeadHtml26) */
function getHeadHtml26() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml26) */
/*function-begin (getBodyHtml26) */
function getBodyHtml26() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml26) */
/*global-block-begin*/


barrique.base.security.DirectoryDomain.prototype.hasContextMenu = function(context) {
    return artifactManager.hasLinkRight(this);
}
barrique.base.security.DirectoryDomain.prototype.onContextMenu = function(context, menu) {
    var artifact = this;
    if (artifactManager.hasLinkRight(artifact)) {
        menu.addEntry('Group', dojo.hitch(this, 'onCreateGroup') , 'New' );
        menu.addEntry('User', dojo.hitch(this, 'onCreateUser') , 'New' );
    }
}

barrique.base.security.DirectoryDomain.prototype.onCreateGroup = function() {
    this.createNewSubItem('barrique.base.security.Group', 'barrique.base.security.HasSubject', 'Groups');
}
barrique.base.security.DirectoryDomain.prototype.onCreateUser = function() {
    this.createNewSubItem('barrique.base.security.User', 'barrique.base.security.HasSubject', 'Users');
}

barrique.base.security.Group.prototype.hasContextMenu = function(context) {
    return artifactManager.hasLinkRight(this);
}
barrique.base.security.Group.prototype.onContextMenu = function(context, menu) {
    if (browserContext.isEditable()) {
        if (artifactManager.hasLinkRight(this)) {
            menu.addEntry('Sub-Group', dojo.hitch(this, 'onCreateSubGroup') , 'New' );
            menu.addEntry('User', dojo.hitch(this, 'addUser'), 'Add', 'actions/add_user');
        }
    }
}
barrique.base.security.Group.prototype.onCreateSubGroup = function() {
    this.createNewSubItem('barrique.base.security.Group', 'barrique.base.security.HasSubject', 'SubGroups');
}
barrique.base.security.Group.prototype.addUser = function() {
    var users = this.getUsers(), 
        skipArtifacts = '';
    for (var i = 0; i < users.length; i++) {
        if (i > 0) { skipArtifacts += ' '; }
        skipArtifacts += users[i].getTarget().getArtifactId();
    }
    windowManager.openModalWindow(barrique.contextPath+'/sx/ArtifactHtmlPage?class=barrique.base.artifact.Artifact'+
            '&view=search-artifact-dialog&artifactClass=barrique.base.security.User'+
            '&callback='+serverManager.addCallback(dojo.hitch(this, 'onUserSelected'))+
            '&skipArtifacts='+encodeURIComponent(skipArtifacts)+
            '&right=LINK', 'SelectUser');
}
barrique.base.security.Group.prototype.onUserSelected = function(artifactId) {
    if (artifactId == null) { return; }
    var artifact = this;
    serverManager.loadArtifact(artifactId, null, 
        {onOk: dojo.hitch(artifact, 'onSelectedUserLoaded'), onError:dojo.hitch(artifact, 'onSelectedUserLoadedError')}, 
        'barrique.base.artifact.compound.EmptyCompound');
}
barrique.base.security.Group.prototype.onSelectedUserLoaded = function(user) {
    var hasUser = new barrique.base.security.HasUser();
    hasUser.setTarget(user);
    hasUser.setName(user.getName());
    this.addToUsers(hasUser);
}
barrique.base.security.Group.prototype.onSelectedUserLoadedError = function(exceptions) {
    console.debug('Cannot load user', exceptions);
    alert('Cannot load user');
}

barrique.base.security.User.prototype.onContextMenu = function(context, menu) {
    var artifact = this;
    if (artifactManager.isOwner(artifact) || browserContext.isSystemUser()) {
        menu.addEntry('Change Password', dojo.hitch(this, 'onChangePassword') );
    }
}
barrique.base.security.User.prototype.onChangePassword = function() {
    windowManager.openModalWindow(this.getHref()+'?view=changepassword', 'ChangePasswordDialog');
}


/*global-block-end*/
/*function-begin (getHeadHtml29) */
function getHeadHtml29() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml29) */
/*function-begin (getBodyHtml29) */
function getBodyHtml29() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml29) */
/*global-block-begin*/


barrique.module.tag.TagCategory.prototype.hasContextMenu = function(context) {
    return artifactManager.hasLinkRight(this);
}

barrique.module.tag.TagCategory.prototype.onContextMenu = function(context, menu) {
    var artifact = this;
    
    if (artifactManager.hasLinkRight(artifact)) {
        menu.addEntry('Category', dojo.hitch(this, 'onCreateCategory'), 'New');
        menu.addEntry('Concept Tag', dojo.hitch(this, 'onCreateConceptTag'), 'New');
        menu.addEntry('Geo Tag', dojo.hitch(this, 'onCreateGeoTag'), 'New');
        menu.addEntry('License Tag', dojo.hitch(this, 'onCreateLicenseTag'), 'New');
    }
}

barrique.module.tag.TagCategory.prototype.onCreateCategory = function() {
    this.createNewSubItem('barrique.module.tag.TagCategory', 'barrique.base.artifact.HasChildItem', 'SubCategories');
}

barrique.module.tag.TagCategory.prototype.onCreateConceptTag = function() {
    this.createNewSubItem('barrique.module.tag.ConceptTag', 'barrique.module.tag.HasTag', 'Tags');
}
barrique.module.tag.TagCategory.prototype.onCreateGeoTag = function() {
    this.createNewSubItem('barrique.module.tag.geo.GeoTag', 'barrique.module.tag.HasTag', 'Tags');
}
barrique.module.tag.TagCategory.prototype.onCreateLicenseTag = function() {
    this.createNewSubItem('barrique.module.tag.license.LicenseTag', 'barrique.module.tag.HasTag', 'Tags');
}

barrique.module.tag.Tag.prototype.addAutoTags = function(tag) {
    return this.copyRelations(tag, 'tags');
}



/*global-block-end*/
/*function-begin (getHeadHtml1c) */
function getHeadHtml1c() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml1c) */
/*function-begin (getBodyHtml1c) */
function getBodyHtml1c() {
    var html = '';
    html += '\n\n\n';
    return html;
}
/*function-end (getBodyHtml1c) */
/*global-block-begin*/


if (browserContext.isSystemUser()) { 
    contextMenuManager.addContextMenuMixin('barrique.base.artifact.domain.RootDomain', function(context, menu) {
            menu.addEntry('Rebuild Index', dojo.hitch(this, 'onRebuildIndex'), 'Admin');
            menu.addEntry('Garbage Collect', dojo.hitch(this, 'onGarbageCollect'), 'Admin');
    });
}

barrique.base.artifact.domain.RootDomain.prototype.onRebuildIndex = function() {
    serverManager.asyncRequest('admin/RebuildIndex', null, 
        new AsyncRequestCallback(function() {
            alert('Index Rebuild');
        }, function() {
            alert('Error while building index');
        }));
}

barrique.base.artifact.domain.RootDomain.prototype.onGarbageCollect = function() {
    serverManager.asyncRequest('admin/GarbageCollect', null, 
        new AsyncRequestCallback(function() {
            alert('Garbage Collected');
        }, function() {
            alert('Error while garbage collecting');
        }));
}



/*global-block-end*/
/*function-begin (getHeadHtml23) */
function getHeadHtml23() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml23) */
/*function-begin (getBodyHtml23) */
function getBodyHtml23() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml23) */
/*global-block-begin*/


barrique.base.artifact.domain.RootDomain.prototype.hasContextMenu = function(context) {
    return artifactManager.hasLinkRight(this);
} 

barrique.base.artifact.domain.RootDomain.prototype.onContextMenu = function(context, menu) {
    if (artifactManager.hasLinkRight(this)) {
        var artifact = this;
        menu.addEntry('Tags', function() { artifact.createCollection('barrique.module.tag.TagDomain'); }, 'New');
        menu.addEntry('Documents', function() { artifact.createCollection('barrique.module.docs.DocumentDomain'); }, 'New');
    }
}

barrique.base.artifact.domain.RootDomain.prototype.createCollection = function(artifactClass) {
    this.createNewSubItem(artifactClass, 'barrique.base.artifact.HasNamedSubItem', 'SubDomains');
}


/*global-block-end*/
/*function-begin (getHeadHtml1y) */
function getHeadHtml1y() {
    var html = '';
    html += '\n\n\n\n';
    return html;
}
/*function-end (getHeadHtml1y) */
/*function-begin (getBodyHtml1y) */
function getBodyHtml1y() {
    var html = '';
    html += '\n';
    return html;
}
/*function-end (getBodyHtml1y) */
