/*---LEFT BAR ACCORDION----*/
$(function() {
    $('#nav-accordion').dcAccordion({
        eventType: 'click',
        autoClose: true,
        saveState: true,
        disableLink: true,
        speed: 'slow',
        showCount: false,
        autoExpand: true,
//        cookie: 'dcjq-accordion-1',
        classExpand: 'dcjq-current-parent'
    });

    // 右侧-用户详情
    $('.btn-user-detail').unbind('click').bind('click', function (event) {
        event.stopPropagation();
        event.preventDefault();

        document.getElementById('right-detail-content').innerHTML = "";
        var url = "/admin/User/read";
        var oid = $(this).attr('oid');
        showRightDetail(function () {
            $.get(url + "/id/" + oid, function (res) {
                var res = JSON.parse(res);
                if (res.error) {
                    new PNotify({
                        title: '提示',
                        text: res.msg,
                        'type': 'error'
                    });
                } else {
                    var html = template('user', res.data);
                    document.getElementById('right-detail-content').innerHTML = html;
                }
            });
        });
    });

    // 右侧-课程详情
    $('.btn-course-detail').unbind('click').bind('click', function (event) {
        event.stopPropagation();
        event.preventDefault();

        document.getElementById('right-detail-content').innerHTML = "";
        var url = "/admin/Course/read";
        var oid = $(this).attr('oid');
        showRightDetail(function () {
            $.get(url + "/id/" + oid, function (res) {
                var res = JSON.parse(res);
                if (res.error) {
                    new PNotify({
                        title: '提示',
                        text: res.msg,
                        'type': 'error'
                    });
                } else {
                    var html = template('item', res.data);
                    document.getElementById('right-detail-content').innerHTML = html;
                }
            });
        });
    });

    // 右侧-授课详情
    $('.btn-class-course-detail').unbind('click').bind('click', function (event) {
        event.stopPropagation();
        event.preventDefault();

        document.getElementById('right-detail-content').innerHTML = "";
        var url = "/admin/Teach/class_course_detail";
        var oid = $(this).attr('oid');
        showRightDetail(function () {
            $.get(url + "/id/" + oid, function (res) {
                var res = JSON.parse(res);
                if (res.error) {
                    new PNotify({
                        title: '提示',
                        text: res.msg,
                        'type': 'error'
                    });
                } else {
                    // console.log(res.data);
                    var html = template('classcourse', res.data);
                    document.getElementById('right-detail-content').innerHTML = html;
                }
            });
        });
    });

});

var Script = function () {
//    sidebar dropdown menu auto scrolling
    jQuery('#sidebar .sub-menu > a').click(function () {
        var o = ($(this).offset());
        diff = 250 - o.top;
        if(diff>0)
            $("#sidebar").scrollTo("-="+Math.abs(diff),500);
        else
            $("#sidebar").scrollTo("+="+Math.abs(diff),500);
    });

//    sidebar toggle
    $(function() {
        function responsiveView() {
            var wSize = $(window).width();
            if (wSize <= 768) {
                $('#container').addClass('sidebar-close');
                $('#sidebar > ul').hide();
            }

            if (wSize > 768) {
                $('#container').removeClass('sidebar-close');
                $('#sidebar > ul').show();
            }
        }
        $(window).on('load', responsiveView);
        $(window).on('resize', responsiveView);
    });

// 左侧导航
    $('.fa-bars').click(function () {
        if ($('#sidebar > ul').is(":visible") === true) {
            $('#main-content').css({
                'margin-left': '0px'
            });
            $('#sidebar').css({
                'margin-left': '-210px'
            });
            $('#sidebar > ul').hide();
            $("#container").addClass("sidebar-closed");
        } else {
            $('#main-content').css({
                'margin-left': '210px'
            });
            $('#sidebar > ul').show();
            $('#sidebar').css({
                'margin-left': '0'
            });
            $("#container").removeClass("sidebar-closed");
        }
    });

    // 右侧关闭
    $('#right-detail .right-detail-close > .fa-times').click(function () {
        closeRightDetail();
    });
    //$("#right-detail").niceScroll({styler:"fb",cursorcolor:"#4ECDC4", cursorwidth: '3', cursorborderradius: '10px', background: '#404040', spacebarenabled:false, cursorborder: ''});

// custom scrollbar
   // $("#sidebar").niceScroll({styler:"fb",cursorcolor:"#4ECDC4", cursorwidth: '3', cursorborderradius: '10px', background: '#404040', spacebarenabled:false, cursorborder: ''});

    //$("html").niceScroll({styler:"fb",cursorcolor:"#4ECDC4", cursorwidth: '6', cursorborderradius: '10px', background: '#404040', spacebarenabled:false,  cursorborder: '', zindex: '1000'});

// widget tools

    jQuery('.panel .tools .fa-chevron-down').click(function () {
        var el = jQuery(this).parents(".panel").children(".panel-body");
        if (jQuery(this).hasClass("fa-chevron-down")) {
            jQuery(this).removeClass("fa-chevron-down").addClass("fa-chevron-up");
            el.slideUp(200);
        } else {
            jQuery(this).removeClass("fa-chevron-up").addClass("fa-chevron-down");
            el.slideDown(200);
        }
    });

    jQuery('.panel .tools .fa-times').click(function () {
        jQuery(this).parents(".panel").parent().remove();
    });


//    tool tips
    $('.tooltips').tooltip();

//    popovers
    $('.popovers').popover();

// custom bar chart
    if ($(".custom-bar-chart")) {
        $(".bar").each(function () {
            var i = $(this).find(".value").html();
            $(this).find(".value").html("");
            $(this).find(".value").animate({
                height: i
            }, 2000)
        })
    }
// go top
    $('.go-top').click(function(event) {
        $.scrollTo(0, 800, 'easing');
    });

    $('.btn_left').unbind('click').bind('click', function(){
        history.go(-1)
    });
    //表格图片预览
    $('.table td img').click(function(){
        if($("#imageview")[0]){
            $("#imageview img").attr('src',$(this).attr('src'));
            $("#imageview").show();
        }else{
            $("body").append('<div class="imageview" id="imageview"><span class="remove">&times;</span><img src=\"'+$(this).attr('src')+'\" alt=""></div>');
        }
        var width = $("#imageview").width();
        var height = $("#imageview").height();
        var left = ($(window).width() - width)/2;
        var top = ($(window).height() - height)/2;
        $("#imageview").css({'top':top+'px','left':left+'px'});
    })
    $('body').on('click','#imageview .remove',function(){
        $(this).parent().hide();
    })
}();
