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