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