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