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