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