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