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