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