var lineCanvas = (function() {
	var b = 1, d = 1;
	function a(e) {
		if (e >= Math.PI * 1.5) {
			return 4;
		}
		if (e >= Math.PI) {
			return 3;
		}
		if (e >= Math.PI / 2) {
			return 2;
		}
		return 1;
	}
	var c = (function() {
		var h, f, g, e;
		return {
			create : function(i, j) {
				h = document.getElementById("cv" + d++);
				g = i;
				j = (j % 360) * Math.PI / 180;
				if (j < 0) {
					j += Math.PI * 2;
				}
				e = j;
				h.width = Math.abs(Math.round(Math.cos(j % Math.PI) * i));
				h.height = Math.abs(Math.round(Math.sin(j % Math.PI) * i));
				f = h.getContext("2d");
				f.lineWidth = b;
				return h;
			},
			clear : function() {
				f.clearRect(0, 0, h.width, h.height);
			},
			draw : function(n, m, k, j) {
				var l = 0, i = g;
				cur_point = g * n;
				if (m) {
					l = g;
					i = 0;
					cur_point = g - cur_point;
				}
				this.clear();
				f.save();
				switch (a(e)) {
					case 2 :
						f.translate(h.width, 0);
						break;
					case 3 :
						f.translate(h.width, h.height);
						break;
					case 4 :
						f.translate(0, h.height);
						break;
				}
				f.rotate(e);
				f.strokeStyle = k;
				f.beginPath();
				f.moveTo(l, 0);
				f.lineTo(cur_point, 0);
				f.stroke();
				if (j) {
					f.strokeStyle = j;
					f.beginPath();
					f.moveTo(cur_point, 0);
					f.lineTo(i, 0);
					f.stroke();
				}
				f.restore();
			},
			getElement : function() {
				return h;
			}
		};
	});
	return function(f, g) {
		var e = c();
		e.create(f, g);
		return e;
	};
})();
$(function() {
			var a = ["#f00", "#0f0", "#fc0", "#00f"], b = $("#buttons"), i = [];
			function d(k, l) {
				var j = lineCanvas(k, l);
				return {
					length : k,
					angle : l,
					cv : j,
					color1 : a[0],
					color2 : null,
					painted : false
				};
			}
			function f(j) {
				var k;
				do {
					k = a[Math.floor(Math.random() * a.length)];
				} while (k == j.color1);
				j.color2 = j.color1;
				j.color1 = k;
				return k;
			}
			i.push(d(632, -5.5));
			i.push(d(552, 118.5));
			i.push(d(560, -130));
			var e = [[null, -1, 3], [1, null, -2], [-3, 2, null]];
			var c = {
				time : 0.6,
				transition : "easeinoutcubic"
			};
			var h = $("input[name=btn]").click(function() {
						var l = parseInt(this.value);
						if (l != g) {
							var m = e[l][g];
							var j = i[Math.abs(m) - 1], k = (m < 0);
							if (j.painted) {
								f(j);
							}
							$t({}, c).percent(function(n) {
										j.cv.draw(n, k, j.color1, j.color2);
									});
							j.painted = true;
							g = l;
						}
					});
			var g = parseInt(h.filter(":checked").val());
		});