define(['kb.widget.base', 'kb.session', 'jquery', 'postal', 'q'], function (BaseWidget, Session, $, Postal, Q) {
    'use strict';
    // make a widget ... on the fly?
    var W = Object.create(BaseWidget, {
        init: {
            value: function (cfg) {
                cfg.name = 'LoginWidget';
                cfg.title = 'Login Widget';
                this.BaseWidget_init(cfg);
                
                
                Postal.channel('session').subscribe('login.success', function (data) {
                    // close the dialog if open
                    this.closeLoginDialog();
                }.bind(this));

                
                var widget = this;
                this.container
                    .on('submit', '[data-dialog="login-dialog"] form', function (e) {
                        e.preventDefault();
                        var username = widget.container.find('form [name="username"]').val();
                        var password = widget.container.find('form [name="password"]').val();
                        widget.login(username, password);
                    });
                    
                    
                return this;
            }
        },
        showErrorMessage: {
            value: function (msg) {
                var alert = this.container.find('[data-alert="login-error"]');
                alert.html(data.error.message);
                alert.removeClass('hidden');
            }
        },
        hideErrorMessage: {
            value: function () {
                var alert = this.container.find('[data-alert="login-error"]');
                alert.addClass('hidden');
            }
        },
        createTemplateContext: {
            value: function () {
                return {
                    isLoggedIn: Session.isLoggedIn(),
                    username: Session.getUsername(),
                    realname: Session.getRealname()
                };
            }
        },
        render: {
            value: function () {
                if (Session.isLoggedIn()) {
                    this.container.html(this.renderTemplate('loggedin'));
                    this.container.find('[data-menu-item="logout"]').on('click', function (e) {
                        e.preventDefault();
                        Postal.channel('session').publish('logout.request');
                        //Session.logout()
                        //    .then(function () {
                                // Simply issues the logout
                        //        Postal.channel('session').publish('logout.success');
                                /*
                                var hash = window.location.hash;
                                var nextPath;
                                if (hash) {
                                    nextPath = hash.substr(1);
                                } else {
                                    nextPath = '';
                                }
                                window.location.href = '#/login/';
                                */
                        //    })
                        //    .catch(function (err) {
                        //        console.error('Error');
                        //        console.error(err);
                        //    })
                        //    .done();
                           
                        //Postal.channel('session').publish('logout.request');
                        // $(this).trigger('logout.kbase');
                    });
                } else {
                    // just a quick hack until the login widget is incorporated
                    // into the navbar, etc.
                    if (/^#\/login\//.test(window.location.hash)) {
                        this.container.empty();
                    } else {
                        this.container.html(this.renderTemplate('loggedout'));
                        this.container.find('[data-button="signin"]').on('click', function (e) {
                            e.preventDefault();
                            window.location.href ='#/login/';
                        }.bind(this));
                    }
                }
                return this;
            }
        },
        showLoginDialog: {
            value: function () {
                var dialog = this.container.find('[data-dialog="login-dialog"]');
                if (dialog) {
                    dialog.modal('show');
                }
            }
        },
        closeLoginDialog: {
            value: function () {
                var dialog = this.container.find('[data-dialog="login-dialog"]');
                if (dialog) {
                    dialog.modal('hide');
                }
            }
        },
        login: {
            value: function (userId, password) {
                Session.login({
                    username: userId,
                    password: password
                })
                .then(function(session) {
                    // omg this is the callback protocol 
                    session.status = 1;
                    session.success = 1;

                    // Awaiting clients can get the session object directly, from the cookie, or query the 
                    // global singleton session object.
                    Postal.channel('session').publish('login.success', {session: session});
                })
                .catch(function(err) {
                    var errObject = {
                        status: 0,
                        success: 0,
                        message: err
                    };
                    Postal.channel('session').publish('login.failure', {error: errObject});
                })
                .done();
            }
        }
    });
    return W;
});
@@ -2359,1167 +2359,8 @@
 ');%0A
-                        //Session.logout()%0A                        //    .then(function () %7B%0A                                // Simply issues the logout%0A                        //        Postal.channel('session').publish('logout.success');%0A                                /*%0A                                var hash = window.location.hash;%0A                                var nextPath;%0A                                if (hash) %7B%0A                                    nextPath = hash.substr(1);%0A                                %7D else %7B%0A                                    nextPath = '';%0A                                %7D%0A                                window.location.href = '#/login/';%0A                                */%0A                        //    %7D)%0A                        //    .catch(function (err) %7B%0A                        //        console.error('Error');%0A                        //        console.error(err);%0A                        //    %7D)%0A                        //    .done();%0A                           %0A                        //Postal.channel('session').publish('logout.request');%0A                        // $(this).trigger('logout.kbase');%0A
     
@@ -2371,32 +2371,32 @@
             %7D);%0A
+
                 
@@ -2930,40 +2930,61 @@
     
-window.location.href ='#/login/'
+Postal.channel('loginwidget').publish('login.prompt')
 ;%0A  

Use login.prompt event in loginWidget, replacing direct loading of login page.