(function(context,$) {

    "use strict";

    context.JK = context.JK || {};
    context.JK.HomeScreen = function(app) {
        var logger = context.JK.logger;
        var isFtueComplete = false;
        var $screen = null;

        function beforeShow(data) {

        }

        function afterShow(data) {
          context.JK.HelpBubbleHelper.jamtrackGuideTile($('.homecard.createsession'), $screen.find('.createsession'));
        }

        function beforeHide() {
          context.JK.HelpBubbleHelper.clearJamTrackGuide();
        }

        function mouseenterTile() {
            $(this).addClass('hover');
        }

        function mouseleaveTile() {
            $(this).removeClass('hover');
        }

        function switchClientMode(e) {
            // ctrl + shift + 0
            if(e.ctrlKey && e.shiftKey && e.keyCode == 48) {
                logger.debug("switch client mode!");
                var act_as_native_client = $.cookie('act_as_native_client');

                logger.debug("currently: " + act_as_native_client);
                if(act_as_native_client == null || act_as_native_client != "true") {
                    logger.debug("forcing act as native client!");
                    $.cookie('act_as_native_client', 'true', { expires: 120, path: '/' });
                }
                else {
                    logger.debug("remove act as native client!");
                    $.removeCookie('act_as_native_client');
                }
                window.location.reload();
            }
        }

        function userHasDevices() {


            // and enable features
            $($createSession).on('mouseenter', mouseenterTile);
            $($createSession).on('mouseleave', mouseleaveTile);
            $($findSession).on('mouseenter', mouseenterTile);
            $($findSession).on('mouseleave', mouseleaveTile);
        }

        function userHasNoDevices() {

            var $createSession = $('div[type="createSession"]');
            var $findSession = $('div[type="findSession"]');

            $createSession.removeAttr('layout-link');
            $findSession.removeAttr('layout-link');

            // undo any earlier enabling
            $($createSession).off('mouseenter', mouseenterTile);
            $($createSession).off('mouseleave', mouseleaveTile);

            $($findSession).off('mouseenter', mouseenterTile);
            $($findSession).off('mouseleave', mouseleaveTile);

            // disable Create Session and Find Session tiles
            $('h2', $createSession).addClass('disabled');
            $('h2', $findSession).addClass('disabled');
            $createSession.addClass('createsession-disabled');
            $createSession.removeClass('createsession');
            $findSession.addClass('findsession-disabled');
            $findSession.removeClass('findsession');
        }

        // used to initialize things that do not have to be touched up in the same UI sessiion
        function events() {
            // initialize profile, feed and account tiles normally
            $('.homecard').on('mouseenter', mouseenterTile);
            $('.homecard').on('mouseleave', mouseleaveTile);

            if(gon.allow_force_native_client) {
                $('body').on('keyup', switchClientMode);
            }

            $('.homecard.jamclass').on('click', function() {

                if (gon.jamclass_enabled) {
                    return true;
                }
                else {
                    context.JK.Banner.showNotice("Coming Soon!", "JamClass is just around the corner.")
                    return false;

                }
            })
        }

        this.initialize = function() {
            var screenBindings = { 'beforeShow': beforeShow, 'afterShow': afterShow, 'beforeHide' : beforeHide };
            app.bindScreen('home', screenBindings);
            events();
            $screen = $('.screen[layout-id="home"]')

            $screen.find('.profile').on('click', function() {
              var $destination = $('[layout-id="profile"]');
              if(!context.JK.currentUserId && !$destination.is('.no-login-required')) {
                // if there is no user and login is required, then stop user from clicknig through
                app.layout.showDialog('login-required-dialog')
              }
              else
              {
                context.location = '/client#/profile/' + context.JK.currentUserId;
              }


            });
        };

        this.beforeShow = beforeShow;
    };

 })(window,jQuery);