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