/* ------------------------------------------------------------------------------
*
*  # Sticky sidebar with custom scrollbar
*
*  Demo JS code for layout_sidebar_sticky_custom.html page
*
* ---------------------------------------------------------------------------- */

document.addEventListener('DOMContentLoaded', function() {

     // Select with search
    $('.select-search').select2();

    // Format Text Bold
    /*function iconFormat(icon) {
        var originalOption = icon.element;
        if (!icon.id) { return icon.text; }
        var $icon = "<i class='" + $(icon.element).data('icon') + "'></i>" + icon.text;
        return $icon;
    }

    function formatOutput(optionElement) {
        if (!optionElement.id) { return optionElement.text; }
          var $state = $('<span><strong>' + optionElement.element.id + '</strong> ' + optionElement.text + '</span>');
          return $state;
    };*/

    function formatbold(optionElement){
        if (!optionElement.id) { return optionElement.text; }
        if(optionElement.element.id=='1'){
          var $state = $('<span><strong>' + optionElement.text + '</strong></span>');
          
        }else{
          var $state = $('<span>' + optionElement.text + '</span>'); 
        }
        return $state;
    };

    // Initialize with options
    $(".select-bold").select2({
        templateResult: formatbold,
        escapeMarkup: function(m) { return m; }
    });

    // Select multiple
    $('.multiselect-select-all-filtering').multiselect({
        includeSelectAllOption: true,
        enableFiltering: true,
        templates: {
            filter: '<li class="multiselect-item multiselect-filter"><i class="icon-search4"></i> <input class="form-control" type="text"></li>'
        }
    });


    // Mini sidebar
    // -------------------------

    // Setup
    function miniSidebar() {
        if ($('body').hasClass('sidebar-xs')) {
            $('.sidebar-main.sidebar-fixed .sidebar-content').on('mouseenter', function () {
                if ($('body').hasClass('sidebar-xs')) {

                    // Expand fixed navbar
                    $('body').removeClass('sidebar-xs').addClass('sidebar-fixed-expanded');
                }
            }).on('mouseleave', function () {
                if ($('body').hasClass('sidebar-fixed-expanded')) {

                    // Collapse fixed navbar
                    $('body').removeClass('sidebar-fixed-expanded').addClass('sidebar-xs');
                }
            });
        }
    }

    // Initialize
    miniSidebar();


    // Toggle mini sidebar
    $('.sidebar-main-toggle').on('click', function (e) {

        // Initialize mini sidebar 
        miniSidebar();
    });


    // Nice scroll
    // ------------------------------

	// Setup
	function initScroll() {
	    $(".sidebar-fixed .sidebar-content").niceScroll({
	        mousescrollstep: 100,
	        cursorcolor: '#ccc',
	        cursorborder: '',
	        cursorwidth: 3,
	        hidecursordelay: 100,
	        autohidemode: 'scroll',
	        horizrailenabled: false,
	        preservenativescrolling: false,
	        railpadding: {
	        	right: 0.5,
	        	top: 1.5,
	        	bottom: 1.5
	        }
	    });
	}

	// Remove
	function removeScroll() {
		$(".sidebar-fixed .sidebar-content").getNiceScroll().remove();
		$(".sidebar-fixed .sidebar-content").removeAttr('style').removeAttr('tabindex');
	}

    // Initialize
    initScroll();



    // Remove scrollbar on mobile
    $(window).on('resize', function() {
        setTimeout(function() {            
            if($(window).width() <= 768) {

                // Remove nicescroll on mobiles
                removeScroll();
            }
            else {

                // Init scrollbar
                initScroll();
            }
        }, 100);
    }).resize();


    // Primary
    $(".control-primary").uniform({
        wrapperClass: 'border-primary-400 text-primary-600'
    });

    // Danger
    $(".control-danger").uniform({
        wrapperClass: 'border-danger-400 text-danger-600'
    });

    // Success
    $(".control-success").uniform({
        wrapperClass: 'border-success-400 text-success-600'
    });

    // Warning
    $(".control-warning").uniform({
        wrapperClass: 'border-warning-400 text-warning-600'
    });

    // Info
    $(".control-info").uniform({
        wrapperClass: 'border-info-400 text-info-600'
    });

});
