cf41ab08b8ac3a01b6a46a6a1f56e2b36accb75c
[raphael] / raphael.js
1 /*!
2  * Raphael 1.3.2 - JavaScript Vector Library
3  *
4  * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7  
8  
9 Raphael = (function () {
10     var separator = /[, ]+/,
11         elements = /^(circle|rect|path|ellipse|text|image)$/,
12         has = "hasOwnProperty",
13         doc = document,
14         win = window,
15         oldRaphael = {
16             was: win[has]("Raphael"),
17             is: win.Raphael
18         },
19         R = function () {
20             if (R.is(arguments[0], "array")) {
21                 var a = arguments[0],
22                     cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
23                     res = cnv.set();
24                 for (var i = 0, ii = a[length]; i < ii; i++) {
25                     var j = a[i] || {};
26                     elements.test(j.type) && res[push](cnv[j.type]().attr(j));
27                 }
28                 return res;
29             }
30             return create[apply](R, arguments);
31         },
32         Paper = function () {},
33         appendChild = "appendChild",
34         apply = "apply",
35         concat = "concat",
36         E = "",
37         S = " ",
38         split = "split",
39         events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
40         join = "join",
41         length = "length",
42         proto = "prototype",
43         lowerCase = String[proto].toLowerCase,
44         math = Math,
45         mmax = math.max,
46         mmin = math.min,
47         nu = "number",
48         toString = "toString",
49         objectToString = Object[proto][toString],
50         paper = {},
51         pow = math.pow,
52         push = "push",
53         rg = /^(?=[\da-f]$)/,
54         ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
55         colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
56         round = math.round,
57         setAttribute = "setAttribute",
58         toFloat = parseFloat,
59         toInt = parseInt,
60         upperCase = String[proto].toUpperCase,
61         availableAttrs = {"clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
62         availableAnimAttrs = {along: "along", "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
63         rp = "replace";
64     R.version = "1.3.2";
65     R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66     if (R.type == "VML") {
67         var d = document.createElement("div");
68         d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
69         if (d.childNodes[length] != 2) {
70             return null;
71         }
72     }
73     R.svg = !(R.vml = R.type == "VML");
74     Paper[proto] = R[proto];
75     R._id = 0;
76     R._oid = 0;
77     R.fn = {};
78     R.is = function (o, type) {
79         type = lowerCase.call(type);
80         return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
81     };
82     R.setWindow = function (newwin) {
83         win = newwin;
84         doc = win.document;
85     };
86     // colour utilities
87     var toHex = function (color) {
88         if (R.vml) {
89             // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
90             var trim = /^\s+|\s+$/g;
91             toHex = cacher(function (color) {
92                 var bod;
93                 color = (color + E)[rp](trim, E);
94                 try {
95                     var docum = new ActiveXObject("htmlfile");
96                     docum.write("<body>");
97                     docum.close();
98                     bod = docum.body;
99                 } catch(e) {
100                     bod = createPopup().document.body;
101                 }
102                 var range = bod.createTextRange();
103                 try {
104                     bod.style.color = color;
105                     var value = range.queryCommandValue("ForeColor");
106                     value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
107                     return "#" + ("000000" + value[toString](16)).slice(-6);
108                 } catch(e) {
109                     return "none";
110                 }
111             });
112         } else {
113             var i = doc.createElement("i");
114             i.title = "Rapha\xebl Colour Picker";
115             i.style.display = "none";
116             doc.body[appendChild](i);
117             toHex = cacher(function (color) {
118                 i.style.color = color;
119                 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
120             });
121         }
122         return toHex(color);
123     };
124     R.hsb2rgb = cacher(function (hue, saturation, brightness) {
125         if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
126             brightness = hue.b;
127             saturation = hue.s;
128             hue = hue.h;
129         }
130         var red,
131             green,
132             blue;
133         if (brightness == 0) {
134             return {r: 0, g: 0, b: 0, hex: "#000"};
135         }
136         if (hue > 1 || saturation > 1 || brightness > 1) {
137             hue /= 255;
138             saturation /= 255;
139             brightness /= 255;
140         }
141         var i = ~~(hue * 6),
142             f = (hue * 6) - i,
143             p = brightness * (1 - saturation),
144             q = brightness * (1 - (saturation * f)),
145             t = brightness * (1 - (saturation * (1 - f)));
146         red = [brightness, q, p, p, t, brightness, brightness][i];
147         green = [t, brightness, brightness, q, p, p, t][i];
148         blue = [p, p, t, brightness, brightness, q, p][i];
149         red *= 255;
150         green *= 255;
151         blue *= 255;
152         var rgb = {r: red, g: green, b: blue},
153             r = (~~red)[toString](16),
154             g = (~~green)[toString](16),
155             b = (~~blue)[toString](16);
156         r = r[rp](rg, "0");
157         g = g[rp](rg, "0");
158         b = b[rp](rg, "0");
159         rgb.hex = "#" + r + g + b;
160         return rgb;
161     }, R);
162     R.rgb2hsb = cacher(function (red, green, blue) {
163         if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
164             blue = red.b;
165             green = red.g;
166             red = red.r;
167         }
168         if (R.is(red, "string")) {
169             var clr = R.getRGB(red);
170             red = clr.r;
171             green = clr.g;
172             blue = clr.b;
173         }
174         if (red > 1 || green > 1 || blue > 1) {
175             red /= 255;
176             green /= 255;
177             blue /= 255;
178         }
179         var max = mmax(red, green, blue),
180             min = mmin(red, green, blue),
181             hue,
182             saturation,
183             brightness = max;
184         if (min == max) {
185             return {h: 0, s: 0, b: max};
186         } else {
187             var delta = (max - min);
188             saturation = delta / max;
189             if (red == max) {
190                 hue = (green - blue) / delta;
191             } else if (green == max) {
192                 hue = 2 + ((blue - red) / delta);
193             } else {
194                 hue = 4 + ((red - green) / delta);
195             }
196             hue /= 6;
197             hue < 0 && hue++;
198             hue > 1 && hue--;
199         }
200         return {h: hue, s: saturation, b: brightness};
201     }, R);
202     var p2s = /,?([achlmqrstvxz]),?/gi;
203     R._path2string = function () {
204         return this.join(",")[rp](p2s, "$1");
205     };
206     function cacher(f, scope, postprocessor) {
207         function newf() {
208             var arg = Array[proto].slice.call(arguments, 0),
209                 args = arg[join]("\u25ba"),
210                 cache = newf.cache = newf.cache || {},
211                 count = newf.count = newf.count || [];
212             if (cache[has](args)) {
213                 return postprocessor ? postprocessor(cache[args]) : cache[args];
214             }
215             count[length] >= 1e3 && delete cache[count.shift()];
216             count[push](args);
217             cache[args] = f[apply](scope, arg);
218             return postprocessor ? postprocessor(cache[args]) : cache[args];
219         }
220         return newf;
221     }
222  
223     R.getRGB = cacher(function (colour) {
224         if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
225             return {r: -1, g: -1, b: -1, hex: "none", error: 1};
226         }
227         if (colour == "none") {
228             return {r: -1, g: -1, b: -1, hex: "none"};
229         }
230         !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
231         var res,
232             red,
233             green,
234             blue,
235             t,
236             rgb = colour.match(colourRegExp);
237         if (rgb) {
238             if (rgb[2]) {
239                 blue = toInt(rgb[2].substring(5), 16);
240                 green = toInt(rgb[2].substring(3, 5), 16);
241                 red = toInt(rgb[2].substring(1, 3), 16);
242             }
243             if (rgb[3]) {
244                 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
245                 green = toInt((t = rgb[3].charAt(2)) + t, 16);
246                 red = toInt((t = rgb[3].charAt(1)) + t, 16);
247             }
248             if (rgb[4]) {
249                 rgb = rgb[4][split](/\s*,\s*/);
250                 red = toFloat(rgb[0]);
251                 green = toFloat(rgb[1]);
252                 blue = toFloat(rgb[2]);
253             }
254             if (rgb[5]) {
255                 rgb = rgb[5][split](/\s*,\s*/);
256                 red = toFloat(rgb[0]) * 2.55;
257                 green = toFloat(rgb[1]) * 2.55;
258                 blue = toFloat(rgb[2]) * 2.55;
259             }
260             if (rgb[6]) {
261                 rgb = rgb[6][split](/\s*,\s*/);
262                 red = toFloat(rgb[0]);
263                 green = toFloat(rgb[1]);
264                 blue = toFloat(rgb[2]);
265                 return R.hsb2rgb(red, green, blue);
266             }
267             if (rgb[7]) {
268                 rgb = rgb[7][split](/\s*,\s*/);
269                 red = toFloat(rgb[0]) * 2.55;
270                 green = toFloat(rgb[1]) * 2.55;
271                 blue = toFloat(rgb[2]) * 2.55;
272                 return R.hsb2rgb(red, green, blue);
273             }
274             rgb = {r: red, g: green, b: blue};
275             var r = (~~red)[toString](16),
276                 g = (~~green)[toString](16),
277                 b = (~~blue)[toString](16);
278             r = r[rp](rg, "0");
279             g = g[rp](rg, "0");
280             b = b[rp](rg, "0");
281             rgb.hex = "#" + r + g + b;
282             return rgb;
283         }
284         return {r: -1, g: -1, b: -1, hex: "none", error: 1};
285     }, R);
286     R.getColor = function (value) {
287         var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
288             rgb = this.hsb2rgb(start.h, start.s, start.b);
289         start.h += .075;
290         if (start.h > 1) {
291             start.h = 0;
292             start.s -= .2;
293             start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
294         }
295         return rgb.hex;
296     };
297     R.getColor.reset = function () {
298         delete this.start;
299     };
300     // path utilities
301     var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
302         pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
303     R.parsePathString = cacher(function (pathString) {
304         if (!pathString) {
305             return null;
306         }
307         var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
308             data = [];
309         if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
310             data = pathClone(pathString);
311         }
312         if (!data[length]) {
313             (pathString + E)[rp](pathCommand, function (a, b, c) {
314                 var params = [],
315                     name = lowerCase.call(b);
316                 c[rp](pathValues, function (a, b) {
317                     b && params[push](+b);
318                 });
319                 if (name == "m" && params[length] > 2) {
320                     data[push]([b][concat](params.splice(0, 2)));
321                     name = "l";
322                     b = b == "m" ? "l" : "L";
323                 }
324                 while (params[length] >= paramCounts[name]) {
325                     data[push]([b][concat](params.splice(0, paramCounts[name])));
326                     if (!paramCounts[name]) {
327                         break;
328                     };
329                 }
330             });
331         }
332         data[toString] = R._path2string;
333         return data;
334     });
335     R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
336         var t1 = 1 - t,
337             x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
338             y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
339             mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
340             my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
341             nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
342             ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
343             ax = (1 - t) * p1x + t * c1x,
344             ay = (1 - t) * p1y + t * c1y,
345             cx = (1 - t) * c2x + t * p2x,
346             cy = (1 - t) * c2y + t * p2y,
347             alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
348         (mx > nx || my < ny) && (alpha += 180);
349         return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
350     };
351     var pathDimensions = cacher(function (path) {
352         if (!path) {
353             return {x: 0, y: 0, width: 0, height: 0};
354         }
355         path = path2curve(path);
356         var x = 0, 
357             y = 0,
358             X = [],
359             Y = [],
360             p;
361         for (var i = 0, ii = path[length]; i < ii; i++) {
362             p = path[i];
363             if (p[0] == "M") {
364                 x = p[1];
365                 y = p[2];
366                 X[push](x);
367                 Y[push](y);
368             } else {
369                 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
370                 X = X[concat](dim.min.x, dim.max.x);
371                 Y = Y[concat](dim.min.y, dim.max.y);
372                 x = p[5];
373                 y = p[6];
374             }
375         }
376         var xmin = mmin[apply](0, X),
377             ymin = mmin[apply](0, Y);
378         return {
379             x: xmin,
380             y: ymin,
381             width: mmax[apply](0, X) - xmin,
382             height: mmax[apply](0, Y) - ymin
383         };
384     }),
385         pathClone = function (pathArray) {
386             var res = [];
387             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
388                 pathArray = R.parsePathString(pathArray);
389             }
390             for (var i = 0, ii = pathArray[length]; i < ii; i++) {
391                 res[i] = [];
392                 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
393                     res[i][j] = pathArray[i][j];
394                 }
395             }
396             res[toString] = R._path2string;
397             return res;
398         },
399         pathToRelative = cacher(function (pathArray) {
400             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
401                 pathArray = R.parsePathString(pathArray);
402             }
403             var res = [],
404                 x = 0,
405                 y = 0,
406                 mx = 0,
407                 my = 0,
408                 start = 0;
409             if (pathArray[0][0] == "M") {
410                 x = pathArray[0][1];
411                 y = pathArray[0][2];
412                 mx = x;
413                 my = y;
414                 start++;
415                 res[push](["M", x, y]);
416             }
417             for (var i = start, ii = pathArray[length]; i < ii; i++) {
418                 var r = res[i] = [],
419                     pa = pathArray[i];
420                 if (pa[0] != lowerCase.call(pa[0])) {
421                     r[0] = lowerCase.call(pa[0]);
422                     switch (r[0]) {
423                         case "a":
424                             r[1] = pa[1];
425                             r[2] = pa[2];
426                             r[3] = pa[3];
427                             r[4] = pa[4];
428                             r[5] = pa[5];
429                             r[6] = +(pa[6] - x).toFixed(3);
430                             r[7] = +(pa[7] - y).toFixed(3);
431                             break;
432                         case "v":
433                             r[1] = +(pa[1] - y).toFixed(3);
434                             break;
435                         case "m":
436                             mx = pa[1];
437                             my = pa[2];
438                         default:
439                             for (var j = 1, jj = pa[length]; j < jj; j++) {
440                                 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
441                             }
442                     }
443                 } else {
444                     r = res[i] = [];
445                     if (pa[0] == "m") {
446                         mx = pa[1] + x;
447                         my = pa[2] + y;
448                     }
449                     for (var k = 0, kk = pa[length]; k < kk; k++) {
450                         res[i][k] = pa[k];
451                     }
452                 }
453                 var len = res[i][length];
454                 switch (res[i][0]) {
455                     case "z":
456                         x = mx;
457                         y = my;
458                         break;
459                     case "h":
460                         x += +res[i][len - 1];
461                         break;
462                     case "v":
463                         y += +res[i][len - 1];
464                         break;
465                     default:
466                         x += +res[i][len - 2];
467                         y += +res[i][len - 1];
468                 }
469             }
470             res[toString] = R._path2string;
471             return res;
472         }, 0, pathClone),
473         pathToAbsolute = cacher(function (pathArray) {
474             if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
475                 pathArray = R.parsePathString(pathArray);
476             }
477             var res = [],
478                 x = 0,
479                 y = 0,
480                 mx = 0,
481                 my = 0,
482                 start = 0;
483             if (pathArray[0][0] == "M") {
484                 x = +pathArray[0][1];
485                 y = +pathArray[0][2];
486                 mx = x;
487                 my = y;
488                 start++;
489                 res[0] = ["M", x, y];
490             }
491             for (var i = start, ii = pathArray[length]; i < ii; i++) {
492                 var r = res[i] = [],
493                     pa = pathArray[i];
494                 if (pa[0] != upperCase.call(pa[0])) {
495                     r[0] = upperCase.call(pa[0]);
496                     switch (r[0]) {
497                         case "A":
498                             r[1] = pa[1];
499                             r[2] = pa[2];
500                             r[3] = pa[3];
501                             r[4] = pa[4];
502                             r[5] = pa[5];
503                             r[6] = +(pa[6] + x);
504                             r[7] = +(pa[7] + y);
505                             break;
506                         case "V":
507                             r[1] = +pa[1] + y;
508                             break;
509                         case "H":
510                             r[1] = +pa[1] + x;
511                             break;
512                         case "M":
513                             mx = +pa[1] + x;
514                             my = +pa[2] + y;
515                         default:
516                             for (var j = 1, jj = pa[length]; j < jj; j++) {
517                                 r[j] = +pa[j] + ((j % 2) ? x : y);
518                             }
519                     }
520                 } else {
521                     for (var k = 0, kk = pa[length]; k < kk; k++) {
522                         res[i][k] = pa[k];
523                     }
524                 }
525                 switch (r[0]) {
526                     case "Z":
527                         x = mx;
528                         y = my;
529                         break;
530                     case "H":
531                         x = r[1];
532                         break;
533                     case "V":
534                         y = r[1];
535                         break;
536                     default:
537                         x = res[i][res[i][length] - 2];
538                         y = res[i][res[i][length] - 1];
539                 }
540             }
541             res[toString] = R._path2string;
542             return res;
543         }, null, pathClone),
544         l2c = function (x1, y1, x2, y2) {
545             return [x1, y1, x2, y2, x2, y2];
546         },
547         q2c = function (x1, y1, ax, ay, x2, y2) {
548             var _13 = 1 / 3,
549                 _23 = 2 / 3;
550             return [
551                     _13 * x1 + _23 * ax,
552                     _13 * y1 + _23 * ay,
553                     _13 * x2 + _23 * ax,
554                     _13 * y2 + _23 * ay,
555                     x2,
556                     y2
557                 ];
558         },
559         a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
560             // for more information of where this math came from visit:
561             // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
562             var PI = math.PI,
563                 _120 = PI * 120 / 180,
564                 rad = PI / 180 * (+angle || 0),
565                 res = [],
566                 xy,
567                 rotate = cacher(function (x, y, rad) {
568                     var X = x * math.cos(rad) - y * math.sin(rad),
569                         Y = x * math.sin(rad) + y * math.cos(rad);
570                     return {x: X, y: Y};
571                 });
572             if (!recursive) {
573                 xy = rotate(x1, y1, -rad);
574                 x1 = xy.x;
575                 y1 = xy.y;
576                 xy = rotate(x2, y2, -rad);
577                 x2 = xy.x;
578                 y2 = xy.y;
579                 var cos = math.cos(PI / 180 * angle),
580                     sin = math.sin(PI / 180 * angle),
581                     x = (x1 - x2) / 2,
582                     y = (y1 - y2) / 2;
583                 // rx = mmax(rx, math.abs(x));
584                 // ry = mmax(ry, math.abs(y));
585                 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
586                 if (h > 1) {
587                     h = math.sqrt(h);
588                     rx = h * rx;
589                     ry = h * ry;
590                 }
591                 var rx2 = rx * rx,
592                     ry2 = ry * ry,
593                     k = (large_arc_flag == sweep_flag ? -1 : 1) *
594                         math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
595                     cx = k * rx * y / ry + (x1 + x2) / 2,
596                     cy = k * -ry * x / rx + (y1 + y2) / 2,
597                     f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
598                     f2 = math.asin(((y2 - cy) / ry).toFixed(7));
599
600                 f1 = x1 < cx ? PI - f1 : f1;
601                 f2 = x2 < cx ? PI - f2 : f2;
602                 f1 < 0 && (f1 = PI * 2 + f1);
603                 f2 < 0 && (f2 = PI * 2 + f2);
604                 if (sweep_flag && f1 > f2) {
605                     f1 = f1 - PI * 2;
606                 }
607                 if (!sweep_flag && f2 > f1) {
608                     f2 = f2 - PI * 2;
609                 }
610             } else {
611                 f1 = recursive[0];
612                 f2 = recursive[1];
613                 cx = recursive[2];
614                 cy = recursive[3];
615             }
616             var df = f2 - f1;
617             if (math.abs(df) > _120) {
618                 var f2old = f2,
619                     x2old = x2,
620                     y2old = y2;
621                 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
622                 x2 = cx + rx * math.cos(f2);
623                 y2 = cy + ry * math.sin(f2);
624                 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
625             }
626             df = f2 - f1;
627             var c1 = math.cos(f1),
628                 s1 = math.sin(f1),
629                 c2 = math.cos(f2),
630                 s2 = math.sin(f2),
631                 t = math.tan(df / 4),
632                 hx = 4 / 3 * rx * t,
633                 hy = 4 / 3 * ry * t,
634                 m1 = [x1, y1],
635                 m2 = [x1 + hx * s1, y1 - hy * c1],
636                 m3 = [x2 + hx * s2, y2 - hy * c2],
637                 m4 = [x2, y2];
638             m2[0] = 2 * m1[0] - m2[0];
639             m2[1] = 2 * m1[1] - m2[1];
640             if (recursive) {
641                 return [m2, m3, m4][concat](res);
642             } else {
643                 res = [m2, m3, m4][concat](res)[join]()[split](",");
644                 var newres = [];
645                 for (var i = 0, ii = res[length]; i < ii; i++) {
646                     newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
647                 }
648                 // alert(newres);
649                 return newres;
650             }
651         },
652         findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
653             var t1 = 1 - t;
654             return {
655                 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
656                 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
657             };
658         },
659         curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
660             var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
661                 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
662                 c = p1x - c1x,
663                 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
664                 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
665                 y = [p1y, p2y],
666                 x = [p1x, p2x],
667                 dot;
668             math.abs(t1) > 1e12 && (t1 = .5);
669             math.abs(t2) > 1e12 && (t2 = .5);
670             if (t1 > 0 && t1 < 1) {
671                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
672                 x[push](dot.x);
673                 y[push](dot.y);
674             }
675             if (t2 > 0 && t2 < 1) {
676                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
677                 x[push](dot.x);
678                 y[push](dot.y);
679             }
680             a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
681             b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
682             c = p1y - c1y;
683             t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
684             t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
685             math.abs(t1) > 1e12 && (t1 = .5);
686             math.abs(t2) > 1e12 && (t2 = .5);
687             if (t1 > 0 && t1 < 1) {
688                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
689                 x[push](dot.x);
690                 y[push](dot.y);
691             }
692             if (t2 > 0 && t2 < 1) {
693                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
694                 x[push](dot.x);
695                 y[push](dot.y);
696             }
697             return {
698                 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
699                 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
700             };
701         }),
702         path2curve = cacher(function (path, path2) {
703             var p = pathToAbsolute(path),
704                 p2 = path2 && pathToAbsolute(path2),
705                 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
706                 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
707                 processPath = function (path, d) {
708                     var nx, ny;
709                     if (!path) {
710                         return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
711                     }
712                     !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
713                     switch (path[0]) {
714                         case "M":
715                             d.X = path[1];
716                             d.Y = path[2];
717                             break;
718                         case "A":
719                             path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
720                             break;
721                         case "S":
722                             nx = d.x + (d.x - (d.bx || d.x));
723                             ny = d.y + (d.y - (d.by || d.y));
724                             path = ["C", nx, ny][concat](path.slice(1));
725                             break;
726                         case "T":
727                             d.qx = d.x + (d.x - (d.qx || d.x));
728                             d.qy = d.y + (d.y - (d.qy || d.y));
729                             path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
730                             break;
731                         case "Q":
732                             d.qx = path[1];
733                             d.qy = path[2];
734                             path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
735                             break;
736                         case "L":
737                             path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
738                             break;
739                         case "H":
740                             path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
741                             break;
742                         case "V":
743                             path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
744                             break;
745                         case "Z":
746                             path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
747                             break;
748                     }
749                     return path;
750                 },
751                 fixArc = function (pp, i) {
752                     if (pp[i][length] > 7) {
753                         pp[i].shift();
754                         var pi = pp[i];
755                         while (pi[length]) {
756                             pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
757                         }
758                         pp.splice(i, 1);
759                         ii = mmax(p[length], p2 && p2[length] || 0);
760                     }
761                 },
762                 fixM = function (path1, path2, a1, a2, i) {
763                     if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
764                         path2.splice(i, 0, ["M", a2.x, a2.y]);
765                         a1.bx = 0;
766                         a1.by = 0;
767                         a1.x = path1[i][1];
768                         a1.y = path1[i][2];
769                         ii = mmax(p[length], p2 && p2[length] || 0);
770                     }
771                 };
772             for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
773                 p[i] = processPath(p[i], attrs);
774                 fixArc(p, i);
775                 p2 && (p2[i] = processPath(p2[i], attrs2));
776                 p2 && fixArc(p2, i);
777                 fixM(p, p2, attrs, attrs2, i);
778                 fixM(p2, p, attrs2, attrs, i);
779                 var seg = p[i],
780                     seg2 = p2 && p2[i],
781                     seglen = seg[length],
782                     seg2len = p2 && seg2[length];
783                 attrs.x = seg[seglen - 2];
784                 attrs.y = seg[seglen - 1];
785                 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
786                 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
787                 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
788                 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
789                 attrs2.x = p2 && seg2[seg2len - 2];
790                 attrs2.y = p2 && seg2[seg2len - 1];
791             }
792             return p2 ? [p, p2] : p;
793         }, null, pathClone),
794         parseDots = cacher(function (gradient) {
795             var dots = [];
796             for (var i = 0, ii = gradient[length]; i < ii; i++) {
797                 var dot = {},
798                     par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
799                 dot.color = R.getRGB(par[1]);
800                 if (dot.color.error) {
801                     return null;
802                 }
803                 dot.color = dot.color.hex;
804                 par[2] && (dot.offset = par[2] + "%");
805                 dots[push](dot);
806             }
807             for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
808                 if (!dots[i].offset) {
809                     var start = toFloat(dots[i - 1].offset || 0),
810                         end = 0;
811                     for (var j = i + 1; j < ii; j++) {
812                         if (dots[j].offset) {
813                             end = dots[j].offset;
814                             break;
815                         }
816                     }
817                     if (!end) {
818                         end = 100;
819                         j = ii;
820                     }
821                     end = toFloat(end);
822                     var d = (end - start) / (j - i + 1);
823                     for (; i < j; i++) {
824                         start += d;
825                         dots[i].offset = start + "%";
826                     }
827                 }
828             }
829             return dots;
830         }),
831         getContainer = function () {
832             var container,
833                 x,
834                 y,
835                 width,
836                 height;
837             if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
838                 if (R.is(arguments[0], "string")) {
839                     container = doc.getElementById(arguments[0]);
840                 } else {
841                     container = arguments[0];
842                 }
843                 if (container.tagName) {
844                     if (arguments[1] == null) {
845                         return {
846                             container: container,
847                             width: container.style.pixelWidth || container.offsetWidth,
848                             height: container.style.pixelHeight || container.offsetHeight
849                         };
850                     } else {
851                         return {container: container, width: arguments[1], height: arguments[2]};
852                     }
853                 }
854             } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
855                 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
856             }
857         },
858         plugins = function (con, add) {
859             var that = this;
860             for (var prop in add) if (add[has](prop) && !(prop in con)) {
861                 switch (typeof add[prop]) {
862                     case "function":
863                         (function (f) {
864                             con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
865                         })(add[prop]);
866                     break;
867                     case "object":
868                         con[prop] = con[prop] || {};
869                         plugins.call(this, con[prop], add[prop]);
870                     break;
871                     default:
872                         con[prop] = add[prop];
873                     break;
874                 }
875             }
876         },
877         tear = function (el, paper) {
878             el == paper.top && (paper.top = el.prev);
879             el == paper.bottom && (paper.bottom = el.next);
880             el.next && (el.next.prev = el.prev);
881             el.prev && (el.prev.next = el.next);
882         },
883         tofront = function (el, paper) {
884             if (paper.top === el) {
885                 return;
886             }
887             tear(el, paper);
888             el.next = null;
889             el.prev = paper.top;
890             paper.top.next = el;
891             paper.top = el;
892         },
893         toback = function (el, paper) {
894             if (paper.bottom === el) {
895                 return;
896             }
897             tear(el, paper);
898             el.next = paper.bottom;
899             el.prev = null;
900             paper.bottom.prev = el;
901             paper.bottom = el;
902         },
903         insertafter = function (el, el2, paper) {
904             tear(el, paper);
905             el2 == paper.top && (paper.top = el);
906             el2.next && (el2.next.prev = el);
907             el.next = el2.next;
908             el.prev = el2;
909             el2.next = el;
910         },
911         insertbefore = function (el, el2, paper) {
912             tear(el, paper);
913             el2 == paper.bottom && (paper.bottom = el);
914             el2.prev && (el2.prev.next = el);
915             el.prev = el2.prev;
916             el2.prev = el;
917             el.next = el2;
918         },
919         removed = function (methodname) {
920             return function () {
921                 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
922             };
923         },
924         radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
925  
926     // SVG
927     if (R.svg) {
928         Paper[proto].svgns = "http://www.w3.org/2000/svg";
929         Paper[proto].xlink = "http://www.w3.org/1999/xlink";
930         var round = function (num) {
931             return +num + (~~num === num) * .5;
932         },
933             roundPath = function (path) {
934                 for (var i = 0, ii = path[length]; i < ii; i++) {
935                     if (lowerCase.call(path[i][0]) != "a") {
936                         for (var j = 1, jj = path[i][length]; j < jj; j++) {
937                             path[i][j] = round(path[i][j]);
938                         }
939                     } else {
940                         path[i][6] = round(path[i][6]);
941                         path[i][7] = round(path[i][7]);
942                     }
943                 }
944                 return path;
945             },
946             $ = function (el, attr) {
947                 if (attr) {
948                     for (var key in attr) if (attr[has](key)) {
949                         el[setAttribute](key, attr[key]);
950                     }
951                 } else {
952                     return doc.createElementNS(Paper[proto].svgns, el);
953                 }
954             };
955         R[toString] = function () {
956             return  "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
957         };
958         var thePath = function (pathString, SVG) {
959             var el = $("path");
960             SVG.canvas && SVG.canvas[appendChild](el);
961             var p = new Element(el, SVG);
962             p.type = "path";
963             setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
964             return p;
965         };
966         var addGradientFill = function (o, gradient, SVG) {
967             var type = "linear",
968                 fx = .5, fy = .5,
969                 s = o.style;
970             gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
971                 type = "radial";
972                 if (_fx && _fy) {
973                     fx = toFloat(_fx);
974                     fy = toFloat(_fy);
975                     var dir = ((fy > .5) * 2 - 1);
976                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
977                         (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
978                         fy != .5 &&
979                         (fy = fy.toFixed(5) - 1e-5 * dir);
980                 }
981                 return E;
982             });
983             gradient = gradient[split](/\s*\-\s*/);
984             if (type == "linear") {
985                 var angle = gradient.shift();
986                 angle = -toFloat(angle);
987                 if (isNaN(angle)) {
988                     return null;
989                 }
990                 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
991                     max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
992                 vector[2] *= max;
993                 vector[3] *= max;
994                 if (vector[2] < 0) {
995                     vector[0] = -vector[2];
996                     vector[2] = 0;
997                 }
998                 if (vector[3] < 0) {
999                     vector[1] = -vector[3];
1000                     vector[3] = 0;
1001                 }
1002             }
1003             var dots = parseDots(gradient);
1004             if (!dots) {
1005                 return null;
1006             }
1007             var el = $(type + "Gradient");
1008             el.id = "r" + (R._id++)[toString](36);
1009             $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1010             SVG.defs[appendChild](el);
1011             for (var i = 0, ii = dots[length]; i < ii; i++) {
1012                 var stop = $("stop");
1013                 $(stop, {
1014                     offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1015                     "stop-color": dots[i].color || "#fff"
1016                 });
1017                 el[appendChild](stop);
1018             };
1019             $(o, {
1020                 fill: "url(#" + el.id + ")",
1021                 opacity: 1,
1022                 "fill-opacity": 1
1023             });
1024             s.fill = E;
1025             s.opacity = 1;
1026             s.fillOpacity = 1;
1027             return 1;
1028         };
1029         var updatePosition = function (o) {
1030             var bbox = o.getBBox();
1031             $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1032         };
1033         var setFillAndStroke = function (o, params) {
1034             var dasharray = {
1035                     "": [0],
1036                     "none": [0],
1037                     "-": [3, 1],
1038                     ".": [1, 1],
1039                     "-.": [3, 1, 1, 1],
1040                     "-..": [3, 1, 1, 1, 1, 1],
1041                     ". ": [1, 3],
1042                     "- ": [4, 3],
1043                     "--": [8, 3],
1044                     "- .": [4, 3, 1, 3],
1045                     "--.": [8, 3, 1, 3],
1046                     "--..": [8, 3, 1, 3, 1, 3]
1047                 },
1048                 node = o.node,
1049                 attrs = o.attrs,
1050                 rot = o.rotate(),
1051                 addDashes = function (o, value) {
1052                     value = dasharray[lowerCase.call(value)];
1053                     if (value) {
1054                         var width = o.attrs["stroke-width"] || "1",
1055                             butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1056                             dashes = [];
1057                         var i = value[length];
1058                         while (i--) {
1059                             dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1060                         }
1061                         $(node, {"stroke-dasharray": dashes[join](",")});
1062                     }
1063                 };
1064             params[has]("rotation") && (rot = params.rotation);
1065             var rotxy = (rot + E)[split](separator);
1066             if (!(rotxy.length - 1)) {
1067                 rotxy = null;
1068             } else {
1069                 rotxy[1] = +rotxy[1];
1070                 rotxy[2] = +rotxy[2];
1071             }
1072             toFloat(rot) && o.rotate(0, true);
1073             for (var att in params) if (params[has](att)) {
1074                 if (!availableAttrs[has](att)) {
1075                     continue;
1076                 }
1077                 var value = params[att];
1078                 attrs[att] = value;
1079                 switch (att) {
1080                     case "rotation":
1081                         o.rotate(value, true);
1082                         break;
1083                     // Hyperlink
1084                     case "href":
1085                     case "title":
1086                     case "target":
1087                         var pn = node.parentNode;
1088                         if (lowerCase.call(pn.tagName) != "a") {
1089                             var hl = $("a");
1090                             pn.insertBefore(hl, node);
1091                             hl[appendChild](node);
1092                             pn = hl;
1093                         }
1094                         pn.setAttributeNS(o.paper.xlink, att, value);
1095                         break;
1096                     case "cursor":
1097                         node.style.cursor = value;
1098                         break;
1099                     case "clip-rect":
1100                         var rect = (value + E)[split](separator);
1101                         if (rect[length] == 4) {
1102                             o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1103                             var el = $("clipPath"),
1104                                 rc = $("rect");
1105                             el.id = "r" + (R._id++)[toString](36);
1106                             $(rc, {
1107                                 x: rect[0],
1108                                 y: rect[1],
1109                                 width: rect[2],
1110                                 height: rect[3]
1111                             });
1112                             el[appendChild](rc);
1113                             o.paper.defs[appendChild](el);
1114                             $(node, {"clip-path": "url(#" + el.id + ")"});
1115                             o.clip = rc;
1116                         }
1117                         if (!value) {
1118                             var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1119                             clip && clip.parentNode.removeChild(clip);
1120                             $(node, {"clip-path": E});
1121                             delete o.clip;
1122                         }
1123                     break;
1124                     case "path":
1125                         if (o.type == "path") {
1126                             $(node, {d: value ? attrs.path = roundPath(pathToAbsolute(value)) : "M0,0"});
1127                         }
1128                         break;
1129                     case "width":
1130                         node[setAttribute](att, value);
1131                         if (attrs.fx) {
1132                             att = "x";
1133                             value = attrs.x;
1134                         } else {
1135                             break;
1136                         }
1137                     case "x":
1138                         if (attrs.fx) {
1139                             value = -attrs.x - (attrs.width || 0);
1140                         }
1141                     case "rx":
1142                         if (att == "rx" && o.type == "rect") {
1143                             break;
1144                         }
1145                     case "cx":
1146                         rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1147                         node[setAttribute](att, round(value));
1148                         o.pattern && updatePosition(o);
1149                         break;
1150                     case "height":
1151                         node[setAttribute](att, value);
1152                         if (attrs.fy) {
1153                             att = "y";
1154                             value = attrs.y;
1155                         } else {
1156                             break;
1157                         }
1158                     case "y":
1159                         if (attrs.fy) {
1160                             value = -attrs.y - (attrs.height || 0);
1161                         }
1162                     case "ry":
1163                         if (att == "ry" && o.type == "rect") {
1164                             break;
1165                         }
1166                     case "cy":
1167                         rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1168                         node[setAttribute](att, round(value));
1169                         o.pattern && updatePosition(o);
1170                         break;
1171                     case "r":
1172                         if (o.type == "rect") {
1173                             $(node, {rx: value, ry: value});
1174                         } else {
1175                             node[setAttribute](att, value);
1176                         }
1177                         break;
1178                     case "src":
1179                         if (o.type == "image") {
1180                             node.setAttributeNS(o.paper.xlink, "href", value);
1181                         }
1182                         break;
1183                     case "stroke-width":
1184                         node.style.strokeWidth = value;
1185                         // Need following line for Firefox
1186                         node[setAttribute](att, value);
1187                         if (attrs["stroke-dasharray"]) {
1188                             addDashes(o, attrs["stroke-dasharray"]);
1189                         }
1190                         break;
1191                     case "stroke-dasharray":
1192                         addDashes(o, value);
1193                         break;
1194                     case "translation":
1195                         var xy = (value + E)[split](separator);
1196                         xy[0] = +xy[0] || 0;
1197                         xy[1] = +xy[1] || 0;
1198                         if (rotxy) {
1199                             rotxy[1] += xy[0];
1200                             rotxy[2] += xy[1];
1201                         }
1202                         translate.call(o, xy[0], xy[1]);
1203                         break;
1204                     case "scale":
1205                         var xy = (value + E)[split](separator);
1206                         o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1207                         break;
1208                     case "fill":
1209                         var isURL = (value + E).match(ISURL);
1210                         if (isURL) {
1211                             var el = $("pattern"),
1212                                 ig = $("image");
1213                             el.id = "r" + (R._id++)[toString](36);
1214                             $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1215                             $(ig, {x: 0, y: 0});
1216                             ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1217                             el[appendChild](ig);
1218  
1219                             var img = doc.createElement("img");
1220                             img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1221                             img.onload = function () {
1222                                 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1223                                 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1224                                 doc.body.removeChild(this);
1225                                 o.paper.safari();
1226                             };
1227                             doc.body[appendChild](img);
1228                             img.src = isURL[1];
1229                             o.paper.defs[appendChild](el);
1230                             node.style.fill = "url(#" + el.id + ")";
1231                             $(node, {fill: "url(#" + el.id + ")"});
1232                             o.pattern = el;
1233                             o.pattern && updatePosition(o);
1234                             break;
1235                         }
1236                         if (!R.getRGB(value).error) {
1237                             delete params.gradient;
1238                             delete attrs.gradient;
1239                             !R.is(attrs.opacity, "undefined") &&
1240                                 R.is(params.opacity, "undefined") &&
1241                                 $(node, {opacity: attrs.opacity});
1242                             !R.is(attrs["fill-opacity"], "undefined") &&
1243                                 R.is(params["fill-opacity"], "undefined") &&
1244                                 $(node, {"fill-opacity": attrs["fill-opacity"]});
1245                         } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1246                             attrs.gradient = value;
1247                             attrs.fill = "none";
1248                             break;
1249                         }
1250                     case "stroke":
1251                         node[setAttribute](att, R.getRGB(value).hex);
1252                         break;
1253                     case "gradient":
1254                         (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1255                         break;
1256                     case "opacity":
1257                     case "fill-opacity":
1258                         if (attrs.gradient) {
1259                             var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1260                             if (gradient) {
1261                                 var stops = gradient.getElementsByTagName("stop");
1262                                 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1263                             }
1264                             break;
1265                         }
1266                     default:
1267                         att == "font-size" && (value = toInt(value, 10) + "px");
1268                         var cssrule = att[rp](/(\-.)/g, function (w) {
1269                             return upperCase.call(w.substring(1));
1270                         });
1271                         node.style[cssrule] = value;
1272                         // Need following line for Firefox
1273                         node[setAttribute](att, value);
1274                         break;
1275                 }
1276             }
1277             
1278             tuneText(o, params);
1279             if (rotxy) {
1280                 o.rotate(rotxy.join(S));
1281             } else {
1282                 toFloat(rot) && o.rotate(rot, true);
1283             }
1284         };
1285         var leading = 1.2;
1286         var tuneText = function (el, params) {
1287             if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1288                 return;
1289             }
1290             var a = el.attrs,
1291                 node = el.node,
1292                 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1293  
1294             if (params[has]("text")) {
1295                 a.text = params.text;
1296                 while (node.firstChild) {
1297                     node.removeChild(node.firstChild);
1298                 }
1299                 var texts = (params.text + E)[split]("\n");
1300                 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1301                     var tspan = $("tspan");
1302                     i && $(tspan, {dy: fontSize * leading, x: a.x});
1303                     tspan[appendChild](doc.createTextNode(texts[i]));
1304                     node[appendChild](tspan);
1305                 }
1306             } else {
1307                 var texts = node.getElementsByTagName("tspan");
1308                 for (var i = 0, ii = texts[length]; i < ii; i++) {
1309                     i && $(texts[i], {dy: fontSize * leading, x: a.x});
1310                 }
1311             }
1312             $(node, {y: a.y});
1313             var bb = el.getBBox(),
1314                 dif = a.y - (bb.y + bb.height / 2);
1315             dif && isFinite(dif) && $(node, {y: a.y + dif});
1316         };
1317         var Element = function (node, svg) {
1318             var X = 0,
1319                 Y = 0;
1320             this[0] = node;
1321             this.id = R._oid++;
1322             this.node = node;
1323             node.raphael = this;
1324             this.paper = svg;
1325             this.attrs = this.attrs || {};
1326             this.transformations = []; // rotate, translate, scale
1327             this._ = {
1328                 tx: 0,
1329                 ty: 0,
1330                 rt: {deg: 0, cx: 0, cy: 0},
1331                 sx: 1,
1332                 sy: 1
1333             };
1334             !svg.bottom && (svg.bottom = this);
1335             this.prev = svg.top;
1336             svg.top && (svg.top.next = this);
1337             svg.top = this;
1338             this.next = null;
1339         };
1340         Element[proto].rotate = function (deg, cx, cy) {
1341             if (this.removed) {
1342                 return this;
1343             }
1344             if (deg == null) {
1345                 if (this._.rt.cx) {
1346                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1347                 }
1348                 return this._.rt.deg;
1349             }
1350             var bbox = this.getBBox();
1351             deg = (deg + E)[split](separator);
1352             if (deg[length] - 1) {
1353                 cx = toFloat(deg[1]);
1354                 cy = toFloat(deg[2]);
1355             }
1356             deg = toFloat(deg[0]);
1357             if (cx != null) {
1358                 this._.rt.deg = deg;
1359             } else {
1360                 this._.rt.deg += deg;
1361             }
1362             (cy == null) && (cx = null);
1363             this._.rt.cx = cx;
1364             this._.rt.cy = cy;
1365             cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1366             cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1367             if (this._.rt.deg) {
1368                 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1369                 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1370             } else {
1371                 this.transformations[0] = E;
1372                 this.clip && $(this.clip, {transform: E});
1373             }
1374             $(this.node, {transform: this.transformations[join](S)});
1375             return this;
1376         };
1377         Element[proto].hide = function () {
1378             !this.removed && (this.node.style.display = "none");
1379             return this;
1380         };
1381         Element[proto].show = function () {
1382             !this.removed && (this.node.style.display = "");
1383             return this;
1384         };
1385         Element[proto].remove = function () {
1386             if (this.removed) {
1387                 return;
1388             }
1389             tear(this, this.paper);
1390             this.node.parentNode.removeChild(this.node);
1391             for (var i in this) {
1392                 delete this[i];
1393             }
1394             this.removed = true;
1395         };
1396         Element[proto].getBBox = function () {
1397             if (this.removed) {
1398                 return this;
1399             }
1400             if (this.type == "path") {
1401                 return pathDimensions(this.attrs.path);
1402             }
1403             if (this.node.style.display == "none") {
1404                 this.show();
1405                 var hide = true;
1406             }
1407             var bbox = {};
1408             try {
1409                 bbox = this.node.getBBox();
1410             } catch(e) {
1411                 // Firefox 3.0.x plays badly here
1412             } finally {
1413                 bbox = bbox || {};
1414             }
1415             if (this.type == "text") {
1416                 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1417                 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1418                     var bb = this.node.getExtentOfChar(i);
1419                     (bb.y < bbox.y) && (bbox.y = bb.y);
1420                     (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1421                     (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1422                 }
1423             }
1424             hide && this.hide();
1425             return bbox;
1426         };
1427         Element[proto].attr = function () {
1428             if (this.removed) {
1429                 return this;
1430             }
1431             if (arguments[length] == 0) {
1432                 var res = {};
1433                 for (var i in this.attrs) if (this.attrs[has](i)) {
1434                     res[i] = this.attrs[i];
1435                 }
1436                 this._.rt.deg && (res.rotation = this.rotate());
1437                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1438                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1439                 return res;
1440             }
1441             if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1442                 if (arguments[0] == "translation") {
1443                     return translate.call(this);
1444                 }
1445                 if (arguments[0] == "rotation") {
1446                     return this.rotate();
1447                 }
1448                 if (arguments[0] == "scale") {
1449                     return this.scale();
1450                 }
1451                 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1452                     return this.attrs.gradient;
1453                 }
1454                 return this.attrs[arguments[0]];
1455             }
1456             if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1457                 var values = {};
1458                 for (var j in arguments[0]) if (arguments[0][has](j)) {
1459                     values[arguments[0][j]] = this.attrs[arguments[0][j]];
1460                 }
1461                 return values;
1462             }
1463             if (arguments[length] == 2) {
1464                 var params = {};
1465                 params[arguments[0]] = arguments[1];
1466                 setFillAndStroke(this, params);
1467             } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1468                 setFillAndStroke(this, arguments[0]);
1469             }
1470             return this;
1471         };
1472         Element[proto].toFront = function () {
1473             if (this.removed) {
1474                 return this;
1475             }
1476             this.node.parentNode[appendChild](this.node);
1477             var svg = this.paper;
1478             svg.top != this && tofront(this, svg);
1479             return this;
1480         };
1481         Element[proto].toBack = function () {
1482             if (this.removed) {
1483                 return this;
1484             }
1485             if (this.node.parentNode.firstChild != this.node) {
1486                 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1487                 toback(this, this.paper);
1488                 var svg = this.paper;
1489             }
1490             return this;
1491         };
1492         Element[proto].insertAfter = function (element) {
1493             if (this.removed) {
1494                 return this;
1495             }
1496             var node = element.node;
1497             if (node.nextSibling) {
1498                 node.parentNode.insertBefore(this.node, node.nextSibling);
1499             } else {
1500                 node.parentNode[appendChild](this.node);
1501             }
1502             insertafter(this, element, this.paper);
1503             return this;
1504         };
1505         Element[proto].insertBefore = function (element) {
1506             if (this.removed) {
1507                 return this;
1508             }
1509             var node = element.node;
1510             node.parentNode.insertBefore(this.node, node);
1511             insertbefore(this, element, this.paper);
1512             return this;
1513         };
1514         var theCircle = function (svg, x, y, r) {
1515             x = round(x);
1516             y = round(y);
1517             var el = $("circle");
1518             svg.canvas && svg.canvas[appendChild](el);
1519             var res = new Element(el, svg);
1520             res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1521             res.type = "circle";
1522             $(el, res.attrs);
1523             return res;
1524         };
1525         var theRect = function (svg, x, y, w, h, r) {
1526             x = round(x);
1527             y = round(y);
1528             var el = $("rect");
1529             svg.canvas && svg.canvas[appendChild](el);
1530             var res = new Element(el, svg);
1531             res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1532             res.type = "rect";
1533             $(el, res.attrs);
1534             return res;
1535         };
1536         var theEllipse = function (svg, x, y, rx, ry) {
1537             x = round(x);
1538             y = round(y);
1539             var el = $("ellipse");
1540             svg.canvas && svg.canvas[appendChild](el);
1541             var res = new Element(el, svg);
1542             res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1543             res.type = "ellipse";
1544             $(el, res.attrs);
1545             return res;
1546         };
1547         var theImage = function (svg, src, x, y, w, h) {
1548             var el = $("image");
1549             $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1550             el.setAttributeNS(svg.xlink, "href", src);
1551             svg.canvas && svg.canvas[appendChild](el);
1552             var res = new Element(el, svg);
1553             res.attrs = {x: x, y: y, width: w, height: h, src: src};
1554             res.type = "image";
1555             return res;
1556         };
1557         var theText = function (svg, x, y, text) {
1558             var el = $("text");
1559             $(el, {x: x, y: y, "text-anchor": "middle"});
1560             svg.canvas && svg.canvas[appendChild](el);
1561             var res = new Element(el, svg);
1562             res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1563             res.type = "text";
1564             setFillAndStroke(res, res.attrs);
1565             return res;
1566         };
1567         var setSize = function (width, height) {
1568             this.width = width || this.width;
1569             this.height = height || this.height;
1570             this.canvas[setAttribute]("width", this.width);
1571             this.canvas[setAttribute]("height", this.height);
1572             return this;
1573         };
1574         var create = function () {
1575             var con = getContainer[apply](null, arguments),
1576                 container = con && con.container,
1577                 x = con.x,
1578                 y = con.y,
1579                 width = con.width,
1580                 height = con.height;
1581             if (!container) {
1582                 throw new Error("SVG container not found.");
1583             }
1584             var cnvs = $("svg");
1585             width = width || 512;
1586             height = height || 342;
1587             $(cnvs, {
1588                 xmlns: "http://www.w3.org/2000/svg",
1589                 version: 1.1,
1590                 width: width,
1591                 height: height
1592             });
1593             if (container == 1) {
1594                 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1595                 doc.body[appendChild](cnvs);
1596             } else {
1597                 if (container.firstChild) {
1598                     container.insertBefore(cnvs, container.firstChild);
1599                 } else {
1600                     container[appendChild](cnvs);
1601                 }
1602             }
1603             container = new Paper;
1604             container.width = width;
1605             container.height = height;
1606             container.canvas = cnvs;
1607             plugins.call(container, container, R.fn);
1608             container.clear();
1609             return container;
1610         };
1611         Paper[proto].clear = function () {
1612             var c = this.canvas;
1613             while (c.firstChild) {
1614                 c.removeChild(c.firstChild);
1615             }
1616             this.bottom = this.top = null;
1617             (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1618             c[appendChild](this.desc);
1619             c[appendChild](this.defs = $("defs"));
1620         };
1621         Paper[proto].remove = function () {
1622             this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1623             for (var i in this) {
1624                 this[i] = removed(i);
1625             }
1626         };
1627     }
1628  
1629     // VML
1630     if (R.vml) {
1631         var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1632             bites = /([clmz]),?([^clmz]*)/gi,
1633             val = /-?[^,\s-]+/g,
1634             path2vml = function (path) {
1635                 var total =  /[ahqstv]/ig,
1636                     command = pathToAbsolute;
1637                 (path + E).match(total) && (command = path2curve);
1638                 total = /[clmz]/g;
1639                 if (command == pathToAbsolute && !(path + E).match(total)) {
1640                     var res = (path + E)[rp](bites, function (all, command, args) {
1641                         var vals = [],
1642                             isMove = lowerCase.call(command) == "m",
1643                             res = map[command];
1644                         args[rp](val, function (value) {
1645                             if (isMove && vals[length] == 2) {
1646                                 res += vals + map[command == "m" ? "l" : "L"];
1647                                 vals = [];
1648                             }
1649                             vals[push](round(value));
1650                         });
1651                         return res + vals;
1652                     });
1653                     return res;
1654                 }
1655                 var pa = command(path), p, res = [], r;
1656                 for (var i = 0, ii = pa[length]; i < ii; i++) {
1657                     p = pa[i];
1658                     r = lowerCase.call(pa[i][0]);
1659                     r == "z" && (r = "x");
1660                     for (var j = 1, jj = p[length]; j < jj; j++) {
1661                         r += round(p[j]) + (j != jj - 1 ? "," : E);
1662                     }
1663                     res[push](r);
1664                 }
1665                 return res[join](S);
1666             };
1667         
1668         R[toString] = function () {
1669             return  "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1670         };
1671         var thePath = function (pathString, VML) {
1672             var g = createNode("group");
1673             g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1674             g.coordsize = VML.coordsize;
1675             g.coordorigin = VML.coordorigin;
1676             var el = createNode("shape"), ol = el.style;
1677             ol.width = VML.width + "px";
1678             ol.height = VML.height + "px";
1679             el.coordsize = this.coordsize;
1680             el.coordorigin = this.coordorigin;
1681             g[appendChild](el);
1682             var p = new Element(el, g, VML),
1683                 attr = {fill: "none", stroke: "#000"};
1684             pathString && (attr.path = pathString);
1685             p.isAbsolute = true;
1686             p.type = "path";
1687             p.path = [];
1688             p.Path = E;
1689             setFillAndStroke(p, attr);
1690             VML.canvas[appendChild](g);
1691             return p;
1692         };
1693         var setFillAndStroke = function (o, params) {
1694             o.attrs = o.attrs || {};
1695             var node = o.node,
1696                 a = o.attrs,
1697                 s = node.style,
1698                 xy,
1699                 res = o;
1700             for (var par in params) if (params[has](par)) {
1701                 a[par] = params[par];
1702             }
1703             params.href && (node.href = params.href);
1704             params.title && (node.title = params.title);
1705             params.target && (node.target = params.target);
1706             params.cursor && (s.cursor = params.cursor);
1707             if (params.path && o.type == "path") {
1708                 a.path = params.path;
1709                 node.path = path2vml(a.path);
1710             }
1711             if (params.rotation != null) {
1712                 o.rotate(params.rotation, true);
1713             }
1714             if (params.translation) {
1715                 xy = (params.translation + E)[split](separator);
1716                 translate.call(o, xy[0], xy[1]);
1717                 if (o._.rt.cx != null) {
1718                     o._.rt.cx +=+ xy[0];
1719                     o._.rt.cy +=+ xy[1];
1720                     o.setBox(o.attrs, xy[0], xy[1]);
1721                 }
1722             }
1723             if (params.scale) {
1724                 xy = (params.scale + E)[split](separator);
1725                 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1726             }
1727             if ("clip-rect" in params) {
1728                 var rect = (params["clip-rect"] + E)[split](separator);
1729                 if (rect[length] == 4) {
1730                     rect[2] = +rect[2] + (+rect[0]);
1731                     rect[3] = +rect[3] + (+rect[1]);
1732                     var div = node.clipRect || doc.createElement("div"),
1733                         dstyle = div.style,
1734                         group = node.parentNode;
1735                     dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1736                     if (!node.clipRect) {
1737                         dstyle.position = "absolute";
1738                         dstyle.top = 0;
1739                         dstyle.left = 0;
1740                         dstyle.width = o.paper.width + "px";
1741                         dstyle.height = o.paper.height + "px";
1742                         group.parentNode.insertBefore(div, group);
1743                         div[appendChild](group);
1744                         node.clipRect = div;
1745                     }
1746                 }
1747                 if (!params["clip-rect"]) {
1748                     node.clipRect && (node.clipRect.style.clip = E);
1749                 }
1750             }
1751             if (o.type == "image" && params.src) {
1752                 node.src = params.src;
1753             }
1754             if (o.type == "image" && params.opacity) {
1755                 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1756                 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1757             }
1758             params.font && (s.font = params.font);
1759             params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1760             params["font-size"] && (s.fontSize = params["font-size"]);
1761             params["font-weight"] && (s.fontWeight = params["font-weight"]);
1762             params["font-style"] && (s.fontStyle = params["font-style"]);
1763             if (params.opacity != null || 
1764                 params["stroke-width"] != null ||
1765                 params.fill != null ||
1766                 params.stroke != null ||
1767                 params["stroke-width"] != null ||
1768                 params["stroke-opacity"] != null ||
1769                 params["fill-opacity"] != null ||
1770                 params["stroke-dasharray"] != null ||
1771                 params["stroke-miterlimit"] != null ||
1772                 params["stroke-linejoin"] != null ||
1773                 params["stroke-linecap"] != null) {
1774                 node = o.shape || node;
1775                 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1776                     newfill = false;
1777                 !fill && (newfill = fill = createNode("fill"));
1778                 if ("fill-opacity" in params || "opacity" in params) {
1779                     var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1780                     opacity < 0 && (opacity = 0);
1781                     opacity > 1 && (opacity = 1);
1782                     fill.opacity = opacity;
1783                 }
1784                 params.fill && (fill.on = true);
1785                 if (fill.on == null || params.fill == "none") {
1786                     fill.on = false;
1787                 }
1788                 if (fill.on && params.fill) {
1789                     var isURL = params.fill.match(ISURL);
1790                     if (isURL) {
1791                         fill.src = isURL[1];
1792                         fill.type = "tile";
1793                     } else {
1794                         fill.color = R.getRGB(params.fill).hex;
1795                         fill.src = E;
1796                         fill.type = "solid";
1797                         if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1798                             a.fill = "none";
1799                             a.gradient = params.fill;
1800                         }
1801                     }
1802                 }
1803                 newfill && node[appendChild](fill);
1804                 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1805                 newstroke = false;
1806                 !stroke && (newstroke = stroke = createNode("stroke"));
1807                 if ((params.stroke && params.stroke != "none") ||
1808                     params["stroke-width"] ||
1809                     params["stroke-opacity"] != null ||
1810                     params["stroke-dasharray"] ||
1811                     params["stroke-miterlimit"] ||
1812                     params["stroke-linejoin"] ||
1813                     params["stroke-linecap"]) {
1814                     stroke.on = true;
1815                 }
1816                 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1817                 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1818                 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1819                     width = (toFloat(params["stroke-width"]) || 1) * .75;
1820                 opacity < 0 && (opacity = 0);
1821                 opacity > 1 && (opacity = 1);
1822                 params["stroke-width"] == null && (width = a["stroke-width"]);
1823                 params["stroke-width"] && (stroke.weight = width);
1824                 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1825                 stroke.opacity = opacity;
1826                 
1827                 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1828                 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1829                 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1830                 if (params["stroke-dasharray"]) {
1831                     var dasharray = {
1832                         "-": "shortdash",
1833                         ".": "shortdot",
1834                         "-.": "shortdashdot",
1835                         "-..": "shortdashdotdot",
1836                         ". ": "dot",
1837                         "- ": "dash",
1838                         "--": "longdash",
1839                         "- .": "dashdot",
1840                         "--.": "longdashdot",
1841                         "--..": "longdashdotdot"
1842                     };
1843                     stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1844                 }
1845                 newstroke && node[appendChild](stroke);
1846             }
1847             if (res.type == "text") {
1848                 var s = res.paper.span.style;
1849                 a.font && (s.font = a.font);
1850                 a["font-family"] && (s.fontFamily = a["font-family"]);
1851                 a["font-size"] && (s.fontSize = a["font-size"]);
1852                 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1853                 a["font-style"] && (s.fontStyle = a["font-style"]);
1854                 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "&#60;")[rp](/&/g, "&#38;")[rp](/\n/g, "<br>"));
1855                 res.W = a.w = res.paper.span.offsetWidth;
1856                 res.H = a.h = res.paper.span.offsetHeight;
1857                 res.X = a.x;
1858                 res.Y = a.y + round(res.H / 2);
1859  
1860                 // text-anchor emulationm
1861                 switch (a["text-anchor"]) {
1862                     case "start":
1863                         res.node.style["v-text-align"] = "left";
1864                         res.bbx = round(res.W / 2);
1865                     break;
1866                     case "end":
1867                         res.node.style["v-text-align"] = "right";
1868                         res.bbx = -round(res.W / 2);
1869                     break;
1870                     default:
1871                         res.node.style["v-text-align"] = "center";
1872                     break;
1873                 }
1874             }
1875         };
1876         var addGradientFill = function (o, gradient) {
1877             o.attrs = o.attrs || {};
1878             var attrs = o.attrs,
1879                 fill = o.node.getElementsByTagName("fill"),
1880                 type = "linear",
1881                 fxfy = ".5 .5";
1882             o.attrs.gradient = gradient;
1883             gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1884                 type = "radial";
1885                 if (fx && fy) {
1886                     fx = toFloat(fx);
1887                     fy = toFloat(fy);
1888                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1889                     fxfy = fx + S + fy;
1890                 }
1891                 return E;
1892             });
1893             gradient = gradient[split](/\s*\-\s*/);
1894             if (type == "linear") {
1895                 var angle = gradient.shift();
1896                 angle = -toFloat(angle);
1897                 if (isNaN(angle)) {
1898                     return null;
1899                 }
1900             }
1901             var dots = parseDots(gradient);
1902             if (!dots) {
1903                 return null;
1904             }
1905             o = o.shape || o.node;
1906             fill = fill[0] || createNode("fill");
1907             if (dots[length]) {
1908                 fill.on = true;
1909                 fill.method = "none";
1910                 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1911                 fill.color = dots[0].color;
1912                 fill.color2 = dots[dots[length] - 1].color;
1913                 var clrs = [];
1914                 for (var i = 0, ii = dots[length]; i < ii; i++) {
1915                     dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1916                 }
1917                 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1918                 if (type == "radial") {
1919                     fill.focus = "100%";
1920                     fill.focussize = fxfy;
1921                     fill.focusposition = fxfy;
1922                 } else {
1923                     fill.angle = (270 - angle) % 360;
1924                 }
1925             }
1926             return 1;
1927         };
1928         var Element = function (node, group, vml) {
1929             var Rotation = 0,
1930                 RotX = 0,
1931                 RotY = 0,
1932                 Scale = 1;
1933             this[0] = node;
1934             this.id = R._oid++;
1935             this.node = node;
1936             node.raphael = this;
1937             this.X = 0;
1938             this.Y = 0;
1939             this.attrs = {};
1940             this.Group = group;
1941             this.paper = vml;
1942             this._ = {
1943                 tx: 0,
1944                 ty: 0,
1945                 rt: {deg:0},
1946                 sx: 1,
1947                 sy: 1
1948             };
1949             !vml.bottom && (vml.bottom = this);
1950             this.prev = vml.top;
1951             vml.top && (vml.top.next = this);
1952             vml.top = this;
1953             this.next = null;
1954         };
1955         Element[proto].rotate = function (deg, cx, cy) {
1956             if (this.removed) {
1957                 return this;
1958             }
1959             if (deg == null) {
1960                 if (this._.rt.cx) {
1961                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1962                 }
1963                 return this._.rt.deg;
1964             }
1965             deg = (deg + E)[split](separator);
1966             if (deg[length] - 1) {
1967                 cx = toFloat(deg[1]);
1968                 cy = toFloat(deg[2]);
1969             }
1970             deg = toFloat(deg[0]);
1971             if (cx != null) {
1972                 this._.rt.deg = deg;
1973             } else {
1974                 this._.rt.deg += deg;
1975             }
1976             cy == null && (cx = null);
1977             this._.rt.cx = cx;
1978             this._.rt.cy = cy;
1979             this.setBox(this.attrs, cx, cy);
1980             this.Group.style.rotation = this._.rt.deg;
1981             // gradient fix for rotation. TODO
1982             // var fill = (this.shape || this.node).getElementsByTagName("fill");
1983             // fill = fill[0] || {};
1984             // var b = ((360 - this._.rt.deg) - 270) % 360;
1985             // !R.is(fill.angle, "undefined") && (fill.angle = b);
1986             return this;
1987         };
1988         Element[proto].setBox = function (params, cx, cy) {
1989             if (this.removed) {
1990                 return this;
1991             }
1992             var gs = this.Group.style,
1993                 os = (this.shape && this.shape.style) || this.node.style;
1994             params = params || {};
1995             for (var i in params) if (params[has](i)) {
1996                 this.attrs[i] = params[i];
1997             }
1998             cx = cx || this._.rt.cx;
1999             cy = cy || this._.rt.cy;
2000             var attr = this.attrs,
2001                 x,
2002                 y,
2003                 w,
2004                 h;
2005             switch (this.type) {
2006                 case "circle":
2007                     x = attr.cx - attr.r;
2008                     y = attr.cy - attr.r;
2009                     w = h = attr.r * 2;
2010                     break;
2011                 case "ellipse":
2012                     x = attr.cx - attr.rx;
2013                     y = attr.cy - attr.ry;
2014                     w = attr.rx * 2;
2015                     h = attr.ry * 2;
2016                     break;
2017                 case "rect":
2018                 case "image":
2019                     x = +attr.x;
2020                     y = +attr.y;
2021                     w = attr.width || 0;
2022                     h = attr.height || 0;
2023                     break;
2024                 case "text":
2025                     this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2026                     x = attr.x - round(this.W / 2);
2027                     y = attr.y - this.H / 2;
2028                     w = this.W;
2029                     h = this.H;
2030                     break;
2031                 case "path":
2032                     if (!this.attrs.path) {
2033                         x = 0;
2034                         y = 0;
2035                         w = this.paper.width;
2036                         h = this.paper.height;
2037                     } else {
2038                         var dim = pathDimensions(this.attrs.path);
2039                         x = dim.x;
2040                         y = dim.y;
2041                         w = dim.width;
2042                         h = dim.height;
2043                     }
2044                     break;
2045                 default:
2046                     x = 0;
2047                     y = 0;
2048                     w = this.paper.width;
2049                     h = this.paper.height;
2050                     break;
2051             }
2052             cx = (cx == null) ? x + w / 2 : cx;
2053             cy = (cy == null) ? y + h / 2 : cy;
2054             var left = cx - this.paper.width / 2,
2055                 top = cy - this.paper.height / 2;
2056             if (this.type == "path" || this.type == "text") {
2057                 (gs.left != left + "px") && (gs.left = left + "px");
2058                 (gs.top != top + "px") && (gs.top = top + "px");
2059                 this.X = this.type == "text" ? x : -left;
2060                 this.Y = this.type == "text" ? y : -top;
2061                 this.W = w;
2062                 this.H = h;
2063                 (os.left != -left + "px") && (os.left = -left + "px");
2064                 (os.top != -top + "px") && (os.top = -top + "px");
2065             } else {
2066                 (gs.left != left + "px") && (gs.left = left + "px");
2067                 (gs.top != top + "px") && (gs.top = top + "px");
2068                 this.X = x;
2069                 this.Y = y;
2070                 this.W = w;
2071                 this.H = h;
2072                 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2073                 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2074                 (os.left != x - left + "px") && (os.left = x - left + "px");
2075                 (os.top != y - top + "px") && (os.top = y - top + "px");
2076                 (os.width != w + "px") && (os.width = w + "px");
2077                 (os.height != h + "px") && (os.height = h + "px");
2078                 var arcsize = (+params.r || 0) / mmin(w, h);
2079                 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2080                     // We should replace element with the new one
2081                     var o = createNode("roundrect"),
2082                         a = {},
2083                         i = 0,
2084                         ii = this.events && this.events[length];
2085                     o.arcsize = arcsize;
2086                     o.raphael = this;
2087                     this.Group[appendChild](o);
2088                     this.Group.removeChild(this.node);
2089                     this[0] = this.node = o;
2090                     this.arcsize = arcsize;
2091                     for (var i in attr) {
2092                         a[i] = attr[i];
2093                     }
2094                     delete a.scale;
2095                     this.attr(a);
2096                     if (this.events) for (; i < ii; i++) {
2097                         this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2098                     }
2099                 }
2100             }
2101         };
2102         Element[proto].hide = function () {
2103             !this.removed && (this.Group.style.display = "none");
2104             return this;
2105         };
2106         Element[proto].show = function () {
2107             !this.removed && (this.Group.style.display = "block");
2108             return this;
2109         };
2110         Element[proto].getBBox = function () {
2111             if (this.removed) {
2112                 return this;
2113             }
2114             if (this.type == "path") {
2115                 return pathDimensions(this.attrs.path);
2116             }
2117             return {
2118                 x: this.X + (this.bbx || 0),
2119                 y: this.Y,
2120                 width: this.W,
2121                 height: this.H
2122             };
2123         };
2124         Element[proto].remove = function () {
2125             if (this.removed) {
2126                 return;
2127             }
2128             tear(this, this.paper);
2129             this.node.parentNode.removeChild(this.node);
2130             this.Group.parentNode.removeChild(this.Group);
2131             this.shape && this.shape.parentNode.removeChild(this.shape);
2132             for (var i in this) {
2133                 delete this[i];
2134             }
2135             this.removed = true;
2136         };
2137         Element[proto].attr = function () {
2138             if (this.removed) {
2139                 return this;
2140             }
2141             if (arguments[length] == 0) {
2142                 var res = {};
2143                 for (var i in this.attrs) if (this.attrs[has](i)) {
2144                     res[i] = this.attrs[i];
2145                 }
2146                 this._.rt.deg && (res.rotation = this.rotate());
2147                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2148                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2149                 return res;
2150             }
2151             if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2152                 if (arguments[0] == "translation") {
2153                     return translate.call(this);
2154                 }
2155                 if (arguments[0] == "rotation") {
2156                     return this.rotate();
2157                 }
2158                 if (arguments[0] == "scale") {
2159                     return this.scale();
2160                 }
2161                 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2162                     return this.attrs.gradient;
2163                 }
2164                 return this.attrs[arguments[0]];
2165             }
2166             if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2167                 var values = {};
2168                 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2169                     values[arguments[0][i]] = this.attrs[arguments[0][i]];
2170                 };
2171                 return values;
2172             }
2173             var params;
2174             if (arguments[length] == 2) {
2175                 params = {};
2176                 params[arguments[0]] = arguments[1];
2177             }
2178             arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2179             if (params) {
2180                 if (params.text && this.type == "text") {
2181                     this.node.string = params.text;
2182                 }
2183                 setFillAndStroke(this, params);
2184                 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2185                     addGradientFill(this, params.gradient);
2186                 }
2187                 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2188             }
2189             return this;
2190         };
2191         Element[proto].toFront = function () {
2192             !this.removed && this.Group.parentNode[appendChild](this.Group);
2193             this.paper.top != this && tofront(this, this.paper);
2194             return this;
2195         };
2196         Element[proto].toBack = function () {
2197             if (this.removed) {
2198                 return this;
2199             }
2200             if (this.Group.parentNode.firstChild != this.Group) {
2201                 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2202                 toback(this, this.paper);
2203             }
2204             return this;
2205         };
2206         Element[proto].insertAfter = function (element) {
2207             if (this.removed) {
2208                 return this;
2209             }
2210             if (element.Group.nextSibling) {
2211                 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2212             } else {
2213                 element.Group.parentNode[appendChild](this.Group);
2214             }
2215             insertafter(this, element, this.paper);
2216             return this;
2217         };
2218         Element[proto].insertBefore = function (element) {
2219             if (this.removed) {
2220                 return this;
2221             }
2222             element.Group.parentNode.insertBefore(this.Group, element.Group);
2223             insertbefore(this, element, this.paper);
2224             return this;
2225         };
2226  
2227         var theCircle = function (vml, x, y, r) {
2228             var g = createNode("group"),
2229                 o = createNode("oval"),
2230                 ol = o.style;
2231             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2232             g.coordsize = vml.coordsize;
2233             g.coordorigin = vml.coordorigin;
2234             g[appendChild](o);
2235             var res = new Element(o, g, vml);
2236             res.type = "circle";
2237             setFillAndStroke(res, {stroke: "#000", fill: "none"});
2238             res.attrs.cx = x;
2239             res.attrs.cy = y;
2240             res.attrs.r = r;
2241             res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2242             vml.canvas[appendChild](g);
2243             return res;
2244         },
2245         theRect = function (vml, x, y, w, h, r) {
2246             var g = createNode("group"),
2247                 o = createNode("roundrect"),
2248                 arcsize = (+r || 0) / (mmin(w, h));
2249             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2250             g.coordsize = vml.coordsize;
2251             g.coordorigin = vml.coordorigin;
2252             g[appendChild](o);
2253             o.arcsize = arcsize;
2254             var res = new Element(o, g, vml);
2255             res.type = "rect";
2256             setFillAndStroke(res, {stroke: "#000"});
2257             res.arcsize = arcsize;
2258             res.setBox({x: x, y: y, width: w, height: h, r: r});
2259             vml.canvas[appendChild](g);
2260             return res;
2261         },
2262         theEllipse = function (vml, x, y, rx, ry) {
2263             var g = createNode("group"),
2264                 o = createNode("oval"),
2265                 ol = o.style;
2266             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2267             g.coordsize = vml.coordsize;
2268             g.coordorigin = vml.coordorigin;
2269             g[appendChild](o);
2270             var res = new Element(o, g, vml);
2271             res.type = "ellipse";
2272             setFillAndStroke(res, {stroke: "#000"});
2273             res.attrs.cx = x;
2274             res.attrs.cy = y;
2275             res.attrs.rx = rx;
2276             res.attrs.ry = ry;
2277             res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2278             vml.canvas[appendChild](g);
2279             return res;
2280         },
2281         theImage = function (vml, src, x, y, w, h) {
2282             var g = createNode("group"),
2283                 o = createNode("image"),
2284                 ol = o.style;
2285             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2286             g.coordsize = vml.coordsize;
2287             g.coordorigin = vml.coordorigin;
2288             o.src = src;
2289             g[appendChild](o);
2290             var res = new Element(o, g, vml);
2291             res.type = "image";
2292             res.attrs.src = src;
2293             res.attrs.x = x;
2294             res.attrs.y = y;
2295             res.attrs.w = w;
2296             res.attrs.h = h;
2297             res.setBox({x: x, y: y, width: w, height: h});
2298             vml.canvas[appendChild](g);
2299             return res;
2300         },
2301         theText = function (vml, x, y, text) {
2302             var g = createNode("group"),
2303                 el = createNode("shape"),
2304                 ol = el.style,
2305                 path = createNode("path"),
2306                 ps = path.style,
2307                 o = createNode("textpath");
2308             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2309             g.coordsize = vml.coordsize;
2310             g.coordorigin = vml.coordorigin;
2311             path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2312             path.textpathok = true;
2313             ol.width = vml.width;
2314             ol.height = vml.height;
2315             o.string = text + E;
2316             o.on = true;
2317             el[appendChild](o);
2318             el[appendChild](path);
2319             g[appendChild](el);
2320             var res = new Element(o, g, vml);
2321             res.shape = el;
2322             res.textpath = path;
2323             res.type = "text";
2324             res.attrs.text = text;
2325             res.attrs.x = x;
2326             res.attrs.y = y;
2327             res.attrs.w = 1;
2328             res.attrs.h = 1;
2329             setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2330             res.setBox();
2331             vml.canvas[appendChild](g);
2332             return res;
2333         },
2334         setSize = function (width, height) {
2335             var cs = this.canvas.style;
2336             width == +width && (width += "px");
2337             height == +height && (height += "px");
2338             cs.width = width;
2339             cs.height = height;
2340             cs.clip = "rect(0 " + width + " " + height + " 0)";
2341             return this;
2342         },
2343         createNode;
2344         doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2345         try {
2346             !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2347             createNode = function (tagName) {
2348                 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2349             };
2350         } catch (e) {
2351             createNode = function (tagName) {
2352                 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2353             };
2354         }
2355         var create = function () {
2356             var con = getContainer[apply](null, arguments),
2357                 container = con.container,
2358                 height = con.height,
2359                 s,
2360                 width = con.width,
2361                 x = con.x,
2362                 y = con.y;
2363             if (!container) {
2364                 throw new Error("VML container not found.");
2365             }
2366             var res = new Paper,
2367                 c = res.canvas = doc.createElement("div"),
2368                 cs = c.style;
2369             width = width || 512;
2370             height = height || 342;
2371             width == +width && (width += "px");
2372             height == +height && (height += "px");
2373             res.width = 1e3;
2374             res.height = 1e3;
2375             res.coordsize = "1000 1000";
2376             res.coordorigin = "0 0";
2377             res.span = doc.createElement("span");
2378             res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2379             c[appendChild](res.span);
2380             cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2381             if (container == 1) {
2382                 doc.body[appendChild](c);
2383                 cs.left = x + "px";
2384                 cs.top = y + "px";
2385             } else {
2386                 container.style.width = width;
2387                 container.style.height = height;
2388                 if (container.firstChild) {
2389                     container.insertBefore(c, container.firstChild);
2390                 } else {
2391                     container[appendChild](c);
2392                 }
2393             }
2394             plugins.call(res, res, R.fn);
2395             return res;
2396         };
2397         Paper[proto].clear = function () {
2398             this.canvas.innerHTML = E;
2399             this.span = doc.createElement("span");
2400             this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2401             this.canvas[appendChild](this.span);
2402             this.bottom = this.top = null;
2403         };
2404         Paper[proto].remove = function () {
2405             this.canvas.parentNode.removeChild(this.canvas);
2406             for (var i in this) {
2407                 this[i] = removed(i);
2408             }
2409         };
2410     }
2411  
2412     // rest
2413     // Safari or Chrome (WebKit) rendering bug workaround method
2414     if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2415         Paper[proto].safari = function () {
2416             var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2417             setTimeout(function () {rect.remove();});
2418         };
2419     } else {
2420         Paper[proto].safari = function () {};
2421     }
2422  
2423     // Events
2424     var addEvent = (function () {
2425         if (doc.addEventListener) {
2426             return function (obj, type, fn, element) {
2427                 var f = function (e) {
2428                     return fn.call(element, e);
2429                 };
2430                 obj.addEventListener(type, f, false);
2431                 return function () {
2432                     obj.removeEventListener(type, f, false);
2433                     return true;
2434                 };
2435             };
2436         } else if (doc.attachEvent) {
2437             return function (obj, type, fn, element) {
2438                 var f = function (e) {
2439                     return fn.call(element, e || win.event);
2440                 };
2441                 obj.attachEvent("on" + type, f);
2442                 var detacher = function () {
2443                     obj.detachEvent("on" + type, f);
2444                     return true;
2445                 };
2446                 return detacher;
2447             };
2448         }
2449     })();
2450     for (var i = events[length]; i--;) {
2451         (function (eventName) {
2452             Element[proto][eventName] = function (fn) {
2453                 if (R.is(fn, "function")) {
2454                     this.events = this.events || [];
2455                     this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2456                 }
2457                 return this;
2458             };
2459             Element[proto]["un" + eventName] = function (fn) {
2460                 var events = this.events,
2461                     l = events[length];
2462                 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2463                     events[l].unbind();
2464                     events.splice(l, 1);
2465                     !events.length && delete this.events;
2466                     return this;
2467                 }
2468                 return this;
2469             };
2470         })(events[i]);
2471     }
2472     Element[proto].hover = function (f_in, f_out) {
2473         return this.mouseover(f_in).mouseout(f_out);
2474     };
2475     Element[proto].unhover = function (f_in, f_out) {
2476         return this.unmouseover(f_in).unmouseout(f_out);
2477     };
2478     Paper[proto].circle = function (x, y, r) {
2479         return theCircle(this, x || 0, y || 0, r || 0);
2480     };
2481     Paper[proto].rect = function (x, y, w, h, r) {
2482         return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2483     };
2484     Paper[proto].ellipse = function (x, y, rx, ry) {
2485         return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2486     };
2487     Paper[proto].path = function (pathString) {
2488         pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2489         return thePath(R.format[apply](R, arguments), this);
2490     };
2491     Paper[proto].image = function (src, x, y, w, h) {
2492         return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2493     };
2494     Paper[proto].text = function (x, y, text) {
2495         return theText(this, x || 0, y || 0, text || E);
2496     };
2497     Paper[proto].set = function (itemsArray) {
2498         arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2499         return new Set(itemsArray);
2500     };
2501     Paper[proto].setSize = setSize;
2502     Paper[proto].top = Paper[proto].bottom = null;
2503     Paper[proto].raphael = R;
2504     function x_y() {
2505         return this.x + S + this.y;
2506     };
2507     Element[proto].scale = function (x, y, cx, cy) {
2508         if (x == null && y == null) {
2509             return {
2510                 x: this._.sx,
2511                 y: this._.sy,
2512                 toString: x_y
2513             };
2514         }
2515         y = y || x;
2516         !+y && (y = x);
2517         var dx,
2518             dy,
2519             dcx,
2520             dcy,
2521             a = this.attrs;
2522         if (x != 0) {
2523             var bb = this.getBBox(),
2524                 rcx = bb.x + bb.width / 2,
2525                 rcy = bb.y + bb.height / 2,
2526                 kx = x / this._.sx,
2527                 ky = y / this._.sy;
2528             cx = (+cx || cx == 0) ? cx : rcx;
2529             cy = (+cy || cy == 0) ? cy : rcy;
2530             var dirx = ~~(x / math.abs(x)),
2531                 diry = ~~(y / math.abs(y)),
2532                 s = this.node.style,
2533                 ncx = cx + (rcx - cx) * kx,
2534                 ncy = cy + (rcy - cy) * ky;
2535             switch (this.type) {
2536                 case "rect":
2537                 case "image":
2538                     var neww = a.width * dirx * kx,
2539                         newh = a.height * diry * ky;
2540                     this.attr({
2541                         height: newh,
2542                         r: a.r * mmin(dirx * kx, diry * ky),
2543                         width: neww,
2544                         x: ncx - neww / 2,
2545                         y: ncy - newh / 2
2546                     });
2547                     break;
2548                 case "circle":
2549                 case "ellipse":
2550                     this.attr({
2551                         rx: a.rx * dirx * kx,
2552                         ry: a.ry * diry * ky,
2553                         r: a.r * mmin(dirx * kx, diry * ky),
2554                         cx: ncx,
2555                         cy: ncy
2556                     });
2557                     break;
2558                 case "path":
2559                     var path = pathToRelative(a.path),
2560                         skip = true;
2561                     for (var i = 0, ii = path[length]; i < ii; i++) {
2562                         var p = path[i],
2563                             j,
2564                             P0 = upperCase.call(p[0]);
2565                         if (P0 == "M" && skip) {
2566                             continue;
2567                         } else {
2568                             skip = false;
2569                         }
2570                         if (P0 == "A") {
2571                             p[path[i][length] - 2] *= kx;
2572                             p[path[i][length] - 1] *= ky;
2573                             p[1] *= dirx * kx;
2574                             p[2] *= diry * ky;
2575                             p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2576                         } else if (P0 == "H") {
2577                             for (j = 1, jj = p[length]; j < jj; j++) {
2578                                 p[j] *= kx;
2579                             }
2580                         } else if (P0 == "V") {
2581                             for (j = 1, jj = p[length]; j < jj; j++) {
2582                                 p[j] *= ky;
2583                             }
2584                          } else {
2585                             for (j = 1, jj = p[length]; j < jj; j++) {
2586                                 p[j] *= (j % 2) ? kx : ky;
2587                             }
2588                         }
2589                     }
2590                     var dim2 = pathDimensions(path),
2591                         dx = ncx - dim2.x - dim2.width / 2,
2592                         dy = ncy - dim2.y - dim2.height / 2;
2593                     path[0][1] += dx;
2594                     path[0][2] += dy;
2595                     this.attr({path: path});
2596                 break;
2597             }
2598             if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2599                 if (this.transformations) {
2600                     this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2601                     this.node[setAttribute]("transform", this.transformations[join](S));
2602                     dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2603                     dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2604                     this.attr({x: dx, y: dy});
2605                     a.fx = dirx - 1;
2606                     a.fy = diry - 1;
2607                 } else {
2608                     this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2609                         ", M12=0, M21=0, M22=", diry,
2610                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2611                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2612                 }
2613             } else {
2614                 if (this.transformations) {
2615                     this.transformations[2] = E;
2616                     this.node[setAttribute]("transform", this.transformations[join](S));
2617                     a.fx = 0;
2618                     a.fy = 0;
2619                 } else {
2620                     this.node.filterMatrix = E;
2621                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2622                 }
2623             }
2624             a.scale = [x, y, cx, cy][join](S);
2625             this._.sx = x;
2626             this._.sy = y;
2627         }
2628         return this;
2629     };
2630     Element[proto].clone = function () {
2631         var attr = this.attr();
2632         delete attr.scale;
2633         delete attr.translation;
2634         return this.paper[this.type]().attr(attr);
2635     };
2636     var getLengthFactory = function (istotal, subpath) {
2637         return function (path, length, onlystart) {
2638             path = path2curve(path);
2639             var x, y, p, l, sp = "", subpaths = {}, point,
2640                 len = 0;
2641             for (var i = 0, ii = path.length; i < ii; i++) {
2642                 p = path[i];
2643                 if (p[0] == "M") {
2644                     x = +p[1];
2645                     y = +p[2];
2646                 } else {
2647                     l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2648                     if (len + l > length) {
2649                         if (subpath && !subpaths.start) {
2650                             point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2651                             sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2652                             if (onlystart) return sp;
2653                             subpaths.start = sp;
2654                             sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2655                             len += l;
2656                             x = +p[5];
2657                             y = +p[6];
2658                             continue;
2659                         }
2660                         if (!istotal && !subpath) {
2661                             point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2662                             return {x: point.x, y: point.y, alpha: point.alpha};
2663                         }
2664                     }
2665                     len += l;
2666                     x = +p[5];
2667                     y = +p[6];
2668                 }
2669                 sp += p;
2670             }
2671             subpaths.end = sp;
2672             point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2673             point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2674             return point;
2675         };
2676     },
2677     segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2678         var old = {x: 0, y: 0},
2679             len = 0;
2680         for (var i = 0; i < 1.01; i+=.01) {
2681             var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2682             i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
2683             old = dot;
2684         }
2685         return len;
2686     });
2687     var getTotalLength = getLengthFactory(1),
2688         getPointAtLength = getLengthFactory(),
2689         getSubpathsAtLength = getLengthFactory(0, 1);
2690     Element[proto].getTotalLength = function () {
2691         if (this.type != "path") return;
2692         return getTotalLength(this.attrs.path);
2693     };
2694     Element[proto].getPointAtLength = function (length) {
2695         if (this.type != "path") return;
2696         return getPointAtLength(this.attrs.path, length);
2697     };
2698     Element[proto].getSubpath = function (from, to) {
2699         if (this.type != "path") return;
2700         if (math.abs(this.getTotalLength() - to) < 1e-6) {
2701             return getSubpathsAtLength(this.attrs.path, from).end;
2702         }
2703         var a = getSubpathsAtLength(this.attrs.path, to, 1);
2704         return from ? getSubpathsAtLength(a, from).end : a;
2705     };
2706
2707     // animation easing formulas
2708     R.easing_formulas = {
2709         linear: function (n) {
2710             return n;
2711         },
2712         "<": function (n) {
2713             return pow(n, 3);
2714         },
2715         ">": function (n) {
2716             return pow(n - 1, 3) + 1;
2717         },
2718         "<>": function (n) {
2719             n = n * 2;
2720             if (n < 1) {
2721                 return pow(n, 3) / 2;
2722             }
2723             n -= 2;
2724             return (pow(n, 3) + 2) / 2;
2725         },
2726         backIn: function (n) {
2727             var s = 1.70158;
2728             return n * n * ((s + 1) * n - s);
2729         },
2730         backOut: function (n) {
2731             n = n - 1;
2732             var s = 1.70158;
2733             return n * n * ((s + 1) * n + s) + 1;
2734         },
2735         elastic: function (n) {
2736             if (n == 0 || n == 1) {
2737                 return n;
2738             }
2739             var p = .3,
2740                 s = p / 4;
2741             return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2742         },
2743         bounce: function (n) {
2744             var s = 7.5625,
2745                 p = 2.75,
2746                 l;
2747             if (n < (1 / p)) {
2748                 l = s * n * n;
2749             } else {
2750                 if (n < (2 / p)) {
2751                     n -= (1.5 / p);
2752                     l = s * n * n + .75;
2753                 } else {
2754                     if (n < (2.5 / p)) {
2755                         n -= (2.25 / p);
2756                         l = s * n * n + .9375;
2757                     } else {
2758                         n -= (2.625 / p);
2759                         l = s * n * n + .984375;
2760                     }
2761                 }
2762             }
2763             return l;
2764         }
2765     };
2766  
2767     var animationElements = {length : 0},
2768         animation = function () {
2769             var Now = +new Date;
2770             for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2771                 var e = animationElements[l];
2772                 if (e.stop || e.el.removed) {
2773                     delete animationElements[l];
2774                     animationElements[length]--;
2775                     continue;
2776                 }
2777                 var time = Now - e.start,
2778                     ms = e.ms,
2779                     easing = e.easing,
2780                     from = e.from,
2781                     diff = e.diff,
2782                     to = e.to,
2783                     t = e.t,
2784                     prev = e.prev || 0,
2785                     that = e.el,
2786                     callback = e.callback,
2787                     set = {},
2788                     now;
2789                 if (time < ms) {
2790                     var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2791                     for (var attr in from) if (from[has](attr)) {
2792                         switch (availableAnimAttrs[attr]) {
2793                             case "along":
2794                                 now = pos * ms * diff[attr];
2795                                 to.back && (now = to.len - now);
2796                                 var point = getPointAtLength(to[attr], now);
2797                                 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2798                                 diff.x = point.x;
2799                                 diff.y = point.y;
2800                                 that.translate(point.x - diff.sx, point.y - diff.sy);
2801                                 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2802                                 break;
2803                             case "number":
2804                                 now = +from[attr] + pos * ms * diff[attr];
2805                                 break;
2806                             case "colour":
2807                                 now = "rgb(" + [
2808                                     upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2809                                     upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2810                                     upto255(round(from[attr].b + pos * ms * diff[attr].b))
2811                                 ][join](",") + ")";
2812                                 break;
2813                             case "path":
2814                                 now = [];
2815                                 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2816                                     now[i] = [from[attr][i][0]];
2817                                     for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2818                                         now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2819                                     }
2820                                     now[i] = now[i][join](S);
2821                                 }
2822                                 now = now[join](S);
2823                                 break;
2824                             case "csv":
2825                                 switch (attr) {
2826                                     case "translation":
2827                                         var x = diff[attr][0] * (time - prev),
2828                                             y = diff[attr][1] * (time - prev);
2829                                         t.x += x;
2830                                         t.y += y;
2831                                         now = x + S + y;
2832                                     break;
2833                                     case "rotation":
2834                                         now = +from[attr][0] + pos * ms * diff[attr][0];
2835                                         from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2836                                     break;
2837                                     case "scale":
2838                                         now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S);
2839                                     break;
2840                                     case "clip-rect":
2841                                         now = [];
2842                                         var i = 4;
2843                                         while (i--) {
2844                                             now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2845                                         }
2846                                     break;
2847                                 }
2848                                 break;
2849                         }
2850                         set[attr] = now;
2851                     }
2852                     that.attr(set);
2853                     that._run && that._run.call(that);
2854                 } else {
2855                     if (to.along) {
2856                         var point = getPointAtLength(to.along, to.len * !to.back);
2857                         that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
2858                         to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2859                     }
2860                     (t.x || t.y) && that.translate(-t.x, -t.y);
2861                     to.scale && (to.scale = to.scale + E);
2862                     that.attr(to);
2863                     delete animationElements[l];
2864                     animationElements[length]--;
2865                     that.in_animation = null;
2866                     R.is(callback, "function") && callback.call(that);
2867                 }
2868                 e.prev = time;
2869             }
2870             R.svg && that && that.paper.safari();
2871             animationElements[length] && setTimeout(animation);
2872         },
2873         upto255 = function (color) {
2874             return color > 255 ? 255 : (color < 0 ? 0 : color);
2875         },
2876         translate = function (x, y) {
2877             if (x == null) {
2878                 return {x: this._.tx, y: this._.ty, toString: x_y};
2879             }
2880             this._.tx += +x;
2881             this._.ty += +y;
2882             switch (this.type) {
2883                 case "circle":
2884                 case "ellipse":
2885                     this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2886                     break;
2887                 case "rect":
2888                 case "image":
2889                 case "text":
2890                     this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2891                     break;
2892                 case "path":
2893                     var path = pathToRelative(this.attrs.path);
2894                     path[0][1] += +x;
2895                     path[0][2] += +y;
2896                     this.attr({path: path});
2897                 break;
2898             }
2899             return this;
2900         };
2901     Element[proto].animateWith = function (element, params, ms, easing, callback) {
2902         animationElements[element.id] && (params.start = animationElements[element.id].start);
2903         return this.animate(params, ms, easing, callback);
2904     };
2905     Element[proto].animateAlong = along();
2906     Element[proto].animateAlongBack = along(1);
2907     function along(isBack) {
2908         return function (path, ms, rotate, callback) {
2909             var params = {back: isBack};
2910             R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
2911             path && path.constructor == Element && (path = path.attrs.path);
2912             path && (params.along = path);
2913             return this.animate(params, ms, callback);
2914         };
2915     }
2916     Element[proto].onAnimation = function (f) {
2917         this._run = f || 0;
2918         return this;
2919     };
2920     Element[proto].animate = function (params, ms, easing, callback) {
2921         if (R.is(easing, "function") || !easing) {
2922             callback = easing || null;
2923         }
2924         var from = {},
2925             to = {},
2926             diff = {};
2927         for (var attr in params) if (params[has](attr)) {
2928             if (availableAnimAttrs[has](attr)) {
2929                 from[attr] = this.attr(attr);
2930                 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2931                 to[attr] = params[attr];
2932                 switch (availableAnimAttrs[attr]) {
2933                     case "along":
2934                         var len = getTotalLength(params[attr]),
2935                             point = getPointAtLength(params[attr], len * !!params.back),
2936                             bb = this.getBBox();
2937                         diff[attr] = len / ms;
2938                         diff.tx = bb.x;
2939                         diff.ty = bb.y;
2940                         diff.sx = point.x;
2941                         diff.sy = point.y;
2942                         to.rot = params.rot;
2943                         to.back = params.back;
2944                         to.len = len;
2945                         params.rot && (diff.r = toFloat(this.rotate()) || 0);
2946                         break;
2947                     case "number":
2948                         diff[attr] = (to[attr] - from[attr]) / ms;
2949                         break;
2950                     case "colour":
2951                         from[attr] = R.getRGB(from[attr]);
2952                         var toColour = R.getRGB(to[attr]);
2953                         diff[attr] = {
2954                             r: (toColour.r - from[attr].r) / ms,
2955                             g: (toColour.g - from[attr].g) / ms,
2956                             b: (toColour.b - from[attr].b) / ms
2957                         };
2958                         break;
2959                     case "path":
2960                         var pathes = path2curve(from[attr], to[attr]);
2961                         from[attr] = pathes[0];
2962                         var toPath = pathes[1];
2963                         diff[attr] = [];
2964                         for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2965                             diff[attr][i] = [0];
2966                             for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2967                                 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2968                             }
2969                         }
2970                         break;
2971                     case "csv":
2972                         var values = (params[attr] + E)[split](separator),
2973                             from2 = (from[attr] + E)[split](separator);
2974                         switch (attr) {
2975                             case "translation":
2976                                 from[attr] = [0, 0];
2977                                 diff[attr] = [values[0] / ms, values[1] / ms];
2978                             break;
2979                             case "rotation":
2980                                 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2981                                 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2982                             break;
2983                             case "scale":
2984                                 params[attr] = values;
2985                                 from[attr] = (from[attr] + E)[split](separator);
2986                                 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2987                             break;
2988                             case "clip-rect":
2989                                 from[attr] = (from[attr] + E)[split](separator);
2990                                 diff[attr] = [];
2991                                 var i = 4;
2992                                 while (i--) {
2993                                     diff[attr][i] = (values[i] - from[attr][i]) / ms;
2994                                 }
2995                             break;
2996                         }
2997                         to[attr] = values;
2998                 }
2999             }
3000         }
3001         this.stop();
3002         this.in_animation = 1;
3003         animationElements[this.id] = {
3004             start: params.start || +new Date,
3005             ms: ms,
3006             easing: easing,
3007             from: from,
3008             diff: diff,
3009             to: to,
3010             el: this,
3011             callback: callback,
3012             t: {x: 0, y: 0}
3013         };
3014         ++animationElements[length] == 1 && animation();
3015         return this;
3016     };
3017     Element[proto].stop = function () {
3018         animationElements[this.id] && animationElements[length]--;
3019         delete animationElements[this.id];
3020         return this;
3021     };
3022     Element[proto].translate = function (x, y) {
3023         return this.attr({translation: x + " " + y});
3024     };
3025     Element[proto][toString] = function () {
3026         return "Rapha\xebl\u2019s object";
3027     };
3028     R.ae = animationElements;
3029  
3030     // Set
3031     var Set = function (items) {
3032         this.items = [];
3033         this[length] = 0;
3034         if (items) {
3035             for (var i = 0, ii = items[length]; i < ii; i++) {
3036                 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3037                     this[this.items[length]] = this.items[this.items[length]] = items[i];
3038                     this[length]++;
3039                 }
3040             }
3041         }
3042     };
3043     Set[proto][push] = function () {
3044         var item,
3045             len;
3046         for (var i = 0, ii = arguments[length]; i < ii; i++) {
3047             item = arguments[i];
3048             if (item && (item.constructor == Element || item.constructor == Set)) {
3049                 len = this.items[length];
3050                 this[len] = this.items[len] = item;
3051                 this[length]++;
3052             }
3053         }
3054         return this;
3055     };
3056     Set[proto].pop = function () {
3057         delete this[this[length]--];
3058         return this.items.pop();
3059     };
3060     for (var method in Element[proto]) if (Element[proto][has](method)) {
3061         Set[proto][method] = (function (methodname) {
3062             return function () {
3063                 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3064                     this.items[i][methodname][apply](this.items[i], arguments);
3065                 }
3066                 return this;
3067             };
3068         })(method);
3069     }
3070     Set[proto].attr = function (name, value) {
3071         if (name && R.is(name, "array") && R.is(name[0], "object")) {
3072             for (var j = 0, jj = name[length]; j < jj; j++) {
3073                 this.items[j].attr(name[j]);
3074             }
3075         } else {
3076             for (var i = 0, ii = this.items[length]; i < ii; i++) {
3077                 this.items[i].attr[apply](this.items[i], arguments);
3078             }
3079         }
3080         return this;
3081     };
3082     Set[proto].animate = function (params, ms, easing, callback) {
3083         (R.is(easing, "function") || !easing) && (callback = easing || null);
3084         var len = this.items[length],
3085             i = len,
3086             set = this,
3087             collector;
3088         callback && (collector = function () {
3089             !--len && callback.call(set);
3090         });
3091         this.items[--i].animate(params, ms, easing || collector, collector);
3092         while (i--) {
3093             this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3094         }
3095         return this;
3096     };
3097     Set[proto].insertAfter = function (el) {
3098         var i = this.items[length];
3099         while (i--) {
3100             this.items[i].insertAfter(el);
3101         }
3102         return this;
3103     };
3104     Set[proto].getBBox = function () {
3105         var x = [],
3106             y = [],
3107             w = [],
3108             h = [];
3109         for (var i = this.items[length]; i--;) {
3110             var box = this.items[i].getBBox();
3111             x[push](box.x);
3112             y[push](box.y);
3113             w[push](box.x + box.width);
3114             h[push](box.y + box.height);
3115         }
3116         x = mmin[apply](0, x);
3117         y = mmin[apply](0, y);
3118         return {
3119             x: x,
3120             y: y,
3121             width: mmax[apply](0, w) - x,
3122             height: mmax[apply](0, h) - y
3123         };
3124     };
3125     Set[proto].clone = function (s) {
3126         s = new Set;
3127         for (var i = 0, ii = this.items[length]; i < ii; i++) {
3128             s[push](this.items[i].clone());
3129         }
3130         return s;
3131     };
3132
3133     R.registerFont = function (font) {
3134         if (!font.face) {
3135             return font;
3136         }
3137         this.fonts = this.fonts || {};
3138         var fontcopy = {
3139                 w: font.w,
3140                 face: {},
3141                 glyphs: {}
3142             },
3143             family = font.face["font-family"];
3144         for (var prop in font.face) if (font.face[has](prop)) {
3145             fontcopy.face[prop] = font.face[prop];
3146         }
3147         if (this.fonts[family]) {
3148             this.fonts[family][push](fontcopy);
3149         } else {
3150             this.fonts[family] = [fontcopy];
3151         }
3152         if (!font.svg) {
3153             fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3154             for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3155                 var path = font.glyphs[glyph];
3156                 fontcopy.glyphs[glyph] = {
3157                     w: path.w,
3158                     k: {},
3159                     d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3160                             return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3161                         }) + "z"
3162                 };
3163                 if (path.k) {
3164                     for (var k in path.k) if (path[has](k)) {
3165                         fontcopy.glyphs[glyph].k[k] = path.k[k];
3166                     }
3167                 }
3168             }
3169         }
3170         return font;
3171     };
3172     Paper[proto].getFont = function (family, weight, style, stretch) {
3173         stretch = stretch || "normal";
3174         style = style || "normal";
3175         weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3176         var font = R.fonts[family];
3177         if (!font) {
3178             var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3179             for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3180                 if (name.test(fontName)) {
3181                     font = R.fonts[fontName];
3182                     break;
3183                 }
3184             }
3185         }
3186         var thefont;
3187         if (font) {
3188             for (var i = 0, ii = font[length]; i < ii; i++) {
3189                 thefont = font[i];
3190                 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3191                     break;
3192                 }
3193             }
3194         }
3195         return thefont;
3196     };
3197     Paper[proto].print = function (x, y, string, font, size, origin) {
3198         origin = origin || "middle"; // baseline|middle
3199         var out = this.set(),
3200             letters = (string + E)[split](E),
3201             shift = 0,
3202             path = E,
3203             scale;
3204         R.is(font, "string") && (font = this.getFont(font));
3205         if (font) {
3206             scale = (size || 16) / font.face["units-per-em"];
3207             var bb = font.face.bbox.split(separator),
3208                 top = +bb[0],
3209                 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3210             for (var i = 0, ii = letters[length]; i < ii; i++) {
3211                 var prev = i && font.glyphs[letters[i - 1]] || {},
3212                     curr = font.glyphs[letters[i]];
3213                 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3214                 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3215             }
3216             out.scale(scale, scale, top, height).translate(x - top, y - height);
3217         }
3218         return out;
3219     };
3220  
3221     R.format = function (token) {
3222         var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3223             rg = /\{(\d+)\}/g;
3224         token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3225             return args[++i] == null ? E : args[i];
3226         }));
3227         return token || E;
3228     };
3229     R.ninja = function () {
3230         oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3231         return R;
3232     };
3233     R.el = Element[proto];
3234     return R;
3235 })();