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