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