$(document).ready(function() {
    
    "use strict";
    
    // Options
    var submenu_animation_speed = 200,
        submenu_opacity_animation = true,
        page_boxed = false,
        page_sidebar_fixed = false,
        page_sidebar_collapsed = false,
        page_header_fixed = false;
    
    // Elements
    var body = $('body'),
        page_header = $('.page-header'),
        page_sidebar = $('.page-sidebar'),
        page_content = $('.page-content');
    
    
    
    
    
    
    // Sidebar and content height
    /*var adjustSidebarContent = function() {
        var content = $('.page-inner'),
            sidebar = $('.page-sidebar'),
            sidebarInner = $('.page-sidebar-inner'),
            body = $('body'),
            height,
            footerHeight = $('.page-footer').outerHeight(),
            pageContentHeight = $('.page-container').height();

        function checkForChanges() {
            content.attr('style', 'min-height:' + sidebarInner.height() + 'px !important');
            
            if (body.hasClass('page-sidebar-fixed')) {
                height = sidebar.height();
            } else {
                height = sidebarInner.height();
                if (height  < $(window).height()) {
                    height = $(window).height();
                }
            };
            if (body.hasClass('page-sidebar-collapsed')) {
                if(sidebar.height() < content.height()){
                    var collapsedSidebarHeight = content.height() + 35;
                    sidebar.attr('style', 'min-height:' + collapsedSidebarHeight + 'px');
                    sidebarInner.attr('style', 'min-height:' + collapsedSidebarHeight + 'px');
                    content.attr('style', 'min-height:' + collapsedSidebarHeight + 'px');
                    return false;
                } else {
                    $('.page-sidebar').attr('style', 'min-height:' + $(window).height() + 'px !important');
                    var innerSidebarHeight = sidebar.height() - 100;
                    if (body.hasClass('page-sidebar-fixed')) {
                        height = $(window).height();
                    };
                }
            } else {
            };
            
            if((!sidebar.length)&&($('.page-header').length)) {
                height = $(window).height();
            } else if((!sidebar.length)&&(!$('.page-header').length)) {
                height = $(window).height();
            };
        
        if (height >= content.height()) {
            content.attr('style', 'min-height:' + height + 'px !important');
        } else {
            content.addClass();
        }
            setTimeout(checkForChanges, 0);
        };
        checkForChanges();
    };
    
    window.onresize = adjustSidebarContent();*/
    
    
    // Boxed Page 
    var boxed_page = function() {
        if(page_boxed == true) {
            $('.page-container').addClass('container');
        };
    };
    
    
    // Fixed Header
    var fixed_header = function() {
        if(page_header_fixed == true) {
            $('body').addClass('page-header-fixed');
        };
    };
    
    
    // Sidebar
    var page_sidebar_init = function() {
        
        // Slimscroll
        $('.page-sidebar-inner').slimScroll({
            height: '100%'
        });
        $('.page-sidebar-inner').mouseover();  
        
        // Fixed Sidebar
        var fixed_sidebar = function() {
            if((body.hasClass('page-sidebar-fixed'))&&(page_sidebar_fixed == false)) {
                page_sidebar_fixed = true;
            };
            
            if(page_sidebar_fixed == true) {
                body.addClass('page-sidebar-fixed');
                $('#fixed-sidebar-toggle-button').removeClass('icon-radio_button_unchecked');
                $('#fixed-sidebar-toggle-button').addClass('icon-radio_button_checked');
            };
            
            var fixed_sidebar_toggle = function() {
                body.toggleClass('page-sidebar-fixed');
                if(body.hasClass('page-sidebar-fixed')) {
                    page_sidebar_fixed = true;
                } else {
                    page_sidebar_fixed = false;
                }
            };
    
            $('#fixed-sidebar-toggle-button').on('click', function() {
                fixed_sidebar_toggle();
                $(this).toggleClass('icon-radio_button_unchecked');
                $(this).toggleClass('icon-radio_button_checked');
                return false;
            });
        };
        
        
        // Collapsed Sidebar
        /*var collapsed_sidebar = function() {
            if(page_sidebar_collapsed == true) {
                body.addClass('page-sidebar-collapsed');
            };
            
            var collapsed_sidebar_toggle = function() {
                body.toggleClass('page-sidebar-collapsed');
                if(body.hasClass('page-sidebar-collapsed')) {
                    page_sidebar_collapsed = true;
                } else {
                    page_sidebar_collapsed = false;
                };
                $('.page-sidebar-collapsed .page-sidebar .accordion-menu > li').hover(
                    function(){ $('.page-sidebar').addClass('fixed-sidebar-scroll') },
                    function(){ $('.page-sidebar').removeClass('fixed-sidebar-scroll') }
                );
            };
            
            $('.page-sidebar-collapsed .page-sidebar .accordion-menu > li').on('mouseenter', function(){
                $('.page-sidebar').addClass('fixed-sidebar-scroll');
            });
            
            $('.page-sidebar-collapsed .page-sidebar .accordion-menu > li').on('mouseleave', function(){
                $('.page-sidebar').removeClass('fixed-sidebar-scroll');
            });
    
            $('#collapsed-sidebar-toggle-button').on('click', function() {
                collapsed_sidebar_toggle();
                return false;
                adjustSidebarContent();
            });
        };*/
        
        var small_screen_sidebar = function(){
            if(($(window).width() < 768)&&($('#fixed-sidebar-toggle-button').hasClass('icon-radio_button_unchecked'))){
                $('#fixed-sidebar-toggle-button').click();
            }
            $(window).on('resize', function() {
                if(($(window).width() < 768)&&($('#fixed-sidebar-toggle-button').hasClass('icon-radio_button_unchecked'))){
                    $('#fixed-sidebar-toggle-button').click();
                }
            });
            $('#sidebar-toggle-button').on('click', function() {
                body.toggleClass('page-sidebar-visible');
                return false;
            });
            $('#sidebar-toggle-button-close').on('click', function() {
                body.toggleClass('page-sidebar-visible');
                return false;
            });
        };
        
        fixed_sidebar();
        //collapsed_sidebar();
        small_screen_sidebar();
        
    };
    
        
    // Accordion menu
    var accordion_menu = function() {
        
        var select_sub_menus = $('.page-sidebar li:not(.open) .sub-menu'),
            active_page_sub_menu_link = $('.page-sidebar li.active-page > a');
        
        // Hide all sub-menus
        select_sub_menus.hide();
        
        
        if(submenu_opacity_animation == false) {
            $('.sub-menu li').each(function(i){
                $(this).addClass('animation');
            });
        };
        
        // Accordion
        $('.accordion-menu li a').on('click', function() {
            var sub_menu = $(this).next('.sub-menu'),
                parent_list_el = $(this).parent('li'),
                active_list_element = $('.accordion-menu > li.open'),
                show_sub_menu = function() {
                    sub_menu.slideDown(submenu_animation_speed);
                    parent_list_el.addClass('open');
                    if(submenu_opacity_animation === true) {
                        $('.open .sub-menu li').each(function(i){
                            var t = $(this);
                            setTimeout(function(){ t.addClass('animation'); }, (i+1) * 25);
                        });
                    };
                },
                hide_sub_menu = function() {
                    if(submenu_opacity_animation === true) {
                        $('.open .sub-menu li').each(function(i){
                            var t = $(this);
                            setTimeout(function(){ t.removeClass('animation'); }, (i+1) * 15);
                        });
                    };
                    sub_menu.slideUp(submenu_animation_speed);
                    parent_list_el.removeClass('open');
                },
                hide_active_menu = function() {
                    $('.accordion-menu > li.open > .sub-menu').slideUp(submenu_animation_speed);
                    active_list_element.removeClass('open');
                };
            
            if((sub_menu.length)&&(!body.hasClass('page-sidebar-collapsed'))) {
                
                if(!parent_list_el.hasClass('open')) {
                    if(active_list_element.length) {
                        hide_active_menu();
                    };
                    show_sub_menu();
                } else {
                    hide_sub_menu();
                };
                
                return false;
                
            };
            if((sub_menu.length)&&(body.hasClass('page-sidebar-collapsed'))){
                return false;
            };
            //adjustSidebarContent();
        });
        
        if($('.active-page > .sub-menu').length) {
            active_page_sub_menu_link.click();
        };
    };

    
    // Fulscreen Function
    function toggleFullScreen() {
        if (!document.fullscreenElement &&    // alternative standard method
            !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {  // current working methods
            if (document.documentElement.requestFullscreen) {
                document.documentElement.requestFullscreen();
            } else if (document.documentElement.msRequestFullscreen) {
                document.documentElement.msRequestFullscreen();
            } else if (document.documentElement.mozRequestFullScreen) {
                document.documentElement.mozRequestFullScreen();
            } else if (document.documentElement.webkitRequestFullscreen) {
                document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
            }
        } else {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.msExitFullscreen) {
                document.msExitFullscreen();
            } else if (document.mozCancelFullScreen) {
                document.mozCancelFullScreen();
            } else if (document.webkitExitFullscreen) {
                document.webkitExitFullscreen();
            }
        }
    };
    
    // Navbar
    var navbar_init = function(){
        
        $('#toggle-fullscreen').on('click', function(){ 
            toggleFullScreen();
            return false;
            
        });
        
        $('#search-button').on('click', function(){
            body.toggleClass('search-open')
            if(body.hasClass('search-open')) {
                $('.search-form input').focus();
            }
        });
        
        $('#close-search').on('click', function(){
            body.toggleClass('search-open')
        });
        
    };
    
    // Right Sidebar
    var right_sidebar = function(){
        $('.right-sidebar-toggle').on('click', function(){
            var sidebarId = $(this).data("sidebar-id");
            $('#' + sidebarId).toggleClass('visible');
        });
        
        var write_message = function(){
            $(".chat-write form input").on('keypress', function (e) {
                if ((e.which == 13)&&(!$(this).val().length == 0)) {
                    if($('.right-sidebar-chat .chat-bubbles .chat-bubble:last-child').hasClass('me')) {
                        
                    $('<span class="chat-bubble-text">' + $(this).val() + '</span>').insertAfter(".right-sidebar-chat .chat-bubbles .chat-bubble:last-child span:last-child");
                    } else {
                        $('<div class="chat-bubble me"><div class="chat-bubble-text-container"><span class="chat-bubble-text">' + $(this).val() + '</span></div></div>').insertAfter(".right-sidebar-chat .chat-bubbles .chat-bubble:last-child");
                    };
                    $(this).val('');
                } else if(e.which == 13) {
                    return;
                }
                var scrollTo_int = $('.right-sidebar-chat').prop('scrollHeight') + 'px';
                $('.right-sidebar-chat').slimscroll({
                    allowPageScroll: true,
                    scrollTo : scrollTo_int
                });
            });
        };
        write_message();
    };
    
    // Plugins
    var plugins_init = function(){
        // Slimscroll
        $('.slimscroll').slimScroll();
        
        // Uniform
        var checkBox = $("input[type=checkbox]:not(.js-switch), input[type=radio]:not(.no-uniform)");
        if (checkBox.length > 0) {
            checkBox.each(function() {
                $(this).uniform();
            });
        };
        
        var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
        elems.forEach(function(html) {
            var switchery = new Switchery(html, {size: 'small', color: '#637282'});
        });
    };
    
    page_sidebar_init();
    boxed_page();
    accordion_menu();
    navbar_init();
    right_sidebar();
    //adjustSidebarContent();
    plugins_init();
    
});