<%= stylesheet_link_tag  'polls', :media => 'all' %>
<script type="text/javascript">
    $(function(){
        $("#RSide").removeAttr("id");
        $("#homework_page_right").css("min-height",$("#LSide").height()-30);
        $("#Container").css("width","1000px");
    });
    //编辑问卷描述之后
    var popWindow ; //弹出框的引用
    var importPollPopWindow; //选择导入的弹出框引用
    function edit_head(){
        $("#exercise_description").val($("#exercise_description_div").html());
    }
    $(function(){
        //点击空白处
        $(document).bind('click',function(e){
            //弹出框非空 不是a标签 点击的不是弹出框 ，那么弹出框就会隐藏
            if(popWindow && e.target.nodeName != 'A' && !popWindow.is(e.target) && popWindow.has(e.target).length === 0){ // Mark 1
                popWindow.css('display', 'none');
            }
            if(importPollPopWindow && e.target.nodeName != 'A' && !importPollPopWindow.is(e.target) && importPollPopWindow.has(e.target).length === 0){
                importPollPopWindow.css('display', 'none');
            }
        });
    })

    function dismiss(quest_type,quest_id){
        popWindow = $("#div_"+quest_type+"_"+quest_id);
        if(popWindow){
            popWindow.css('display', 'none');
        }
    }

    function chooseQuestionType(quest_type,quest_id){
        //quest_type 分为 mc mcq single multi
        //quest_id 是quetion的id  下同
        if(popWindow){
            popWindow.css('display', 'none');
        }
        popWindow = $("#div_"+quest_type+"_"+quest_id);
        $("#div_"+quest_type+"_"+quest_id).click(function(e){
            e.stopPropagation(); //组织冒泡到document.body中去
        });
        $("#div_"+quest_type+"_"+quest_id).css("position", "absolute");

        $("#div_"+quest_type+"_"+quest_id).css("top", $("#add_"+quest_type+"_"+quest_id).offset().top+30);

        $("#div_"+quest_type+"_"+quest_id).css("left", $("#add_"+quest_type+"_"+quest_id).offset().left-10);
        if( $("#div_"+quest_type+"_"+quest_id).css('display') == 'block') {
            $("#div_"+quest_type+"_"+quest_id).css('display', 'none');
        }
        else{
            $("#div_"+quest_type+"_"+quest_id).css('display', 'block');
        }
    }

    //选择导入调查问卷
    function importPoll(){
        importPollPopWindow = $("#import_poll");
        $("#import_poll").css("position", "absolute");

        $("#import_poll").css("top", $("#import_btn").offset().top+30);

        $("#import_poll").css("left", $("#import_btn").offset().left-65);
        $("#import_poll").css("display","block")
    }

    function remote_import(){
        importPollPopWindow.css('display', 'none');
        if($("#import_poll").val() === 0){
            return;
        }else{
            if(confirm("确认导入问卷"+$("#import_poll").find("option:selected").text()+"?")){
                $("#import_form").submit();
            }else{
                return;
            }
        }

    }

    //添加标题时确定按钮
    function add_poll_question(doc,quest_type)
    {
        var title = $.trim($("#poll_questions_title").val());
        var score = $.trim($("#question_score").val());
        var standard_ans = $.trim($("#question_standard_ans").val());
        if(title.length == 0 || score.length == 0){
            alert("题目标题/分数不能为空");
            doc.one('click', function(){
                add_poll_question($(this),quest_type);
            });
        }else if(!/^[1-9][0-9]*$/.test(score)) {
            alert("分数必须是非零开头的数字");
            doc.one('click', function(){
                add_poll_question($(this),quest_type);
            });
        }else if(quest_type !=3 && standard_ans.length == 0) {
            alert("标准答案不能为空");
            doc.one('click', function(){
                add_poll_question($(this),quest_type);
            });
        }else{
            doc.parent().parent().parent().submit();}
    }
    //修改标题时确定按钮
    function edit_poll_question(doc,id,quest_type)
    {
        var title = $.trim($("#poll_questions_title_" + id).val());
        var score = $.trim($("#poll_question_score_"+ id).val());
        var standard_ans = $.trim($("#poll_question_standard_answer_" + id).val());
        if(title.length == 0 || score.length == 0){
            alert("题目标题/分数不能为空");
        }else if(!/^[1-9][0-9]*$/.test(score)) {
            alert("分数必须是非零开头的数字");
        }else if(quest_type !=3 && standard_ans.length == 0) {
            alert("标准答案不能为空");
        }else if(quest_type ==3) {
            var div = $("#poll_answers_" + id);
            var candiate_answer = $(".candiate_answer",div);
            if(candiate_answer.length > 0) {
                for(i=0;i<candiate_answer.length;i++) {
                    if(i<candiate_answer.length-1 && $.trim($(candiate_answer[i]).val()) == "") {
                        continue;
                    } else if(i == (candiate_answer.length-1) && $.trim($(candiate_answer[i]).val()) == "") {
                        alert("候选答案不能为空");
                    } else if($.trim($(candiate_answer[i]).val()) != ""){
                        doc.parent().parent().parent().submit();
                        break;
                    }

                }
            } else{
                alert("候选答案不能为空");
            }
        }else{
            doc.parent().parent().parent().submit();}
    }

    //问卷头
    function pollsCancel(){$("#polls_head_edit").hide();$("#polls_head_show").show();}
    function pollsSubmit(doc){
        var title = $.trim($("#exercise_name").val());
        if(title.length == 0){
            alert("测验标题不能为空");
        } else if($.trim($("#exercise_end_time").val()) =="") {
            alert("截止时间不能为空");
        } else if((Date.parse($("#exercise_end_time").val())+(24*60*60-1)*1000) < Date.now()) {
            alert("截止时间不能小于当前时间");
        } else if($.trim($("#exercise_time").val()) !="" && !/^[1-9][0-9]*$/.test($.trim($("#exercise_time").val()))) {
            alert("测验时长必须为非零开头的数字");
        } else if($.trim($("#exercise_publish_time").val()) !="" && Date.parse($("#exercise_publish_time").val()) < Date.now()) {
            alert("发布时间不能小于当前时间");
        } else if($.trim($("#exercise_publish_time").val()) !="" && Date.parse($("#exercise_end_time").val()) < Date.parse($("#exercise_publish_time").val())) {
            alert("截止时间不能小于发布时间");
        } else {
            doc.parent().parent().parent().submit();
        }
    }
    function pollsEdit(){$("#polls_head_edit").show();$("#polls_head_show").hide();}
    //
    function pollQuestionCancel(question_id){
        $("#show_poll_questions_"+question_id).show();
        $("#edit_poll_questions_"+question_id).hide();
    }
    function pollQuestionEdit(question_id){
        $("#show_poll_questions_"+question_id).hide();
        $("#edit_poll_questions_"+question_id).show();
        $("#poll_questions_title_"+question_id).focus();
    }
    //单选题
    function add_single_answer(doc)
    {
        var li = doc.parent().after("<li class='ur_item'><label name='select_items' class='w56'>选项&nbsp;<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;</label><input maxlength='200' type='text' name='question_answer["+new Date().getTime()+"]' placeholder='输入选项内容'/>" +
                "<a class='icon_add' title='向下插入选项' onclick='add_single_answer($(this));'></a><a class='icon_remove'  title='删除' onclick='remove_single_answer($(this))'></a>"+
                "</li><div class='cl'></div>");
        var select_items =$("label[name='select_items']",li.parent());
        for(var i=0; i<select_items.length; i++){
            $(select_items[i]).html("选项"+String.fromCharCode(64 + parseInt(i+1))+"<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;");
        }
    }
    function add_candidate_answer(doc)
    {
        doc.parent().after("<li class='ur_item'><label name='candiate_items'>候选答案<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;</label><input maxlength='200' type='text' name='exercise_choice["+new Date().getTime()+"]' placeholder='请输入候选答案（选填）'/>" +
                "<a class='icon_add' title='向下插入选项' onclick='add_candidate_answer($(this));'></a><a class='icon_remove'  title='删除' onclick='remove_single_answer($(this))'></a>"+
                "</li><div class='cl'></div>");
        var select_items =$("label[name='candiate_items']",doc.parent().parent());
        for(var i=0; i<select_items.length; i++){
            $(select_items[i]).html("候选答案"+revert_to_chinese_num(i + 1)+"<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;");
        }
    }
    function revert_to_chinese_num(num){
        var s_num = "";
        switch (num) {
            case 1: s_num = '一'; break;
            case 2: s_num = '二'; break;
            case 3: s_num = '三'; break;
            case 4: s_num = '四'; break;
            case 5: s_num = '五'; break;
            case 6: s_num = '六'; break;
            case 7: s_num = '七'; break;
            case 8: s_num = '八'; break;
            case 9: s_num = '九'; break;
        }
        return s_num;
    }
    function remove_single_answer(doc)
    {
        if(doc.parent().siblings("li").length == 0)
        {
            alert("至少有一个选项或一个候选答案");
        }
        else
        {
            var parent = doc.parent().parent();
            doc.parent().remove();
            var select_items =$("label[name='select_items']",parent);
            var candiate_items =$("label[name='candiate_items']",parent);
            for(var i=0; i<select_items.length; i++){
                $(select_items[i]).html("选项"+String.fromCharCode(64 + parseInt(i+1))+"<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;");
            }
            for(var i=0; i<candiate_items.length; i++){
                $(candiate_items[i]).html("候选答案"+revert_to_chinese_num(i + 1)+"<span class='ur_index'></span>：&nbsp;&nbsp;&nbsp;");
            }
        }
    }
