• 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 "rotation":
831                         o.rotate(value, true);
832                         break;
833                     case "translation":
834                         var xy = value.split(separator);
835                         o.translate(xy[0], xy[1]);
836                         break;
837                     case "scale":
838                         var xy = value.split(separator);
839                         o.scale(xy[0], xy[1]);
840                         break;
841                     case "fill":
842                         var isURL = value.match(/^url\(([^\)]+)\)$/i);
843                         if (isURL) {
844                             var el = doc.createElementNS(o.svg.svgns, "pattern");
845                             var ig = doc.createElementNS(o.svg.svgns, "image");
846                             el.id = "raphael-pattern-" + Raphael.idGenerator++;
847                             el.setAttribute("x", 0);
848                             el.setAttribute("y", 0);
849                             el.setAttribute("patternUnits", "userSpaceOnUse");
850                             ig.setAttribute("x", 0);
851                             ig.setAttribute("y", 0);
852                             ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
853                             el.appendChild(ig);
854
855                             var img = doc.createElement("img");
856                             img.style.position = "absolute";
857                             img.style.top = "-9999em";
858                             img.style.left = "-9999em";
859                             img.onload = function () {
860                                 el.setAttribute("width", this.offsetWidth);
861                                 el.setAttribute("height", this.offsetHeight);
862                                 ig.setAttribute("width", this.offsetWidth);
863                                 ig.setAttribute("height", this.offsetHeight);
864                                 doc.body.removeChild(this);
865                                 paper.safari();
866                             };
867                             doc.body.appendChild(img);
868                             img.src = isURL[1];
869                             o.svg.defs.appendChild(el);
870                             o.node.style.fill = "url(#" + el.id + ")";
871                             o.node.setAttribute("fill", "url(#" + el.id + ")");
872                             o.pattern = el;
873                             updatePosition(o);
874                             break;
875                         }
876                         delete params.gradient;
877                         delete o.attrs.gradient;
878                         if (typeof o.attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
879                             o.node.style.opacity = o.attrs.opacity;
880                             // Need following line for Firefox
881                             o.node.setAttribute("opacity", o.attrs.opacity);
882                         }
883                         if (typeof o.attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
884                             o.node.style.fillOpacity = o.attrs["fill-opacity"];
885                             // Need following line for Firefox
886                             o.node.setAttribute("fill-opacity", o.attrs["fill-opacity"]);
887                         }
888                     case "stroke":
889                         o.node.style[att] = getRGB(value).hex;
890                         // Need following line for Firefox
891                         o.node.setAttribute(att, getRGB(value).hex);
892                         break;
893                     case "gradient":
894                         addGrdientFill(o.node, value, o.svg);
895                         break;
896                     case "opacity":
897                     case "fill-opacity":
898                         if (o.attrs.gradient) {
899                             var gradient = document.getElementById(o.node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
900                             if (gradient) {
901                                 var stops = gradient.getElementsByTagName("stop");
902                                 stops[stops.length - 1].setAttribute("stop-opacity", value);
903                             }
904                             break;
905                         }
906                     default :
907                         var cssrule = att.replace(/(\-.)/g, function (w) {
908                             return w.substring(1).toUpperCase();
909                         });
910                         o.node.style[cssrule] = value;
911                         // Need following line for Firefox
912                         o.node.setAttribute(att, value);
913                         break;
914                 }
915             }
916             tuneText(o, params);
917         };
918         var Element = function (node, svg) {
919             var X = 0,
920                 Y = 0;
921             this[0] = node;
922             this.node = node;
923             this.svg = svg;
924             this.attrs = this.attrs || {};
925             this.transformations = []; // rotate, translate, scale
926             this._ = {
927                 tx: 0,
928                 ty: 0,
929                 rt: {deg: 0, x: 0, y: 0},
930                 sx: 1,
931                 sy: 1
932             };
933         };
934         Element.prototype.translate = function (x, y) {
935             if (x == undefined && y == undefined) {
936                 return {x: this._.tx, y: this._.ty};
937             }
938             this._.tx += +x;
939             this._.ty += +y;
940             switch (this.type) {
941                 case "circle":
942                 case "ellipse":
943                     this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
944                     break;
945                 case "rect":
946                 case "image":
947                 case "text":
948                     this.attr({x: this.attrs.x + +x, y: this.attrs.y + +y});
949                     break;
950                 case "path":
951                     var path = pathToRelative(this.attrs.path);
952                     path[0][1] += +x;
953                     path[0][2] += +y;
954                     this.attr({path: path.join(" ")});
955                 break;
956             }
957             return this;
958         };
959         Element.prototype.rotate = function (deg, cx, cy) {
960             if (deg == null) {
961                 return this._.rt.deg;
962             }
963             var bbox = this.getBBox();
964             deg = deg.toString().split(separator);
965             if (deg.length - 1) {
966                 cx = parseFloat(deg[1], 10);
967                 cy = parseFloat(deg[2], 10);
968             }
969             deg = parseFloat(deg[0], 10);
970             if (cx != null) {
971                 this._.rt.deg = deg;
972             } else {
973                 this._.rt.deg += deg;
974             }
975             if (cy == null) {
976                 cx = null;
977             }
978             cx = cx == null ? bbox.x + bbox.width / 2 : cx;
979             cy = cy == null ? bbox.y + bbox.height / 2 : cy;
980             if (this._.rt.deg) {
981                 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
982             } else {
983                 this.transformations[0] = "";
984             }
985             this.node.setAttribute("transform", this.transformations.join(" "));
986             return this;
987         };
988         Element.prototype.hide = function () {
989             this.node.style.display = "none";
990             return this;
991         };
992         Element.prototype.show = function () {
993             this.node.style.display = "block";
994             return this;
995         };
996         Element.prototype.remove = function () {
997             this.node.parentNode.removeChild(this.node);
998         };
999         Element.prototype.getBBox = function () {
1000             return this.node.getBBox();
1001         };
1002         Element.prototype.attr = function () {
1003             if (arguments.length == 1 && typeof arguments[0] == "string") {
1004                 if (arguments[0] == "translation") {
1005                     return this.translate();
1006                 }
1007                 return this.attrs[arguments[0]];
1008             }
1009             if (arguments.length == 1 && arguments[0] instanceof Array) {
1010                 var values = {};
1011                 for (var j in arguments[0]) {
1012                     values[arguments[0][j]] = this.attrs[arguments[0][j]];
1013                 }
1014                 return values;
1015             }
1016             if (arguments.length == 2) {
1017                 var params = {};
1018                 params[arguments[0]] = arguments[1];
1019                 setFillAndStroke(this, params);
1020             } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1021                 setFillAndStroke(this, arguments[0]);
1022             }
1023             return this;
1024         };
1025         Element.prototype.toFront = function () {
1026             this.node.parentNode.appendChild(this.node);
1027             return this;
1028         };
1029         Element.prototype.toBack = function () {
1030             if (this.node.parentNode.firstChild != this.node) {
1031                 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1032             }
1033             return this;
1034         };
1035         Element.prototype.insertAfter = function (element) {
1036             if (element.node.nextSibling) {
1037                 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1038             } else {
1039                 element.node.parentNode.appendChild(this.node);
1040             }
1041             return this;
1042         };
1043         Element.prototype.insertBefore = function (element) {
1044             element.node.parentNode.insertBefore(this.node, element.node);
1045             return this;
1046         };
1047         var theCircle = function (svg, x, y, r) {
1048             var el = doc.createElementNS(svg.svgns, "circle");
1049             el.setAttribute("cx", x);
1050             el.setAttribute("cy", y);
1051             el.setAttribute("r", r);
1052             el.setAttribute("fill", "none");
1053             el.setAttribute("stroke", "#000");
1054             if (svg.canvas) {
1055                 svg.canvas.appendChild(el);
1056             }
1057             var res = new Element(el, svg);
1058             res.attrs = res.attrs || {};
1059             res.attrs.cx = x;
1060             res.attrs.cy = y;
1061             res.attrs.r = r;
1062             res.attrs.stroke = "#000";
1063             res.type = "circle";
1064             return res;
1065         };
1066         var theRect = function (svg, x, y, w, h, r) {
1067             var el = doc.createElementNS(svg.svgns, "rect");
1068             el.setAttribute("x", x);
1069             el.setAttribute("y", y);
1070             el.setAttribute("width", w);
1071             el.setAttribute("height", h);
1072             if (r) {
1073                 el.setAttribute("rx", r);
1074                 el.setAttribute("ry", r);
1075             }
1076             el.setAttribute("fill", "none");
1077             el.setAttribute("stroke", "#000");
1078             if (svg.canvas) {
1079                 svg.canvas.appendChild(el);
1080             }
1081             var res = new Element(el, svg);
1082             res.attrs = res.attrs || {};
1083             res.attrs.x = x;
1084             res.attrs.y = y;
1085             res.attrs.width = w;
1086             res.attrs.height = h;
1087             res.attrs.stroke = "#000";
1088             if (r) {
1089                 res.attrs.rx = res.attrs.ry = r;
1090             }
1091             res.type = "rect";
1092             return res;
1093         };
1094         var theEllipse = function (svg, x, y, rx, ry) {
1095             var el = doc.createElementNS(svg.svgns, "ellipse");
1096             el.setAttribute("cx", x);
1097             el.setAttribute("cy", y);
1098             el.setAttribute("rx", rx);
1099             el.setAttribute("ry", ry);
1100             el.setAttribute("fill", "none");
1101             el.setAttribute("stroke", "#000");
1102             if (svg.canvas) {
1103                 svg.canvas.appendChild(el);
1104             }
1105             var res = new Element(el, svg);
1106             res.attrs = res.attrs || {};
1107             res.attrs.cx = x;
1108             res.attrs.cy = y;
1109             res.attrs.rx = rx;
1110             res.attrs.ry = ry;
1111             res.attrs.stroke = "#000";
1112             res.type = "ellipse";
1113             return res;
1114         };
1115         var theImage = function (svg, src, x, y, w, h) {
1116             var el = doc.createElementNS(svg.svgns, "image");
1117             el.setAttribute("x", x);
1118             el.setAttribute("y", y);
1119             el.setAttribute("width", w);
1120             el.setAttribute("height", h);
1121             el.setAttribute("preserveAspectRatio", "none");
1122             el.setAttributeNS(svg.xlink, "href", src);
1123             if (svg.canvas) {
1124                 svg.canvas.appendChild(el);
1125             }
1126             var res = new Element(el, svg);
1127             res.attrs = res.attrs || {};
1128             res.attrs.x = x;
1129             res.attrs.y = y;
1130             res.attrs.width = w;
1131             res.attrs.height = h;
1132             res.type = "image";
1133             return res;
1134         };
1135         var leading = 1.2;
1136         var tuneText = function (element, params) {
1137             if (element.type != "text") {
1138                 return;
1139             }
1140             var fontSize = element.node.firstChild ? parseInt(doc.defaultView.getComputedStyle(element.node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
1141             var height = 0;
1142             
1143             if ("text" in params) {
1144                 while (element.node.firstChild) {
1145                     element.node.removeChild(element.node.firstChild);
1146                 }
1147                 var texts = (params.text + "").split("\n");
1148                 for (var i = 0, ii = texts.length; i < ii; i++) {
1149                     var tspan = doc.createElementNS(element.svg.svgns, "tspan");
1150                     i && tspan.setAttribute("dy", fontSize * leading);
1151                     tspan.setAttribute("x", element.attrs.x);
1152                     tspan.appendChild(doc.createTextNode(texts[i]));
1153                     element.node.appendChild(tspan);
1154                     height += fontSize * leading;
1155                 }
1156             } else if ("font" in params || "font-size" in params) {
1157                 var texts = element.node.getElementsByTagName("tspan");
1158                 for (var i = 0, ii = texts.length; i < ii; i++) {
1159                     i && texts[i].setAttribute("dy", fontSize * leading);
1160                     height += fontSize * leading;
1161                 }
1162             }
1163             height -= fontSize * (leading - 1);
1164             var dif = height / 2 - fontSize;
1165             if (dif) {
1166                 element.node.setAttribute("y", element.attrs.y - dif);
1167             }
1168             setTimeout(function () {
1169             });
1170         };
1171         var theText = function (svg, x, y, text) {
1172             var el = doc.createElementNS(svg.svgns, "text");
1173             el.setAttribute("x", x);
1174             el.setAttribute("y", y);
1175             el.setAttribute("text-anchor", "middle");
1176             if (svg.canvas) {
1177                 svg.canvas.appendChild(el);
1178             }
1179             var res = new Element(el, svg);
1180             res.attrs = res.attrs || {};
1181             res.attrs.x = x;
1182             res.attrs.y = y;
1183             res.type = "text";
1184             setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000", text: text});
1185             return res;
1186         };
1187         var theGroup = function (svg) {
1188             var el = doc.createElementNS(svg.svgns, "g");
1189             if (svg.canvas) {
1190                 svg.canvas.appendChild(el);
1191             }
1192             var i = new Element(el, svg);
1193             for (var f in svg) {
1194                 if (f[0] != "_" && typeof svg[f] == "function") {
1195                     i[f] = (function (f) {
1196                         return function () {
1197                             var e = svg[f].apply(svg, arguments);
1198                             el.appendChild(e[0]);
1199                             return e;
1200                         };
1201                     })(f);
1202                 }
1203             }
1204             i.type = "group";
1205             return i;
1206         };
1207         var setSize = function (width, height) {
1208             this.width = width || this.width;
1209             this.height = height || this.height;
1210             this.canvas.setAttribute("width", this.width);
1211             this.canvas.setAttribute("height", this.height);
1212             return this;
1213         };
1214         create = function () {
1215             // container, width, height
1216             // x, y, width, height
1217             if (typeof arguments[0] == "string") {
1218                 var container = doc.getElementById(arguments[0]);
1219                 var width = arguments[1];
1220                 var height = arguments[2];
1221             }
1222             if (typeof arguments[0] == "object") {
1223                 var container = arguments[0];
1224                 var width = arguments[1];
1225                 var height = arguments[2];
1226             }
1227             if (typeof arguments[0] == "number") {
1228                 var container = 1,
1229                     x = arguments[0],
1230                     y = arguments[1],
1231                     width = arguments[2],
1232                     height = arguments[3];
1233             }
1234             if (!container) {
1235                 throw new Error("SVG container not found.");
1236             }
1237             paper.canvas = doc.createElementNS(paper.svgns, "svg");
1238             paper.canvas.setAttribute("width", width || 320);
1239             paper.width = width || 320;
1240             paper.canvas.setAttribute("height", height || 200);
1241             paper.height = height || 200;
1242             if (container == 1) {
1243                 doc.body.appendChild(paper.canvas);
1244                 paper.canvas.style.position = "absolute";
1245                 paper.canvas.style.left = x + "px";
1246                 paper.canvas.style.top = y + "px";
1247             } else {
1248                 if (container.firstChild) {
1249                     container.insertBefore(paper.canvas, container.firstChild);
1250                 } else {
1251                     container.appendChild(paper.canvas);
1252                 }
1253             }
1254             container = {
1255                 canvas: paper.canvas,
1256                 clear: function () {
1257                     while (this.canvas.firstChild) {
1258                         this.canvas.removeChild(this.canvas.firstChild);
1259                     }
1260                     this.defs = doc.createElementNS(paper.svgns, "defs");
1261                     this.canvas.appendChild(this.defs);
1262                 }
1263             };
1264             for (var prop in paper) {
1265                 if (prop != "create") {
1266                     container[prop] = paper[prop];
1267                 }
1268             }
1269             for (var prop in R.fn) {
1270                 if (!container[prop]) {
1271                     container[prop] = R.fn[prop];
1272                 }
1273             }
1274             container.clear();
1275             return container;
1276         };
1277         paper.remove = function () {
1278             this.canvas.parentNode.removeChild(this.canvas);
1279         };
1280         paper.svgns = "http://www.w3.org/2000/svg";
1281         paper.xlink = "http://www.w3.org/1999/xlink";
1282         paper.safari = function () {
1283             if (navigator.vendor == "Apple Computer, Inc.") {
1284                 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1285                 setTimeout(function () {rect.remove();}, 0);
1286             }
1287         };
1288     }
1289
1290     // VML
1291     if (R.vml) {
1292         thePath = function (params, pathString, VML) {
1293             var g = doc.createElement("rvml:group"), gl = g.style;
1294             gl.position = "absolute";
1295             gl.left = 0;
1296             gl.top = 0;
1297             gl.width = VML.width + "px";
1298             gl.height = VML.height + "px";
1299             var el = doc.createElement("rvml:shape"), ol = el.style;
1300             ol.width = VML.width + "px";
1301             ol.height = VML.height + "px";
1302             el.path = "";
1303             if (params["class"]) {
1304                 el.className = params["class"];
1305             }
1306             el.coordsize = this.coordsize;
1307             el.coordorigin = this.coordorigin;
1308             g.appendChild(el);
1309             VML.canvas.appendChild(g);
1310             var p = new Element(el, g, VML);
1311             p.isAbsolute = true;
1312             p.type = "path";
1313             p.path = [];
1314             p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1315             p.Path = "";
1316             p.absolutely = function () {
1317                 this.isAbsolute = true;
1318                 return this;
1319             };
1320             p.relatively = function () {
1321                 this.isAbsolute = false;
1322                 return this;
1323             };
1324             p.moveTo = function (x, y) {
1325                 var d = this.isAbsolute?"m":"t";
1326                 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1327                 this.node.path = this.Path += d;
1328                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1329                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1330                 this.last.isAbsolute = this.isAbsolute;
1331                 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1332                 return this;
1333             };
1334             p.lineTo = function (x, y) {
1335                 var d = this.isAbsolute?"l":"r";
1336                 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1337                 this[0].path = this.Path += d;
1338                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1339                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1340                 this.last.isAbsolute = this.isAbsolute;
1341                 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1342                 return this;
1343             };
1344             p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1345                 // for more information of where this math came from visit:
1346                 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1347                 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1348                 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1349                 var x1 = this.last.x,
1350                     y1 = this.last.y,
1351                     x = (x1 - x2) / 2,
1352                     y = (y1 - y2) / 2,
1353                     k = (large_arc_flag == sweep_flag ? -1 : 1) *
1354                         Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1355                     cx = k * rx * y / ry + (x1 + x2) / 2,
1356                     cy = k * -ry * x / rx + (y1 + y2) / 2,
1357                     d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1358                     left = Math.round(cx - rx),
1359                     top = Math.round(cy - ry);
1360                 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(", ");
1361                 this.node.path = this.Path += d;
1362                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2, 10);
1363                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2, 10);
1364                 this.last.isAbsolute = this.isAbsolute;
1365                 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1366                 return this;
1367             };
1368             p.cplineTo = function (x1, y1, w1) {
1369                 if (!w1) {
1370                     return this.lineTo(x1, y1);
1371                 } else {
1372                     var x = Math.round(Math.round(parseFloat(x1, 10) * 100) / 100),
1373                         y = Math.round(Math.round(parseFloat(y1, 10) * 100) / 100),
1374                         w = Math.round(Math.round(parseFloat(w1, 10) * 100) / 100),
1375                         d = this.isAbsolute ? "c" : "v",
1376                         attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1377                         svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1378                     d += attr.join(" ") + " ";
1379                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1380                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1381                     this.last.bx = attr[2];
1382                     this.last.by = attr[3];
1383                     this.node.path = this.Path += d;
1384                     this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1385                     return this;
1386                 }
1387             };
1388             p.curveTo = function () {
1389                 var d = this.isAbsolute ? "c" : "v";
1390                 if (arguments.length == 6) {
1391                     this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1392                     this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1393                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4], 10);
1394                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5], 10);
1395                     d += [Math.round(parseFloat(arguments[0], 10)),
1396                          Math.round(parseFloat(arguments[1], 10)),
1397                          Math.round(parseFloat(arguments[2], 10)),
1398                          Math.round(parseFloat(arguments[3], 10)),
1399                          Math.round(parseFloat(arguments[4], 10)),
1400                          Math.round(parseFloat(arguments[5], 10))].join(" ") + " ";
1401                     this.last.isAbsolute = this.isAbsolute;
1402                     this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1403                 }
1404                 if (arguments.length == 4) {
1405                     var bx = this.last.x * 2 - this.last.bx;
1406                     var by = this.last.y * 2 - this.last.by;
1407                     this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1408                     this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1409                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1410                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1411                     d += [Math.round(bx), Math.round(by),
1412                          Math.round(parseFloat(arguments[0], 10)),
1413                          Math.round(parseFloat(arguments[1], 10)),
1414                          Math.round(parseFloat(arguments[2], 10)),
1415                          Math.round(parseFloat(arguments[3], 10))].join(" ") + " ";
1416                      this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1417                 }
1418                 this.node.path = this.Path += d;
1419                 return this;
1420             };
1421             p.qcurveTo = function () {
1422                 var d = "qb";
1423                 if (arguments.length == 4) {
1424                     this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1425                     this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1426                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1427                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1428                     d += [Math.round(this.last.qx),
1429                          Math.round(this.last.qy),
1430                          Math.round(this.last.x),
1431                          Math.round(this.last.y)].join(" ") + " ";
1432                     this.last.isAbsolute = this.isAbsolute;
1433                     this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1434                 }
1435                 if (arguments.length == 2) {
1436                     this.last.qx = this.last.x * 2 - this.last.qx;
1437                     this.last.qy = this.last.y * 2 - this.last.qy;
1438                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1439                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1440                     d += [Math.round(this.last.qx),
1441                          Math.round(this.last.qy),
1442                          Math.round(this.last.x),
1443                          Math.round(this.last.y)].join(" ") + " ";
1444                      this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1445                 }
1446                 this.node.path = this.Path += d;
1447                 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1448                 return this;
1449             };
1450             p.addRoundedCorner = function (r, dir) {
1451                 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1452                 if (rollback) {
1453                     this.relatively();
1454                     rollback = function () {
1455                         o.absolutely();
1456                     };
1457                 } else {
1458                     rollback = function () {};
1459                 }
1460                 var actions = {
1461                     l: function () {
1462                         return {
1463                             u: function () {
1464                                 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1465                             },
1466                             d: function () {
1467                                 o.curveTo(-R, 0, -r, r - R, -r, r);
1468                             }
1469                         };
1470                     },
1471                     r: function () {
1472                         return {
1473                             u: function () {
1474                                 o.curveTo(R, 0, r, -(r - R), r, -r);
1475                             },
1476                             d: function () {
1477                                 o.curveTo(R, 0, r, r - R, r, r);
1478                             }
1479                         };
1480                     },
1481                     u: function () {
1482                         return {
1483                             r: function () {
1484                                 o.curveTo(0, -R, -(R - r), -r, r, -r);
1485                             },
1486                             l: function () {
1487                                 o.curveTo(0, -R, R - r, -r, -r, -r);
1488                             }
1489                         };
1490                     },
1491                     d: function () {
1492                         return {
1493                             r: function () {
1494                                 o.curveTo(0, R, -(R - r), r, r, r);
1495                             },
1496                             l: function () {
1497                                 o.curveTo(0, R, R - r, r, -r, r);
1498                             }
1499                         };
1500                     }
1501                 };
1502                 actions[dir.charAt(0)]()[dir.charAt(1)]();
1503                 rollback();
1504                 return o;
1505             };
1506             p.andClose = function () {
1507                 this.node.path = (this.Path += "x e");
1508                 this.attrs.path += "z";
1509                 return this;
1510             };
1511             if (pathString) {
1512                 p.absolutely();
1513                 p.attrs.path = "";
1514                 paper.pathfinder(p, "" + pathString);
1515             }
1516             // p.setBox();
1517             setFillAndStroke(p, params);
1518             if (params.gradient) {
1519                 addGrdientFill(p, params.gradient);
1520             }
1521             return p;
1522         };
1523         var setFillAndStroke = function (o, params) {
1524             var s = o.node.style,
1525                 res = o;
1526             o.attrs = o.attrs || {};
1527             for (var par in params) {
1528                 o.attrs[par] = params[par];
1529             }
1530             if (params.path && o.type == "path") {
1531                 o.Path = "";
1532                 o.path = [];
1533                 paper.pathfinder(o, params.path);
1534             }
1535             if (params.rotation != null) {
1536                 o.rotate(params.rotation, true);
1537             }
1538             if (params.translation) {
1539                 var xy = params.translation.split(separator);
1540                 o.translate(xy[0], xy[1]);
1541             }
1542             if (params.scale) {
1543                 var xy = params.scale.split(separator);
1544                 o.scale(xy[0], xy[1]);
1545             }
1546             if (o.type == "image" && params.src) {
1547                 o.node.src = params.src;
1548             }
1549             if (o.type == "image" && params.opacity) {
1550                 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1551                 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1552             }
1553             params.font && (s.font = params.font);
1554             params["font-family"] && (s.fontFamily = params["font-family"]);
1555             params["font-size"] && (s.fontSize = params["font-size"]);
1556             params["font-weight"] && (s.fontWeight = params["font-weight"]);
1557             params["font-style"] && (s.fontStyle = params["font-style"]);
1558             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"]) {
1559                 o = o.shape || o.node;
1560                 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || doc.createElement("rvml:fill");
1561                 if ("fill-opacity" in params || "opacity" in params) {
1562                     fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1563                 }
1564                 if (params.fill) {
1565                     fill.on = true;
1566                 }
1567                 if (typeof fill.on == "undefined" || params.fill == "none") {
1568                     fill.on = false;
1569                 }
1570                 if (fill.on && params.fill) {
1571                     var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1572                     if (isURL) {
1573                         fill.src = isURL[1];
1574                         fill.type = "tile";
1575                     } else {
1576                         fill.color = getRGB(params.fill).hex;
1577                         fill.src = "";
1578                         fill.type = "solid";
1579                     }
1580                 }
1581                 o.appendChild(fill);
1582                 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || doc.createElement("rvml:stroke");
1583                 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"]) {
1584                     stroke.on = true;
1585                 }
1586                 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1587                     stroke.on = false;
1588                 }
1589                 if (stroke.on && params.stroke) {
1590                     stroke.color = getRGB(params.stroke).hex;
1591                 }
1592                 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1593                 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1594                 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1595                 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1596                 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
1597                 if (params["stroke-dasharray"]) {
1598                     var dasharray = {
1599                         "-": "shortdash",
1600                         ".": "shortdot",
1601                         "-.": "shortdashdot",
1602                         "-..": "shortdashdotdot",
1603                         ". ": "dot",
1604                         "- ": "dash",
1605                         "--": "longdash",
1606                         "- .": "dashdot",
1607                         "--.": "longdashdot",
1608                         "--..": "longdashdotdot"
1609                     };
1610                     stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1611                 }
1612                 o.appendChild(stroke);
1613             }
1614             if (res.type == "text") {
1615                 var span = doc.createElement("span"),
1616                     s = span.style;
1617                 res.attrs.font && (s.font = res.attrs.font);
1618                 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1619                 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1620                 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1621                 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1622                 res.node.parentNode.appendChild(span);
1623                 span.innerText = res.node.string;
1624                 res.W = res.attrs.w = span.offsetWidth;
1625                 res.H = res.attrs.h = span.offsetHeight;
1626                 res.X = res.attrs.x - Math.round(res.W / 2);
1627                 res.Y = res.attrs.y - Math.round(res.H / 2);
1628                 res.node.parentNode.removeChild(span);
1629             }
1630         };
1631         var getAngle = function (a, b, c, d) {
1632             var angle = Math.round(Math.atan((parseFloat(c, 10) - parseFloat(a, 10)) / (parseFloat(d, 10) - parseFloat(b, 10))) * 57.29) || 0;
1633             if (!angle && parseFloat(a, 10) < parseFloat(b, 10)) {
1634                 angle = 180;
1635             }
1636             angle -= 180;
1637             if (angle < 0) {
1638                 angle += 360;
1639             }
1640             return angle;
1641         };
1642         var addGrdientFill = function (o, gradient) {
1643             gradient = toGradient(gradient);
1644             o.attrs = o.attrs || {};
1645             var attrs = o.attrs;
1646             o.attrs.gradient = gradient;
1647             o = o.shape || o[0];
1648             var fill = o.getElementsByTagName("fill");
1649             if (fill.length) {
1650                 fill = fill[0];
1651             } else {
1652                 fill = doc.createElement("rvml:fill");
1653             }
1654             if (gradient.dots.length) {
1655                 fill.on = true;
1656                 fill.method = "none";
1657                 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1658                 if (typeof gradient.dots[0].color != "undefined") {
1659                     fill.color = getRGB(gradient.dots[0].color).hex;
1660                 }
1661                 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1662                     fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1663                 }
1664                 var colors = [];
1665                 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1666                     if (gradient.dots[i].offset) {
1667                         colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1668                     }
1669                 };
1670                 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;
1671                 if (colors.length) {
1672                     fill.colors.value = colors.join(",");
1673                     fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1674                 } else {
1675                     fill.colors.value = "0% " + fill.color;
1676                 }
1677                 fill.opacity = fillOpacity;
1678                 if (typeof gradient.angle != "undefined") {
1679                     fill.angle = (-gradient.angle + 270) % 360;
1680                 } else if (gradient.vector) {
1681                     fill.angle = getAngle.apply(null, gradient.vector);
1682                 }
1683                 if ((gradient.type + "").toLowerCase() == "radial") {
1684                     fill.focus = "100%";
1685                     fill.focusposition = "0.5 0.5";
1686                 }
1687             }
1688         };
1689         var Element = function (node, group, vml) {
1690             var Rotation = 0,
1691                 RotX = 0,
1692                 RotY = 0,
1693                 Scale = 1;
1694             this[0] = node;
1695             this.node = node;
1696             this.X = 0;
1697             this.Y = 0;
1698             this.attrs = {};
1699             this.Group = group;
1700             this.vml = vml;
1701             this._ = {
1702                 tx: 0,
1703                 ty: 0,
1704                 rt: {deg:0},
1705                 sx: 1,
1706                 sy: 1
1707             };
1708         };
1709         Element.prototype.rotate = function (deg, cx, cy) {
1710             if (deg == null) {
1711                 return this._.rt.deg;
1712             }
1713             deg = deg.toString().split(separator);
1714             if (deg.length - 1) {
1715                 cx = parseFloat(deg[1], 10);
1716                 cy = parseFloat(deg[2], 10);
1717             }
1718             deg = parseFloat(deg[0], 10);
1719             if (cy == null) {
1720                 cx = null;
1721             }
1722             if (cx != null) {
1723                 this._.rt.deg = deg;
1724             } else {
1725                 this._.rt.deg += deg;
1726             }
1727             this._.rt.cx = cx;
1728             this._.rt.cy = cy;
1729             this.setBox(null, cx, cy);
1730             this.Group.style.rotation = this._.rt.deg;
1731             return this;
1732         };
1733         Element.prototype.setBox = function (params, cx, cy) {
1734             var gs = this.Group.style,
1735                 os = (this.shape && this.shape.style) || this.node.style;
1736             for (var i in params) {
1737                 this.attrs[i] = params[i];
1738             }
1739             cx = cx || this._.rt.cx;
1740             cy = cy || this._.rt.cy;
1741             var attr = this.attrs, x, y, w, h;
1742             switch (this.type) {
1743                 case "circle": 
1744                     x = attr.cx - attr.r;
1745                     y = attr.cy - attr.r;
1746                     w = h = attr.r * 2;
1747                     break;
1748                 case "ellipse":
1749                     x = attr.cx - attr.rx;
1750                     y = attr.cy - attr.ry;
1751                     w = attr.rx * 2;
1752                     h = attr.ry * 2;
1753                     break;
1754                 case "rect":
1755                 case "image":
1756                     x = attr.x;
1757                     y = attr.y;
1758                     w = attr.width || 0;
1759                     h = attr.height || 0;
1760                     break;
1761                 case "text":
1762                     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("");
1763                     x = attr.x - Math.round(this.W / 2);
1764                     y = attr.y - this.H / 2;
1765                     w = this.W;
1766                     h = this.H;
1767                     break;
1768                 case "path":
1769                     if (!this.attrs.path) {
1770                         x = 0;
1771                         y = 0;
1772                         w = this.vml.width;
1773                         h = this.vml.height;
1774                     } else {
1775                         var dim = pathDimensions(this.attrs.path),
1776                         x = dim.x;
1777                         y = dim.y;
1778                         w = dim.width;
1779                         h = dim.height;
1780                     }
1781                     break;
1782                 default:
1783                     x = 0;
1784                     y = 0;
1785                     w = this.vml.width;
1786                     h = this.vml.height;
1787                     break;
1788             }
1789             cx = (cx == null) ? x + w / 2 : cx;
1790             cy = (cy == null) ? y + h / 2 : cy;
1791             var left = cx - this.vml.width / 2,
1792                 top = cy - this.vml.height / 2;
1793             if (this.type == "path" || this.type == "text") {
1794                 gs.left = left + "px";
1795                 gs.top = top + "px";
1796                 this.X = this.type == "text" ? x : -left;
1797                 this.Y = this.type == "text" ? y : -top;
1798                 this.W = w;
1799                 this.H = h;
1800                 os.left = -left + "px";
1801                 os.top = -top + "px";
1802             } else {
1803                 gs.left = left + "px";
1804                 gs.top = top + "px";
1805                 this.X = x;
1806                 this.Y = y;
1807                 this.W = w;
1808                 this.H = h;
1809                 gs.width = this.vml.width + "px";
1810                 gs.height = this.vml.height + "px";
1811                 os.left = x - left + "px";
1812                 os.top = y - top + "px";
1813                 os.width = w + "px";
1814                 os.height = h + "px";
1815             }
1816         };
1817         Element.prototype.hide = function () {
1818             this.Group.style.display = "none";
1819             return this;
1820         };
1821         Element.prototype.show = function () {
1822             this.Group.style.display = "block";
1823             return this;
1824         };
1825         Element.prototype.translate = function (x, y) {
1826             if (x == undefined && y == undefined) {
1827                 return {x: this._.tx, y: this._.ty};
1828             }
1829             this._.tx += +x;
1830             this._.ty += +y;
1831             if (this.type == "path") {
1832                 var path = this.attrs.path;
1833                 path = pathToRelative(path);
1834                 path[0][1] += +x;
1835                 path[0][2] += +y;
1836                 this.attr({path: path.join(" ")});
1837             }
1838             this.setBox({x: this._.tx, y: this._.ty});
1839             return this;
1840         };
1841         Element.prototype.getBBox = function () {
1842             return {
1843                 x: this.X,
1844                 y: this.Y,
1845                 width: this.W,
1846                 height: this.H
1847             };
1848         };
1849         Element.prototype.remove = function () {
1850             this[0].parentNode.removeChild(this[0]);
1851             this.Group.parentNode.removeChild(this.Group);
1852             this.shape && this.shape.parentNode.removeChild(this.shape);
1853         };
1854         Element.prototype.attr = function () {
1855             if (arguments.length == 1 && typeof arguments[0] == "string") {
1856                 if (arguments[0] == "translation") {
1857                     return this.translate();
1858                 }
1859                 return this.attrs[arguments[0]];
1860             }
1861             if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
1862                 var values = {};
1863                 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1864                     values[arguments[0][i]] = this.attrs[arguments[0][i]];
1865                 };
1866                 return values;
1867             }
1868             var params;
1869             if (arguments.length == 2) {
1870                 params = {};
1871                 params[arguments[0]] = arguments[1];
1872             }
1873             if (arguments.length == 1 && typeof arguments[0] == "object") {
1874                 params = arguments[0];
1875             }
1876             if (params) {
1877                 if (params.gradient) {
1878                     addGrdientFill(this, params.gradient);
1879                 }
1880                 if (params.text && this.type == "text") {
1881                     this.node.string = params.text;
1882                 }
1883                 if (params.id) {
1884                     this.node.id = params.id;
1885                 }
1886                 setFillAndStroke(this, params);
1887                 this.setBox(params);
1888             }
1889             return this;
1890         };
1891         Element.prototype.toFront = function () {
1892             this.Group.parentNode.appendChild(this.Group);
1893             return this;
1894         };
1895         Element.prototype.toBack = function () {
1896             if (this.Group.parentNode.firstChild != this.Group) {
1897                 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1898             }
1899             return this;
1900         };
1901         Element.prototype.insertAfter = function (element) {
1902             if (element.Group.nextSibling) {
1903                 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1904             } else {
1905                 element.Group.parentNode.appendChild(this.Group);
1906             }
1907             return this;
1908         };
1909         Element.prototype.insertBefore = function (element) {
1910             element.Group.parentNode.insertBefore(this.Group, element.Group);
1911             return this;
1912         };
1913         var theCircle = function (vml, x, y, r) {
1914             var g = doc.createElement("rvml:group");
1915             var o = doc.createElement("rvml:oval");
1916             g.appendChild(o);
1917             vml.canvas.appendChild(g);
1918             var res = new Element(o, g, vml);
1919             res.type = "circle";
1920             setFillAndStroke(res, {stroke: "#000", fill: "none"});
1921             res.attrs.cx = x;
1922             res.attrs.cy = y;
1923             res.attrs.r = r;
1924             res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1925             return res;
1926         };
1927         var theRect = function (vml, x, y, w, h, r) {
1928             var g = doc.createElement("rvml:group");
1929             var o = doc.createElement(r ? "rvml:roundrect" : "rvml:rect");
1930             if (r) {
1931                 o.arcsize = r / (Math.min(w, h));
1932             }
1933             g.appendChild(o);
1934             vml.canvas.appendChild(g);
1935             var res = new Element(o, g, vml);
1936             res.type = "rect";
1937             setFillAndStroke(res, {stroke: "#000"});
1938             res.attrs.x = x;
1939             res.attrs.y = y;
1940             res.attrs.w = w;
1941             res.attrs.h = h;
1942             res.attrs.r = r;
1943             res.setBox({x: x, y: y, width: w, height: h});
1944             return res;
1945         };
1946         var theEllipse = function (vml, x, y, rx, ry) {
1947             var g = doc.createElement("rvml:group");
1948             var o = doc.createElement("rvml:oval");
1949             g.appendChild(o);
1950             vml.canvas.appendChild(g);
1951             var res = new Element(o, g, vml);
1952             res.type = "ellipse";
1953             setFillAndStroke(res, {stroke: "#000"});
1954             res.attrs.cx = x;
1955             res.attrs.cy = y;
1956             res.attrs.rx = rx;
1957             res.attrs.ry = ry;
1958             res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
1959             return res;
1960         };
1961         var theImage = function (vml, src, x, y, w, h) {
1962             var g = doc.createElement("rvml:group");
1963             var o = doc.createElement("rvml:image");
1964             o.src = src;
1965             g.appendChild(o);
1966             vml.canvas.appendChild(g);
1967             var res = new Element(o, g, vml);
1968             res.type = "image";
1969             res.attrs.x = x;
1970             res.attrs.y = y;
1971             res.attrs.w = w;
1972             res.attrs.h = h;
1973             res.setBox({x: x, y: y, width: w, height: h});
1974             return res;
1975         };
1976         var theText = function (vml, x, y, text) {
1977             var g = doc.createElement("rvml:group"), gs = g.style;
1978             var el = doc.createElement("rvml:shape"), ol = el.style;
1979             var path = doc.createElement("rvml:path"), ps = path.style;
1980             path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
1981             path.textpathok = true;
1982             ol.width = vml.width;
1983             ol.height = vml.height;
1984             gs.position = "absolute";
1985             gs.left = 0;
1986             gs.top = 0;
1987             gs.width = vml.width;
1988             gs.height = vml.height;
1989             var o = doc.createElement("rvml:textpath");
1990             o.string = text;
1991             o.on = true;
1992             o.coordsize = vml.coordsize;
1993             o.coordorigin = vml.coordorigin;
1994             el.appendChild(o);
1995             el.appendChild(path);
1996             g.appendChild(el);
1997             vml.canvas.appendChild(g);
1998             var res = new Element(o, g, vml);
1999             res.shape = el;
2000             res.textpath = path;
2001             res.type = "text";
2002             res.attrs.x = x;
2003             res.attrs.y = y;
2004             res.attrs.w = 1;
2005             res.attrs.h = 1;
2006             setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000"});
2007             return res;
2008         };
2009         var setSize = function (width, height) {
2010             this.width = width || this.width;
2011             this.height = height || this.height;
2012             this.canvas.style.width = this.width + "px";
2013             this.canvas.style.height = this.height + "px";
2014             this.canvas.parentNode.style.clip = "rect(0 " + this.width + " " + this.height + " 0)";
2015             this.canvas.coordsize = this.width + " " + this.height;
2016             return this;
2017         };
2018         create = function () {
2019             // container, width, height
2020             // x, y, width, height
2021             var container, width, height;
2022             if (typeof arguments[0] == "string") {
2023                 container = doc.getElementById(arguments[0]);
2024                 width = arguments[1];
2025                 height = arguments[2];
2026             }
2027             if (typeof arguments[0] == "object") {
2028                 container = arguments[0];
2029                 width = arguments[1];
2030                 height = arguments[2];
2031             }
2032             if (typeof arguments[0] == "number") {
2033                 container = 1;
2034                 x = arguments[0];
2035                 y = arguments[1];
2036                 width = arguments[2];
2037                 height = arguments[3];
2038             }
2039             if (!container) {
2040                 throw new Error("VML container not found.");
2041             }
2042             if (!doc.namespaces["rvml"]) {
2043                 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2044                 doc.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
2045             }
2046             var c = doc.createElement("div"),
2047                 d = doc.createElement("div"),
2048                 r = paper.canvas = doc.createElement("rvml:group"),
2049                 cs = c.style, rs = r.style;
2050             paper.width = width;
2051             paper.height = height;
2052             width = width || "320px";
2053             height = height || "200px";
2054             cs.clip = "rect(0 " + width + " " + height + " 0)";
2055             cs.top = "-2px";
2056             cs.left = "-2px";
2057             cs.position = "absolute";
2058             rs.position = "absolute";
2059             d.style.position = "relative";
2060             rs.width  = width;
2061             rs.height = height;
2062             r.coordsize = (/%$/.test(width) ? width : parseFloat(width, 10)) + " " + (/%$/.test(height) ? height : parseFloat(height, 10));
2063             r.coordorigin = "0 0";
2064
2065             var b = doc.createElement("rvml:rect"), bs = b.style;
2066             bs.left = bs.top = 0;
2067             bs.width  = rs.width;
2068             bs.height = rs.height;
2069             b.filled = b.stroked = "f";
2070
2071             r.appendChild(b);
2072             c.appendChild(r);
2073             d.appendChild(c);
2074             if (container == 1) {
2075                 doc.body.appendChild(d);
2076                 cs.position = "absolute";
2077                 cs.left = x + "px";
2078                 cs.top = y + "px";
2079                 cs.width = width;
2080                 cs.height = height;
2081                 container = {
2082                     style: {
2083                         width: width,
2084                         height: height
2085                     }
2086                 };
2087             } else {
2088                 cs.width = container.style.width = width;
2089                 cs.height = container.style.height = height;
2090                 if (container.firstChild) {
2091                     container.insertBefore(d, container.firstChild);
2092                 } else {
2093                     container.appendChild(d);
2094                 }
2095             }
2096             for (var prop in paper) {
2097                 container[prop] = paper[prop];
2098             }
2099             for (var prop in R.fn) {
2100                 if (!container[prop]) {
2101                     container[prop] = R.fn[prop];
2102                 }
2103             }
2104             container.clear = function () {
2105                 var todel = [];
2106                 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2107                     if (r.childNodes[i] != b) {
2108                         todel.push(r.childNodes[i]);
2109                     }
2110                 }
2111                 for (i = 0, ii = todel.length; i < ii; i++) {
2112                     r.removeChild(todel[i]);
2113                 }
2114             };
2115             return container;
2116         };
2117         paper.remove = function () {
2118             this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2119         };
2120         paper.safari = function () {};
2121     }
2122
2123     // rest
2124
2125     // Events
2126     var addEvent = (function () {
2127         if (doc.addEventListener) {
2128             return function (obj, type, fn, element) {
2129                 obj.addEventListener(type, function (e) {
2130                     return fn.call(element, e);
2131                 }, false);
2132             };
2133         } else if (doc.attachEvent) {
2134             return function (obj, type, fn, element) {
2135                 obj.attachEvent("on" + type, function (e) {
2136                     return fn.call(element, e || win.event);
2137                 });
2138             };
2139         }
2140     })();
2141     var events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
2142     for (var i = events.length; i--;) {
2143         (function (eventName) {
2144             Element.prototype[eventName] = function (fn) {
2145                 (typeof fn == "function") && addEvent(this.node, eventName, fn, this);
2146                 return this;
2147             };
2148         })(events[i]);
2149     }
2150     paper.circle = function (x, y, r) {
2151         return theCircle(this, x, y, r);
2152     };
2153     paper.rect = function (x, y, w, h, r) {
2154         return theRect(this, x, y, w, h, r);
2155     };
2156     paper.ellipse = function (x, y, rx, ry) {
2157         return theEllipse(this, x, y, rx, ry);
2158     };
2159     paper.path = function (params, pathString) {
2160         return thePath(params, pathString, this);
2161     };
2162     paper.image = function (src, x, y, w, h) {
2163         return theImage(this, src, x, y, w, h);
2164     };
2165     paper.text = function (x, y, text) {
2166         return theText(this, x, y, text);
2167     };
2168     paper.group = function () {
2169         return this;
2170     };
2171     paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2172         color = color || "#000";
2173         var p = this.path({stroke: color, "stroke-width": 1})
2174                 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y),
2175             rowHeight = h / hv,
2176             columnWidth = w / wv;
2177         for (var i = 1; i < hv; i++) {
2178             p.moveTo(x, y + i * rowHeight).lineTo(x + w, y + i * rowHeight);
2179         }
2180         for (var i = 1; i < wv; i++) {
2181             p.moveTo(x + i * columnWidth, y).lineTo(x + i * columnWidth, y + h);
2182         }
2183         return p;
2184     };
2185     paper.pathfinder = function (p, path) {
2186         var commands = {
2187             M: function (x, y) {
2188                 this.moveTo(x, y);
2189             },
2190             C: function (x1, y1, x2, y2, x3, y3) {
2191                 this.curveTo(x1, y1, x2, y2, x3, y3);
2192             },
2193             Q: function (x1, y1, x2, y2) {
2194                 this.qcurveTo(x1, y1, x2, y2);
2195             },
2196             T: function (x, y) {
2197                 this.qcurveTo(x, y);
2198             },
2199             S: function (x1, y1, x2, y2) {
2200                 p.curveTo(x1, y1, x2, y2);
2201             },
2202             L: function (x, y) {
2203                 p.lineTo(x, y);
2204             },
2205             H: function (x) {
2206                 this.lineTo(x, this.last.y);
2207             },
2208             V: function (y) {
2209                 this.lineTo(this.last.x, y);
2210             },
2211             A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2212                 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2213             },
2214             Z: function () {
2215                 this.andClose();
2216             }
2217         };
2218
2219         path = pathToAbsolute(path);
2220         for (var i = 0, ii = path.length; i < ii; i++) {
2221             var b = path[i].shift();
2222             commands[b].apply(p, path[i]);
2223         }
2224     };
2225     paper.set = function (itemsArray) {
2226         return new Set(itemsArray);
2227     };
2228     paper.setSize = setSize;
2229     Element.prototype.stop = function () {
2230         clearTimeout(this.animation_in_progress);
2231     };
2232     Element.prototype.scale = function (x, y) {
2233         if (x == undefined && y == undefined) {
2234             return {x: this._.sx, y: this._.sy};
2235         }
2236         y = y || x;
2237         var dx, dy, cx, cy;
2238         if (x != 0) {
2239             var dirx = Math.round(x / Math.abs(x)),
2240                 diry = Math.round(y / Math.abs(y)),
2241                 s = this.node.style;
2242             dx = this.attr("x");
2243             dy = this.attr("y");
2244             cx = this.attr("cx");
2245             cy = this.attr("cy");
2246             if (dirx != 1 || diry != 1) {
2247                 if (this.transformations) {
2248                     this.transformations[2] = "scale(" + [dirx, diry] + ")";
2249                     this.node.setAttribute("transform", this.transformations.join(" "));
2250                     dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2251                     dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2252                     cx = this.attr("cx") * dirx;
2253                     cy = this.attr("cy") * diry;
2254                 } else {
2255                     this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2256                         ", M12=0, M21=0, M22=" + diry +
2257                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2258                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2259                 }
2260             } else {
2261                 if (this.transformations) {
2262                     this.transformations[2] = "";
2263                     this.node.setAttribute("transform", this.transformations.join(" "));
2264                 } else {
2265                     this.node.filterMatrix = "";
2266                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2267                 }
2268             }
2269             switch (this.type) {
2270                 case "rect":
2271                 case "image":
2272                     this.attr({
2273                         width: this.attrs.width * x * dirx / this._.sx,
2274                         height: this.attrs.height * y * diry / this._.sy,
2275                         x: dx,
2276                         y: dy
2277                     });
2278                     break;
2279                 case "circle":
2280                 case "ellipse":
2281                     this.attr({
2282                         rx: this.attrs.rx * x * dirx / this._.sx,
2283                         ry: this.attrs.ry * y * diry / this._.sy,
2284                         r: this.attrs.r * x * diry / this._.sx,
2285                         cx: cx,
2286                         cy: cy
2287                     });
2288                     break;
2289                 case "path":
2290                     var path = pathToRelative(Raphael.parsePathString(this.attr("path"))), 
2291                         skip = true,
2292                         dim = pathDimensions(this.attrs.path),
2293                         dx = -dim.width * (x - 1) / 2,
2294                         dy = -dim.height * (y - 1) / 2;
2295                     for (var i = 0, ii = path.length; i < ii; i++) {
2296                         if (path[i][0].toUpperCase() == "M" && skip) {
2297                             continue;
2298                         } else {
2299                             skip = false;
2300                         }
2301                         if (path[i][0].toUpperCase() == "A") {
2302                             path[i][path[i].length - 2] *= x * dirx;
2303                             path[i][path[i].length - 1] *= y * diry;
2304                         } else {
2305                             for (var j = 1, jj = path[i].length; j < jj; j++) {
2306                                 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2307                             }
2308                         }
2309                     }
2310                     var dim2 = pathDimensions(path),
2311                         dx = dim.x + dim.width / 2 - dim2.x - dim2.width / 2,
2312                         dy = dim.y + dim.height / 2 - dim2.y - dim2.height / 2;
2313                     path = pathToRelative(path);
2314                     path[0][1] += dx;
2315                     path[0][2] += dy;
2316                     
2317                     this.attr({path: path.join(" ")});
2318             }
2319         }
2320         this._.sx = x;
2321         this._.sy = y;
2322         return this;
2323     };
2324     Element.prototype.animate = function (params, ms, callback) {
2325         clearTimeout(this.animation_in_progress);
2326         var from = {},
2327             to = {},
2328             diff = {},
2329             t = {x: 0, y: 0};
2330         for (var attr in params) {
2331             if (attr in availableAnimAttrs) {
2332                 from[attr] = this.attr(attr);
2333                 if (typeof from[attr] == "undefined") {
2334                     from[attr] = availableAttrs[attr];
2335                 }
2336                 to[attr] = params[attr];
2337                 switch (availableAnimAttrs[attr]) {
2338                     case "number":
2339                         diff[attr] = (to[attr] - from[attr]) / ms;
2340                         break;
2341                     case "colour":
2342                         from[attr] = getRGB(from[attr]);
2343                         var toColour = getRGB(to[attr]);
2344                         diff[attr] = {
2345                             r: (toColour.r - from[attr].r) / ms,
2346                             g: (toColour.g - from[attr].g) / ms,
2347                             b: (toColour.b - from[attr].b) / ms
2348                         };
2349                         break;
2350                     case "path":
2351                         var pathes = pathEqualiser(from[attr], to[attr]);
2352                         from[attr] = pathes[0];
2353                         to[attr] = pathes[1];
2354                         diff[attr] = [];
2355                         for (var i = 0, ii = from[attr].length; i < ii; i++) {
2356                             diff[attr][i] = [0];
2357                             for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2358                                 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2359                             }
2360                         }
2361                         break;
2362                     case "csv":
2363                         var values = params[attr].toString().split(separator),
2364                             from2 = from[attr].toString().split(separator);
2365                         if (attr == "translation") {
2366                             from[attr] = [0, 0];
2367                             diff[attr] = [values[0] / ms, values[1] / ms];
2368                         } else if (attr == "rotation") {
2369                             from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2370                             diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2371                         } else {
2372                             from[attr] = from[attr].split(separator);
2373                             diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2374                         }
2375                         to[attr] = values;
2376                 }
2377             }
2378         }
2379         var start = new Date(),
2380             prev = 0,
2381             that = this;
2382         (function () {
2383             var time = (new Date()).getTime() - start.getTime(),
2384                 set = {},
2385                 now;
2386             if (time < ms) {
2387                 for (var attr in from) {
2388                     switch (availableAnimAttrs[attr]) {
2389                         case "number":
2390                             now = +from[attr] + time * diff[attr];
2391                             break;
2392                         case "colour":
2393                             now = "rgb(" + [
2394                                 Math.round(from[attr].r + time * diff[attr].r),
2395                                 Math.round(from[attr].g + time * diff[attr].g),
2396                                 Math.round(from[attr].b + time * diff[attr].b)
2397                             ].join(",") + ")";
2398                             break;
2399                         case "path":
2400                             now = [];
2401                             for (var i = 0, ii = from[attr].length; i < ii; i++) {
2402                                 now[i] = [from[attr][i][0]];
2403                                 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2404                                     now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2405                                 }
2406                                 now[i] = now[i].join(" ");
2407                             }
2408                             now = now.join(" ");
2409                             break;
2410                         case "csv":
2411                             if (attr == "translation") {
2412                                 var x = diff[attr][0] * (time - prev),
2413                                     y = diff[attr][1] * (time - prev);
2414                                 t.x += x;
2415                                 t.y += y;
2416                                 now = [x, y].join(" ");
2417                             } else if (attr == "rotation") {
2418                                 now = +from[attr][0] + time * diff[attr][0];
2419                                 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2420                             } else {
2421                                 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2422                             }
2423                             break;
2424                     }
2425                     if (attr == "font-size") {
2426                         set[attr] = now + "px";
2427                     } else {
2428                         set[attr] = now;
2429                     }
2430                 }
2431                 that.attr(set);
2432                 that.animation_in_progress = setTimeout(arguments.callee, 0);
2433                 paper.safari();
2434             } else {
2435                 (t.x || t.y) && that.translate(-t.x, -t.y);
2436                 that.attr(params);
2437                 clearTimeout(that.animation_in_progress);
2438                 paper.safari();
2439                 (typeof callback == "function") && callback.call(that);
2440             }
2441             prev = time;
2442         })();
2443         return this;
2444     };
2445
2446     // Set
2447     var Set = function (itemsArray) {
2448         this.items = [];
2449         if (itemsArray && itemsArray.constructor == Array) {
2450             for (var i = itemsArray.length; i--;) {
2451                 if (itemsArray[i].constructor == Element) {
2452                     this.items[this.items.length] = itemsArray[i];
2453                 }
2454             }
2455         }
2456     };
2457     Set.prototype.push = function (item) {
2458         if (item && item.constructor == Element) {
2459             var len = this.items.length;
2460             this.items[len] = item;
2461             this[len] = item;
2462         }
2463         return this;
2464     };
2465     Set.prototype.pull = function (id) {
2466         var res = this.items.splice(id, 1)[0];
2467         for (var j = id, jj = this.items.length; j < jj; j++) {
2468             this[j] = this[j + 1];
2469         }
2470         delete this[jj + 1];
2471         return res;
2472     };
2473     for (var method in Element.prototype) {
2474         Set.prototype[method] = (function (methodname) {
2475             return function () {
2476                 for (var i = this.items.length; i--;) {
2477                     this.items[i][methodname].apply(this.items[i], arguments);
2478                 }
2479                 return this;
2480             };
2481         })(method);
2482     }
2483     Set.prototype.getBBox = function () {
2484         var x = [], y = [], w = [], h = [];
2485         for (var i = this.items.length; i--;) {
2486             var box = this.items[i].getBBox();
2487             x.push(box.x);
2488             y.push(box.y);
2489             w.push(box.x + box.width);
2490             h.push(box.y + box.height);
2491         }
2492         x = Math.min.apply(Math, x);
2493         y = Math.min.apply(Math, y);
2494         return {
2495             x: x,
2496             y: y,
2497             width: Math.max.apply(Math, w) - x,
2498             height: Math.max.apply(Math, h) - y
2499         };
2500     };
2501
2502     return R;
2503 })();