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