'use strict';

var $ = require('jquery');

class StickySidebarComponent {

    /**
     * StickySidebarComponent
     * @constructor 
     * @param {jQuery} $rootWindow - jQuery wrapper of the window object
     * @param {jQuery} $html - jQuery wrapper of the html node
     */
    constructor ($html, window) {
        this.$html = $html;
        this.window = window;
    }

    init () {
        if (typeof this.window === 'undefined' || !this.window) {
            throw new Error('window must be passed to StickySidebarComponent');
        }

        if (typeof this.$html === 'undefined' || !this.$html) {
            throw new Error('$html must be passed to StickySidebarComponent');
        }

        this.sticky()
        $(this.window).on('scroll resize', (event) => this.sticky(event));
    }

    sticky () {
console.log('sticky');        

        const $container = this.$html.find('.tab__container.has-settings .tab__inner');

        if (!$container.length) {
            return false;
        }
        
        let windowTop = $(window).scrollTop(),
            containerTop = $container.offset().top;
console.log(windowTop);
console.log(containerTop);

console.log(this.$html.find('.tab__container.has-settings .tab__content').outerHeight());
console.log(this.$html.find('.tab__container.has-settings .tab__settings').outerHeight());
            
        if (this.$html.find('.tab__container.has-settings .tab__content').outerHeight() > this.$html.find('.tab__container.has-settings .tab__settings').outerHeight()) {
            if (windowTop > containerTop) {
                $container.addClass('is-sticky');
            } else {
                $container.removeClass('is-sticky');
            }
        } else {
            $container.removeClass('is-sticky');
        }
        
    }
}

module.exports = StickySidebarComponent;


// (function($) {
//     function sticky() {
        
//         var windowTop = $(window).scrollTop(),
//             $container = $('.tab__container.has-settings .tab__inner'),
//             $settings = $('.tab__container.has-settings .tab__settings'),
//             containerTop = $container.offset().top;

//         if ($('.tab__container.has-settings .tab__content').outerHeight() > $('.tab__container.has-settings .tab__settings').outerHeight()) {
//             if (windowTop > containerTop) {
//                 $container.addClass('is-sticky');
//             } else {
//                 $container.removeClass('is-sticky');
//             }
//         } else {
//             $container.removeClass('is-sticky');
//         }
//     }

//     sticky();

//     $(window).on('scroll resize', sticky);

// })(jQuery);
@@ -146,51 +146,24 @@
 am %7B
-jQuery%7D $rootWindow - jQuery wrapper of the
+object%7D window -
  win
@@ -339,24 +339,225 @@
 dow;%0A    %7D%0A%0A
+    /**%0A     * Initialise component%0A     * %0A     * Should be fired on page load, checks for required options, performs an %0A     * initial sticky calculation and binds to resize & scroll events%0A     */%0A
     init () 
@@ -582,22 +582,21 @@
 of this.
-window
+$html
  === 'un
@@ -613,22 +613,21 @@
 %7C !this.
-window
+$html
 ) %7B%0A    
@@ -651,22 +651,21 @@
  Error('
-window
+$html
  must be
@@ -728,37 +728,38 @@
 if (typeof this.
-$html
+window
  === 'undefined'
@@ -760,37 +760,38 @@
 fined' %7C%7C !this.
-$html
+window
 ) %7B%0A            
@@ -799,37 +799,38 @@
 hrow new Error('
-$html
+window
  must be passed 
@@ -981,1094 +981,254 @@
     
-sticky () %7B%0Aconsole.log('sticky');        %0A%0A        const $container = this.$html.find('.tab__container.has-settings .tab__inner');%0A%0A        if (!$container.length) %7B%0A            return false;%0A        %7D%0A        %0A        let windowTop = $(window).scrollTop(),
+/**%0A     * Sticky calculation
 %0A     
+* %0A
      
- 
+* If
  cont
-ainerTop = $container.offset().top;%0Aconsole.log(windowTop);%0Aconsole.log(containerTop);%0A%0Aconsole.log(this.$html.find('.tab__container.has-settings .tab__content').outerHeight());%0Aconsole.log(this.$html.find('.tab__container.has-settings .tab__settings').outerHeight());%0A            %0A        if (this.$html.find('.tab__container.has-settings .tab__content').outerHeight() %3E this.$html.find('.tab__container.has-settings .tab__settings').outerHeight()) %7B%0A            if (windowTop %3E containerTop) %7B%0A                $container.addClass('is-sticky');%0A            %7D else %7B%0A                $container.removeClass('is-sticky');%0A            %7D%0A        %7D else %7B%0A            $container.removeClass('is-sticky');%0A        %7D%0A        %0A    %7D%0A%7D%0A%0Amodule.exports = StickySidebarComponent;%0A%0A%0A// (function($) %7B%0A//     function
+ent is taller than settings, and the viewport has been scrolled %0A     * enough then add the required sticky class to the container, and remove it%0A     * if no longer required%0A     */%0A   
  sticky
+ 
 () %7B
@@ -1240,283 +1240,188 @@
     
-%0A//         var windowTop = $(window).scrollTop(),%0A//             $container = $('.tab__container.has-settings .tab__inner'),%0A//             $settings = $('.tab__container.has-settings .tab__settings'),%0A//             containerTop = $container.offset().top;%0A%0A//         if ($
+const $container = this.$html.find('.tab__container.has-settings .tab__inner');%0A%0A        if (!$container.length) %7B%0A            return false;%0A        %7D%0A%0A        if ((this.$html.find
 ('.t
@@ -1479,17 +1479,31 @@
 ght() %3E 
-$
+this.$html.find
 ('.tab__
@@ -1561,41 +1561,37 @@
 ()) 
-%7B%0A//             if (window
+&& ($(window).scroll
 Top
+()
  %3E 
+$
 cont
@@ -1599,19 +1599,26 @@
 iner
-T
+.offset().t
 op)
+)
  %7B%0A
-//  
     
@@ -1617,35 +1617,32 @@
 ) %7B%0A            
-   
 $container.addCl
@@ -1651,39 +1651,32 @@
 s('is-sticky');%0A
-//     
         %7D else %7B
@@ -1668,36 +1668,32 @@
        %7D else %7B%0A
-//  
                $
@@ -1680,35 +1680,32 @@
 e %7B%0A            
-   
 $container.remov
@@ -1721,31 +1721,24 @@
 s-sticky');%0A
-//     
         %7D%0A//
@@ -1735,27 +1735,24 @@
       %7D%0A
-// 
         
 %7D else %7B
@@ -1747,167 +1747,55 @@
     
-%7D else %7B%0A//             $container.removeClass('is-sticky');%0A//         %7D%0A//     %7D%0A%0A//     sticky();%0A%0A//     $(window).on('scroll resize', sticky);%0A%0A// %7D)(jQuery);
+%0A    %7D%0A%7D%0A%0Amodule.exports = StickySidebarComponent;%0A

Remove commented code and component is functional