</script>
<div class="homepageRight mt0 ml10">
  <div class="resources">
    <!-- 头部 -->
    <div id="polls_head_show" style="display: none;">
      <%= render :partial => 'show_head', :locals => {:exercise => @exercise} %>
    </div>
    <div id="polls_head_edit">
      <%= render :partial => 'edit_head', :locals => {:exercise => @exercise} %>
    </div>
    <% current_score = get_current_score @exercise %>
    <div class="mb5" style="display: <%= current_score == 0 ? "none" : "" %>" id="current_score_div">目前试卷总分：<span class="c_red" id="current_score"><%= current_score %>
      分</span></div>
    <!-- 问题 -->
    <div id="poll_content">
      <%= render :partial => 'exercise_content', :locals => {:exercise => @exercise} %>
    </div>

    <div class="testQuestion" id="new_exercise_question">
      <%= render :partial => 'new_question', :locals => {:exercise => @exercise} %>
    </div>
    <!--选项 end-->

    <!-- 新增问题 -->
    <div id="new_poll_question">
    </div>

    <div id="exercise_submit">
      <%= render :partial => 'exercise_submit', :locals => {:exercise => @exercise} %>
    </div>
    <div class="cl"></div>
    <!--contentbox end-->
  </div>
</div><!--编辑end-->
