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