//查看原始题卡模块
var sheet_view = function(option, success) {
	var object = new SheetView(option);
	success();
	return object;
}

function SheetView(option) {
	this.markControl = option.markControl;
	this.server = option.server;
	this.list = [];
	this.markControl.on('task.get.success', this, function(event, context, eventObject) {
		this.render(context.task.sheetUrls);
	});
	this.markControl.on('task.get.none', this, function(event, context, eventObject) {
		this.render();
	});
	this.markControl.on('task.get.before', this, function(event, context, eventObject) {
		this.render();
	});
}

SheetView.prototype.render = function(urls) {
	if(urls != undefined && urls.length > 0) {
		for(var i = 0; i < urls.length; i++) {
			var url = urls[i];
			if(this.list.length > i) {
				var item = this.list[i];
				item.nav.show();
				this.renderContent(item.content, url, i);
			} else {
				var content = $(this.container_dom).appendTo(this.markControl.container.centerContent);
				this.renderContent(content, url, i);
				this.list.push({
				    nav: this.markControl.addNavGroup('原卷' + (i + 1), content),
				    content: content
				});
			} 
		}
	} else {
		for(var i = 0; i < this.list.length; i++) {
			var item = this.list[i];
			item.nav.hide();
			item.content.empty();
		}
	}
}

SheetView.prototype.renderContent = function(content, url, i) {
	content.width(this.markControl.container.centerContent.width());
	content.height(this.markControl.container.centerContent.height());
	content.empty();
//	content.append($('<img src="'+this.server + url +'">'));
	var show  = false;
	if(i%2==0){
		show = true;
	}
	content.append($('<canvas id="sheet-canvas'+i+'"></canvas>'));
	var image = new Image();
	image.crossOrigin = '';
	image.src = this.server + url ;
    image.canvas = document.getElementById('sheet-canvas'+i);
    image.content = content;
    image.onload = function() {
    	var ctx = this.canvas.getContext("2d");
    	this.canvas.width = Math.min(this.width, this.content.width());
    	this.canvas.height = this.canvas.width * this.height / this.width;
    	ctx.drawImage(image, 0,0,this.width,this.height,0, 0, this.canvas.width, this.canvas.height);
    	if(show){
    		ctx.fillStyle = "#FFFFFF";
    		ctx.fillRect(0, 0, this.canvas.width/2, this.canvas.height*3.7/10);
    	}
    	this.canvas.toDataURL("image/jpeg");
    }
}

SheetView.prototype.container_dom = '<div style="display:none; overflow: scroll"></div>';