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