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