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