$(document).ready(function() {
	//初始化设置：显示题型、试题的隐藏及首个显示；时间设置
	$(".bank_type_num").css("display","none");
	$(".bank").css("display","none");
	
	$(".bank_type_num_1").css("display","");
	$(".bank_1").css("display","");
	//$(".bank_num_1").css("background","green");
	$("#single").css("color","red");
	
	//var restTime = parseInt($("#total_time").attr('value'))*60;
	//setTime(restTime,0);
	
	//考生须知
	$(".paper_doc").click(function(){
		$("#productsMenu_zzjs").css("display","");
	});
	
	$(".hide_paper_doc").click(function(){
		$("#productsMenu_zzjs").css("display","none");
	});
	
	//标记 2012-9-30
	$(".mark_checkbox").click(function(){
		var bank_num_now = parseInt($("#info_bank_now").attr("bank_num"));
		
		if ($(this).attr("checked")) {
//			$(".domark").click();
			$("#t_"+bank_num_now).css('display',"none");
			$("#ct_"+bank_num_now).css('display',"");
			$("#t_"+bank_num_now).attr('dotag_c','0');
			$("#ct_"+bank_num_now).attr('dotag_c','1');
		} else {
//			$(".dounmark").click();

			var dotag = $(".bank_"+bank_num_now).attr('dotag');
			//var doTag = parseInt($(".bank_"+bank_num).attr("dotag"));
			if(dotag == 1)
			{
			$("#t_"+bank_num_now).css('display',"none");
			$("#ct_"+bank_num_now).css('display',"none");
			}
			else{
			$("#t_"+bank_num_now).css('display',"");
			$("#ct_"+bank_num_now).css('display',"none");
			}
			$("#t_"+bank_num_now).attr('dotag_c','1');
			$("#ct_"+bank_num_now).attr('dotag_c','0');

		}
	});
	
	
	//试题跳转：跳转、题号点击、上一题、下一题
	$("#jump").click(function() {
		var bank_num_new = parseInt($("#jump_num").attr("value"));

		var bank_total = parseInt($("#info_bank_now").attr("bank_total"));
		
		if (isNaN(bank_num_new)) {
			alert("请输入要跳转的题号");
		} else if (bank_num_new > bank_total || bank_num_new <= 0) {
			alert("请输入存在的题号");
		} else {
			jump(bank_num_new);
			if (showtype == 'list') location.hash="bank_"+bank_num_new;
		}
	});
	
	$(".bank_num").click(function() {
		var bank_num_new = parseInt($(this).attr("bank_num"));
		jump(bank_num_new);
	});
	
	$("#prev").click(function() {
		
		var bank_num_new = parseInt($("#info_bank_now").attr("bank_num"));
		if (bank_num_new == 1) {
			alert("已是最前一题");
		} else {
			bank_num_new -= 1;
				
			jump(bank_num_new);
		}
		

		
	});
	
	$("#next").click(function() {

		var bank_num_new = parseInt($("#info_bank_now").attr("bank_num"));

		var bank_total = parseInt($("#info_bank_now").attr("bank_total"));
		if (bank_num_new == bank_total) {
			alert("已是最后一题，如果要交卷，请点击“交卷”按钮");
		} else {
			bank_num_new += 1;

			jump(bank_num_new);
			

		}
		
		

		
		
	});
	
	//显示选择的答案并保存到session
	//单选、多选、判断
	
	$(".option").click(function() {
		var bank_id = $(this).attr("bank_id");
		var answer = '';
		$(".option_"+bank_id).each(function() {
			if ($(this).attr('checked') == true) {
				answer += $(this).attr("value");
			}
		});
		
		var undone_num = parseInt($("#undone_num").html());
		
		
		setTimeout(function() {
			var bank_num_now = $("#info_bank_now").attr("bank_num");

			if (answer) {
				changeDoTag(bank_num_now,1);
				changeDoTag_c(bank_num_now,1);
				$("#show_"+bank_id).html(answer);
			} else {
				changeDoTag(bank_num_now,0);
				changeDoTag_c(bank_num_now,0);
				$("#show_"+bank_id).html('　');
			}
			
			answer = (answer == "正确")?1:((answer == "错误")?2:answer);
			saveAnswer(bank_id,answer);
		},'10');
			
	});
	
	//对于含有附件且做题时复制该的试题（word、excel），点击附件就认为其作答了该题
	$(".copy_annex_open").click(function() {
		var bank_id = $(this).attr("bank_id");
		var answer = $(this).attr("answer");
		
		saveAnswer(bank_id,answer);
		
		setTimeout(function() {
			var bank_num_now = $("#info_bank_now").attr("bank_num");
			changeDoTag(bank_num_now,1);
		},'10');
		
	});
	//对于flash试题，在关闭flash时会调用examination()方法
	
	//windows
	
	
	//主观题
	$(".my_answer").mousemove(function() {
		content = $(this).html();
		if(content.length>0)
		{
			var bank_num_now = $("#info_bank_now").attr("bank_num");
			changeDoTag(bank_num_now,1);

		
			$(this).attr('this_do_tag',1);
				var bank_id = $(this).attr('bank_id');
				saveAnswer(bank_id,true);	//仅标记
		}
		else
		{
			var bank_num_now = $("#info_bank_now").attr("bank_num");
			changeDoTag(bank_num_now,0);
		}

	});
	/*
	$(".my_answer").click(function() {
		
		setTimeout(function() {
			var bank_num_now = $("#info_bank_now").attr("bank_num");
			changeDoTag(bank_num_now,1);
		},'10');
		
		$(this).attr('this_do_tag',1);
		var bank_id = $(this).attr('bank_id');
		saveAnswer(bank_id,true);	//仅标记
	});
	*/
	
	
	//试卷提交
	$("#overIt").click(function() {		
		
		var undone_num = parseInt($("#undone_num").html());
		var msg = "确认要交卷吗";
		
		if (confirm(msg)) {
			//保存主观题
			var answer = '';
			var bank_id = '';
			$(".my_answer").each(function(){
				if ($(this).attr('this_do_tag') == 1)
				{
					answer = $(this).html();
					bank_id = $(this).attr('bank_id');
					saveAnswer(bank_id,answer);
				}
			});
			
			if (confirm("请您再次确认是否交卷！")) {
				$(".tc2").css("display","");
				
//				if (confirm("请您最后确认是否交卷！")) {
//					
//					finishExam();
//				}
			}
		}
	});
	
	$(".goon_exam").click(function(){
		$(".tc2").css("display","none");
		finishExam(1);
	})
	
	$(".finish_exam").click(function(){
		$(".tc2").css("display","none");
		finishExam(2);
	})
	
	
	$("#single").click(function(){
		showtype = 'single';
		
		$("#single").css("color","red");
		$("#list").css("color","black");
		
		$(".bank_type_num").css("display","none");
		$(".bank").css("display","none");
		
		var bank_type_num_now = $("#info_bank_now").attr("bank_type_num");
		var bank_num_now = $("#info_bank_now").attr("bank_num");
		$(".bank_type_num_"+bank_type_num_now).css("display","");
		$(".bank_"+bank_num_now).css("display","");
		
		$(".bank").css("height","430px");
		$(".dtconn").css("height","468px");
		$(".dtconn").css("overflow-y","hidden");
	});
	$("#list").click(function(){
		showtype = 'list';
		
		$("#single").css("color","black");
		$("#list").css("color","red");
		
		$(".bank_type_num").css("display","");
		$(".bank").css("display","");
		$(".bank").css("height","auto");
		$(".dtconn").css("height","468px");
		$(".dtconn").css("overflow-y","scroll");
	});
	
	$(".bank").click(function(){
		var bank_num_new = parseInt($(this).attr("bank_num"));
		if (showtype == 'list')
			jump(bank_num_new);
	});
});


