$(function(){
    //右侧最小高度 = 左侧高度 - 15px   保证两边高度基本一样，页面美观
    $("#RSide").css("min-height",$("#LSide").height()-35);
    $("#users_setting").css("min-height",$("#LSide").height()-100);

    //头像相关
    $("#homepage_portrait_image").on("mouseover",function(){
        $("#edit_user_file_btn").show();
        $("#watch_user_btn").show();
    }).on("mouseout",function(){
        $("#edit_user_file_btn").hide();
        $("#watch_user_btn").hide();
    });

    //日历选择样式
    //$(".ui-datepicker-trigger").replaceWith("<div class='fl DateBorder mr10'><img class='ui-datepicker-trigger'></div>")
});

//编辑个人简介
function show_edit_user_introduction() {
    $("#user_brief_introduction_show").hide();
    $("#user_brief_introduction_edit").show();
    $("#user_brief_introduction_edit").focus();
}

//编辑个人简介完成之后提交
function edit_user_introduction(url){
    $.get(
        url,
        { brief_introduction: $("#user_brief_introduction_edit").val() },
        function (data) {

        }
    );
}

//显示更多的课程
function show_more_course(url){
    $.get(
        url,
        { page: $("#course_page_num").val() },
        function (data) {
        }
    );
}

//显示更多的项目
function show_more_project(url){
    $.get(
        url,
        { page: $("#project_page_num").val() },
        function (data) {

        }
    );
}

//老师提交 新建/修改 作业
function submit_homework(id){
    if(!regex_homework_name()){
        $("#homework_name").focus();
    }
    else if(!regex_homework_end_time()){
        $("#homework_end_time").focus();
    }
    else if(!regex_course_id()){
        $("#course_id").focus();
    }
    else{
        homework_description_editor.sync();
        $("#"+id).submit();
    }
}

//验证新建作业的名字
function regex_homework_name()
{
    var name = $.trim($("#homework_name").val());

    if(name=="")
    {
        $("#homework_name_span").text("名称不能为空");
        return false;
    }
    else
    {
        $("#homework_name_span").text("");
        return true;
    }
}
//验证截止时间
function regex_homework_end_time()
{
    var name = $.trim($("#homework_end_time").val());
    if(name=="")
    {
        $("#homework_end_time_span").text("截止时间不能为空");
        return false;
    }
    else
    {
        $("#homework_end_time_span").text("");
        return true;
    }
}

//验证发送到课程
function regex_course_id(){
    var course_id = $("#course_id").val();
    if(course_id == -1)
    {
        $("#homework_course_id_span").text("发布课程不能为空");
        return false;
    }
    else
    {
        $("#homework_course_id_span").text("");
        return true;
    }
}

//老师导入作业时查询作业
function search_homework_by_name(url){
    $.get(
        url,
        { name: $("#search_homework_name").val() },
        function (data) {
        }
    );
}

//提交匿评参数设置
function submit_set_evaluation_attr(end_time){
    if(!regex_evaluation_start(end_time)){
        $("#evaluation_start_time").focus();
    }
    else if(!regex_evaluation_end()){
        $("#evaluation_end_time").focus();
    }
    else if(!regex_evaluation_num()){
        $("#evaluation_num").focus();
    }
    else{
        $('#popbox02 form').submit();
    }
}

//验证匿评开启时间：大于截止时间,或者为空
function regex_evaluation_start(end_time){
    var evaluation_start = $.trim($("#evaluation_start_time").val());
    if(evaluation_start == ""){
        $("#homework_evaluation_start_time").text("开启匿评日期不能为空");
        return false;
    }
    var end_time = new Date(end_time);
    var evaluation_start_time = new Date(evaluation_start);
    if(evaluation_start_time > end_time){
        $("#homework_evaluation_start_time").text("");
        return true;
    }else{
        $("#homework_evaluation_start_time").text("开启匿评日期必须大于截止日期");
        return false;
    }
}

//验证匿评结束时间：大于匿评开启时间,或者为空。当匿评开启时间为空时，匿评结束时间必须为空
function regex_evaluation_end(){
    var evaluation_start = $.trim($("#evaluation_start_time").val());
    var evaluation_end = $.trim($("#evaluation_end_time").val());
    if(evaluation_end == ""){
        $("#homework_evaluation_end_time").text("关闭匿评日期不能为空");
        return true;
    }
    var evaluation_start_time = new Date(evaluation_start);
    var evaluation_end_time = new Date(evaluation_end);
    if(evaluation_end_time > evaluation_start_time){
        $("#homework_evaluation_end_time").text("");
        return true;
    }else{
        $("#homework_evaluation_end_time").text("关闭匿评日期必须大于开启匿评日期");
        return false;
    }
}

