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