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