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