if ( typeof Fractal === 'undefined' ) throw "Fractal core.js not loaded";

Fractal.FileBrowser = (function($){

    var FileBrowser = function( el ){
        this.el = el;
        this.navOpen = false;
        this.links = this.el.find('.FileBrowser-fileListLink');
        this.items = this.el.find('.FileBrowser-fileListItem');
        this.navToggle = this.el.find('.FileBrowser-toggle');
        this.fileList = this.el.find('.FileBrowser-fileList');
        this.contentItems = this.el.find('.FileBrowser-contentItem');
    };

    FileBrowser.prototype = {

        init: function(){
            var self = this;
            this.links.on('click', function(){
                var link = $(this);
                self.items.removeClass('is-current');
                link.closest('.FileBrowser-fileListItem').addClass('is-current');
                self.showContent(link.attr('href'));
                if (self.isSmallScreen()) {
                    self.toggleNav();
                }
                return false;
            });
            this.navToggle.on('click', function(){
                self.toggleNav();
            });
        },

        showContent: function(id){
            this.contentItems.removeClass('is-current');
            this.contentItems.filter(id).addClass('is-current');
        },

        toggleNav: function(){
            this.navOpen = this.navOpen ? this.closeNav() : this.openNav();
        },

        openNav: function(){
            this.fileList.addClass('is-open');
            this.navToggle.addClass('is-active').removeClass('is-inactive');
            return true;
        },

        closeNav: function(){
            this.fileList.removeClass('is-open');
            this.navToggle.removeClass('is-active').addClass('is-inactive');
            return false;
        },

        isSmallScreen: function(){
            return (this.navToggle.css('display') !== 'none');
        }

    };

    return FileBrowser;

})(jQuery, window);
