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