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