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