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