var pkuvis_sketchpad_initialize;
(function() {
	pkuvis_sketchpad_initialize = function(f, p) {
		if (!f) {
			return
		}
		if (!p) {
			p = {}
		}
		var b = document.createElement("canvas");
		b.width = p.width ? p.width : $(f).width();
		b.height = p.height ? p.height : $(f).height();
		var o = b.getContext("2d");
		var j = document.createElement("div");
		j.setAttribute("class", "toolbox");
		j.innerHTML = ' <span class="button btn-arrow">箭头</span> <span class="button btn-text">文本</span> <input type="text" class="input-text" /> <span class="btn-color btn-color-0-0-0"></span> <span class="btn-color btn-color-31-119-180"></span> <span class="btn-color btn-color-255-127-14"></span> <span class="btn-color btn-color-44-160-44"></span> <span class="button btn-remove">删除</span> <span class="button btn-close">关闭</span>';
		f.appendChild(b);
		f.appendChild(j);
		f.sketchpad_redraw = n;
		f.sketchpad_enabled = true;
		f.sketchpad_canvas = b;
		var a = [];
		var e = function(v, q, z) {
			if (v.w) {
				return q >= v.x && z >= v.y && q <= v.x + v.w && z <= v.y + v.h
			} else {
				if (v.r) {
					var u = (q - v.x) * (q - v.x) + (z - v.y) * (z - v.y);
					return v.r * v.r >= u
				} else {
					if (v.theta) {
						var t = q - v.x;
						var w = z - v.y;
						var s = t * Math.cos(v.theta) + w * Math.sin(v.theta);
						var r = -t * Math.sin(v.theta) + w * Math.cos(v.theta);
						return Math.abs(s) <= v.rx && Math.abs(r) <= v.ry
					}
				}
			}
		};
		var l = function(u) {
			var v = [];
			if (u.type == "text") {
				o.font = u.font.size + "px " + u.font.family;
				var B = o.measureText(u.text).width;
				var t = u.font.size;
				var A = u.x - 5;
				var z = u.y - t - 3;
				t += 10;
				B += 10;
				v.push({
					x: A,
					y: z,
					w: B,
					h: t
				})
			}
			if (u.type == "arrow") {
				v.push({
					x: u.x1,
					y: u.y1,
					r: 10
				});
				v.push({
					x: u.x2,
					y: u.y2,
					r: 10
				});
				var D = u.x2 - u.x1,
					C = u.y2 - u.y1;
				var s = (u.x1 + u.x2) / 2;
				var r = (u.y1 + u.y2) / 2;
				var q = Math.atan2(C, D);
				v.push({
					x: s,
					y: r,
					rx: Math.sqrt(D * D + C * C) / 2,
					ry: 5,
					theta: q
				})
			}
			return v
		};
		var n = function() {
			o.clearRect(0, 0, b.width, b.height);
			for (var y in a) {
				var A = a[y];
				o.save();
				if (A.type == "arrow") {
					if (A.style) {
						o.fillStyle = o.strokeStyle = A.style
					}
					if (A.width) {
						o.lineWidth = A.width
					}
					var D = A.x2 - A.x1,
						C = A.y2 - A.y1;
					var s = Math.atan2(-C, -D);
					var z = Math.sqrt(D * D + C * C);
					if (z >= 10) {
						var q = 15 / 180 * Math.PI;
						var t = z / 8,
							x, w;
						if (t < 10) {
							t = 10
						}
						if (t > 40) {
							t = 40
						}
						var v = A.x2 + t * Math.cos(s) * 0.7;
						var u = A.y2 + t * Math.sin(s) * 0.7;
						x = A.x2 + t * Math.cos(s + q);
						w = A.y2 + t * Math.sin(s + q);
						o.beginPath();
						o.moveTo(A.x1, A.y1);
						o.lineTo(v, u);
						o.stroke();
						o.beginPath();
						o.moveTo(v, u);
						o.lineTo(x, w);
						o.lineTo(A.x2, A.y2);
						x = A.x2 + t * Math.cos(s - q);
						w = A.y2 + t * Math.sin(s - q);
						o.lineTo(x, w);
						o.lineTo(v, u);
						o.fill()
					} else {
						o.beginPath();
						o.moveTo(A.x1, A.y1);
						o.lineTo(A.x2, A.y2);
						o.stroke()
					}
				}
				if (A.type == "text") {
					o.beginPath();
					if (A.style) {
						o.fillStyle = A.style
					}
					o.font = A.font.size + "px " + A.font.family;
					o.fillText(A.text, A.x, A.y)
				}
				if (A.type == "rect") {
					if (A.style) {
						o.fillStyle = A.style
					}
					o.strokeRect(Math.min(A.x1, A.x2), Math.min(A.y1, A.y2), Math.abs(A.x2 - A.x1), Math.abs(A.y2 - A.y1))
				}
				if (h == A) {
					var r = l(A);
					o.strokeStyle = "gray";
					o.lineWidth = 1;
					for (var y in r) {
						var B = r[y];
						if (B.theta) {
							o.translate(B.x, B.y);
							o.rotate(B.theta);
							o.strokeRect(-B.rx, -B.ry, B.rx * 2, B.ry * 2)
						} else {
							if (B.w) {
								o.strokeRect(B.x, B.y, B.w, B.h)
							}
						}
					}
				}
				o.restore()
			}
		};
		var m = null;
		var h = null;
		var d, i;
		var c = null;
		var g = "";
		$(b).mousedown(function(u) {
			var t = u.pageX - $(f).offset().left;
			var s = u.pageY - $(f).offset().top;
			d = t;
			i = s;
			if (!f.sketchpad_enabled) {
				return true
			}
			if (c) {
				var w = {};
				if (c == "arrow") {
					w = {
						type: "arrow",
						x1: t,
						y1: s,
						x2: t + 1,
						y2: s + 1,
						width: 2
					};
					w.select_index = 1
				}
				if (c == "text") {
					w = {
						type: "text",
						x: t,
						y: s,
						font: {
							family: "Helvetica",
							size: 24
						},
						text: g
					};
					w.select_index = 0
				}
				h = w;
				h.selected = true;
				m = w;
				w.prev_object = $.parseJSON(JSON.stringify(w));
				a.push(w);
				c = undefined
			} else {
				h = null;
				for (var r in a) {
					var w = a[r];
					w.selected = false;
					var v = l(w);
					for (var q in v) {
						var x = v[q];
						if (e(x, t, s)) {
							h = w;
							h.select_index = q;
							break
						}
					}
					if (h) {
						break
					}
				}
				if (h) {
					h.selected = true
				}
				m = h;
				if (m) {
					var w = m;
					if (w.prev_object) {
						delete w.prev_object
					}
					w.prev_object = $.parseJSON(JSON.stringify(w))
				}
			} if (h && h.type == "text") {
				$(j).children(".input-text").val(h.text)
			} else {
				$(j).children(".input-text").val("")
			}
			n();
			if (m) {
				u.stopPropagation()
			}
		});
		$(window).mousemove(function(s) {
			var r = s.pageX - $(f).offset().left;
			var q = s.pageY - $(f).offset().top;
			if (m) {
				var s = m;
				if (s.type == "text") {
					s.x = s.prev_object.x + r - d;
					s.y = s.prev_object.y + q - i
				}
				if (s.type == "arrow") {
					if (s.select_index == 0) {
						s.x1 = s.prev_object.x1 + r - d;
						s.y1 = s.prev_object.y1 + q - i
					} else {
						if (s.select_index == 1) {
							s.x2 = s.prev_object.x2 + r - d;
							s.y2 = s.prev_object.y2 + q - i
						} else {
							s.x1 = s.prev_object.x1 + r - d;
							s.y1 = s.prev_object.y1 + q - i;
							s.x2 = s.prev_object.x2 + r - d;
							s.y2 = s.prev_object.y2 + q - i
						}
					}
				}
				n()
			}
		});
		$(window).mouseup(function() {
			m = null
		});
		$(j).children(".btn-arrow").click(function() {
			c = "arrow"
		});
		$(j).children(".btn-text").click(function() {
			g = $(j).children(".input-text").val();
			if (g == "") {
				g = "text"
			}
			if (g != "") {
				c = "text"
			}
		});
		$(j).children(".input-text").keyup(function() {
			if (h) {
				h.text = this.value
			}
			n()
		});
		$(j).children(".btn-color").each(function() {
			var r = $(this).attr("class");
			var q = r.match(/btn-color-(\d+)-(\d+)-(\d+)/);
			var s = "rgb(" + q[1] + "," + q[2] + "," + q[3] + ")";
			$(this).css("color", s);
			$(this).css("background-color", s)
		});
		$(j).children(".btn-color").click(function() {
			var r = $(this).attr("class");
			var q = r.match(/btn-color-(\d+)-(\d+)-(\d+)/);
			var s = "rgb(" + q[1] + "," + q[2] + "," + q[3] + ")";
			if (h) {
				h.style = s;
				n()
			}
		});
		$(j).children(".btn-remove").click(function() {
			if (h) {
				var r = [];
				for (var q in a) {
					if (a[q] != h) {
						r.push(a[q])
					}
				}
				a = r;
				n();
				h = null
			}
		});
		$(j).children(".btn-close").click(function() {
			if (f.on_sketchpad_close) {
				f.on_sketchpad_close()
			}
		});
		$(j).children(".input-text").mousedown(function(q) {
			q.stopPropagation()
		});
		if (p.disable) {
			f.sketchpad_enabled = false
		}
		var k = ["x.posx", "x1.posx", "x2.posx", "y.posy", "y1.posy", "y2.posy", "text", "style", "font", "width"];
		f.sketchpad_export = function() {
			var v = [];
			for (var u in a) {
				var w = a[u];
				var y = {
					type: w.type,
				};
				for (var t in k) {
					var s = k[t];
					var q = s.match(/([^\.]+)(\.(.*))?/);
					if (w[q[1]]) {
						var x = w[q[1]];
						if (q[3] == "posx") {
							x = x / b.width
						}
						if (q[3] == "posy") {
							x = x / b.height
						}
						y[q[1]] = x
					}
				}
				v.push(y)
			}
			return {
				elements: v
			}
		};
		f.sketchpad_import = function(t) {
			a = [];
			for (var s in t.elements) {
				var w = t.elements[s];
				var u = {
					type: w.type
				};
				for (var s in k) {
					var r = k[s];
					var q = r.match(/([^\.]+)(\.(.*))?/);
					if (w[q[1]]) {
						var v = w[q[1]];
						if (q[3] == "posx") {
							v = v * b.width
						}
						if (q[3] == "posy") {
							v = v * b.height
						}
						u[q[1]] = v
					}
				}
				a.push(u)
			}
			n()
		};
		n()
	}
})();