var showtype='single';
//var showtype='list';
function jump(bank_num_new) {
	var bank_type_num_now = $("#info_bank_now").attr("bank_type_num");
	var bank_num_now = $("#info_bank_now").attr("bank_num");
	var bank_type_num_new = $(".bank_num_"+bank_num_new).attr("bank_type_num");
	
	$(".bank_num_"+bank_num_now).css("background","");
	//$(".bank_num_"+bank_num_new).css("background","red");
	
	if (bank_type_num_now != bank_type_num_new) {
		if (showtype == 'single')
			$(".bank_type_num_"+bank_type_num_now).css("display","none");
		
		$(".bank_type_num_"+bank_type_num_new).css("display","");
	}
	
	if (showtype == 'single')
	{
		$(".bank_"+bank_num_now).css("display","none");
		$("#list").attr("href","#bank_"+bank_num_new);
	}
		
		
	$(".bank_"+bank_num_new).css("display","");
	
	$("#info_bank_now").attr("bank_type_num",bank_type_num_new);
	$("#info_bank_now").attr("bank_num",bank_num_new);
	
	$("#jump_num").val(bank_num_new);
	
	//保存答案：windows题
	var bank_type_en = $(".bank_num_"+bank_num_new).attr('bank_type_en');
	if (bank_type_en == 'windows')
	{
		changeDoTag(bank_num_new,1);
		var bank_id = $(".bank_num_"+bank_num_new).attr('bank_id');
		var answer = $(".windows_"+bank_id).attr("answer");
		saveAnswer(bank_id,answer);
	}
	
		
			//add by 
		var dotag = $("#ct_"+bank_num_new).attr('dotag_c');
		$(".mark_checkbox").each(function(){
			var dotag = $("#ct_"+bank_num_new).attr('dotag_c');
			if(dotag == 1)
			{
				$(this).attr('checked','true');
			}else if(dotag ==0)
			{
				if($(this).attr('checked') == true)
				{
					$(this).removeAttr('checked');
				}
			}
//			if($(this).attr('checked') == true)
//			{
//				alert(3);
//				var dotag = $("#ct_"+bank_num_new).attr('dotag');
//				
//				if(dotag == 0)
//				{
//					alert(4);
//					$(this).removeAttr('checked');
//				}else{
//					alert(1);
//					$(this).attr('checked','true');
//				}
//			}
					
			
		});
	
	
}


