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