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