define([
        'Global',
        'Text!../html/templates/sessionWarning.html'
    ],
    function (Global, template) {
        'use strict';
        return Backbone.Marionette.ItemView.extend({
            id: "session-warning-dialog",
            template: _.template(template),
            attributes: {
                "data-role": "dialog",
                "data-corners": "false",
                "data-overlay-theme": "d",
                "data-close-btn": "none"
            },
            events: {
                "click #end-session": "endSession",
                "click #keep-session": "keepSession"
            },
            modelEvents:{
                "change sessionTimeoutIn": "updateTime"
            },
            initialize: function () {
                this.bindUIElements();
            },
            ui: {
                'timeLeft': '#countdown-session',
                'units': '#countdown-units'
            },
            onRender: function () {
                this.current = true;
                var $container = $('body'),
                    that = this;

                $container
                    .append(this.$el);

                $(window).resize();
                this.delegateEvents();
                return this.$el;
            },
            templateHelpers: {
                getSessionTimeoutIn: function(){
                    return this.sessionTimeoutIn;
                },
                getTimeUnits: function(){
                    return this.timeUnits;
                }
            },
            keepSession: function(){
                Global.getUtilities().eventAggregator.trigger("do:session:refresh", true);
                this.current = false;
            },
            endSession: function(){
                Global.getUtilities().eventAggregator.trigger("do:logout");
                this.current = false;
            },
            updateTime: function(){
                this.ui.timeLeft.text(this.model.get("sessionTimeoutIn"));
                this.ui.units.text(this.model.get("timeUnits"));
            }
        });
    }
);
