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