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