/www/jssrc/blerg/Blerg.js
// Listen for onsubmit events
enyo.dispatcher.listen(document, "submit");

enyo.kind({
    name: "blerg.Blerg",
    kind: "Control",
    lastHash: null,
    pathHandlers: [ blerg.User, blerg.Tag, blerg.Feed, blerg.ExternalURLPost, blerg.Welcome, blerg.AccountCenter, blerg.Recovery, blerg.EmailVerify ],
    handlers: {
        onStartSignup: "showSignupDialog",
        onTryLogin: "tryLogin",
        onTryLogout: "tryLogout",
        onSetTitle: "setTitle",
        onPostVisibility: "postVisibilityUpdate",
        onReload: "sendReload",
        onShowChangePassword: "showChangePassword",
        onAuthFailure: "authFailure"
    },
    components: [
        {classes: "blerg-header", components: [
            {name: "title", kind: "blerg.Title"},
            {name: "controls", kind: "blerg.Controls"},
            {style: "clear: both;"},
            {name: "post", kind: "blerg.Post", showing: false},
            {name: "help", kind: "blerg.Help"}
        ]},
        {name: "main", kind: "blerg.Main"},
        {name: "signupDialog", kind: "blerg.SignupDialog"},
        {name: "api", kind: "blerg.API",
         onLoginSuccessful: "loginSuccessful",
         onLoginFailed: "loginFailed",
         onLogoutSuccessful: "logout"}
    ],
    rendered: function() {
        this.inherited(arguments);

        this.lastHash = location.hash;
        this.urlSwitch();

        setInterval(this.hashCheck.bind(this), 250);

        document.body.addEventListener('keyup', function(event) {
            if (event.shiftKey && event.keyCode == 32) {
                this.waterfall('onPostVisibility', {showing: true});
                event.stopPropagation();
            }
        }.bind(this), false);
    },
    hashCheck: function() {
        if (location.hash != this.lastHash) {
            this.lastHash = location.hash;
            this.urlSwitch();
        }
    },
    urlSwitch: function() {
        var m;
        var objdef = null;

        for (var i = 0; i < this.pathHandlers.length; i++) {
            var handler = this.pathHandlers[i];
            objdef = handler.locationDetect(window.location);
            if (objdef)
                break;
        }
        if (!objdef)
            objdef = {classes: "blerg-error", content: "No handler found"}

        this.$.main.updateView(objdef);
    },
    showSignupDialog: function() {
        this.$.signupDialog.show();
    },
    setTitle: function(inSender, inEvent) {
        this.$.title.waterfall('onSetTitle', inEvent);
    },
    tryLogin: function(inSender, inEvent) {
        this.$.api.login(inEvent.username, inEvent.password);
    },
    tryLogout: function(inSender, inEvent) {
        this.$.api.logout();
    },
    loginSuccessful: function(inSender, inEvent) {
        this.waterfall('onLogin', inEvent);
    },
    loginFailed: function(inSender, inEvent) {
        alert('Login failed');
        this.waterfall('onShowForgotPasswordLink');
        this.logout();
    },
    logout: function(inSender, inEvent) {
        clearInterval(this.feedStatusUpdateInterval);
        this.waterfall('onLogout');
    },
    postVisibilityUpdate: function(inSender, inEvent) {
        this.$.post.waterfall('onPostVisibility', inEvent);
        this.$.controls.waterfall('onPostVisibility', inEvent);
    },
    sendReload: function() {
        this.$.main.waterfall('onReload');
    },
    authFailure: function(inSender, inEvent) {
        this.logout();
    }
});