/**
 * Access control dialogs
 */
define(function(require, exports, module) {
"use strict";

    main.consumes = [
        "Plugin", "api", "dialog.alert", "dialog.question", "dialog.error",
        "ui", "layout", "commands"
    ];
    main.provides = ["accessControl"];
    return main;

    function main(options, imports, register) {
        var Plugin = imports.Plugin;
        var api = imports.api;
        var showAlert = imports["dialog.alert"].show;
        var showQuestion = imports["dialog.question"].show;
        var showError = imports["dialog.error"].show;
        var ui = imports.ui;
        var layout = imports.layout;
        var commands = imports.commands;

        var plugin = new Plugin("Ajax.org", main.consumes);
        var readonly = options.readonly;
        var dashboardUrl = options.dashboardUrl;
        var lastInfo = {};

        var loaded = false;
        function load() {
            if (loaded) return;
            loaded = true;
            
            if (!readonly)
                return;

            api.collab.get("access_info", function (err, info) {
                if (err) return showAlert("Error", info);
                
                lastInfo = info;

                if (info.private) {
                    if (!info.member) {
                        // Do you want to request access to this workspace
                        showRequestAccessDialog();
                    }
                    else if (info.pending) {
                        // Already requested, do you want to cancel ?
                        showCancelAccessDialog();
                    }
                }
                else {
                    addRequestButton();
                    if (!info.member)
                        showError("Workspace is read only. Use \"Request Access\" to request write access.");
                }
                
            });
        }
        
        function addRequestButton() {
            var btn = new ui.button({
                skin: "c9-menu-btn",
                caption: "Request Access",
                tooltip: "Request Write Access",
                command: "request_access"
            });

            commands.addCommand({
                name: "request_access",
                hint: "Share the workspace",
                group: "General",
                exec: showRequestAccessDialog
            }, plugin);

            ui.insertByIndex(layout.findParent({
                name: "preferences"
            }), btn, 600, plugin);
        }

        function showRequestAccessDialog(write) {
            if (lastInfo.pending)
                return showCancelAccessDialog();
            
            showQuestion("Workspace Access",
              "You don't currently have " + (write ? "write " : "")
              + "access to this workspace",
              "Would you like to request access?",
              function(){
                  // Yes
                 requestAccess();
                 lastInfo.pending = true;
              },
              redirectToDashboardIfPrivate
            );
        }
        
        function requestAccess() {
             api.collab.post("request_access", function (err, member) {
                 if (err) return showAlert("Error Requesting access", err.message || err);
                 showAlert("Done", "Access request sent", "We have sent an access request to the admin of this workspace.");
             });
        }

        function showCancelAccessDialog() {
            showQuestion("Workspace Access",
              "Request access pending approval",
              "Would you like to cancel your access request?",
              function(){
                  // Yes
                  api.collab.delete("cancel_request", function (err) {
                      if (err) return showAlert("Error", err);
                      lastInfo.pending = false;
                      showAlert("Done", "Access request cancelled", "We don't currently have access to this workspace");
                  });
              },
              redirectToDashboardIfPrivate
            );
        }

        function redirectToDashboardIfPrivate() {
            if (lastInfo.private)
                window.top.location.href = dashboardUrl;
        }

        /***** Lifecycle *****/
        plugin.on("load", function(){
            load();
        });
        plugin.on("enable", function(){

        });
        plugin.on("disable", function(){
        });

        plugin.on("unload", function(){
            loaded = false;
        });

        plugin.freezePublicAPI({
            requestAccess: requestAccess,
            
            showRequestAccessDialog: showRequestAccessDialog
        });

        register(null, {
            accessControl: plugin
        });
    }
});
@@ -171,24 +171,8 @@
 on%22,
- %22dialog.error%22,
 %0A   
@@ -202,16 +202,38 @@
 ommands%22
+, %22dialog.notifcation%22
 %0A    %5D;%0A
@@ -531,25 +531,22 @@
     var 
-showError
+notify
  = impor
@@ -560,13 +560,20 @@
 log.
-error
+notification
 %22%5D.s
@@ -1731,135 +1731,622 @@
     
-if (!info.member)%0A                        showError(%22Workspace is read only. Use %5C%22R
+%0A                    if (!info.member %7C%7C info.pending) %7B%0A                        notify(%22%3Cdiv class='c9-readonly'%3EYou are in Read-Only Mode. %22%0A                            + (info.pending ? %22%22 : %22Click on this bar to r
 equest 
-A
+a
 ccess
-%5C%22 to request write access.%22);
+ to the owner.%22)%0A                            + %22%3C/div%3E%22, true);%0A                        %0A                        if (!info.pending) %7B%0A                            document.querySelector(%22.c9-readonly%22).addEventListener(%22click%22, function()%7B%0A                                showRequestAccessDialog();%0A                            %7D, false);%0A                        %7D%0A                    %7D
 %0A   

Add notify to read only mode from collab