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