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