/**
 * 新课标--评测客户端
 * ============================================================================
 * 版权所有 (C) 2007-2009 上海学舟信息科技有限公司，并保留所有权利。
 * 网站地址: http://www.xinkb.org
 * ============================================================================
 * $Author: yongzh $
 * $Date: 2009-04-27 11:11:40 +0800 (星期一, 27 四月 2009) $
 * $Id: test.js 1698 2009-04-27 03:11:40Z yongzh $
*/

var test = {
    version: '1.0',
    pageNum: 6,
    offset: 0,
    choiceNum: 0,
    answer: {},
	answerNum:0,
	useranswer:{},
	expendtime: 0,
	status:0,
    start: function(id){
		title=$("#div_test_questions_title").html();
		$("#div_test_questions_title").html("<img src='Public/shgao/images/loading.gif'>测试加载中···");
        $.get('Test,ajaxStartTest', {
            "id": id
        }, function(data){
            if (data.status) {
                $("#div_test_questions_title").html(data.info.test.title);
                $.each(data.info.test, function(k, v){
                    test[k] = v;
                });
                if (data.info.useranswer.status != 2) {
                    $("#div_test_description").hide();
                    $("#div_test_questions").show();
                    if (data.info.useranswer.status && data.info.useranswer.answer) {
                        test.comeback(data.info.useranswer);
                    }
                    else {
                        test.status = 1;
                        test.countdown(test.testtime * 60 + 1);
                        test.useranswer = data.info.useranswer;
                        test.useranswer.status = test.status;
                    }
                    test.setQuestions();
                }
                else {
                    test.useranswer = data.info.useranswer;
                    test.status = data.info.useranswer.status;
                    if (data.info.useranswer.answer && typeof data.info.useranswer.answer == "object") {
                        test.answer = data.info.useranswer.answer;
                    }
                    test.answerNum = data.info.useranswer.version;
                    $("#div_test_description").hide();
                    $("#div_test_questions").hide();
                    $("#div_test_result").show();
			        $("#div_test_result_title").html("<img src='Public/shgao/images/loading.gif'>您已经完成测试，正在处理测试结果，请稍后···");
                    test.showResult(data.info.useranswer.result);
                }
            }
            else {
                xkb.ajaxlogin();
                afterLogin = function(){
                    test.start(id);
                }
            }
        }, "json");
    },
	comeback:function(useranswer){
		this.useranswer=useranswer;
		this.status=useranswer.status;
		if (useranswer.answer && typeof useranswer.answer == "object") {
			this.answer = useranswer.answer;
		}
		this.answerNum=useranswer.version;
		this.offset=parseInt(this.answerNum/this.pageNum)*this.pageNum;
		this.countdown(useranswer.expendtime);
		if (this.questions.length == this.answerNum) {
            this.end();
        }
	},
	countdown:function(time){       
        if (this.status == 1) {
            this.expendtime = time - 1;
            if (this.expendtime < 0) {
                if (this.questions.length == this.answerNum) {
                    this.end();
                }
                else {
					alert("您未能在规定时间内完成测评，点击确认重新开始测评！")
					this.restart(this.id);
                    return;
                }
            }
            $("#time_display").html(this.timeformat(this.expendtime));
            if ((this.expendtime % 30) == 0) {
                this.useranswer.answer = this.answer;
                now = new Date();
                this.useranswer.expendtime = this.expendtime;
                this.useranswer.updatetime = now.getFullYear() + "-" + (now.getMonth() + 1) + '-' + now.getDate() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
                this.setCookie("LFZ_test_useranswer_" + this.useranswer.uid, $.encode(this.useranswer));
            }
            setTimeout(function(){
                test.countdown(test.expendtime);
            }, 1000);
        }
	},
	restart:function(id){
		this.clearCookie("LFZ_test_useranswer_" + this.useranswer.uid);
		if (this.id&&this.useranswer && this.questions) {
            $("#div_test_description").hide();
            $("#div_test_result").hide();
            $("#div_test_questions").show();
            this.useranswer.status = 1;
			this.useranswer.result = "";
			this.useranswer.expendtime = 0;
			this.useranswer.answer = {};
			this.useranswer.version=0;
			this.answer={};
			this.answerNum=0;
			this.choiceNum=0;
            this.status = 1;
            this.offset = 0;
            this.countdown(this.testtime * 60 + 1);
            this.setQuestions();
		}else{
		    title=$("#div_test_questions_title").html();
			$("#div_test_questions_title").html("<img src='Public/shgao/images/loading.gif'>数据加载中···");
	        $.get('Test,ajaxStartTest', {
	            "id": id
	        }, function(data){
	            if (data.status) {
	                    $("#div_test_description").hide();
						$("#div_test_result").hide();
	                    $("#div_test_questions").show();
	                    $("#div_test_questions_title").html(data.info.test.title);
	                    $.each(data.info.test, function(k, v){
	                        test[k] = v;
	                    });
	                        test.status = 1;
	                        test.countdown(test.testtime * 60 + 1);
	                        test.useranswer = data.info.useranswer;
							test.useranswer.answer={};
							test.useranswer.version=0;
	                        test.useranswer.status = test.status;
	                        test.setQuestions();
	            }
	            else {
	                $("#div_test_questions_title").html(title);
	                $('#T_button_landing').click();
	                afterLogin = function(){
	                    test.start(id);
	                }
	            }
	        }, "json");
		}
		$("#show_result").hide();
		$("#next_pager").html("<a>下一页</a>");
	},
	timeformat:function(time){
		var timeStr = "";
		h = parseInt(time/(60*60));
		m = parseInt((time%(60*60))/60);
		s = parseInt(time%60);
		timeStr +=  h            ?    String(h + 100).toString().substring(1, 4)       + ":" : "00:";
		timeStr += (m || h )     ? (m?String(m + 100).toString().substring(1, 4):"00") + ":" : "00:";
		timeStr += (s || m || h) ? (s?String(s + 100).toString().substring(1, 4):"00")       : "00";
		timeStr +="&nbsp;("+ this.answerNum +"/"+ this.questions.length +")"
		return timeStr;
	},
    getQuestionsStr: function(){
        var questionsStr = "";
        $.each(this.getQuestions(this.offset, this.pageNum), function(k, v){
			var style="";
            questionsStr += '<p><span class="orange_test">' + v.subject + '</span><br />' +
			//a
                            '<span style="margin-left:10px" ><input onclick="test.choice(this)" name="' +
			                v.order +'" type="radio" value="A" ';
			if (test.answer[v.order-1] == "A") {
				questionsStr += 'checked="checked"';
				test.choiceNum++;
				style="background:#f90; padding:2px; color:#fff;";
			}
			questionsStr +='/><span id="choice_'+v.order+'_A" style="padding:2px;'+
			               style + '">' +
			               v.choice_a +
						   '</span></span>' +
			//b
                           '<span style="margin-left:20px" ><input onclick="test.choice(this)" name="' +
                           v.order + '" type="radio" value="B" ';
			style="";
			if (test.answer[v.order-1] == "B") {
				questionsStr += 'checked="checked"';
				test.choiceNum++;
				style="background:#f90; padding:2px; color:#fff;";
			}
			questionsStr +='/><span id="choice_'+v.order+'_B" style="padding:2px;'+
			               style + '">' +
                           v.choice_b + 
						   '</span></span>' +
			//c
                           '<span style="margin-left:20px" ><input onclick="test.choice(this)" name="' +
                           v.order +
                           '" type="radio" value="C" ';
			style="";
			if (test.answer[v.order-1] == "C") {
				questionsStr += 'checked="checked"';
				test.choiceNum++;
				style="background:#f90;color:#fff;";
			}
			questionsStr +='/><span id="choice_'+v.order+'_C" style="padding:2px;'+
			               style + '">' +
                           v.choice_c +
                           '</span></span>' +
                           '</p>';
        });
        return questionsStr;
    },
	colorUp:function(_sid,_value){
		var choice_a=$("#choice_"+_sid + "_A");
		var choice_b=$("#choice_"+_sid + "_B");
		var choice_c=$("#choice_"+_sid + "_C");
		switch (_value){
			case "A":
			choice_a.css({color:"#fff",background:"#f90"});
			choice_b.css({color:"",background:""});
			choice_c.css({color:"",background:""});
			break;
			case "B":
			choice_a.css({color:"",background:""});
			choice_b.css({color:"#fff",background:"#f90"});
			choice_c.css({color:"",background:""});
			break;
			case "C":
			choice_a.css({color:"",background:""});
			choice_b.css({color:"",background:""});
			choice_c.css({color:"#fff",background:"#f90"});
			break;
		}
		
	},
    getQuestions: function(offset, pageNum){
        var questions = new Array;
        var num = offset + pageNum;
        if (this.questions.length < num) {
            num = this.questions.length;
        }
        for (i = offset; i < num; i++) {
            questions.push(this.questions[i]);
        }
        return questions;
    },
    setQuestions: function(){
        $("#test_questions").html(this.getQuestionsStr());
    },
    autoSave: function(){
      
    },
    choice: function(dom){
        if (!this.answer[dom.name-1]) {
            this.choiceNum++;
			this.answerNum++;
			this.useranswer.version++;
        }
        this.answer[dom.name-1] = dom.value;
		this.useranswer.answer=this.answer;
		now=new Date();
		this.useranswer.expendtime=this.expendtime;
		this.useranswer.updatetime=now.getFullYear()+"-"+String(now.getMonth()+ 1 + 100).toString().substring(1, 4)+'-'+String(now.getDate() + 100).toString().substring(1, 4)+" "+String(now.getHours() + 100).toString().substring(1, 4)+":"+String(now.getMinutes() + 100).toString().substring(1, 4)+":"+String(now.getSeconds() + 100).toString().substring(1, 4);
		$("#time_display").html(this.timeformat(this.expendtime));
		this.setCookie("LFZ_test_useranswer_"+this.useranswer.uid,$.encode(this.useranswer));
        		this.colorUp(dom.name,dom.value);
        if (this.questions.length == this.answerNum) {
            this.end();
			return;
        }
        if (this.choiceNum >= this.pageNum) {
			if (this.offset < (parseInt(this.questions.length / this.pageNum) - 1)*this.pageNum) {
				this.nextPage();
			}else if(this.questions.length == this.answerNum){
				this.end();
			}
        }
    },
    nextPage: function(){
        if (this.questions.length == this.answerNum) {
            this.end();
        }
        else {
            this.choiceNum = 0;
            this.offset += this.pageNum;
			if (this.offset > (parseInt(this.questions.length / this.pageNum) - 1)*this.pageNum) {
				alert("测评题目尚未答完！");
				 this.offset -= this.pageNum;
				return;
			}
            this.setQuestions();
			if (this.offset == (parseInt(this.questions.length / this.pageNum) - 1)*this.pageNum) {
				$("#next_pager").html("<a>提交完成</a>");
			}
        }
    },
    previousPage: function(){
            this.choiceNum = 0;
            this.offset -= this.pageNum;
			if(this.offset < 0)this.offset=0;
            this.setQuestions();
			if (this.offset < (parseInt(this.questions.length / this.pageNum) - 1)*this.pageNum) {
				$("#next_pager").html("<a>下一页</a>");
			}
    },
    end: function(){
        $("#div_test_result_title").html("<img src='Public/shgao/images/loading.gif'>您已经完成测试，正在处理测试结果，请稍后···");
		if (this.status == 1) {
			$("#div_test_description").hide();
            $("#div_test_questions").hide();
            $("#div_test_result").show();
			this.useranswer.status = this.status = 2;
			$("#time_display").html(this.timeformat(this.expendtime));
			this.setCookie("LFZ_test_useranswer_"+this.useranswer.uid,$.encode(this.useranswer));
			$.post('Test,ajaxEndTest', {
				"id": this.id,
				"useranswer": $.encode(this.useranswer)
			}, function(data){
				if (data.status) {
                    test.showResult(data.info);
					window.scrollTo(0,305);
				}
			}, "json");
		}
    },
	showResult:function(result){
		$("#radarmap").attr("src",result.radarmap);
		$("#character_category").html(result.character.category+"型");
		$("#character_content").html(result.character.content);
		$("#metier_master").html(result.metier.master);
        var metier_other = "";
        if (result.metier.other) {
            $.each(result.metier.other,function(k, v){
               if(v) metier_other += v;
            });
        }
		$("#metier_other").html(metier_other);
		$("#div_test_result_title").html("您已经完成测试，以下是测试结果");
		$("#show_result").show();
	},
    setCookie: function(Name, cookievalue, time){
		if(!time) time= 1 * 24 * 60 * 60 * 1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + time);
        var newcookie = Name + "=" + escape(cookievalue)+";expires="+expDate.toGMTString();
        document.cookie = newcookie;
    },
    getCookie: function(name){
        var result = null;
        var myCookie = document.cookie + ";";
        var searchName = name + "=";
        var startOfCookie = myCookie.indexOf(searchName);
        var endOfCookie;
        if (startOfCookie != -1) {
            startOfCookie += searchName.length;
            endOfCookie = myCookie.indexOf(";", startOfCookie);
            result = unescape(myCookie.substring(startOfCookie, endOfCookie));
        }
        return result;
    },
    clearCookie: function(name){
        var ThreeDays = 3 * 24 * 60 * 60 * 1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime() - ThreeDays);
        document.cookie = name + "=;expires=" + expDate.toGMTString();
    }

};