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