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