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