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