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