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