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