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