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