/**
 * 修改试题被做标记
 * @param bank_num	需修改的试题编号
 * @param tag		0：修改为未做；1：修改为已做。
 */
function changeDoTag(bank_num,tag) {
	var bank_type_en = $(".bank_num_"+bank_num).attr("bank_type_en");

	if (bank_type_en == 'BigSmall')
	{
		
	}
	
	var doTag = parseInt($(".bank_"+bank_num).attr("dotag"));
	if (doTag != tag)
	{
		var undone_num = parseInt($("#undone_num").html());
		if (doTag == 0)
		{
			$(".bank_num_color_"+bank_num).css('color','blue');
			$("#undone_num").html(undone_num-1);
		}
		else
		{
			$(".bank_num_color_"+bank_num).css('color','red');
			$("#undone_num").html(undone_num+1);
		}
		$(".bank_"+bank_num).attr('dotag',tag);
	}
}
function changeDoTag_c(bank_num,tag) {
	var bank_type_en = $(".bank_num_"+bank_num).attr("bank_type_en");

	if (bank_type_en == 'BigSmall')
	{
		
	}
	
	var doTag = parseInt($(".bank_"+bank_num).attr("dotag"));
	
	if (doTag != tag)
	{
		//var undone_num = parseInt($("#undone_num").html());

		if (doTag == 0)
		{
			$("#t_"+bank_num).css('display','');
			$("#ct_"+bank_num).css('display','');

		}
		$(".bank_"+bank_num).attr('dotag',tag);
	}else{
		if (doTag == 1)
		{
			$("#t_"+bank_num).css('display','none');
			$("#ct_"+bank_num).css('display','none');

		}		
		$(".bank_"+bank_num).attr('dotag',tag);
		
	}
}


function count(i,line,amount){
	if (i == 6) {
		i = 0;
		amount = "。";
	} else {
		amount = amount + line;
	}
	i++;
	$("#chart").html(amount);
	setTimeout(function() {count(i,line,amount);},500);
}

function showFlash(annex_name) {
	var obj = '<object id="obj" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="800" height="600" align="middle">\
	<param name="allowScriptAccess" value="sameDomain" />\
	<param name="movie" value="/examnation/flash/' + annex_name + '" />\
	<param name="quality" value="high" />\
	<param name="wmode" value="opaque" />\
	<embed src="/examnation/flash/' + annex_name + '" width="300" height="480" align="middle" name="flash_wmp" bgcolor="#ece9d8" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
	</object>';
	
	$(".flash_bank").html(obj);
	$(".flash_bank").css("display","");
	
}
function examination(answer){
	var rs = answer.split("|");
	
	var bank_num_now = $("#info_bank_now").attr("bank_num");
	//修改试题被做标记
	changeDoTag(bank_num_now,1);
	//保存答案
	var bank_id = $(".flash_"+rs[0]).attr("bank_id");
	saveAnswer(bank_id,rs[1]);
	//隐藏flash
	$(".flash_bank").css("display","none");
	//跳到下一题
	$("#next_bank[id=next_bank]").click();
	
}