//验证匿评数量
function regex_evaluation_num(){
    var evaluation_num = $.trim($("#evaluation_num").val());
    var regex = /^\d+$/;
    if(evaluation_num==""){
        $("#evaluation_num_notice").text("匿评人数不能为空");
        return false;
    }
    else if(regex.test(evaluation_num)){
        if(evaluation_num > 0){
            $("#evaluation_num_notice").html("");
            return true;
        }
        else{
            $("#evaluation_num_notice").text("匿评人数必须为大于0");
            return false;
        }
    }
    else{
        $("#evaluation_num_notice").text("匿评人数只能为数字");
        return false;
    }
}

//提交新建作品
function new_student_work()
{
    if(regexStudentWorkName()&&regexStudentWorkDescription())
    {$("#new_student_work").submit();}
}

function edit_student_work(id)
{
    if(regexStudentWorkName()&&regexStudentWorkDescription())
    {$("#edit_student_work_" + id).submit();}
}

//验证作品名称
function regexStudentWorkName()
{
    var name = $.trim($("#student_work_name").val());

    if(name=="")
    {
        $("#student_work_name_span").text("作品名称不能为空");
        return false;
    }
    else
    {
        $("#student_work_name_span").text("");
        return true;
    }
}

function regexStudentWorkDescription()
{
    var name = $.trim($("#student_work_description").val());

    if(name=="")
    {
        $("#student_work_description_textarea").text("作品描述不能为空");
        return false;
    }
    else
    {
        $("#student_work_description_textarea").text("");
        return true;
    }
}

//学生作品
function show_project()
{
    $("#about_project").slideToggle();
}

//textarea自适应高度 纯js写的 有浏览器判断
/**
 * 文本框根据输入内容自适应高度
 * @param                {HTMLElement}        输入框元素
 * @param                {Number}                设置光标与输入框保持的距离(默认0)
 * @param                {Number}                设置最大高度(可选)
 */
var autoTextarea = function (elem, extra, maxHeight) {
    extra = extra || 0;
    var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
        isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
        addEvent = function (type, callback) {
            elem.addEventListener ?
                elem.addEventListener(type, callback, false) :
                elem.attachEvent('on' + type, callback);
        },
        getStyle = elem.currentStyle ? function (name) {
            var val = elem.currentStyle[name];

            if (name === 'height' && val.search(/px/i) !== 1) {
                var rect = elem.getBoundingClientRect();
                return rect.bottom - rect.top -
                    parseFloat(getStyle('paddingTop')) -
                    parseFloat(getStyle('paddingBottom')) + 'px';
            };

            return val;
        } : function (name) {
            return getComputedStyle(elem, null)[name];
        },
        minHeight = parseFloat(getStyle('height'));


    elem.style.resize = 'none';

    var change = function () {
        var scrollTop, height,
            padding = 0,
            style = elem.style;

        if (elem._length === elem.value.length) return;
        elem._length = elem.value.length;

        if (!isFirefox && !isOpera) {
            padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
        };
        scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

        elem.style.height = minHeight + 'px';
        if (elem.scrollHeight > minHeight) {
            if (maxHeight && elem.scrollHeight > maxHeight) {
                height = maxHeight - padding;
                style.overflowY = 'auto';
            } else {
                height = elem.scrollHeight - padding;
                style.overflowY = 'hidden';
            };
            style.height = height + extra + 'px';
            scrollTop += parseInt(style.height) - elem.currHeight;
            document.body.scrollTop = scrollTop;
            document.documentElement.scrollTop = scrollTop;
            elem.currHeight = parseInt(style.height);
        };
    };

    addEvent('propertychange', change);
    addEvent('input', change);
    addEvent('focus', change);
    change();
};

function limitStrsize(id,length){
    $('#'+id).keypress(function(e)
    {
        var n = 0;
        var str = this.value;
        for (i = 0; i < str.length; i++) {
            var leg = str.charCodeAt(i);//ASCII码
            if (leg > 255) {//大于255的都是中文
                n += 2;//如果是中文就是2个字节
            } else {
                n += 1;//英文，不多说了
            }
        }

        if(n >= length && e.keyCode !== 8)
            if(document.all)
            {
                e.returnValue = false;
            }
            else
            {
                e.preventDefault();
            }
    })
}

