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