<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<script type="text/javascript" src="${ctx}/static/plugin/swfobject.js"></script>
<script type="text/javascript" src="${ctx}/static/plugin/swfplugin.js"></script>

<div id="picture-view-div" style="display:none">
	<p>当前划块结果</p>
	<c:if test="${flag==false}">
	<a class="btn btn-primary" href="javascript:void(0)" onclick="editPictureConfig()">
	编辑
	</a> 
	</c:if>
	<a class="btn" href="javascript:void(0)" onclick="closePictureConfig()">
	返回
	</a>
	<div id="image-view-div">
	</div>
</div>
<div id="picture-edit-div" style="display:none;padding:15px">
	<p>评卷划块设置</p> 
	<div id="image-edit-div">
	</div>
</div>
<script type="text/javascript">
var currentBlock;
var pictureArray = new Array();
var currentConfig = '';
var imageViewReady=false;
var imageViewLoaded=true;
var configChanged=false;
$(document).ready(function(){
	$('.nav-tabs a').click(function (e) {
		e.preventDefault();
		$(this).tab('show');
	});
	getImage();
	initImageViewPluginDefault("${ctx}/static/plugin/", "image-view-div");
	initImageEditPlugin("${ctx}/static/plugin/", "image-edit-div");
	refreshViewImage();
});

function initPictureConfig(block){
	currentBlock = block;
	var images = getImage();
	if(images==undefined || images.length==0){
		alert('没有题卡图片，请使用客户端扫描识别后再设置！');
		return false;
	}
	if(block.pictureConfig==undefined || block.pictureConfig==''){
		$('#picture-edit-div').show();
	}else{
		buildCurrentConfig(block.pictureConfig);
		$('#picture-view-div').show();
	}
	return true;
}

function editPictureConfig(){
	$('#picture-view-div').hide();
	$('#picture-edit-div').show();
}

function closePictureConfig(){
	$('#picture-view-div').hide();
	$('#picture-edit-div').hide();
	$('#main-div').show();
}

function getImage(){
	if(pictureArray.length==0){
		$.post("${ctx}/admin/exam-param/subjective/urls", {subjectId: "${subjectId}", classTypeValue: "${classTypeValue}"}, function(obj){
			for(var i=0;i<obj.length;i++){
				pictureArray.push("${imageServer}"+obj[i]);
			}
		});
	}
	return pictureArray;
}

function onSaveJson(jsonData){
    if(currentBlock!=undefined){
    	currentBlock.pictureConfig = jsonData;
    	buildCurrentConfig(currentBlock.pictureConfig);
    	$('#picture-view-div').show();
    	$('#picture-edit-div').hide();
    }
}

function loadComplete(){
	imageViewLoaded=true;
}

function creationComplete(){
	imageViewReady=true;
}

function buildCurrentConfig(config){
	var images = getImage();
	if(images!=undefined && images.length>0 && config!=undefined && config!=''){
		for(var i=0;i<images.length;i++){
			try{
				eval('config=config.replace(/\\{'+i+'\\}/, "'+images[i]+'")');
			}catch(e){
			}
		}
		currentConfig=config;
		configChanged=true;
	}
}

function refreshViewImage(){
	if(imageViewReady && imageViewLoaded){
		if(currentConfig!=undefined && currentConfig!='' && configChanged){
			configChanged=false;
			imageViewLoaded=false;
			getSwfObject('ImageViewer').setJson(currentConfig);
		}
	}
	setTimeout(refreshViewImage, 1000);
}
</script>
</html>