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