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