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