/////////////////////////////////////////////////////////////////////////////////////创建项目
//验证项目名称是不是为空
function regex_project_name(){
    var name = $.trim($("#project_name").val());
    if(name=="")
    {
        $("#project_name_error_msg").text("项目名称不能为空");
        return false;
    }
    else
    {
        $("#project_name_error_msg").text("");
        return true;
    }
}

//验证项目名称是否重复---项目名称可以重复。。。。
function regex_project_name_same(){
    var name = $.trim($("#project_name").val());
    return true;
}

//验证项目描述
function regex_project_desc(){
    var desc = $.trim($("#project_description").val());
    if(desc == "")
    {
        $("#project_desc_error_msg").text("项目名称不能为空");
        return false;
    }
    else
    {
        $("#project_desc_error_msg").text("");
        return true;
    }
}
//提交
function submit_project(){
    if(regex_project_name()&&regex_project_desc()){
        $("#new_project").submit();
    }
}
/////////////////////////////////////////////////////////////////////////////////////创建项目 end
//匿评弹框取消按钮
function clickCanel(){hideModal("#popbox02");}
//匿评弹框确定按钮
function clickOK(path)
{
    clickCanel();
    $.ajax({
        type: "GET",
        url: path,
        data: 'text',
        success: function (data) {
        }
    });
}
//关闭引入资源弹框
function hideResource(){
    $('#ajax-modal').parent().removeClass("popbox").removeClass("referenceResourcesPopup");
    hideModal();
}
/////////////////////////////////////////////////////////////////////////////////////////

var autoTextarea2 = function (elem,elem2, extra, maxHeight) {
    extra = extra || 0;
    var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
        isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
        addEvent = function (element, type, callback) {
            element.addEventListener ?
                element.addEventListener(type, callback, false) :
                element.attachEvent('on' + type, callback);
        },
        getFirstStyle = elem.currentStyle ? function (name) {
            var val = elem.currentStyle[name];

            if (name === 'height' && val.search(/px/i) !== 1) {
                var rect = elem.getBoundingClientRect();
                return rect.bottom - rect.top -
                    parseFloat(getFirstStyle('paddingTop')) -
                    parseFloat(getFirstStyle('paddingBottom')) + 'px';
            };

            return val;
        } : function (name) {
            return getComputedStyle(elem, null)[name];
        },
        minHeight = parseFloat(getFirstStyle('height'))

    elem.style.resize = 'none';
    elem2.style.resize = 'none';
    var change = function () {
        var scrollTop, height,
            padding = 0,
            style = elem.style,
            style2 = elem2.style;


        if (elem._length === elem.value.length) return;
        elem._length = elem.value.length;
        elem2._length = elem._length;
        if (!isFirefox && !isOpera) {
            padding = parseInt(getFirstStyle('paddingTop')) + parseInt(getFirstStyle('paddingBottom'));
        };
        scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

        elem.style.height = minHeight + 'px';
        elem2.style.height = minHeight + 'px';
        if (elem.scrollHeight > minHeight) {
            if (maxHeight && elem.scrollHeight > maxHeight) {
                height = maxHeight - padding;
                style.overflowY = 'auto';
                style2.overflowY = 'auto';
            } else {
                height = elem.scrollHeight - padding;
                style.overflowY = 'hidden';
                style2.overflowY = 'hidden';
            };
            style.height = height + extra + 'px';
            style2.height = height + extra + 'px';
            scrollTop += parseInt(style.height) - elem.currHeight;
            document.body.scrollTop = scrollTop;
            document.documentElement.scrollTop = scrollTop;
            elem.currHeight = parseInt(style.height);
        };
        if (elem2.scrollHeight > minHeight) {
            if (maxHeight && elem2.scrollHeight > maxHeight) {
                height = maxHeight - padding;
                style.overflowY = 'auto';
                style2.overflowY = 'auto';
            } else {
                height = elem2.scrollHeight - padding;
                style.overflowY = 'hidden';
                style2.overflowY = 'hidden';
            };
            style.height = height + extra + 'px';
            style2.height = height + extra + 'px';
            scrollTop += parseInt(style2.height) - elem2.currHeight;
            document.body.scrollTop = scrollTop;
            document.documentElement.scrollTop = scrollTop;
            elem2.currHeight = parseInt(style2.height);
        };
    };

    addEvent(elem, 'propertychange', change);
    addEvent(elem, 'input', change);
    addEvent(elem, 'focus', change);
    addEvent(elem2, 'propertychange', change);
    addEvent(elem2, 'input', change);
    addEvent(elem2, 'focus', change);
    change();
};