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