function render(){
    var _componentBus = new aw.ComponentBus();
    _componentBus.setApplicationKey(_awApplicationKey);

    var _DNow=new DNow({
        'container': $(document.getElementsByTagName('body')[0]),
        'componentBus': _componentBus
    });
}

DNow = Class.create(aw.ui, {

    initialize: function (properties, events) {
        aw.ui.prototype.initialize.apply(this, arguments);

        this.componentBus.registerEvent('onauthenticationfailure', this._onAuthenticationFailure.bind(this));
        this.componentBus.registerEvent('onauthenticationsuccess', this._onAuthenticationSuccess.bind(this));
        this.componentBus.registerEvent('onvalidationfailure', this._onAuthenticationFailure.bind(this));
        this.componentBus.registerEvent('onvalidationsuccess', this._onAuthenticationSuccess.bind(this));

        var _m = new aw.Messenger({ 'componentBus': this.componentBus });
        _m.registerEvent('activemessages', this._onActiveMessagesReceived.bind(this));
        _m.loadMessageFile('./custom/js/Messages.js', '_DNowMessages');
    },

    draw: function () {
        aw.ui.prototype.draw.apply(this);

        var am = new aw.AccountManager({ 'componentBus': this.componentBus });
        var hm = new aw.HelpManager({ 'componentBus': this.componentBus });

        var htmlViewer = new DNow.HTMLViewer({
            'componentBus': this.componentBus,
            'title': 'Home'
        });

        var reportViewer = new DNow.ReportViewer({
            'componentBus': this.componentBus,
            'title': 'Report',
            'disabled': true
        });

        var mapViewer = new DNow.MapViewer({
            'componentBus': this.componentBus,
            'title': 'Map',
            'disabled': true
        });

        var fileViewer = new DNow.FileViewer({
            'componentBus': this.componentBus,
            'title': 'Files',
            'disabled': true
        });

        /*
        var queryViewer = new DNow.QueryViewer({
        'componentBus': this.componentBus,
        'title': 'Query',
        'disabled': true
        });
        */

        var wizardViewer = new DNow.WizardViewer({
            'componentBus': this.componentBus,
            'title': 'Wizards',
            'disabled': true
        });

        var aboutContainer = this.canvas.down('span.about');
        if (aboutContainer)
            aw.EventManager.register(aboutContainer, 'click', this._onAboutClick.bind(this));
        aboutContainer = null;

        var DNowPanel = new aw.ui.Panel.Tab({
            'titleVisibility': false,
            'container': this.canvas.down('div[id="app"]'),
            'showBgd': false,
            'cssClass': 'DNow_TabPanel',
            'showBgd': false,
            'items': [
                htmlViewer,
                reportViewer,
                mapViewer,
            //queryViewer, //Removed. Rally ID: US294
                wizardViewer,
                fileViewer
            ]
        });

        aw.EventManager.register((document.onresize ? document : window), 'resize', this._onResize.bind(this, DNowPanel));

        var queryParams = document.getURLParameters();
        if ((queryParams) && (queryParams.activation)) {
            if (queryParams.activation.toLowerCase() == 'success') {
                am.getActivationMessage('success', function (message) {
                    new aw.ui.Dialog.Alert({ 'title': 'Account Activated', 'content': message });
                });
            } else if (queryParams.activation.toLowerCase() == 'failure') {
                am.getActivationMessage('failure', function (message) {
                    new aw.ui.Dialog.Error({ 'title': 'Account Activation Failure', 'content': message });
                });
            }
        }

        return this.canvas;
    },

    _onAboutClick: function () {
        new aw.ui.Dialog({
            'title': 'About Alteryx web',
            'width': 500,
            'centerable': true,
            'content': 'Alteryx web<br />' +
                'Version: ' + aw.BUILD_VERSION + '<br /><br />' +
                'Some Icons are Copyright &copy; <a href="http://p.yusukekamiyamane.com/" target="_blank">Yusuke Kamiyamane</a>. All rights reserved.<br />Licensed under a <a href="http://creativecommons.org/licenses/by/3.0/" target="_blank">Creative Commons Attribution 3.0 license</a>.<br /><br />' +
                'Some Icons are Copyright &copy; <a href="http://www.famfamfam.com/lab/icons/silk/" target="_blank">FamFamFam</a>. All rights reserved.<br />Licensed under a <a href="http://creativecommons.org/licenses/by/3.0/" target="_blank">Creative Commons Attribution 3.0 license</a>.'
        });
    },

    _onAccountClick: function () {
        new DNow.AccountDialog({
            'componentBus': this.componentBus
        });
    },

    _onActiveMessagesReceived: function (messages) {
        if ((messages) && (messages.length > 0)) {
            var m = messages[0];
            var header = this.canvas.down('div.header');
            if (header) {
                header.insert('<div class="notification">' + m.title + '</div>');
                var _n = header.down('div.notification');
                var _t = new aw.ui.Tooltip({ 'title': m.title, 'content': m.shortContent, 'onclick': true, 'target': _n, 'clickCallback': (m.fullContent) ? function (title, content) { new aw.ui.Dialog({ 'title': title, 'content': content, 'centerable': true }); } .bind(this, m.title, m.fullContent) : null });
                if (m.shown !== true)
                    _t.showTip();
            }
        }
    },

    _onAuthenticationFailure: function () {
        var viewerClassNames = ['DNow.ReportViewer', 'DNow.FileViewer', 'DNow.MapViewer', 'DNow.QueryViewer', 'DNow.WizardViewer'];
        var viewerClass = null;
        for (var i = 0, l = viewerClassNames.length; i < l; i++) {
            viewerClass = this.componentBus.getComponentReferences(viewerClassNames[i]);
            if ((viewerClass) && (viewerClass.length > 0))
                viewerClass[0].disable();
        }

        if (this.canvas) {
            var login = this.canvas.down('div.header div.login');
            if (login) {
                //login.update('<span class="signup">Sign-Up</span><span class="login">Log In</span>');
                login.update('<table style="width:600px"><tr><td><div style="color:#FFFFFF; font-size:12pt; font-weight:bold">Sales/Technical Support: +44(0) 207 961 1289</div></td><td align=right><span class="login">Log In</span></td></tr></table>');
                aw.EventManager.register(login.down('span.signup'), 'click', this._onSignup.bind(this));
                aw.EventManager.register(login.down('span.login'), 'click', this._onLoginClick.bind(this));
            }
        }

        var managerClassNames = ['aw.UserDataManager', 'aw.GeographyManager', 'aw.ReportManager', 'aw.QueryManager', 'aw.WizardManager', 'aw.CustomPolygonManager', 'aw.AnnotationManager', 'aw.ThematicsManager', 'aw.IconManager'];
        var managerClass = null;
        for (var j = 0; j < managerClassNames.length; j++) {
            managerClass = this.componentBus.getComponentReferences(managerClassNames[j]);
            if ((managerClass) && (managerClass.length > 0)) {
                managerClass[0].destroy();
                managerClass[0] = null;
            }
        }
    },

    _onAuthenticationSuccess: function () {
        var udm = new aw.UserDataManager({ 'componentBus': this.componentBus });
        var gm = new aw.GeographyManager({ 'componentBus': this.componentBus });
        var rm = new aw.ReportManager({ 'componentBus': this.componentBus });

        // Instantiate the QueryManager after the report dataset changes
        this._onReportDatasetChanged_fx = this._onReportDatasetChanged.bind(this);
        rm.registerEvent('onreportdatasetchanged', this._onReportDatasetChanged_fx);

        var cpm = new aw.CustomPolygonManager({ 'componentBus': this.componentBus });
        var am = new aw.AnnotationManager({ 'componentBus': this.componentBus });
        var tm = new aw.ThematicsManager({ 'componentBus': this.componentBus });
        var wm = new aw.WizardManager({ 'componentBus': this.componentBus });
        var geo = new aw.Geocoder({ 'componentBus': this.componentBus });
        var im = new aw.IconManager({ 'componentBus': this.componentBus });

        // UNCOMMENT: Uncomment the next two lines when
        // ready to pull from user's HistoryGeography
        this._onHistoryGeographiesChanged_fx = this._onHistoryGeographiesChanged.bind(this);
        gm.registerEvent('onhistorygeographieschanged', this._onHistoryGeographiesChanged_fx);

        // REMOVE: Remove the code below when ready to pull 
        // from user's HistoryGeography 
        /*
        var gc = new aw.GeographyCollection(null, '17 Hatfields, London, SE1 8DJ, UK', 'Geography Selection', { x: -0.132508, y: 51.5076 });
        gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '1 Kilometer Radius', 1.0, -0.132508, 51.5076, 'Kilometers'));
        gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '3 Kilometer Radius', 3.0, -0.132508, 51.5076, 'Kilometers'));
        gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '5 Kilometer Radius', 5.0, -0.132508, 51.5076, 'Kilometers'));
        gm.setActiveGeography(gc);
        */
        // END CODE TO BE REMOVED

        var viewerClassNames = ['DNow.ReportViewer', 'DNow.FileViewer', 'DNow.MapViewer', 'DNow.QueryViewer', 'DNow.WizardViewer'];
        var viewerClass = null;
        for (var i = 0, l = viewerClassNames.length; i < l; i++) {
            viewerClass = this.componentBus.getComponentReferences(viewerClassNames[i]);
            if ((viewerClass) && (viewerClass.length > 0)) {
                viewerClass = viewerClass[0];
                viewerClass.enable();
                if (viewerClass.CLASS_NAME == 'DNow.ReportViewer')
                    viewerClass.show();
            }
        }

        //Rally US237 - Display Current Index Base
        //var rs = new aw.RecordStore({
        //    'componentBus': this.componentBus
        //});
        //Get the current index base
        this.getRecordStore("IndexBase", function (records) {
            var ibDiv = document.getElementById('indexBaseDiv');
            if (records == null) {
                //No index Base Set
                ibDiv.innerHTML = "No Index Base Selected";
                return;
            }

            //Set the geography manager's active index base
            //The geography definition that is stored in the record store looks something like this:
            //allocate:dataset=Software\SRC\Portfolio\6.50\SRC_US_AGS_10B;workspace=Geography(State, 06) Geography(State, 08)
            var geoCollection = new aw.GeographyCollection();
            var geoDef = records[0].content.definition;
            var dataSet = geoDef.split(";")[0].split("=")[1];
            var parts = geoDef.split("Geography");
            for (i = 1; i < parts.length; i++) { //The first element is the dataset
                var geo = parts[i].substring(1, parts[i].length - 2); //remove the parens
                var sName = geo.split(", ")[0];
                var gKey = geo.split(", ")[1];
                //console.log("short name: " + sName + ", key: " + gKey);
                geoCollection.addGeography(new aw.Geography.Standard(null, "Geography", "Geography", sName.toUpperCase(), gKey, dataSet));
            }
            var gm = this.componentBus.getComponentReferences('aw.GeographyManager');
            if ((gm) && (gm.length > 0))
                gm = gm[0];
            gm.activeIndexBase = geoCollection;

            //Display the index base in the UI
            var disp = records[0].content.display;
            //Truncate if longer than 100 characters
            if (disp.length >= 100)
                disp = disp.substring(0, 100) + "...";
            ibDiv.innerHTML = disp;
        } .bind(this));
        //--------------------

        if (this.canvas) {
            var login = this.canvas.down('div.header div.login');
            if (login) {
                login.update('<table style="width:600px"><tr><td><div style="color:#FFFFFF; font-size:12pt; font-weight:bold">Sales/Technical Support: +44(0) 207 961 1289</div></td><td align=right><span class="account">Account Info</span><span class="logout">Logout</span><div class="welcome">Welcome ' + this.readCookie('username') + '</div></td></tr></table>');
                aw.EventManager.register(login.down('span.account'), 'click', this._onAccountClick.bind(this));
                aw.EventManager.register(login.down('span.logout'), 'click', this._onLogoutClick.bind(this));
            }
        }
    },

    _onHistoryGeographiesChanged: function () {
        var gm = this.componentBus.getComponentReferences('aw.GeographyManager')[0];
        var hg = gm.getAllHistoryGeographies()[0];
        var udm = this.componentBus.getComponentReferences('aw.UserDataManager')[0];

        if (hg) {
            if (hg.geoCollection && hg.geoCollection !== null) {
                gm.setActiveGeography(hg.geoCollection);
            } else {
                udm.retrieveData('HistoryGeography', hg.name, null, function (gm, data) {
                    if ((data[0]) && (data[0].geoCollection)) {
                        gm.setActiveGeography(data[0].geoCollection);
                    }
                } .bind(this, gm));
            }
        } else {
            var gc = new aw.GeographyCollection(null, '17 Hatfields, London, SE1 8DJ, UK', 'Geography Selection', { x: -0.132508, y: 51.5076 });
            gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '1 Kilometer Radius', 1.0, -0.132508, 51.5076, 'Kilometers'));
            gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '3 Kilometer Radius', 3.0, -0.132508, 51.5076, 'Kilometers'));
            gc.addGeography(new aw.Geography.Radius(null, '17 Hatfields, London, SE1 8DJ, UK', '5 Kilometer Radius', 5.0, -0.132508, 51.5076, 'Kilometers'));

            gm.setActiveGeography(gc);
        }

        // We only need to listen once
        gm.unregisterEvent('onhistorygeographieschanged', this._onHistoryGeographiesChanged_fx);
        this._onHistoryGeographiesChanged_fx = null;
    },

    _onLoginClick: function () {
        new DNow.LoginDialog({ 'componentBus': this.componentBus });
    },

    _onLogoutClick: function () {
        this.componentBus.unloadUser();
    },

    _onReportDatasetChanged: function () {
        var qm = new aw.QueryManager({ 'componentBus': this.componentBus });
        var rm = this.componentBus.getComponentReferences('aw.ReportManager')[0];
        rm.unregisterEvent('onreportdatasetchanged', this._onReportDatasetChanged_fx);
        this._onReportDatasetChanged_fx = null;
    },

    _onResize: function (panel, resize) {
        if (resize === true) {
            panel.setSize();
            if (this._resizeTimeout) {
                clearTimeout(this._resizeTimeout);
                this._resizeTimeout = null;
            }
            return;
        }

        if (this._resizeTimeout)
            clearTimeout(this._resizeTimeout);

        this._resizeTimeout = setTimeout(this._onResize.bind(this, panel, true), 250);
    },

    _onSignup: function (bSubscribe) {
        new DNow.AccountDialog({
            'componentBus': this.componentBus
        });
    },

    _templates: {
        'ui': new Template(
            '<div class="header">' +
            '<img src="./custom/img/DNowLogo.png" alt="DemographicsNowUK - Powered by Alteryx" />' +
            '<div class="login"></div>' +
            '</div>' +
            '<div id="app"></div>' +
            '<div class="footer">' +
                '<div class="copyright">Copyright ' + new Date().getFullYear() + ' Alteryx, Inc. Irvine, CA. | <span class="build">Build: v' + aw.BUILD_VERSION + '</span> | <span class="about">Credits</span></div>' +
                '<div class="contact">+44(0) 207 961 1289 &#160; | &#160; <a href="mailto:GeoSupport@javelingroup.com">GeoSupport@javelingroup.com</a></div>' +
            '</div>')
    },

    VALID_PROPERTIES: ['container', 'componentBus'],

    CLASS_NAME: 'DNow'
});

