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