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