Add sdnr wt devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / resources / elasticsearch / plugins / head / src / vendor / graphael / g.raphael.standalone.js
1 /*!
2  * Raphael 1.5.2 - JavaScript Vector Library
3  *
4  * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://raphaeljs.com/license.html) license.
6  * from fork at git@github.com:mobz/g.raphael.git
7  */
8 (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[has](j.type) && res[push](cnv[j.type]().attr(j));
17             }
18             return res;
19         }
20         return create[apply](R, arguments);
21     }
22     R.version = "1.5.2";
23     var separator = /[, ]+/,
24         elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
25         formatrg = /\{(\d+)\}/g,
26         proto = "prototype",
27         has = "hasOwnProperty",
28         doc = document,
29         win = window,
30         oldRaphael = {
31             was: Object[proto][has].call(win, "Raphael"),
32             is: win.Raphael
33         },
34         Paper = function () {
35             this.customAttributes = {};
36         },
37         paperproto,
38         appendChild = "appendChild",
39         apply = "apply",
40         concat = "concat",
41         supportsTouch = "createTouch" in doc,
42         E = "",
43         S = " ",
44         Str = String,
45         split = "split",
46         events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
47         touchMap = {
48             mousedown: "touchstart",
49             mousemove: "touchmove",
50             mouseup: "touchend"
51         },
52         join = "join",
53         length = "length",
54         lowerCase = Str[proto].toLowerCase,
55         math = Math,
56         mmax = math.max,
57         mmin = math.min,
58         abs = math.abs,
59         pow = math.pow,
60         PI = math.PI,
61         nu = "number",
62         string = "string",
63         array = "array",
64         toString = "toString",
65         fillString = "fill",
66         objectToString = Object[proto][toString],
67         paper = {},
68         push = "push",
69         ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
70         colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
71         isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
72         bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
73         round = math.round,
74         setAttribute = "setAttribute",
75         toFloat = parseFloat,
76         toInt = parseInt,
77         ms = " progid:DXImageTransform.Microsoft",
78         upperCase = Str[proto].toUpperCase,
79         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},
80         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},
81         rp = "replace",
82         animKeyFrames= /^(from|to|\d+%?)$/,
83         commaSpaces = /\s*,\s*/,
84         hsrg = {hs: 1, rg: 1},
85         p2s = /,?([achlmqrstvxz]),?/gi,
86         pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
87         pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
88         radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
89         sortByKey = function (a, b) {
90             return a.key - b.key;
91         };
92
93     R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
94     if (R.type == "VML") {
95         var d = doc.createElement("div"),
96             b;
97         d.innerHTML = '<v:shape adj="1"/>';
98         b = d.firstChild;
99         b.style.behavior = "url(#default#VML)";
100         if (!(b && typeof b.adj == "object")) {
101             return R.type = null;
102         }
103         d = null;
104     }
105     R.svg = !(R.vml = R.type == "VML");
106     Paper[proto] = R[proto];
107     paperproto = Paper[proto];
108     R._id = 0;
109     R._oid = 0;
110     R.fn = {};
111     R.is = function (o, type) {
112         type = lowerCase.call(type);
113         if (type == "finite") {
114             return !isnan[has](+o);
115         }
116         return  (type == "null" && o === null) ||
117                 (type == typeof o) ||
118                 (type == "object" && o === Object(o)) ||
119                 (type == "array" && Array.isArray && Array.isArray(o)) ||
120                 objectToString.call(o).slice(8, -1).toLowerCase() == type;
121     };
122     R.angle = function (x1, y1, x2, y2, x3, y3) {
123         if (x3 == null) {
124             var x = x1 - x2,
125                 y = y1 - y2;
126             if (!x && !y) {
127                 return 0;
128             }
129             return ((x < 0) * 180 + math.atan(-y / -x) * 180 / PI + 360) % 360;
130         } else {
131             return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
132         }
133     };
134     R.rad = function (deg) {
135         return deg % 360 * PI / 180;
136     };
137     R.deg = function (rad) {
138         return rad * 180 / PI % 360;
139     };
140     R.snapTo = function (values, value, tolerance) {
141         tolerance = R.is(tolerance, "finite") ? tolerance : 10;
142         if (R.is(values, array)) {
143             var i = values.length;
144             while (i--) if (abs(values[i] - value) <= tolerance) {
145                 return values[i];
146             }
147         } else {
148             values = +values;
149             var rem = value % values;
150             if (rem < tolerance) {
151                 return value - rem;
152             }
153             if (rem > values - tolerance) {
154                 return value - rem + values;
155             }
156         }
157         return value;
158     };
159     function createUUID() {
160         // http://www.ietf.org/rfc/rfc4122.txt
161         var s = [],
162             i = 0;
163         for (; i < 32; i++) {
164             s[i] = (~~(math.random() * 16))[toString](16);
165         }
166         s[12] = 4;  // bits 12-15 of the time_hi_and_version field to 0010
167         s[16] = ((s[16] & 3) | 8)[toString](16);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
168         return "r-" + s[join]("");
169     }
170
171     R.setWindow = function (newwin) {
172         win = newwin;
173         doc = win.document;
174     };
175     // colour utilities
176     var toHex = function (color) {
177         if (R.vml) {
178             // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
179             var trim = /^\s+|\s+$/g;
180             var bod;
181             try {
182                 var docum = new ActiveXObject("htmlfile");
183                 docum.write("<body>");
184                 docum.close();
185                 bod = docum.body;
186             } catch(e) {
187                 bod = createPopup().document.body;
188             }
189             var range = bod.createTextRange();
190             toHex = cacher(function (color) {
191                 try {
192                     bod.style.color = Str(color)[rp](trim, E);
193                     var value = range.queryCommandValue("ForeColor");
194                     value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
195                     return "#" + ("000000" + value[toString](16)).slice(-6);
196                 } catch(e) {
197                     return "none";
198                 }
199             });
200         } else {
201             var i = doc.createElement("i");
202             i.title = "Rapha\xebl Colour Picker";
203             i.style.display = "none";
204             doc.body[appendChild](i);
205             toHex = cacher(function (color) {
206                 i.style.color = color;
207                 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
208             });
209         }
210         return toHex(color);
211     },
212     hsbtoString = function () {
213         return "hsb(" + [this.h, this.s, this.b] + ")";
214     },
215     hsltoString = function () {
216         return "hsl(" + [this.h, this.s, this.l] + ")";
217     },
218     rgbtoString = function () {
219         return this.hex;
220     };
221     R.hsb2rgb = function (h, s, b, o) {
222         if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
223             b = h.b;
224             s = h.s;
225             h = h.h;
226             o = h.o;
227         }
228         return R.hsl2rgb(h, s, b / 2, o);
229     };
230     R.hsl2rgb = function (h, s, l, o) {
231         if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
232             l = h.l;
233             s = h.s;
234             h = h.h;
235         }
236         if (h > 1 || s > 1 || l > 1) {
237             h /= 360;
238             s /= 100;
239             l /= 100;
240         }
241         var rgb = {},
242             channels = ["r", "g", "b"],
243             t2, t1, t3, r, g, b;
244         if (!s) {
245             rgb = {
246                 r: l,
247                 g: l,
248                 b: l
249             };
250         } else {
251             if (l < .5) {
252                 t2 = l * (1 + s);
253             } else {
254                 t2 = l + s - l * s;
255             }
256             t1 = 2 * l - t2;
257             for (var i = 0; i < 3; i++) {
258                 t3 = h + 1 / 3 * -(i - 1);
259                 t3 < 0 && t3++;
260                 t3 > 1 && t3--;
261                 if (t3 * 6 < 1) {
262                     rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
263                 } else if (t3 * 2 < 1) {
264                     rgb[channels[i]] = t2;
265                 } else if (t3 * 3 < 2) {
266                     rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
267                 } else {
268                     rgb[channels[i]] = t1;
269                 }
270             }
271         }
272         rgb.r *= 255;
273         rgb.g *= 255;
274         rgb.b *= 255;
275         rgb.hex = "#" + (16777216 | rgb.b | (rgb.g << 8) | (rgb.r << 16)).toString(16).slice(1);
276         R.is(o, "finite") && (rgb.opacity = o);
277         rgb.toString = rgbtoString;
278         return rgb;
279     };
280     R.rgb2hsb = function (red, green, blue) {
281         if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
282             blue = red.b;
283             green = red.g;
284             red = red.r;
285         }
286         if (green == null && R.is(red, string)) {
287             var clr = R.getRGB(red);
288             red = clr.r;
289             green = clr.g;
290             blue = clr.b;
291         }
292         if (red > 1 || green > 1 || blue > 1) {
293             red /= 255;
294             green /= 255;
295             blue /= 255;
296         }
297         var max = mmax(red, green, blue),
298             min = mmin(red, green, blue),
299             hue,
300             saturation,
301             brightness = max;
302         if (min == max) {
303             return {h: 0, s: 0, b: max, toString: hsbtoString};
304         } else {
305             var delta = (max - min);
306             saturation = delta / max;
307             if (red == max) {
308                 hue = (green - blue) / delta;
309             } else if (green == max) {
310                 hue = 2 + ((blue - red) / delta);
311             } else {
312                 hue = 4 + ((red - green) / delta);
313             }
314             hue /= 6;
315             hue < 0 && hue++;
316             hue > 1 && hue--;
317         }
318         return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
319     };
320     R.rgb2hsl = function (red, green, blue) {
321         if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
322             blue = red.b;
323             green = red.g;
324             red = red.r;
325         }
326         if (green == null && R.is(red, string)) {
327             var clr = R.getRGB(red);
328             red = clr.r;
329             green = clr.g;
330             blue = clr.b;
331         }
332         if (red > 1 || green > 1 || blue > 1) {
333             red /= 255;
334             green /= 255;
335             blue /= 255;
336         }
337         var max = mmax(red, green, blue),
338             min = mmin(red, green, blue),
339             h,
340             s,
341             l = (max + min) / 2,
342             hsl;
343         if (min == max) {
344             hsl =  {h: 0, s: 0, l: l};
345         } else {
346             var delta = max - min;
347             s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
348             if (red == max) {
349                 h = (green - blue) / delta;
350             } else if (green == max) {
351                 h = 2 + (blue - red) / delta;
352             } else {
353                 h = 4 + (red - green) / delta;
354             }
355             h /= 6;
356             h < 0 && h++;
357             h > 1 && h--;
358             hsl = {h: h, s: s, l: l};
359         }
360         hsl.toString = hsltoString;
361         return hsl;
362     };
363     R._path2string = function () {
364         return this.join(",")[rp](p2s, "$1");
365     };
366     function cacher(f, scope, postprocessor) {
367         function newf() {
368             var arg = Array[proto].slice.call(arguments, 0),
369                 args = arg[join]("\u25ba"),
370                 cache = newf.cache = newf.cache || {},
371                 count = newf.count = newf.count || [];
372             if (cache[has](args)) {
373                 return postprocessor ? postprocessor(cache[args]) : cache[args];
374             }
375             count[length] >= 1e3 && delete cache[count.shift()];
376             count[push](args);
377             cache[args] = f[apply](scope, arg);
378             return postprocessor ? postprocessor(cache[args]) : cache[args];
379         }
380         return newf;
381     }
382  
383     R.getRGB = cacher(function (colour) {
384         if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
385             return {r: -1, g: -1, b: -1, hex: "none", error: 1};
386         }
387         if (colour == "none") {
388             return {r: -1, g: -1, b: -1, hex: "none"};
389         }
390         !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
391         var res,
392             red,
393             green,
394             blue,
395             opacity,
396             t,
397             values,
398             rgb = colour.match(colourRegExp);
399         if (rgb) {
400             if (rgb[2]) {
401                 blue = toInt(rgb[2].substring(5), 16);
402                 green = toInt(rgb[2].substring(3, 5), 16);
403                 red = toInt(rgb[2].substring(1, 3), 16);
404             }
405             if (rgb[3]) {
406                 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
407                 green = toInt((t = rgb[3].charAt(2)) + t, 16);
408                 red = toInt((t = rgb[3].charAt(1)) + t, 16);
409             }
410             if (rgb[4]) {
411                 values = rgb[4][split](commaSpaces);
412                 red = toFloat(values[0]);
413                 values[0].slice(-1) == "%" && (red *= 2.55);
414                 green = toFloat(values[1]);
415                 values[1].slice(-1) == "%" && (green *= 2.55);
416                 blue = toFloat(values[2]);
417                 values[2].slice(-1) == "%" && (blue *= 2.55);
418                 rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
419                 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
420             }
421             if (rgb[5]) {
422                 values = rgb[5][split](commaSpaces);
423                 red = toFloat(values[0]);
424                 values[0].slice(-1) == "%" && (red *= 2.55);
425                 green = toFloat(values[1]);
426                 values[1].slice(-1) == "%" && (green *= 2.55);
427                 blue = toFloat(values[2]);
428                 values[2].slice(-1) == "%" && (blue *= 2.55);
429                 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
430                 rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
431                 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
432                 return R.hsb2rgb(red, green, blue, opacity);
433             }
434             if (rgb[6]) {
435                 values = rgb[6][split](commaSpaces);
436                 red = toFloat(values[0]);
437                 values[0].slice(-1) == "%" && (red *= 2.55);
438                 green = toFloat(values[1]);
439                 values[1].slice(-1) == "%" && (green *= 2.55);
440                 blue = toFloat(values[2]);
441                 values[2].slice(-1) == "%" && (blue *= 2.55);
442                 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
443                 rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
444                 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
445                 return R.hsl2rgb(red, green, blue, opacity);
446             }
447             rgb = {r: red, g: green, b: blue};
448             rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
449             R.is(opacity, "finite") && (rgb.opacity = opacity);
450             return rgb;
451         }
452         return {r: -1, g: -1, b: -1, hex: "none", error: 1};
453     }, R);
454     R.getColor = function (value) {
455         var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
456             rgb = this.hsb2rgb(start.h, start.s, start.b);
457         start.h += .075;
458         if (start.h > 1) {
459             start.h = 0;
460             start.s -= .2;
461             start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
462         }
463         return rgb.hex;
464     };
465     R.getColor.reset = function () {
466         delete this.start;
467     };
468     // path utilities
469     R.parsePathString = cacher(function (pathString) {
470         if (!pathString) {
471             return null;
472         }
473         var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
474             data = [];
475         if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
476             data = pathClone(pathString);
477         }
478         if (!data[length]) {
479             Str(pathString)[rp](pathCommand, function (a, b, c) {
480                 var params = [],
481                     name = lowerCase.call(b);
482                 c[rp](pathValues, function (a, b) {
483                     b && params[push](+b);
484                 });
485                 if (name == "m" && params[length] > 2) {
486                     data[push]([b][concat](params.splice(0, 2)));
487                     name = "l";
488                     b = b == "m" ? "l" : "L";
489                 }
490                 while (params[length] >= paramCounts[name]) {
491                     data[push]([b][concat](params.splice(0, paramCounts[name])));
492                     if (!paramCounts[name]) {
493                         break;
494                     }
495                 }
496             });
497         }
498         data[toString] = R._path2string;
499         return data;
500     });
501     R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
502         var t1 = 1 - t,
503             x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
504             y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
505             mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
506             my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
507             nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
508             ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
509             ax = (1 - t) * p1x + t * c1x,
510             ay = (1 - t) * p1y + t * c1y,
511             cx = (1 - t) * c2x + t * p2x,
512             cy = (1 - t) * c2y + t * p2y,
513             alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / PI);
514         (mx > nx || my < ny) && (alpha += 180);
515         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};
516     };
517     var pathDimensions = cacher(function (path) {
518         if (!path) {
519             return {x: 0, y: 0, width: 0, height: 0};
520         }
521         path = path2curve(path);
522         var x = 0, 
523             y = 0,
524             X = [],
525             Y = [],
526             p;
527         for (var i = 0, ii = path[length]; i < ii; i++) {
528             p = path[i];
529             if (p[0] == "M") {
530                 x = p[1];
531                 y = p[2];
532                 X[push](x);
533                 Y[push](y);
534             } else {
535                 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
536                 X = X[concat](dim.min.x, dim.max.x);
537                 Y = Y[concat](dim.min.y, dim.max.y);
538                 x = p[5];
539                 y = p[6];
540             }
541         }
542         var xmin = mmin[apply](0, X),
543             ymin = mmin[apply](0, Y);
544         return {
545             x: xmin,
546             y: ymin,
547             width: mmax[apply](0, X) - xmin,
548             height: mmax[apply](0, Y) - ymin
549         };
550     }),
551         pathClone = function (pathArray) {
552             var res = [];
553             if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
554                 pathArray = R.parsePathString(pathArray);
555             }
556             for (var i = 0, ii = pathArray[length]; i < ii; i++) {
557                 res[i] = [];
558                 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
559                     res[i][j] = pathArray[i][j];
560                 }
561             }
562             res[toString] = R._path2string;
563             return res;
564         },
565         pathToRelative = cacher(function (pathArray) {
566             if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
567                 pathArray = R.parsePathString(pathArray);
568             }
569             var res = [],
570                 x = 0,
571                 y = 0,
572                 mx = 0,
573                 my = 0,
574                 start = 0;
575             if (pathArray[0][0] == "M") {
576                 x = pathArray[0][1];
577                 y = pathArray[0][2];
578                 mx = x;
579                 my = y;
580                 start++;
581                 res[push](["M", x, y]);
582             }
583             for (var i = start, ii = pathArray[length]; i < ii; i++) {
584                 var r = res[i] = [],
585                     pa = pathArray[i];
586                 if (pa[0] != lowerCase.call(pa[0])) {
587                     r[0] = lowerCase.call(pa[0]);
588                     switch (r[0]) {
589                         case "a":
590                             r[1] = pa[1];
591                             r[2] = pa[2];
592                             r[3] = pa[3];
593                             r[4] = pa[4];
594                             r[5] = pa[5];
595                             r[6] = +(pa[6] - x).toFixed(3);
596                             r[7] = +(pa[7] - y).toFixed(3);
597                             break;
598                         case "v":
599                             r[1] = +(pa[1] - y).toFixed(3);
600                             break;
601                         case "m":
602                             mx = pa[1];
603                             my = pa[2];
604                         default:
605                             for (var j = 1, jj = pa[length]; j < jj; j++) {
606                                 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
607                             }
608                     }
609                 } else {
610                     r = res[i] = [];
611                     if (pa[0] == "m") {
612                         mx = pa[1] + x;
613                         my = pa[2] + y;
614                     }
615                     for (var k = 0, kk = pa[length]; k < kk; k++) {
616                         res[i][k] = pa[k];
617                     }
618                 }
619                 var len = res[i][length];
620                 switch (res[i][0]) {
621                     case "z":
622                         x = mx;
623                         y = my;
624                         break;
625                     case "h":
626                         x += +res[i][len - 1];
627                         break;
628                     case "v":
629                         y += +res[i][len - 1];
630                         break;
631                     default:
632                         x += +res[i][len - 2];
633                         y += +res[i][len - 1];
634                 }
635             }
636             res[toString] = R._path2string;
637             return res;
638         }, 0, pathClone),
639         pathToAbsolute = cacher(function (pathArray) {
640             if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
641                 pathArray = R.parsePathString(pathArray);
642             }
643             var res = [],
644                 x = 0,
645                 y = 0,
646                 mx = 0,
647                 my = 0,
648                 start = 0;
649             if (pathArray[0][0] == "M") {
650                 x = +pathArray[0][1];
651                 y = +pathArray[0][2];
652                 mx = x;
653                 my = y;
654                 start++;
655                 res[0] = ["M", x, y];
656             }
657             for (var i = start, ii = pathArray[length]; i < ii; i++) {
658                 var r = res[i] = [],
659                     pa = pathArray[i];
660                 if (pa[0] != upperCase.call(pa[0])) {
661                     r[0] = upperCase.call(pa[0]);
662                     switch (r[0]) {
663                         case "A":
664                             r[1] = pa[1];
665                             r[2] = pa[2];
666                             r[3] = pa[3];
667                             r[4] = pa[4];
668                             r[5] = pa[5];
669                             r[6] = +(pa[6] + x);
670                             r[7] = +(pa[7] + y);
671                             break;
672                         case "V":
673                             r[1] = +pa[1] + y;
674                             break;
675                         case "H":
676                             r[1] = +pa[1] + x;
677                             break;
678                         case "M":
679                             mx = +pa[1] + x;
680                             my = +pa[2] + y;
681                         default:
682                             for (var j = 1, jj = pa[length]; j < jj; j++) {
683                                 r[j] = +pa[j] + ((j % 2) ? x : y);
684                             }
685                     }
686                 } else {
687                     for (var k = 0, kk = pa[length]; k < kk; k++) {
688                         res[i][k] = pa[k];
689                     }
690                 }
691                 switch (r[0]) {
692                     case "Z":
693                         x = mx;
694                         y = my;
695                         break;
696                     case "H":
697                         x = r[1];
698                         break;
699                     case "V":
700                         y = r[1];
701                         break;
702                     case "M":
703                         mx = res[i][res[i][length] - 2];
704                         my = res[i][res[i][length] - 1];
705                     default:
706                         x = res[i][res[i][length] - 2];
707                         y = res[i][res[i][length] - 1];
708                 }
709             }
710             res[toString] = R._path2string;
711             return res;
712         }, null, pathClone),
713         l2c = function (x1, y1, x2, y2) {
714             return [x1, y1, x2, y2, x2, y2];
715         },
716         q2c = function (x1, y1, ax, ay, x2, y2) {
717             var _13 = 1 / 3,
718                 _23 = 2 / 3;
719             return [
720                     _13 * x1 + _23 * ax,
721                     _13 * y1 + _23 * ay,
722                     _13 * x2 + _23 * ax,
723                     _13 * y2 + _23 * ay,
724                     x2,
725                     y2
726                 ];
727         },
728         a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
729             // for more information of where this math came from visit:
730             // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
731             var _120 = PI * 120 / 180,
732                 rad = PI / 180 * (+angle || 0),
733                 res = [],
734                 xy,
735                 rotate = cacher(function (x, y, rad) {
736                     var X = x * math.cos(rad) - y * math.sin(rad),
737                         Y = x * math.sin(rad) + y * math.cos(rad);
738                     return {x: X, y: Y};
739                 });
740             if (!recursive) {
741                 xy = rotate(x1, y1, -rad);
742                 x1 = xy.x;
743                 y1 = xy.y;
744                 xy = rotate(x2, y2, -rad);
745                 x2 = xy.x;
746                 y2 = xy.y;
747                 var cos = math.cos(PI / 180 * angle),
748                     sin = math.sin(PI / 180 * angle),
749                     x = (x1 - x2) / 2,
750                     y = (y1 - y2) / 2;
751                 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
752                 if (h > 1) {
753                     h = math.sqrt(h);
754                     rx = h * rx;
755                     ry = h * ry;
756                 }
757                 var rx2 = rx * rx,
758                     ry2 = ry * ry,
759                     k = (large_arc_flag == sweep_flag ? -1 : 1) *
760                         math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
761                     cx = k * rx * y / ry + (x1 + x2) / 2,
762                     cy = k * -ry * x / rx + (y1 + y2) / 2,
763                     f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
764                     f2 = math.asin(((y2 - cy) / ry).toFixed(9));
765
766                 f1 = x1 < cx ? PI - f1 : f1;
767                 f2 = x2 < cx ? PI - f2 : f2;
768                 f1 < 0 && (f1 = PI * 2 + f1);
769                 f2 < 0 && (f2 = PI * 2 + f2);
770                 if (sweep_flag && f1 > f2) {
771                     f1 = f1 - PI * 2;
772                 }
773                 if (!sweep_flag && f2 > f1) {
774                     f2 = f2 - PI * 2;
775                 }
776             } else {
777                 f1 = recursive[0];
778                 f2 = recursive[1];
779                 cx = recursive[2];
780                 cy = recursive[3];
781             }
782             var df = f2 - f1;
783             if (abs(df) > _120) {
784                 var f2old = f2,
785                     x2old = x2,
786                     y2old = y2;
787                 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
788                 x2 = cx + rx * math.cos(f2);
789                 y2 = cy + ry * math.sin(f2);
790                 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
791             }
792             df = f2 - f1;
793             var c1 = math.cos(f1),
794                 s1 = math.sin(f1),
795                 c2 = math.cos(f2),
796                 s2 = math.sin(f2),
797                 t = math.tan(df / 4),
798                 hx = 4 / 3 * rx * t,
799                 hy = 4 / 3 * ry * t,
800                 m1 = [x1, y1],
801                 m2 = [x1 + hx * s1, y1 - hy * c1],
802                 m3 = [x2 + hx * s2, y2 - hy * c2],
803                 m4 = [x2, y2];
804             m2[0] = 2 * m1[0] - m2[0];
805             m2[1] = 2 * m1[1] - m2[1];
806             if (recursive) {
807                 return [m2, m3, m4][concat](res);
808             } else {
809                 res = [m2, m3, m4][concat](res)[join]()[split](",");
810                 var newres = [];
811                 for (var i = 0, ii = res[length]; i < ii; i++) {
812                     newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
813                 }
814                 return newres;
815             }
816         },
817         findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
818             var t1 = 1 - t;
819             return {
820                 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
821                 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
822             };
823         },
824         curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
825             var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
826                 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
827                 c = p1x - c1x,
828                 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
829                 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
830                 y = [p1y, p2y],
831                 x = [p1x, p2x],
832                 dot;
833             abs(t1) > "1e12" && (t1 = .5);
834             abs(t2) > "1e12" && (t2 = .5);
835             if (t1 > 0 && t1 < 1) {
836                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
837                 x[push](dot.x);
838                 y[push](dot.y);
839             }
840             if (t2 > 0 && t2 < 1) {
841                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
842                 x[push](dot.x);
843                 y[push](dot.y);
844             }
845             a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
846             b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
847             c = p1y - c1y;
848             t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
849             t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
850             abs(t1) > "1e12" && (t1 = .5);
851             abs(t2) > "1e12" && (t2 = .5);
852             if (t1 > 0 && t1 < 1) {
853                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
854                 x[push](dot.x);
855                 y[push](dot.y);
856             }
857             if (t2 > 0 && t2 < 1) {
858                 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
859                 x[push](dot.x);
860                 y[push](dot.y);
861             }
862             return {
863                 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
864                 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
865             };
866         }),
867         path2curve = cacher(function (path, path2) {
868             var p = pathToAbsolute(path),
869                 p2 = path2 && pathToAbsolute(path2),
870                 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
871                 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
872                 processPath = function (path, d) {
873                     var nx, ny;
874                     if (!path) {
875                         return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
876                     }
877                     !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
878                     switch (path[0]) {
879                         case "M":
880                             d.X = path[1];
881                             d.Y = path[2];
882                             break;
883                         case "A":
884                             path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
885                             break;
886                         case "S":
887                             nx = d.x + (d.x - (d.bx || d.x));
888                             ny = d.y + (d.y - (d.by || d.y));
889                             path = ["C", nx, ny][concat](path.slice(1));
890                             break;
891                         case "T":
892                             d.qx = d.x + (d.x - (d.qx || d.x));
893                             d.qy = d.y + (d.y - (d.qy || d.y));
894                             path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
895                             break;
896                         case "Q":
897                             d.qx = path[1];
898                             d.qy = path[2];
899                             path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
900                             break;
901                         case "L":
902                             path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
903                             break;
904                         case "H":
905                             path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
906                             break;
907                         case "V":
908                             path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
909                             break;
910                         case "Z":
911                             path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
912                             break;
913                     }
914                     return path;
915                 },
916                 fixArc = function (pp, i) {
917                     if (pp[i][length] > 7) {
918                         pp[i].shift();
919                         var pi = pp[i];
920                         while (pi[length]) {
921                             pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
922                         }
923                         pp.splice(i, 1);
924                         ii = mmax(p[length], p2 && p2[length] || 0);
925                     }
926                 },
927                 fixM = function (path1, path2, a1, a2, i) {
928                     if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
929                         path2.splice(i, 0, ["M", a2.x, a2.y]);
930                         a1.bx = 0;
931                         a1.by = 0;
932                         a1.x = path1[i][1];
933                         a1.y = path1[i][2];
934                         ii = mmax(p[length], p2 && p2[length] || 0);
935                     }
936                 };
937             for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
938                 p[i] = processPath(p[i], attrs);
939                 fixArc(p, i);
940                 p2 && (p2[i] = processPath(p2[i], attrs2));
941                 p2 && fixArc(p2, i);
942                 fixM(p, p2, attrs, attrs2, i);
943                 fixM(p2, p, attrs2, attrs, i);
944                 var seg = p[i],
945                     seg2 = p2 && p2[i],
946                     seglen = seg[length],
947                     seg2len = p2 && seg2[length];
948                 attrs.x = seg[seglen - 2];
949                 attrs.y = seg[seglen - 1];
950                 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
951                 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
952                 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
953                 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
954                 attrs2.x = p2 && seg2[seg2len - 2];
955                 attrs2.y = p2 && seg2[seg2len - 1];
956             }
957             return p2 ? [p, p2] : p;
958         }, null, pathClone),
959         parseDots = cacher(function (gradient) {
960             var dots = [];
961             for (var i = 0, ii = gradient[length]; i < ii; i++) {
962                 var dot = {},
963                     par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
964                 dot.color = R.getRGB(par[1]);
965                 if (dot.color.error) {
966                     return null;
967                 }
968                 dot.color = dot.color.hex;
969                 par[2] && (dot.offset = par[2] + "%");
970                 dots[push](dot);
971             }
972             for (i = 1, ii = dots[length] - 1; i < ii; i++) {
973                 if (!dots[i].offset) {
974                     var start = toFloat(dots[i - 1].offset || 0),
975                         end = 0;
976                     for (var j = i + 1; j < ii; j++) {
977                         if (dots[j].offset) {
978                             end = dots[j].offset;
979                             break;
980                         }
981                     }
982                     if (!end) {
983                         end = 100;
984                         j = ii;
985                     }
986                     end = toFloat(end);
987                     var d = (end - start) / (j - i + 1);
988                     for (; i < j; i++) {
989                         start += d;
990                         dots[i].offset = start + "%";
991                     }
992                 }
993             }
994             return dots;
995         }),
996         getContainer = function (x, y, w, h) {
997             var container;
998             if (R.is(x, string) || R.is(x, "object")) {
999                 container = R.is(x, string) ? doc.getElementById(x) : x;
1000                 if (container.tagName) {
1001                     if (y == null) {
1002                         return {
1003                             container: container,
1004                             width: container.style.pixelWidth || container.offsetWidth,
1005                             height: container.style.pixelHeight || container.offsetHeight
1006                         };
1007                     } else {
1008                         return {container: container, width: y, height: w};
1009                     }
1010                 }
1011             } else {
1012                 return {container: 1, x: x, y: y, width: w, height: h};
1013             }
1014         },
1015         plugins = function (con, add) {
1016             var that = this;
1017             for (var prop in add) {
1018                 if (add[has](prop) && !(prop in con)) {
1019                     switch (typeof add[prop]) {
1020                         case "function":
1021                             (function (f) {
1022                                 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1023                             })(add[prop]);
1024                         break;
1025                         case "object":
1026                             con[prop] = con[prop] || {};
1027                             plugins.call(this, con[prop], add[prop]);
1028                         break;
1029                         default:
1030                             con[prop] = add[prop];
1031                         break;
1032                     }
1033                 }
1034             }
1035         },
1036         tear = function (el, paper) {
1037             el == paper.top && (paper.top = el.prev);
1038             el == paper.bottom && (paper.bottom = el.next);
1039             el.next && (el.next.prev = el.prev);
1040             el.prev && (el.prev.next = el.next);
1041         },
1042         tofront = function (el, paper) {
1043             if (paper.top === el) {
1044                 return;
1045             }
1046             tear(el, paper);
1047             el.next = null;
1048             el.prev = paper.top;
1049             paper.top.next = el;
1050             paper.top = el;
1051         },
1052         toback = function (el, paper) {
1053             if (paper.bottom === el) {
1054                 return;
1055             }
1056             tear(el, paper);
1057             el.next = paper.bottom;
1058             el.prev = null;
1059             paper.bottom.prev = el;
1060             paper.bottom = el;
1061         },
1062         insertafter = function (el, el2, paper) {
1063             tear(el, paper);
1064             el2 == paper.top && (paper.top = el);
1065             el2.next && (el2.next.prev = el);
1066             el.next = el2.next;
1067             el.prev = el2;
1068             el2.next = el;
1069         },
1070         insertbefore = function (el, el2, paper) {
1071             tear(el, paper);
1072             el2 == paper.bottom && (paper.bottom = el);
1073             el2.prev && (el2.prev.next = el);
1074             el.prev = el2.prev;
1075             el2.prev = el;
1076             el.next = el2;
1077         },
1078         removed = function (methodname) {
1079             return function () {
1080                 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1081             };
1082         };
1083     R.pathToRelative = pathToRelative;
1084     // SVG
1085     if (R.svg) {
1086         paperproto.svgns = "http://www.w3.org/2000/svg";
1087         paperproto.xlink = "http://www.w3.org/1999/xlink";
1088         round = function (num) {
1089             return +num + (~~num === num) * .5;
1090         };
1091         var $ = function (el, attr) {
1092             if (attr) {
1093                 for (var key in attr) {
1094                     if (attr[has](key)) {
1095                         el[setAttribute](key, Str(attr[key]));
1096                     }
1097                 }
1098             } else {
1099                 el = doc.createElementNS(paperproto.svgns, el);
1100                 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1101                 return el;
1102             }
1103         };
1104         R[toString] = function () {
1105             return  "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1106         };
1107         var thePath = function (pathString, SVG) {
1108             var el = $("path");
1109             SVG.canvas && SVG.canvas[appendChild](el);
1110             var p = new Element(el, SVG);
1111             p.type = "path";
1112             setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1113             return p;
1114         };
1115         var addGradientFill = function (o, gradient, SVG) {
1116             var type = "linear",
1117                 fx = .5, fy = .5,
1118                 s = o.style;
1119             gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1120                 type = "radial";
1121                 if (_fx && _fy) {
1122                     fx = toFloat(_fx);
1123                     fy = toFloat(_fy);
1124                     var dir = ((fy > .5) * 2 - 1);
1125                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1126                         (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1127                         fy != .5 &&
1128                         (fy = fy.toFixed(5) - 1e-5 * dir);
1129                 }
1130                 return E;
1131             });
1132             gradient = gradient[split](/\s*\-\s*/);
1133             if (type == "linear") {
1134                 var angle = gradient.shift();
1135                 angle = -toFloat(angle);
1136                 if (isNaN(angle)) {
1137                     return null;
1138                 }
1139                 var vector = [0, 0, math.cos(angle * PI / 180), math.sin(angle * PI / 180)],
1140                     max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1141                 vector[2] *= max;
1142                 vector[3] *= max;
1143                 if (vector[2] < 0) {
1144                     vector[0] = -vector[2];
1145                     vector[2] = 0;
1146                 }
1147                 if (vector[3] < 0) {
1148                     vector[1] = -vector[3];
1149                     vector[3] = 0;
1150                 }
1151             }
1152             var dots = parseDots(gradient);
1153             if (!dots) {
1154                 return null;
1155             }
1156             var id = o.getAttribute(fillString);
1157             id = id.match(/^url\(#(.*)\)$/);
1158             id && SVG.defs.removeChild(doc.getElementById(id[1]));
1159
1160             var el = $(type + "Gradient");
1161             el.id = createUUID();
1162             $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1163             SVG.defs[appendChild](el);
1164             for (var i = 0, ii = dots[length]; i < ii; i++) {
1165                 var stop = $("stop");
1166                 $(stop, {
1167                     offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1168                     "stop-color": dots[i].color || "#fff"
1169                 });
1170                 el[appendChild](stop);
1171             }
1172             $(o, {
1173                 fill: "url(#" + el.id + ")",
1174                 opacity: 1,
1175                 "fill-opacity": 1
1176             });
1177             s.fill = E;
1178             s.opacity = 1;
1179             s.fillOpacity = 1;
1180             return 1;
1181         };
1182         var updatePosition = function (o) {
1183             var bbox = o.getBBox();
1184             $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1185         };
1186         var setFillAndStroke = function (o, params) {
1187             var dasharray = {
1188                     "": [0],
1189                     "none": [0],
1190                     "-": [3, 1],
1191                     ".": [1, 1],
1192                     "-.": [3, 1, 1, 1],
1193                     "-..": [3, 1, 1, 1, 1, 1],
1194                     ". ": [1, 3],
1195                     "- ": [4, 3],
1196                     "--": [8, 3],
1197                     "- .": [4, 3, 1, 3],
1198                     "--.": [8, 3, 1, 3],
1199                     "--..": [8, 3, 1, 3, 1, 3]
1200                 },
1201                 node = o.node,
1202                 attrs = o.attrs,
1203                 rot = o.rotate(),
1204                 addDashes = function (o, value) {
1205                     value = dasharray[lowerCase.call(value)];
1206                     if (value) {
1207                         var width = o.attrs["stroke-width"] || "1",
1208                             butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1209                             dashes = [];
1210                         var i = value[length];
1211                         while (i--) {
1212                             dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1213                         }
1214                         $(node, {"stroke-dasharray": dashes[join](",")});
1215                     }
1216                 };
1217             params[has]("rotation") && (rot = params.rotation);
1218             var rotxy = Str(rot)[split](separator);
1219             if (!(rotxy.length - 1)) {
1220                 rotxy = null;
1221             } else {
1222                 rotxy[1] = +rotxy[1];
1223                 rotxy[2] = +rotxy[2];
1224             }
1225             toFloat(rot) && o.rotate(0, true);
1226             for (var att in params) {
1227                 if (params[has](att)) {
1228                     if (!availableAttrs[has](att)) {
1229                         continue;
1230                     }
1231                     var value = params[att];
1232                     attrs[att] = value;
1233                     switch (att) {
1234                         case "blur":
1235                             o.blur(value);
1236                             break;
1237                         case "rotation":
1238                             o.rotate(value, true);
1239                             break;
1240                         case "href":
1241                         case "title":
1242                         case "target":
1243                             var pn = node.parentNode;
1244                             if (lowerCase.call(pn.tagName) != "a") {
1245                                 var hl = $("a");
1246                                 pn.insertBefore(hl, node);
1247                                 hl[appendChild](node);
1248                                 pn = hl;
1249                             }
1250                             if (att == "target" && value == "blank") {
1251                                 pn.setAttributeNS(o.paper.xlink, "show", "new");
1252                             } else {
1253                                 pn.setAttributeNS(o.paper.xlink, att, value);
1254                             }
1255                             break;
1256                         case "cursor":
1257                             node.style.cursor = value;
1258                             break;
1259                         case "clip-rect":
1260                             var rect = Str(value)[split](separator);
1261                             if (rect[length] == 4) {
1262                                 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1263                                 var el = $("clipPath"),
1264                                     rc = $("rect");
1265                                 el.id = createUUID();
1266                                 $(rc, {
1267                                     x: rect[0],
1268                                     y: rect[1],
1269                                     width: rect[2],
1270                                     height: rect[3]
1271                                 });
1272                                 el[appendChild](rc);
1273                                 o.paper.defs[appendChild](el);
1274                                 $(node, {"clip-path": "url(#" + el.id + ")"});
1275                                 o.clip = rc;
1276                             }
1277                             if (!value) {
1278                                 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1279                                 clip && clip.parentNode.removeChild(clip);
1280                                 $(node, {"clip-path": E});
1281                                 delete o.clip;
1282                             }
1283                         break;
1284                         case "path":
1285                             if (o.type == "path") {
1286                                 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1287                             }
1288                             break;
1289                         case "width":
1290                             node[setAttribute](att, value);
1291                             if (attrs.fx) {
1292                                 att = "x";
1293                                 value = attrs.x;
1294                             } else {
1295                                 break;
1296                             }
1297                         case "x":
1298                             if (attrs.fx) {
1299                                 value = -attrs.x - (attrs.width || 0);
1300                             }
1301                         case "rx":
1302                             if (att == "rx" && o.type == "rect") {
1303                                 break;
1304                             }
1305                         case "cx":
1306                             rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1307                             node[setAttribute](att, value);
1308                             o.pattern && updatePosition(o);
1309                             break;
1310                         case "height":
1311                             node[setAttribute](att, value);
1312                             if (attrs.fy) {
1313                                 att = "y";
1314                                 value = attrs.y;
1315                             } else {
1316                                 break;
1317                             }
1318                         case "y":
1319                             if (attrs.fy) {
1320                                 value = -attrs.y - (attrs.height || 0);
1321                             }
1322                         case "ry":
1323                             if (att == "ry" && o.type == "rect") {
1324                                 break;
1325                             }
1326                         case "cy":
1327                             rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1328                             node[setAttribute](att, value);
1329                             o.pattern && updatePosition(o);
1330                             break;
1331                         case "r":
1332                             if (o.type == "rect") {
1333                                 $(node, {rx: value, ry: value});
1334                             } else {
1335                                 node[setAttribute](att, value);
1336                             }
1337                             break;
1338                         case "src":
1339                             if (o.type == "image") {
1340                                 node.setAttributeNS(o.paper.xlink, "href", value);
1341                             }
1342                             break;
1343                         case "stroke-width":
1344                             node.style.strokeWidth = value;
1345                             // Need following line for Firefox
1346                             node[setAttribute](att, value);
1347                             if (attrs["stroke-dasharray"]) {
1348                                 addDashes(o, attrs["stroke-dasharray"]);
1349                             }
1350                             break;
1351                         case "stroke-dasharray":
1352                             addDashes(o, value);
1353                             break;
1354                         case "translation":
1355                             var xy = Str(value)[split](separator);
1356                             xy[0] = +xy[0] || 0;
1357                             xy[1] = +xy[1] || 0;
1358                             if (rotxy) {
1359                                 rotxy[1] += xy[0];
1360                                 rotxy[2] += xy[1];
1361                             }
1362                             translate.call(o, xy[0], xy[1]);
1363                             break;
1364                         case "scale":
1365                             xy = Str(value)[split](separator);
1366                             o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1367                             break;
1368                         case fillString:
1369                             var isURL = Str(value).match(ISURL);
1370                             if (isURL) {
1371                                 el = $("pattern");
1372                                 var ig = $("image");
1373                                 el.id = createUUID();
1374                                 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1375                                 $(ig, {x: 0, y: 0});
1376                                 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1377                                 el[appendChild](ig);
1378  
1379                                 var img = doc.createElement("img");
1380                                 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1381                                 img.onload = function () {
1382                                     $(el, {width: this.offsetWidth, height: this.offsetHeight});
1383                                     $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1384                                     doc.body.removeChild(this);
1385                                     o.paper.safari();
1386                                 };
1387                                 doc.body[appendChild](img);
1388                                 img.src = isURL[1];
1389                                 o.paper.defs[appendChild](el);
1390                                 node.style.fill = "url(#" + el.id + ")";
1391                                 $(node, {fill: "url(#" + el.id + ")"});
1392                                 o.pattern = el;
1393                                 o.pattern && updatePosition(o);
1394                                 break;
1395                             }
1396                             var clr = R.getRGB(value);
1397                             if (!clr.error) {
1398                                 delete params.gradient;
1399                                 delete attrs.gradient;
1400                                 !R.is(attrs.opacity, "undefined") &&
1401                                     R.is(params.opacity, "undefined") &&
1402                                     $(node, {opacity: attrs.opacity});
1403                                 !R.is(attrs["fill-opacity"], "undefined") &&
1404                                     R.is(params["fill-opacity"], "undefined") &&
1405                                     $(node, {"fill-opacity": attrs["fill-opacity"]});
1406                             } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1407                                 attrs.gradient = value;
1408                                 attrs.fill = "none";
1409                                 break;
1410                             }
1411                             clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1412                         case "stroke":
1413                             clr = R.getRGB(value);
1414                             node[setAttribute](att, clr.hex);
1415                             att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1416                             break;
1417                         case "gradient":
1418                             (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1419                             break;
1420                         case "opacity":
1421                             if (attrs.gradient && !attrs[has]("stroke-opacity")) {
1422                                 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
1423                             }
1424                             // fall
1425                         case "fill-opacity":
1426                             if (attrs.gradient) {
1427                                 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1428                                 if (gradient) {
1429                                     var stops = gradient.getElementsByTagName("stop");
1430                                     stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1431                                 }
1432                                 break;
1433                             }
1434                         default:
1435                             att == "font-size" && (value = toInt(value, 10) + "px");
1436                             var cssrule = att[rp](/(\-.)/g, function (w) {
1437                                 return upperCase.call(w.substring(1));
1438                             });
1439                             node.style[cssrule] = value;
1440                             // Need following line for Firefox
1441                             node[setAttribute](att, value);
1442                             break;
1443                     }
1444                 }
1445             }
1446             
1447             tuneText(o, params);
1448             if (rotxy) {
1449                 o.rotate(rotxy.join(S));
1450             } else {
1451                 toFloat(rot) && o.rotate(rot, true);
1452             }
1453         };
1454         var leading = 1.2,
1455         tuneText = function (el, params) {
1456             if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1457                 return;
1458             }
1459             var a = el.attrs,
1460                 node = el.node,
1461                 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1462  
1463             if (params[has]("text")) {
1464                 a.text = params.text;
1465                 while (node.firstChild) {
1466                     node.removeChild(node.firstChild);
1467                 }
1468                 var texts = Str(params.text)[split]("\n");
1469                 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1470                     var tspan = $("tspan");
1471                     i && $(tspan, {dy: fontSize * leading, x: a.x});
1472                     tspan[appendChild](doc.createTextNode(texts[i]));
1473                     node[appendChild](tspan);
1474                 }
1475             } else {
1476                 texts = node.getElementsByTagName("tspan");
1477                 for (i = 0, ii = texts[length]; i < ii; i++) {
1478                     i && $(texts[i], {dy: fontSize * leading, x: a.x});
1479                 }
1480             }
1481             $(node, {y: a.y});
1482             var bb = el.getBBox(),
1483                 dif = a.y - (bb.y + bb.height / 2);
1484             dif && R.is(dif, "finite") && $(node, {y: a.y + dif});
1485         },
1486         Element = function (node, svg) {
1487             var X = 0,
1488                 Y = 0;
1489             this[0] = node;
1490             this.id = R._oid++;
1491             this.node = node;
1492             node.raphael = this;
1493             this.paper = svg;
1494             this.attrs = this.attrs || {};
1495             this.transformations = []; // rotate, translate, scale
1496             this._ = {
1497                 tx: 0,
1498                 ty: 0,
1499                 rt: {deg: 0, cx: 0, cy: 0},
1500                 sx: 1,
1501                 sy: 1
1502             };
1503             !svg.bottom && (svg.bottom = this);
1504             this.prev = svg.top;
1505             svg.top && (svg.top.next = this);
1506             svg.top = this;
1507             this.next = null;
1508         };
1509         var elproto = Element[proto];
1510         Element[proto].rotate = function (deg, cx, cy) {
1511             if (this.removed) {
1512                 return this;
1513             }
1514             if (deg == null) {
1515                 if (this._.rt.cx) {
1516                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1517                 }
1518                 return this._.rt.deg;
1519             }
1520             var bbox = this.getBBox();
1521             deg = Str(deg)[split](separator);
1522             if (deg[length] - 1) {
1523                 cx = toFloat(deg[1]);
1524                 cy = toFloat(deg[2]);
1525             }
1526             deg = toFloat(deg[0]);
1527             if (cx != null && cx !== false) {
1528                 this._.rt.deg = deg;
1529             } else {
1530                 this._.rt.deg += deg;
1531             }
1532             (cy == null) && (cx = null);
1533             this._.rt.cx = cx;
1534             this._.rt.cy = cy;
1535             cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1536             cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1537             if (this._.rt.deg) {
1538                 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1539                 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1540             } else {
1541                 this.transformations[0] = E;
1542                 this.clip && $(this.clip, {transform: E});
1543             }
1544             $(this.node, {transform: this.transformations[join](S)});
1545             return this;
1546         };
1547         Element[proto].hide = function () {
1548             !this.removed && (this.node.style.display = "none");
1549             return this;
1550         };
1551         Element[proto].show = function () {
1552             !this.removed && (this.node.style.display = "");
1553             return this;
1554         };
1555         Element[proto].remove = function () {
1556             if (this.removed) {
1557                 return;
1558             }
1559             tear(this, this.paper);
1560             this.node.parentNode.removeChild(this.node);
1561             for (var i in this) {
1562                 delete this[i];
1563             }
1564             this.removed = true;
1565         };
1566         Element[proto].getBBox = function () {
1567             if (this.removed) {
1568                 return this;
1569             }
1570             if (this.type == "path") {
1571                 return pathDimensions(this.attrs.path);
1572             }
1573             if (this.node.style.display == "none") {
1574                 this.show();
1575                 var hide = true;
1576             }
1577             var bbox = {};
1578             try {
1579                 bbox = this.node.getBBox();
1580             } catch(e) {
1581                 // Firefox 3.0.x plays badly here
1582             } finally {
1583                 bbox = bbox || {};
1584             }
1585             if (this.type == "text") {
1586                 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1587                 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1588                     var bb = this.node.getExtentOfChar(i);
1589                     (bb.y < bbox.y) && (bbox.y = bb.y);
1590                     (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1591                     (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1592                 }
1593             }
1594             hide && this.hide();
1595             return bbox;
1596         };
1597         Element[proto].attr = function (name, value) {
1598             if (this.removed) {
1599                 return this;
1600             }
1601             if (name == null) {
1602                 var res = {};
1603                 for (var i in this.attrs) if (this.attrs[has](i)) {
1604                     res[i] = this.attrs[i];
1605                 }
1606                 this._.rt.deg && (res.rotation = this.rotate());
1607                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1608                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1609                 return res;
1610             }
1611             if (value == null && R.is(name, string)) {
1612                 if (name == "translation") {
1613                     return translate.call(this);
1614                 }
1615                 if (name == "rotation") {
1616                     return this.rotate();
1617                 }
1618                 if (name == "scale") {
1619                     return this.scale();
1620                 }
1621                 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1622                     return this.attrs.gradient;
1623                 }
1624                 return this.attrs[name];
1625             }
1626             if (value == null && R.is(name, array)) {
1627                 var values = {};
1628                 for (var j = 0, jj = name.length; j < jj; j++) {
1629                     values[name[j]] = this.attr(name[j]);
1630                 }
1631                 return values;
1632             }
1633             if (value != null) {
1634                 var params = {};
1635                 params[name] = value;
1636             } else if (name != null && R.is(name, "object")) {
1637                 params = name;
1638             }
1639             for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1640                 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1641                 this.attrs[key] = params[key];
1642                 for (var subkey in par) if (par[has](subkey)) {
1643                     params[subkey] = par[subkey];
1644                 }
1645             }
1646             setFillAndStroke(this, params);
1647             return this;
1648         };
1649         Element[proto].toFront = function () {
1650             if (this.removed) {
1651                 return this;
1652             }
1653             this.node.parentNode[appendChild](this.node);
1654             var svg = this.paper;
1655             svg.top != this && tofront(this, svg);
1656             return this;
1657         };
1658         Element[proto].toBack = function () {
1659             if (this.removed) {
1660                 return this;
1661             }
1662             if (this.node.parentNode.firstChild != this.node) {
1663                 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1664                 toback(this, this.paper);
1665                 var svg = this.paper;
1666             }
1667             return this;
1668         };
1669         Element[proto].insertAfter = function (element) {
1670             if (this.removed) {
1671                 return this;
1672             }
1673             var node = element.node || element[element.length - 1].node;
1674             if (node.nextSibling) {
1675                 node.parentNode.insertBefore(this.node, node.nextSibling);
1676             } else {
1677                 node.parentNode[appendChild](this.node);
1678             }
1679             insertafter(this, element, this.paper);
1680             return this;
1681         };
1682         Element[proto].insertBefore = function (element) {
1683             if (this.removed) {
1684                 return this;
1685             }
1686             var node = element.node || element[0].node;
1687             node.parentNode.insertBefore(this.node, node);
1688             insertbefore(this, element, this.paper);
1689             return this;
1690         };
1691         Element[proto].blur = function (size) {
1692             // Experimental. No Safari support. Use it on your own risk.
1693             var t = this;
1694             if (+size !== 0) {
1695                 var fltr = $("filter"),
1696                     blur = $("feGaussianBlur");
1697                 t.attrs.blur = size;
1698                 fltr.id = createUUID();
1699                 $(blur, {stdDeviation: +size || 1.5});
1700                 fltr.appendChild(blur);
1701                 t.paper.defs.appendChild(fltr);
1702                 t._blur = fltr;
1703                 $(t.node, {filter: "url(#" + fltr.id + ")"});
1704             } else {
1705                 if (t._blur) {
1706                     t._blur.parentNode.removeChild(t._blur);
1707                     delete t._blur;
1708                     delete t.attrs.blur;
1709                 }
1710                 t.node.removeAttribute("filter");
1711             }
1712         };
1713         var theCircle = function (svg, x, y, r) {
1714             var el = $("circle");
1715             svg.canvas && svg.canvas[appendChild](el);
1716             var res = new Element(el, svg);
1717             res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1718             res.type = "circle";
1719             $(el, res.attrs);
1720             return res;
1721         },
1722         theRect = function (svg, x, y, w, h, r) {
1723             var el = $("rect");
1724             svg.canvas && svg.canvas[appendChild](el);
1725             var res = new Element(el, svg);
1726             res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1727             res.type = "rect";
1728             $(el, res.attrs);
1729             return res;
1730         },
1731         theEllipse = function (svg, x, y, rx, ry) {
1732             var el = $("ellipse");
1733             svg.canvas && svg.canvas[appendChild](el);
1734             var res = new Element(el, svg);
1735             res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1736             res.type = "ellipse";
1737             $(el, res.attrs);
1738             return res;
1739         },
1740         theImage = function (svg, src, x, y, w, h) {
1741             var el = $("image");
1742             $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1743             el.setAttributeNS(svg.xlink, "href", src);
1744             svg.canvas && svg.canvas[appendChild](el);
1745             var res = new Element(el, svg);
1746             res.attrs = {x: x, y: y, width: w, height: h, src: src};
1747             res.type = "image";
1748             return res;
1749         },
1750         theText = function (svg, x, y, text) {
1751             var el = $("text");
1752             $(el, {x: x, y: y, "text-anchor": "middle"});
1753             svg.canvas && svg.canvas[appendChild](el);
1754             var res = new Element(el, svg);
1755             res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1756             res.type = "text";
1757             setFillAndStroke(res, res.attrs);
1758             return res;
1759         },
1760         setSize = function (width, height) {
1761             this.width = width || this.width;
1762             this.height = height || this.height;
1763             this.canvas[setAttribute]("width", this.width);
1764             this.canvas[setAttribute]("height", this.height);
1765             return this;
1766         },
1767         create = function () {
1768             var con = getContainer[apply](0, arguments),
1769                 container = con && con.container,
1770                 x = con.x,
1771                 y = con.y,
1772                 width = con.width,
1773                 height = con.height;
1774             if (!container) {
1775                 throw new Error("SVG container not found.");
1776             }
1777             var cnvs = $("svg");
1778             x = x || 0;
1779             y = y || 0;
1780             width = width || 512;
1781             height = height || 342;
1782             $(cnvs, {
1783                 xmlns: "http://www.w3.org/2000/svg",
1784                 version: 1.1,
1785                 width: width,
1786                 height: height
1787             });
1788             if (container == 1) {
1789                 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1790                 doc.body[appendChild](cnvs);
1791             } else {
1792                 if (container.firstChild) {
1793                     container.insertBefore(cnvs, container.firstChild);
1794                 } else {
1795                     container[appendChild](cnvs);
1796                 }
1797             }
1798             container = new Paper;
1799             container.width = width;
1800             container.height = height;
1801             container.canvas = cnvs;
1802             plugins.call(container, container, R.fn);
1803             container.clear();
1804             return container;
1805         };
1806         paperproto.clear = function () {
1807             var c = this.canvas;
1808             while (c.firstChild) {
1809                 c.removeChild(c.firstChild);
1810             }
1811             this.bottom = this.top = null;
1812             (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1813             c[appendChild](this.desc);
1814             c[appendChild](this.defs = $("defs"));
1815         };
1816         paperproto.remove = function () {
1817             this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1818             for (var i in this) {
1819                 this[i] = removed(i);
1820             }
1821         };
1822     }
1823
1824     // VML
1825     if (R.vml) {
1826         var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1827             bites = /([clmz]),?([^clmz]*)/gi,
1828             blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1829             val = /-?[^,\s-]+/g,
1830             coordsize = 1e3 + S + 1e3,
1831             zoom = 10,
1832             pathlike = {path: 1, rect: 1},
1833             path2vml = function (path) {
1834                 var total =  /[ahqstv]/ig,
1835                     command = pathToAbsolute;
1836                 Str(path).match(total) && (command = path2curve);
1837                 total = /[clmz]/g;
1838                 if (command == pathToAbsolute && !Str(path).match(total)) {
1839                     var res = Str(path)[rp](bites, function (all, command, args) {
1840                         var vals = [],
1841                             isMove = lowerCase.call(command) == "m",
1842                             res = map[command];
1843                         args[rp](val, function (value) {
1844                             if (isMove && vals[length] == 2) {
1845                                 res += vals + map[command == "m" ? "l" : "L"];
1846                                 vals = [];
1847                             }
1848                             vals[push](round(value * zoom));
1849                         });
1850                         return res + vals;
1851                     });
1852                     return res;
1853                 }
1854                 var pa = command(path), p, r;
1855                 res = [];
1856                 for (var i = 0, ii = pa[length]; i < ii; i++) {
1857                     p = pa[i];
1858                     r = lowerCase.call(pa[i][0]);
1859                     r == "z" && (r = "x");
1860                     for (var j = 1, jj = p[length]; j < jj; j++) {
1861                         r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1862                     }
1863                     res[push](r);
1864                 }
1865                 return res[join](S);
1866             };
1867         
1868         R[toString] = function () {
1869             return  "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1870         };
1871         thePath = function (pathString, vml) {
1872             var g = createNode("group");
1873             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1874             g.coordsize = vml.coordsize;
1875             g.coordorigin = vml.coordorigin;
1876             var el = createNode("shape"), ol = el.style;
1877             ol.width = vml.width + "px";
1878             ol.height = vml.height + "px";
1879             el.coordsize = coordsize;
1880             el.coordorigin = vml.coordorigin;
1881             g[appendChild](el);
1882             var p = new Element(el, g, vml),
1883                 attr = {fill: "none", stroke: "#000"};
1884             pathString && (attr.path = pathString);
1885             p.type = "path";
1886             p.path = [];
1887             p.Path = E;
1888             setFillAndStroke(p, attr);
1889             vml.canvas[appendChild](g);
1890             return p;
1891         };
1892         setFillAndStroke = function (o, params) {
1893             o.attrs = o.attrs || {};
1894             var node = o.node,
1895                 a = o.attrs,
1896                 s = node.style,
1897                 xy,
1898                 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1899                 res = o;
1900
1901             for (var par in params) if (params[has](par)) {
1902                 a[par] = params[par];
1903             }
1904             if (newpath) {
1905                 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1906                 o.X = a.x;
1907                 o.Y = a.y;
1908                 o.W = a.width;
1909                 o.H = a.height;
1910             }
1911             params.href && (node.href = params.href);
1912             params.title && (node.title = params.title);
1913             params.target && (node.target = params.target);
1914             params.cursor && (s.cursor = params.cursor);
1915             "blur" in params && o.blur(params.blur);
1916             if (params.path && o.type == "path" || newpath) {
1917                 node.path = path2vml(a.path);
1918             }
1919             if (params.rotation != null) {
1920                 o.rotate(params.rotation, true);
1921             }
1922             if (params.translation) {
1923                 xy = Str(params.translation)[split](separator);
1924                 translate.call(o, xy[0], xy[1]);
1925                 if (o._.rt.cx != null) {
1926                     o._.rt.cx +=+ xy[0];
1927                     o._.rt.cy +=+ xy[1];
1928                     o.setBox(o.attrs, xy[0], xy[1]);
1929                 }
1930             }
1931             if (params.scale) {
1932                 xy = Str(params.scale)[split](separator);
1933                 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1934             }
1935             if ("clip-rect" in params) {
1936                 var rect = Str(params["clip-rect"])[split](separator);
1937                 if (rect[length] == 4) {
1938                     rect[2] = +rect[2] + (+rect[0]);
1939                     rect[3] = +rect[3] + (+rect[1]);
1940                     var div = node.clipRect || doc.createElement("div"),
1941                         dstyle = div.style,
1942                         group = node.parentNode;
1943                     dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1944                     if (!node.clipRect) {
1945                         dstyle.position = "absolute";
1946                         dstyle.top = 0;
1947                         dstyle.left = 0;
1948                         dstyle.width = o.paper.width + "px";
1949                         dstyle.height = o.paper.height + "px";
1950                         group.parentNode.insertBefore(div, group);
1951                         div[appendChild](group);
1952                         node.clipRect = div;
1953                     }
1954                 }
1955                 if (!params["clip-rect"]) {
1956                     node.clipRect && (node.clipRect.style.clip = E);
1957                 }
1958             }
1959             if (o.type == "image" && params.src) {
1960                 node.src = params.src;
1961             }
1962             if (o.type == "image" && params.opacity) {
1963                 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1964                 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1965             }
1966             params.font && (s.font = params.font);
1967             params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1968             params["font-size"] && (s.fontSize = params["font-size"]);
1969             params["font-weight"] && (s.fontWeight = params["font-weight"]);
1970             params["font-style"] && (s.fontStyle = params["font-style"]);
1971             if (params.opacity != null || 
1972                 params["stroke-width"] != null ||
1973                 params.fill != null ||
1974                 params.stroke != null ||
1975                 params["stroke-width"] != null ||
1976                 params["stroke-opacity"] != null ||
1977                 params["fill-opacity"] != null ||
1978                 params["stroke-dasharray"] != null ||
1979                 params["stroke-miterlimit"] != null ||
1980                 params["stroke-linejoin"] != null ||
1981                 params["stroke-linecap"] != null) {
1982                 node = o.shape || node;
1983                 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1984                     newfill = false;
1985                 !fill && (newfill = fill = createNode(fillString));
1986                 if ("fill-opacity" in params || "opacity" in params) {
1987                     var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1988                     opacity = mmin(mmax(opacity, 0), 1);
1989                     fill.opacity = opacity;
1990                 }
1991                 params.fill && (fill.on = true);
1992                 if (fill.on == null || params.fill == "none") {
1993                     fill.on = false;
1994                 }
1995                 if (fill.on && params.fill) {
1996                     var isURL = params.fill.match(ISURL);
1997                     if (isURL) {
1998                         fill.src = isURL[1];
1999                         fill.type = "tile";
2000                     } else {
2001                         fill.color = R.getRGB(params.fill).hex;
2002                         fill.src = E;
2003                         fill.type = "solid";
2004                         if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
2005                             a.fill = "none";
2006                             a.gradient = params.fill;
2007                         }
2008                     }
2009                 }
2010                 newfill && node[appendChild](fill);
2011                 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
2012                 newstroke = false;
2013                 !stroke && (newstroke = stroke = createNode("stroke"));
2014                 if ((params.stroke && params.stroke != "none") ||
2015                     params["stroke-width"] ||
2016                     params["stroke-opacity"] != null ||
2017                     params["stroke-dasharray"] ||
2018                     params["stroke-miterlimit"] ||
2019                     params["stroke-linejoin"] ||
2020                     params["stroke-linecap"]) {
2021                     stroke.on = true;
2022                 }
2023                 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2024                 var strokeColor = R.getRGB(params.stroke);
2025                 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2026                 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2027                 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2028                 opacity = mmin(mmax(opacity, 0), 1);
2029                 params["stroke-width"] == null && (width = a["stroke-width"]);
2030                 params["stroke-width"] && (stroke.weight = width);
2031                 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2032                 stroke.opacity = opacity;
2033                 
2034                 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2035                 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2036                 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2037                 if (params["stroke-dasharray"]) {
2038                     var dasharray = {
2039                         "-": "shortdash",
2040                         ".": "shortdot",
2041                         "-.": "shortdashdot",
2042                         "-..": "shortdashdotdot",
2043                         ". ": "dot",
2044                         "- ": "dash",
2045                         "--": "longdash",
2046                         "- .": "dashdot",
2047                         "--.": "longdashdot",
2048                         "--..": "longdashdotdot"
2049                     };
2050                     stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2051                 }
2052                 newstroke && node[appendChild](stroke);
2053             }
2054             if (res.type == "text") {
2055                 s = res.paper.span.style;
2056                 a.font && (s.font = a.font);
2057                 a["font-family"] && (s.fontFamily = a["font-family"]);
2058                 a["font-size"] && (s.fontSize = a["font-size"]);
2059                 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2060                 a["font-style"] && (s.fontStyle = a["font-style"]);
2061                 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "&#60;")[rp](/&/g, "&#38;")[rp](/\n/g, "<br>"));
2062                 res.W = a.w = res.paper.span.offsetWidth;
2063                 res.H = a.h = res.paper.span.offsetHeight;
2064                 res.X = a.x;
2065                 res.Y = a.y + round(res.H / 2);
2066  
2067                 // text-anchor emulationm
2068                 switch (a["text-anchor"]) {
2069                     case "start":
2070                         res.node.style["v-text-align"] = "left";
2071                         res.bbx = round(res.W / 2);
2072                     break;
2073                     case "end":
2074                         res.node.style["v-text-align"] = "right";
2075                         res.bbx = -round(res.W / 2);
2076                     break;
2077                     default:
2078                         res.node.style["v-text-align"] = "center";
2079                     break;
2080                 }
2081             }
2082         };
2083         addGradientFill = function (o, gradient) {
2084             o.attrs = o.attrs || {};
2085             var attrs = o.attrs,
2086                 fill,
2087                 type = "linear",
2088                 fxfy = ".5 .5";
2089             o.attrs.gradient = gradient;
2090             gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2091                 type = "radial";
2092                 if (fx && fy) {
2093                     fx = toFloat(fx);
2094                     fy = toFloat(fy);
2095                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2096                     fxfy = fx + S + fy;
2097                 }
2098                 return E;
2099             });
2100             gradient = gradient[split](/\s*\-\s*/);
2101             if (type == "linear") {
2102                 var angle = gradient.shift();
2103                 angle = -toFloat(angle);
2104                 if (isNaN(angle)) {
2105                     return null;
2106                 }
2107             }
2108             var dots = parseDots(gradient);
2109             if (!dots) {
2110                 return null;
2111             }
2112             o = o.shape || o.node;
2113             fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2114             !fill.parentNode && o.appendChild(fill);
2115             if (dots[length]) {
2116                 fill.on = true;
2117                 fill.method = "none";
2118                 fill.color = dots[0].color;
2119                 fill.color2 = dots[dots[length] - 1].color;
2120                 var clrs = [];
2121                 for (var i = 0, ii = dots[length]; i < ii; i++) {
2122                     dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2123                 }
2124                 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2125                 if (type == "radial") {
2126                     fill.type = "gradientradial";
2127                     fill.focus = "100%";
2128                     fill.focussize = fxfy;
2129                     fill.focusposition = fxfy;
2130                 } else {
2131                     fill.type = "gradient";
2132                     fill.angle = (270 - angle) % 360;
2133                 }
2134             }
2135             return 1;
2136         };
2137         Element = function (node, group, vml) {
2138             var Rotation = 0,
2139                 RotX = 0,
2140                 RotY = 0,
2141                 Scale = 1;
2142             this[0] = node;
2143             this.id = R._oid++;
2144             this.node = node;
2145             node.raphael = this;
2146             this.X = 0;
2147             this.Y = 0;
2148             this.attrs = {};
2149             this.Group = group;
2150             this.paper = vml;
2151             this._ = {
2152                 tx: 0,
2153                 ty: 0,
2154                 rt: {deg:0},
2155                 sx: 1,
2156                 sy: 1
2157             };
2158             !vml.bottom && (vml.bottom = this);
2159             this.prev = vml.top;
2160             vml.top && (vml.top.next = this);
2161             vml.top = this;
2162             this.next = null;
2163         };
2164         elproto = Element[proto];
2165         elproto.rotate = function (deg, cx, cy) {
2166             if (this.removed) {
2167                 return this;
2168             }
2169             if (deg == null) {
2170                 if (this._.rt.cx) {
2171                     return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2172                 }
2173                 return this._.rt.deg;
2174             }
2175             deg = Str(deg)[split](separator);
2176             if (deg[length] - 1) {
2177                 cx = toFloat(deg[1]);
2178                 cy = toFloat(deg[2]);
2179             }
2180             deg = toFloat(deg[0]);
2181             if (cx != null) {
2182                 this._.rt.deg = deg;
2183             } else {
2184                 this._.rt.deg += deg;
2185             }
2186             cy == null && (cx = null);
2187             this._.rt.cx = cx;
2188             this._.rt.cy = cy;
2189             this.setBox(this.attrs, cx, cy);
2190             this.Group.style.rotation = this._.rt.deg;
2191             // gradient fix for rotation. TODO
2192             // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2193             // fill = fill[0] || {};
2194             // var b = ((360 - this._.rt.deg) - 270) % 360;
2195             // !R.is(fill.angle, "undefined") && (fill.angle = b);
2196             return this;
2197         };
2198         elproto.setBox = function (params, cx, cy) {
2199             if (this.removed) {
2200                 return this;
2201             }
2202             var gs = this.Group.style,
2203                 os = (this.shape && this.shape.style) || this.node.style;
2204             params = params || {};
2205             for (var i in params) if (params[has](i)) {
2206                 this.attrs[i] = params[i];
2207             }
2208             cx = cx || this._.rt.cx;
2209             cy = cy || this._.rt.cy;
2210             var attr = this.attrs,
2211                 x,
2212                 y,
2213                 w,
2214                 h;
2215             switch (this.type) {
2216                 case "circle":
2217                     x = attr.cx - attr.r;
2218                     y = attr.cy - attr.r;
2219                     w = h = attr.r * 2;
2220                     break;
2221                 case "ellipse":
2222                     x = attr.cx - attr.rx;
2223                     y = attr.cy - attr.ry;
2224                     w = attr.rx * 2;
2225                     h = attr.ry * 2;
2226                     break;
2227                 case "image":
2228                     x = +attr.x;
2229                     y = +attr.y;
2230                     w = attr.width || 0;
2231                     h = attr.height || 0;
2232                     break;
2233                 case "text":
2234                     this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2235                     x = attr.x - round(this.W / 2);
2236                     y = attr.y - this.H / 2;
2237                     w = this.W;
2238                     h = this.H;
2239                     break;
2240                 case "rect":
2241                 case "path":
2242                     if (!this.attrs.path) {
2243                         x = 0;
2244                         y = 0;
2245                         w = this.paper.width;
2246                         h = this.paper.height;
2247                     } else {
2248                         var dim = pathDimensions(this.attrs.path);
2249                         x = dim.x;
2250                         y = dim.y;
2251                         w = dim.width;
2252                         h = dim.height;
2253                     }
2254                     break;
2255                 default:
2256                     x = 0;
2257                     y = 0;
2258                     w = this.paper.width;
2259                     h = this.paper.height;
2260                     break;
2261             }
2262             cx = (cx == null) ? x + w / 2 : cx;
2263             cy = (cy == null) ? y + h / 2 : cy;
2264             var left = cx - this.paper.width / 2,
2265                 top = cy - this.paper.height / 2, t;
2266             gs.left != (t = left + "px") && (gs.left = t);
2267             gs.top != (t = top + "px") && (gs.top = t);
2268             this.X = pathlike[has](this.type) ? -left : x;
2269             this.Y = pathlike[has](this.type) ? -top : y;
2270             this.W = w;
2271             this.H = h;
2272             if (pathlike[has](this.type)) {
2273                 os.left != (t = -left * zoom + "px") && (os.left = t);
2274                 os.top != (t = -top * zoom + "px") && (os.top = t);
2275             } else if (this.type == "text") {
2276                 os.left != (t = -left + "px") && (os.left = t);
2277                 os.top != (t = -top + "px") && (os.top = t);
2278             } else {
2279                 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2280                 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2281                 os.left != (t = x - left + "px") && (os.left = t);
2282                 os.top != (t = y - top + "px") && (os.top = t);
2283                 os.width != (t = w + "px") && (os.width = t);
2284                 os.height != (t = h + "px") && (os.height = t);
2285             }
2286         };
2287         elproto.hide = function () {
2288             !this.removed && (this.Group.style.display = "none");
2289             return this;
2290         };
2291         elproto.show = function () {
2292             !this.removed && (this.Group.style.display = "block");
2293             return this;
2294         };
2295         elproto.getBBox = function () {
2296             if (this.removed) {
2297                 return this;
2298             }
2299             if (pathlike[has](this.type)) {
2300                 return pathDimensions(this.attrs.path);
2301             }
2302             return {
2303                 x: this.X + (this.bbx || 0),
2304                 y: this.Y,
2305                 width: this.W,
2306                 height: this.H
2307             };
2308         };
2309         elproto.remove = function () {
2310             if (this.removed) {
2311                 return;
2312             }
2313             tear(this, this.paper);
2314             this.node.parentNode.removeChild(this.node);
2315             this.Group.parentNode.removeChild(this.Group);
2316             this.shape && this.shape.parentNode.removeChild(this.shape);
2317             for (var i in this) {
2318                 delete this[i];
2319             }
2320             this.removed = true;
2321         };
2322         elproto.attr = function (name, value) {
2323             if (this.removed) {
2324                 return this;
2325             }
2326             if (name == null) {
2327                 var res = {};
2328                 for (var i in this.attrs) if (this.attrs[has](i)) {
2329                     res[i] = this.attrs[i];
2330                 }
2331                 this._.rt.deg && (res.rotation = this.rotate());
2332                 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2333                 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2334                 return res;
2335             }
2336             if (value == null && R.is(name, "string")) {
2337                 if (name == "translation") {
2338                     return translate.call(this);
2339                 }
2340                 if (name == "rotation") {
2341                     return this.rotate();
2342                 }
2343                 if (name == "scale") {
2344                     return this.scale();
2345                 }
2346                 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2347                     return this.attrs.gradient;
2348                 }
2349                 return this.attrs[name];
2350             }
2351             if (this.attrs && value == null && R.is(name, array)) {
2352                 var ii, values = {};
2353                 for (i = 0, ii = name[length]; i < ii; i++) {
2354                     values[name[i]] = this.attr(name[i]);
2355                 }
2356                 return values;
2357             }
2358             var params;
2359             if (value != null) {
2360                 params = {};
2361                 params[name] = value;
2362             }
2363             value == null && R.is(name, "object") && (params = name);
2364             if (params) {
2365                 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2366                     var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2367                     this.attrs[key] = params[key];
2368                     for (var subkey in par) if (par[has](subkey)) {
2369                         params[subkey] = par[subkey];
2370                     }
2371                 }
2372                 if (params.text && this.type == "text") {
2373                     this.node.string = params.text;
2374                 }
2375                 setFillAndStroke(this, params);
2376                 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2377                     addGradientFill(this, params.gradient);
2378                 }
2379                 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2380             }
2381             return this;
2382         };
2383         elproto.toFront = function () {
2384             !this.removed && this.Group.parentNode[appendChild](this.Group);
2385             this.paper.top != this && tofront(this, this.paper);
2386             return this;
2387         };
2388         elproto.toBack = function () {
2389             if (this.removed) {
2390                 return this;
2391             }
2392             if (this.Group.parentNode.firstChild != this.Group) {
2393                 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2394                 toback(this, this.paper);
2395             }
2396             return this;
2397         };
2398         elproto.insertAfter = function (element) {
2399             if (this.removed) {
2400                 return this;
2401             }
2402             if (element.constructor == Set) {
2403                 element = element[element.length - 1];
2404             }
2405             if (element.Group.nextSibling) {
2406                 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2407             } else {
2408                 element.Group.parentNode[appendChild](this.Group);
2409             }
2410             insertafter(this, element, this.paper);
2411             return this;
2412         };
2413         elproto.insertBefore = function (element) {
2414             if (this.removed) {
2415                 return this;
2416             }
2417             if (element.constructor == Set) {
2418                 element = element[0];
2419             }
2420             element.Group.parentNode.insertBefore(this.Group, element.Group);
2421             insertbefore(this, element, this.paper);
2422             return this;
2423         };
2424         elproto.blur = function (size) {
2425             var s = this.node.runtimeStyle,
2426                 f = s.filter;
2427             f = f.replace(blurregexp, E);
2428             if (+size !== 0) {
2429                 this.attrs.blur = size;
2430                 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2431                 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2432             } else {
2433                 s.filter = f;
2434                 s.margin = 0;
2435                 delete this.attrs.blur;
2436             }
2437         };
2438  
2439         theCircle = function (vml, x, y, r) {
2440             var g = createNode("group"),
2441                 o = createNode("oval"),
2442                 ol = o.style;
2443             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2444             g.coordsize = coordsize;
2445             g.coordorigin = vml.coordorigin;
2446             g[appendChild](o);
2447             var res = new Element(o, g, vml);
2448             res.type = "circle";
2449             setFillAndStroke(res, {stroke: "#000", fill: "none"});
2450             res.attrs.cx = x;
2451             res.attrs.cy = y;
2452             res.attrs.r = r;
2453             res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2454             vml.canvas[appendChild](g);
2455             return res;
2456         };
2457         function rectPath(x, y, w, h, r) {
2458             if (r) {
2459                 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);
2460             } else {
2461                 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2462             }
2463         }
2464         theRect = function (vml, x, y, w, h, r) {
2465             var path = rectPath(x, y, w, h, r),
2466                 res = vml.path(path),
2467                 a = res.attrs;
2468             res.X = a.x = x;
2469             res.Y = a.y = y;
2470             res.W = a.width = w;
2471             res.H = a.height = h;
2472             a.r = r;
2473             a.path = path;
2474             res.type = "rect";
2475             return res;
2476         };
2477         theEllipse = function (vml, x, y, rx, ry) {
2478             var g = createNode("group"),
2479                 o = createNode("oval"),
2480                 ol = o.style;
2481             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2482             g.coordsize = coordsize;
2483             g.coordorigin = vml.coordorigin;
2484             g[appendChild](o);
2485             var res = new Element(o, g, vml);
2486             res.type = "ellipse";
2487             setFillAndStroke(res, {stroke: "#000"});
2488             res.attrs.cx = x;
2489             res.attrs.cy = y;
2490             res.attrs.rx = rx;
2491             res.attrs.ry = ry;
2492             res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2493             vml.canvas[appendChild](g);
2494             return res;
2495         };
2496         theImage = function (vml, src, x, y, w, h) {
2497             var g = createNode("group"),
2498                 o = createNode("image");
2499             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2500             g.coordsize = coordsize;
2501             g.coordorigin = vml.coordorigin;
2502             o.src = src;
2503             g[appendChild](o);
2504             var res = new Element(o, g, vml);
2505             res.type = "image";
2506             res.attrs.src = src;
2507             res.attrs.x = x;
2508             res.attrs.y = y;
2509             res.attrs.w = w;
2510             res.attrs.h = h;
2511             res.setBox({x: x, y: y, width: w, height: h});
2512             vml.canvas[appendChild](g);
2513             return res;
2514         };
2515         theText = function (vml, x, y, text) {
2516             var g = createNode("group"),
2517                 el = createNode("shape"),
2518                 ol = el.style,
2519                 path = createNode("path"),
2520                 ps = path.style,
2521                 o = createNode("textpath");
2522             g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2523             g.coordsize = coordsize;
2524             g.coordorigin = vml.coordorigin;
2525             path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2526             path.textpathok = true;
2527             ol.width = vml.width;
2528             ol.height = vml.height;
2529             o.string = Str(text);
2530             o.on = true;
2531             el[appendChild](o);
2532             el[appendChild](path);
2533             g[appendChild](el);
2534             var res = new Element(o, g, vml);
2535             res.shape = el;
2536             res.textpath = path;
2537             res.type = "text";
2538             res.attrs.text = text;
2539             res.attrs.x = x;
2540             res.attrs.y = y;
2541             res.attrs.w = 1;
2542             res.attrs.h = 1;
2543             setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2544             res.setBox();
2545             vml.canvas[appendChild](g);
2546             return res;
2547         };
2548         setSize = function (width, height) {
2549             var cs = this.canvas.style;
2550             width == +width && (width += "px");
2551             height == +height && (height += "px");
2552             cs.width = width;
2553             cs.height = height;
2554             cs.clip = "rect(0 " + width + " " + height + " 0)";
2555             return this;
2556         };
2557         var createNode;
2558         doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2559         try {
2560             !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2561             createNode = function (tagName) {
2562                 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2563             };
2564         } catch (e) {
2565             createNode = function (tagName) {
2566                 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2567             };
2568         }
2569         create = function () {
2570             var con = getContainer[apply](0, arguments),
2571                 container = con.container,
2572                 height = con.height,
2573                 s,
2574                 width = con.width,
2575                 x = con.x,
2576                 y = con.y;
2577             if (!container) {
2578                 throw new Error("VML container not found.");
2579             }
2580             var res = new Paper,
2581                 c = res.canvas = doc.createElement("div"),
2582                 cs = c.style;
2583             x = x || 0;
2584             y = y || 0;
2585             width = width || 512;
2586             height = height || 342;
2587             width == +width && (width += "px");
2588             height == +height && (height += "px");
2589             res.width = 1e3;
2590             res.height = 1e3;
2591             res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2592             res.coordorigin = "0 0";
2593             res.span = doc.createElement("span");
2594             res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2595             c[appendChild](res.span);
2596             cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2597             if (container == 1) {
2598                 doc.body[appendChild](c);
2599                 cs.left = x + "px";
2600                 cs.top = y + "px";
2601                 cs.position = "absolute";
2602             } else {
2603                 if (container.firstChild) {
2604                     container.insertBefore(c, container.firstChild);
2605                 } else {
2606                     container[appendChild](c);
2607                 }
2608             }
2609             plugins.call(res, res, R.fn);
2610             return res;
2611         };
2612         paperproto.clear = function () {
2613             this.canvas.innerHTML = E;
2614             this.span = doc.createElement("span");
2615             this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2616             this.canvas[appendChild](this.span);
2617             this.bottom = this.top = null;
2618         };
2619         paperproto.remove = function () {
2620             this.canvas.parentNode.removeChild(this.canvas);
2621             for (var i in this) {
2622                 this[i] = removed(i);
2623             }
2624             return true;
2625         };
2626     }
2627  
2628     // rest
2629     // WebKit rendering bug workaround method
2630     var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2631     if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2632         paperproto.safari = function () {
2633             var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2634             win.setTimeout(function () {rect.remove();});
2635         };
2636     } else {
2637         paperproto.safari = function () {};
2638     }
2639  
2640     // Events
2641     var preventDefault = function () {
2642         this.returnValue = false;
2643     },
2644     preventTouch = function () {
2645         return this.originalEvent.preventDefault();
2646     },
2647     stopPropagation = function () {
2648         this.cancelBubble = true;
2649     },
2650     stopTouch = function () {
2651         return this.originalEvent.stopPropagation();
2652     },
2653     addEvent = (function () {
2654         if (doc.addEventListener) {
2655             return function (obj, type, fn, element) {
2656                 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2657                 var f = function (e) {
2658                     if (supportsTouch && touchMap[has](type)) {
2659                         for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2660                             if (e.targetTouches[i].target == obj) {
2661                                 var olde = e;
2662                                 e = e.targetTouches[i];
2663                                 e.originalEvent = olde;
2664                                 e.preventDefault = preventTouch;
2665                                 e.stopPropagation = stopTouch;
2666                                 break;
2667                             }
2668                         }
2669                     }
2670                     return fn.call(element, e);
2671                 };
2672                 obj.addEventListener(realName, f, false);
2673                 return function () {
2674                     obj.removeEventListener(realName, f, false);
2675                     return true;
2676                 };
2677             };
2678         } else if (doc.attachEvent) {
2679             return function (obj, type, fn, element) {
2680                 var f = function (e) {
2681                     e = e || win.event;
2682                     e.preventDefault = e.preventDefault || preventDefault;
2683                     e.stopPropagation = e.stopPropagation || stopPropagation;
2684                     return fn.call(element, e);
2685                 };
2686                 obj.attachEvent("on" + type, f);
2687                 var detacher = function () {
2688                     obj.detachEvent("on" + type, f);
2689                     return true;
2690                 };
2691                 return detacher;
2692             };
2693         }
2694     })(),
2695     drag = [],
2696     dragMove = function (e) {
2697         var x = e.clientX,
2698             y = e.clientY,
2699             scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2700             scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2701             dragi,
2702             j = drag.length;
2703         while (j--) {
2704             dragi = drag[j];
2705             if (supportsTouch) {
2706                 var i = e.touches.length,
2707                     touch;
2708                 while (i--) {
2709                     touch = e.touches[i];
2710                     if (touch.identifier == dragi.el._drag.id) {
2711                         x = touch.clientX;
2712                         y = touch.clientY;
2713                         (e.originalEvent ? e.originalEvent : e).preventDefault();
2714                         break;
2715                     }
2716                 }
2717             } else {
2718                 e.preventDefault();
2719             }
2720             x += scrollX;
2721             y += scrollY;
2722             dragi.move && dragi.move.call(dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
2723         }
2724     },
2725     dragUp = function (e) {
2726         R.unmousemove(dragMove).unmouseup(dragUp);
2727         var i = drag.length,
2728             dragi;
2729         while (i--) {
2730             dragi = drag[i];
2731             dragi.el._drag = {};
2732             dragi.end && dragi.end.call(dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
2733         }
2734         drag = [];
2735     };
2736     for (var i = events[length]; i--;) {
2737         (function (eventName) {
2738             R[eventName] = Element[proto][eventName] = function (fn, scope) {
2739                 if (R.is(fn, "function")) {
2740                     this.events = this.events || [];
2741                     this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, scope || this)});
2742                 }
2743                 return this;
2744             };
2745             R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2746                 var events = this.events,
2747                     l = events[length];
2748                 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2749                     events[l].unbind();
2750                     events.splice(l, 1);
2751                     !events.length && delete this.events;
2752                     return this;
2753                 }
2754                 return this;
2755             };
2756         })(events[i]);
2757     }
2758     elproto.hover = function (f_in, f_out, scope_in, scope_out) {
2759         return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
2760     };
2761     elproto.unhover = function (f_in, f_out) {
2762         return this.unmouseover(f_in).unmouseout(f_out);
2763     };
2764     elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
2765         this._drag = {};
2766         this.mousedown(function (e) {
2767             (e.originalEvent || e).preventDefault();
2768             var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2769                 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2770             this._drag.x = e.clientX + scrollX;
2771             this._drag.y = e.clientY + scrollY;
2772             this._drag.id = e.identifier;
2773             onstart && onstart.call(start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
2774             !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2775             drag.push({el: this, move: onmove, end: onend, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
2776         });
2777         return this;
2778     };
2779     elproto.undrag = function (onmove, onstart, onend) {
2780         var i = drag.length;
2781         while (i--) {
2782             drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i++, 1);
2783         }
2784         !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2785     };
2786     paperproto.circle = function (x, y, r) {
2787         return theCircle(this, x || 0, y || 0, r || 0);
2788     };
2789     paperproto.rect = function (x, y, w, h, r) {
2790         return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2791     };
2792     paperproto.ellipse = function (x, y, rx, ry) {
2793         return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2794     };
2795     paperproto.path = function (pathString) {
2796         pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2797         return thePath(R.format[apply](R, arguments), this);
2798     };
2799     paperproto.image = function (src, x, y, w, h) {
2800         return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2801     };
2802     paperproto.text = function (x, y, text) {
2803         return theText(this, x || 0, y || 0, Str(text));
2804     };
2805     paperproto.set = function (itemsArray) {
2806         arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2807         return new Set(itemsArray);
2808     };
2809     paperproto.setSize = setSize;
2810     paperproto.top = paperproto.bottom = null;
2811     paperproto.raphael = R;
2812     function x_y() {
2813         return this.x + S + this.y;
2814     }
2815     elproto.resetScale = function () {
2816         if (this.removed) {
2817             return this;
2818         }
2819         this._.sx = 1;
2820         this._.sy = 1;
2821         this.attrs.scale = "1 1";
2822     };
2823     elproto.scale = function (x, y, cx, cy) {
2824         if (this.removed) {
2825             return this;
2826         }
2827         if (x == null && y == null) {
2828             return {
2829                 x: this._.sx,
2830                 y: this._.sy,
2831                 toString: x_y
2832             };
2833         }
2834         y = y || x;
2835         !+y && (y = x);
2836         var dx,
2837             dy,
2838             dcx,
2839             dcy,
2840             a = this.attrs;
2841         if (x != 0) {
2842             var bb = this.getBBox(),
2843                 rcx = bb.x + bb.width / 2,
2844                 rcy = bb.y + bb.height / 2,
2845                 kx = abs(x / this._.sx),
2846                 ky = abs(y / this._.sy);
2847             cx = (+cx || cx == 0) ? cx : rcx;
2848             cy = (+cy || cy == 0) ? cy : rcy;
2849             var posx = this._.sx > 0,
2850                 posy = this._.sy > 0,
2851                 dirx = ~~(x / abs(x)),
2852                 diry = ~~(y / abs(y)),
2853                 dkx = kx * dirx,
2854                 dky = ky * diry,
2855                 s = this.node.style,
2856                 ncx = cx + abs(rcx - cx) * dkx * (rcx > cx == posx ? 1 : -1),
2857                 ncy = cy + abs(rcy - cy) * dky * (rcy > cy == posy ? 1 : -1),
2858                 fr = (x * dirx > y * diry ? ky : kx);
2859             switch (this.type) {
2860                 case "rect":
2861                 case "image":
2862                     var neww = a.width * kx,
2863                         newh = a.height * ky;
2864                     this.attr({
2865                         height: newh,
2866                         r: a.r * fr,
2867                         width: neww,
2868                         x: ncx - neww / 2,
2869                         y: ncy - newh / 2
2870                     });
2871                     break;
2872                 case "circle":
2873                 case "ellipse":
2874                     this.attr({
2875                         rx: a.rx * kx,
2876                         ry: a.ry * ky,
2877                         r: a.r * fr,
2878                         cx: ncx,
2879                         cy: ncy
2880                     });
2881                     break;
2882                 case "text":
2883                     this.attr({
2884                         x: ncx,
2885                         y: ncy
2886                     });
2887                     break;
2888                 case "path":
2889                     var path = pathToRelative(a.path),
2890                         skip = true,
2891                         fx = posx ? dkx : kx,
2892                         fy = posy ? dky : ky;
2893                     for (var i = 0, ii = path[length]; i < ii; i++) {
2894                         var p = path[i],
2895                             P0 = upperCase.call(p[0]);
2896                         if (P0 == "M" && skip) {
2897                             continue;
2898                         } else {
2899                             skip = false;
2900                         }
2901                         if (P0 == "A") {
2902                             p[path[i][length] - 2] *= fx;
2903                             p[path[i][length] - 1] *= fy;
2904                             p[1] *= kx;
2905                             p[2] *= ky;
2906                             p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2907                         } else if (P0 == "H") {
2908                             for (var j = 1, jj = p[length]; j < jj; j++) {
2909                                 p[j] *= fx;
2910                             }
2911                         } else if (P0 == "V") {
2912                             for (j = 1, jj = p[length]; j < jj; j++) {
2913                                 p[j] *= fy;
2914                             }
2915                          } else {
2916                             for (j = 1, jj = p[length]; j < jj; j++) {
2917                                 p[j] *= (j % 2) ? fx : fy;
2918                             }
2919                         }
2920                     }
2921                     var dim2 = pathDimensions(path);
2922                     dx = ncx - dim2.x - dim2.width / 2;
2923                     dy = ncy - dim2.y - dim2.height / 2;
2924                     path[0][1] += dx;
2925                     path[0][2] += dy;
2926                     this.attr({path: path});
2927                 break;
2928             }
2929             if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2930                 if (this.transformations) {
2931                     this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2932                     this.node[setAttribute]("transform", this.transformations[join](S));
2933                     dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2934                     dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2935                     this.attr({x: dx, y: dy});
2936                     a.fx = dirx - 1;
2937                     a.fy = diry - 1;
2938                 } else {
2939                     this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2940                         ", M12=0, M21=0, M22=", diry,
2941                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2942                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2943                 }
2944             } else {
2945                 if (this.transformations) {
2946                     this.transformations[2] = E;
2947                     this.node[setAttribute]("transform", this.transformations[join](S));
2948                     a.fx = 0;
2949                     a.fy = 0;
2950                 } else {
2951                     this.node.filterMatrix = E;
2952                     s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2953                 }
2954             }
2955             a.scale = [x, y, cx, cy][join](S);
2956             this._.sx = x;
2957             this._.sy = y;
2958         }
2959         return this;
2960     };
2961     elproto.clone = function () {
2962         if (this.removed) {
2963             return null;
2964         }
2965         var attr = this.attr();
2966         delete attr.scale;
2967         delete attr.translation;
2968         return this.paper[this.type]().attr(attr);
2969     };
2970     var curveslengths = {},
2971     getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2972         var len = 0,
2973             precision = 100,
2974             name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2975             cache = curveslengths[name],
2976             old, dot;
2977         !cache && (curveslengths[name] = cache = {data: []});
2978         cache.timer && clearTimeout(cache.timer);
2979         cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2980         if (length != null) {
2981             var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
2982             precision = ~~total * 10;
2983         }
2984         for (var i = 0; i < precision + 1; i++) {
2985             if (cache.data[length] > i) {
2986                 dot = cache.data[i * precision];
2987             } else {
2988                 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
2989                 cache.data[i] = dot;
2990             }
2991             i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2992             if (length != null && len >= length) {
2993                 return dot;
2994             }
2995             old = dot;
2996         }
2997         if (length == null) {
2998             return len;
2999         }
3000     },
3001     getLengthFactory = function (istotal, subpath) {
3002         return function (path, length, onlystart) {
3003             path = path2curve(path);
3004             var x, y, p, l, sp = "", subpaths = {}, point,
3005                 len = 0;
3006             for (var i = 0, ii = path.length; i < ii; i++) {
3007                 p = path[i];
3008                 if (p[0] == "M") {
3009                     x = +p[1];
3010                     y = +p[2];
3011                 } else {
3012                     l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
3013                     if (len + l > length) {
3014                         if (subpath && !subpaths.start) {
3015                             point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3016                             sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
3017                             if (onlystart) {return sp;}
3018                             subpaths.start = sp;
3019                             sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
3020                             len += l;
3021                             x = +p[5];
3022                             y = +p[6];
3023                             continue;
3024                         }
3025                         if (!istotal && !subpath) {
3026                             point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3027                             return {x: point.x, y: point.y, alpha: point.alpha};
3028                         }
3029                     }
3030                     len += l;
3031                     x = +p[5];
3032                     y = +p[6];
3033                 }
3034                 sp += p;
3035             }
3036             subpaths.end = sp;
3037             point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3038             point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3039             return point;
3040         };
3041     };
3042     var getTotalLength = getLengthFactory(1),
3043         getPointAtLength = getLengthFactory(),
3044         getSubpathsAtLength = getLengthFactory(0, 1);
3045     elproto.getTotalLength = function () {
3046         if (this.type != "path") {return;}
3047         if (this.node.getTotalLength) {
3048             return this.node.getTotalLength();
3049         }
3050         return getTotalLength(this.attrs.path);
3051     };
3052     elproto.getPointAtLength = function (length) {
3053         if (this.type != "path") {return;}
3054         return getPointAtLength(this.attrs.path, length);
3055     };
3056     elproto.getSubpath = function (from, to) {
3057         if (this.type != "path") {return;}
3058         if (abs(this.getTotalLength() - to) < "1e-6") {
3059             return getSubpathsAtLength(this.attrs.path, from).end;
3060         }
3061         var a = getSubpathsAtLength(this.attrs.path, to, 1);
3062         return from ? getSubpathsAtLength(a, from).end : a;
3063     };
3064
3065     // animation easing formulas
3066     R.easing_formulas = {
3067         linear: function (n) {
3068             return n;
3069         },
3070         "<": function (n) {
3071             return pow(n, 3);
3072         },
3073         ">": function (n) {
3074             return pow(n - 1, 3) + 1;
3075         },
3076         "<>": function (n) {
3077             n = n * 2;
3078             if (n < 1) {
3079                 return pow(n, 3) / 2;
3080             }
3081             n -= 2;
3082             return (pow(n, 3) + 2) / 2;
3083         },
3084         backIn: function (n) {
3085             var s = 1.70158;
3086             return n * n * ((s + 1) * n - s);
3087         },
3088         backOut: function (n) {
3089             n = n - 1;
3090             var s = 1.70158;
3091             return n * n * ((s + 1) * n + s) + 1;
3092         },
3093         elastic: function (n) {
3094             if (n == 0 || n == 1) {
3095                 return n;
3096             }
3097             var p = .3,
3098                 s = p / 4;
3099             return pow(2, -10 * n) * math.sin((n - s) * (2 * PI) / p) + 1;
3100         },
3101         bounce: function (n) {
3102             var s = 7.5625,
3103                 p = 2.75,
3104                 l;
3105             if (n < (1 / p)) {
3106                 l = s * n * n;
3107             } else {
3108                 if (n < (2 / p)) {
3109                     n -= (1.5 / p);
3110                     l = s * n * n + .75;
3111                 } else {
3112                     if (n < (2.5 / p)) {
3113                         n -= (2.25 / p);
3114                         l = s * n * n + .9375;
3115                     } else {
3116                         n -= (2.625 / p);
3117                         l = s * n * n + .984375;
3118                     }
3119                 }
3120             }
3121             return l;
3122         }
3123     };
3124
3125     var animationElements = [],
3126         animation = function () {
3127             var Now = +new Date;
3128             for (var l = 0; l < animationElements[length]; l++) {
3129                 var e = animationElements[l];
3130                 if (e.stop || e.el.removed) {
3131                     continue;
3132                 }
3133                 var time = Now - e.start,
3134                     ms = e.ms,
3135                     easing = e.easing,
3136                     from = e.from,
3137                     diff = e.diff,
3138                     to = e.to,
3139                     t = e.t,
3140                     that = e.el,
3141                     set = {},
3142                     now;
3143                 if (time < ms) {
3144                     var pos = easing(time / ms);
3145                     for (var attr in from) if (from[has](attr)) {
3146                         switch (availableAnimAttrs[attr]) {
3147                             case "along":
3148                                 now = pos * ms * diff[attr];
3149                                 to.back && (now = to.len - now);
3150                                 var point = getPointAtLength(to[attr], now);
3151                                 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3152                                 diff.x = point.x;
3153                                 diff.y = point.y;
3154                                 that.translate(point.x - diff.sx, point.y - diff.sy);
3155                                 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3156                                 break;
3157                             case nu:
3158                                 now = +from[attr] + pos * ms * diff[attr];
3159                                 break;
3160                             case "colour":
3161                                 now = "rgb(" + [
3162                                     upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3163                                     upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3164                                     upto255(round(from[attr].b + pos * ms * diff[attr].b))
3165                                 ][join](",") + ")";
3166                                 break;
3167                             case "path":
3168                                 now = [];
3169                                 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3170                                     now[i] = [from[attr][i][0]];
3171                                     for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3172                                         now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3173                                     }
3174                                     now[i] = now[i][join](S);
3175                                 }
3176                                 now = now[join](S);
3177                                 break;
3178                             case "csv":
3179                                 switch (attr) {
3180                                     case "translation":
3181                                         var x = pos * ms * diff[attr][0] - t.x,
3182                                             y = pos * ms * diff[attr][1] - t.y;
3183                                         t.x += x;
3184                                         t.y += y;
3185                                         now = x + S + y;
3186                                     break;
3187                                     case "rotation":
3188                                         now = +from[attr][0] + pos * ms * diff[attr][0];
3189                                         from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3190                                     break;
3191                                     case "scale":
3192                                         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);
3193                                     break;
3194                                     case "clip-rect":
3195                                         now = [];
3196                                         i = 4;
3197                                         while (i--) {
3198                                             now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3199                                         }
3200                                     break;
3201                                 }
3202                                 break;
3203                             default:
3204                               var from2 = [].concat(from[attr]);
3205                                 now = [];
3206                                 i = that.paper.customAttributes[attr].length;
3207                                 while (i--) {
3208                                     now[i] = +from2[i] + pos * ms * diff[attr][i];
3209                                 }
3210                                 break;
3211                         }
3212                         set[attr] = now;
3213                     }
3214                     that.attr(set);
3215                     that._run && that._run.call(that);
3216                 } else {
3217                     if (to.along) {
3218                         point = getPointAtLength(to.along, to.len * !to.back);
3219                         that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3220                         to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3221                     }
3222                     (t.x || t.y) && that.translate(-t.x, -t.y);
3223                     to.scale && (to.scale += E);
3224                     that.attr(to);
3225                     animationElements.splice(l--, 1);
3226                 }
3227             }
3228             R.svg && that && that.paper && that.paper.safari();
3229             animationElements[length] && setTimeout(animation);
3230         },
3231         keyframesRun = function (attr, element, time, prev, prevcallback) {
3232             var dif = time - prev;
3233             element.timeouts.push(setTimeout(function () {
3234                 R.is(prevcallback, "function") && prevcallback.call(element);
3235                 element.animate(attr, dif, attr.easing);
3236             }, prev));
3237         },
3238         upto255 = function (color) {
3239             return mmax(mmin(color, 255), 0);
3240         },
3241         translate = function (x, y) {
3242             if (x == null) {
3243                 return {x: this._.tx, y: this._.ty, toString: x_y};
3244             }
3245             this._.tx += +x;
3246             this._.ty += +y;
3247             switch (this.type) {
3248                 case "circle":
3249                 case "ellipse":
3250                     this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3251                     break;
3252                 case "rect":
3253                 case "image":
3254                 case "text":
3255                     this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3256                     break;
3257                 case "path":
3258                     var path = pathToRelative(this.attrs.path);
3259                     path[0][1] += +x;
3260                     path[0][2] += +y;
3261                     this.attr({path: path});
3262                 break;
3263             }
3264             return this;
3265         };
3266     elproto.animateWith = function (element, params, ms, easing, callback) {
3267         for (var i = 0, ii = animationElements.length; i < ii; i++) {
3268             if (animationElements[i].el.id == element.id) {
3269                 params.start = animationElements[i].start;
3270             }
3271         }
3272         return this.animate(params, ms, easing, callback);
3273     };
3274     elproto.animateAlong = along();
3275     elproto.animateAlongBack = along(1);
3276     function along(isBack) {
3277         return function (path, ms, rotate, callback) {
3278             var params = {back: isBack};
3279             R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3280             path && path.constructor == Element && (path = path.attrs.path);
3281             path && (params.along = path);
3282             return this.animate(params, ms, callback);
3283         };
3284     }
3285     function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3286         var cx = 3 * p1x,
3287             bx = 3 * (p2x - p1x) - cx,
3288             ax = 1 - cx - bx,
3289             cy = 3 * p1y,
3290             by = 3 * (p2y - p1y) - cy,
3291             ay = 1 - cy - by;
3292         function sampleCurveX(t) {
3293             return ((ax * t + bx) * t + cx) * t;
3294         }
3295         function solve(x, epsilon) {
3296             var t = solveCurveX(x, epsilon);
3297             return ((ay * t + by) * t + cy) * t;
3298         }
3299         function solveCurveX(x, epsilon) {
3300             var t0, t1, t2, x2, d2, i;
3301             for(t2 = x, i = 0; i < 8; i++) {
3302                 x2 = sampleCurveX(t2) - x;
3303                 if (abs(x2) < epsilon) {
3304                     return t2;
3305                 }
3306                 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3307                 if (abs(d2) < 1e-6) {
3308                     break;
3309                 }
3310                 t2 = t2 - x2 / d2;
3311             }
3312             t0 = 0;
3313             t1 = 1;
3314             t2 = x;
3315             if (t2 < t0) {
3316                 return t0;
3317             }
3318             if (t2 > t1) {
3319                 return t1;
3320             }
3321             while (t0 < t1) {
3322                 x2 = sampleCurveX(t2);
3323                 if (abs(x2 - x) < epsilon) {
3324                     return t2;
3325                 }
3326                 if (x > x2) {
3327                     t0 = t2;
3328                 } else {
3329                     t1 = t2;
3330                 }
3331                 t2 = (t1 - t0) / 2 + t0;
3332             }
3333             return t2;
3334         }
3335         return solve(t, 1 / (200 * duration));
3336     }
3337     elproto.onAnimation = function (f) {
3338         this._run = f || 0;
3339         return this;
3340     };
3341     elproto.animate = function (params, ms, easing, callback) {
3342         var element = this;
3343         element.timeouts = element.timeouts || [];
3344         if (R.is(easing, "function") || !easing) {
3345             callback = easing || null;
3346         }
3347         if (element.removed) {
3348             callback && callback.call(element);
3349             return element;
3350         }
3351         var from = {},
3352             to = {},
3353             animateable = false,
3354             diff = {};
3355         for (var attr in params) if (params[has](attr)) {
3356             if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3357                 animateable = true;
3358                 from[attr] = element.attr(attr);
3359                 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3360                 to[attr] = params[attr];
3361                 switch (availableAnimAttrs[attr]) {
3362                     case "along":
3363                         var len = getTotalLength(params[attr]);
3364                         var point = getPointAtLength(params[attr], len * !!params.back);
3365                         var bb = element.getBBox();
3366                         diff[attr] = len / ms;
3367                         diff.tx = bb.x;
3368                         diff.ty = bb.y;
3369                         diff.sx = point.x;
3370                         diff.sy = point.y;
3371                         to.rot = params.rot;
3372                         to.back = params.back;
3373                         to.len = len;
3374                         params.rot && (diff.r = toFloat(element.rotate()) || 0);
3375                         break;
3376                     case nu:
3377                         diff[attr] = (to[attr] - from[attr]) / ms;
3378                         break;
3379                     case "colour":
3380                         from[attr] = R.getRGB(from[attr]);
3381                         var toColour = R.getRGB(to[attr]);
3382                         diff[attr] = {
3383                             r: (toColour.r - from[attr].r) / ms,
3384                             g: (toColour.g - from[attr].g) / ms,
3385                             b: (toColour.b - from[attr].b) / ms
3386                         };
3387                         break;
3388                     case "path":
3389                         var pathes = path2curve(from[attr], to[attr]);
3390                         from[attr] = pathes[0];
3391                         var toPath = pathes[1];
3392                         diff[attr] = [];
3393                         for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3394                             diff[attr][i] = [0];
3395                             for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3396                                 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3397                             }
3398                         }
3399                         break;
3400                     case "csv":
3401                         var values = Str(params[attr])[split](separator),
3402                             from2 = Str(from[attr])[split](separator);
3403                         switch (attr) {
3404                             case "translation":
3405                                 from[attr] = [0, 0];
3406                                 diff[attr] = [values[0] / ms, values[1] / ms];
3407                             break;
3408                             case "rotation":
3409                                 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3410                                 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3411                             break;
3412                             case "scale":
3413                                 params[attr] = values;
3414                                 from[attr] = Str(from[attr])[split](separator);
3415                                 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3416                             break;
3417                             case "clip-rect":
3418                                 from[attr] = Str(from[attr])[split](separator);
3419                                 diff[attr] = [];
3420                                 i = 4;
3421                                 while (i--) {
3422                                     diff[attr][i] = (values[i] - from[attr][i]) / ms;
3423                                 }
3424                             break;
3425                         }
3426                         to[attr] = values;
3427                         break;
3428                     default:
3429                         values = [].concat(params[attr]);
3430                         from2 = [].concat(from[attr]);
3431                         diff[attr] = [];
3432                         i = element.paper.customAttributes[attr][length];
3433                         while (i--) {
3434                             diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3435                         }
3436                         break;
3437                 }
3438             }
3439         }
3440         if (!animateable) {
3441             var attrs = [],
3442                 lastcall;
3443             for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3444                 attr = {value: params[key]};
3445                 key == "from" && (key = 0);
3446                 key == "to" && (key = 100);
3447                 attr.key = toInt(key, 10);
3448                 attrs.push(attr);
3449             }
3450             attrs.sort(sortByKey);
3451             if (attrs[0].key) {
3452                 attrs.unshift({key: 0, value: element.attrs});
3453             }
3454             for (i = 0, ii = attrs[length]; i < ii; i++) {
3455                 keyframesRun(attrs[i].value, element, ms / 100 * attrs[i].key, ms / 100 * (attrs[i - 1] && attrs[i - 1].key || 0), attrs[i - 1] && attrs[i - 1].value.callback);
3456             }
3457             lastcall = attrs[attrs[length] - 1].value.callback;
3458             if (lastcall) {
3459                 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3460             }
3461         } else {
3462             var easyeasy = R.easing_formulas[easing];
3463             if (!easyeasy) {
3464                 easyeasy = Str(easing).match(bezierrg);
3465                 if (easyeasy && easyeasy[length] == 5) {
3466                     var curve = easyeasy;
3467                     easyeasy = function (t) {
3468                         return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3469                     };
3470                 } else {
3471                     easyeasy = function (t) {
3472                         return t;
3473                     };
3474                 }
3475             }
3476             animationElements.push({
3477                 start: params.start || +new Date,
3478                 ms: ms,
3479                 easing: easyeasy,
3480                 from: from,
3481                 diff: diff,
3482                 to: to,
3483                 el: element,
3484                 t: {x: 0, y: 0}
3485             });
3486             R.is(callback, "function") && (element._ac = setTimeout(function () {
3487                 callback.call(element);
3488             }, ms));
3489             animationElements[length] == 1 && setTimeout(animation);
3490         }
3491         return this;
3492     };
3493     elproto.stop = function () {
3494         for (var i = 0; i < animationElements.length; i++) {
3495             animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3496         }
3497         for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3498             clearTimeout(this.timeouts[i]);
3499         }
3500         this.timeouts = [];
3501         clearTimeout(this._ac);
3502         delete this._ac;
3503         return this;
3504     };
3505     elproto.translate = function (x, y) {
3506         return this.attr({translation: x + " " + y});
3507     };
3508     elproto[toString] = function () {
3509         return "Rapha\xebl\u2019s object";
3510     };
3511     R.ae = animationElements;
3512  
3513     // Set
3514     var Set = function (items) {
3515         this.items = [];
3516         this[length] = 0;
3517         this.type = "set";
3518         if (items) {
3519             for (var i = 0, ii = items[length]; i < ii; i++) {
3520                 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3521                     this[this.items[length]] = this.items[this.items[length]] = items[i];
3522                     this[length]++;
3523                 }
3524             }
3525         }
3526     };
3527     Set[proto][push] = function () {
3528         var item,
3529             len;
3530         for (var i = 0, ii = arguments[length]; i < ii; i++) {
3531             item = arguments[i];
3532             if (item && (item.constructor == Element || item.constructor == Set)) {
3533                 len = this.items[length];
3534                 this[len] = this.items[len] = item;
3535                 this[length]++;
3536             }
3537         }
3538         return this;
3539     };
3540     Set[proto].pop = function () {
3541         delete this[this[length]--];
3542         return this.items.pop();
3543     };
3544     for (var method in elproto) if (elproto[has](method)) {
3545         Set[proto][method] = (function (methodname) {
3546             return function () {
3547                 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3548                     this.items[i][methodname][apply](this.items[i], arguments);
3549                 }
3550                 return this;
3551             };
3552         })(method);
3553     }
3554     Set[proto].attr = function (name, value) {
3555         if (name && R.is(name, array) && R.is(name[0], "object")) {
3556             for (var j = 0, jj = name[length]; j < jj; j++) {
3557                 this.items[j].attr(name[j]);
3558             }
3559         } else {
3560             for (var i = 0, ii = this.items[length]; i < ii; i++) {
3561                 this.items[i].attr(name, value);
3562             }
3563         }
3564         return this;
3565     };
3566     Set[proto].animate = function (params, ms, easing, callback) {
3567         (R.is(easing, "function") || !easing) && (callback = easing || null);
3568         var len = this.items[length],
3569             i = len,
3570             item,
3571             set = this,
3572             collector;
3573         callback && (collector = function () {
3574             !--len && callback.call(set);
3575         });
3576         easing = R.is(easing, string) ? easing : collector;
3577         item = this.items[--i].animate(params, ms, easing, collector);
3578         while (i--) {
3579             this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3580         }
3581         return this;
3582     };
3583     Set[proto].insertAfter = function (el) {
3584         var i = this.items[length];
3585         while (i--) {
3586             this.items[i].insertAfter(el);
3587         }
3588         return this;
3589     };
3590     Set[proto].getBBox = function () {
3591         var x = [],
3592             y = [],
3593             w = [],
3594             h = [];
3595         for (var i = this.items[length]; i--;) {
3596             var box = this.items[i].getBBox();
3597             x[push](box.x);
3598             y[push](box.y);
3599             w[push](box.x + box.width);
3600             h[push](box.y + box.height);
3601         }
3602         x = mmin[apply](0, x);
3603         y = mmin[apply](0, y);
3604         return {
3605             x: x,
3606             y: y,
3607             width: mmax[apply](0, w) - x,
3608             height: mmax[apply](0, h) - y
3609         };
3610     };
3611     Set[proto].clone = function (s) {
3612         s = new Set;
3613         for (var i = 0, ii = this.items[length]; i < ii; i++) {
3614             s[push](this.items[i].clone());
3615         }
3616         return s;
3617     };
3618
3619     R.registerFont = function (font) {
3620         if (!font.face) {
3621             return font;
3622         }
3623         this.fonts = this.fonts || {};
3624         var fontcopy = {
3625                 w: font.w,
3626                 face: {},
3627                 glyphs: {}
3628             },
3629             family = font.face["font-family"];
3630         for (var prop in font.face) if (font.face[has](prop)) {
3631             fontcopy.face[prop] = font.face[prop];
3632         }
3633         if (this.fonts[family]) {
3634             this.fonts[family][push](fontcopy);
3635         } else {
3636             this.fonts[family] = [fontcopy];
3637         }
3638         if (!font.svg) {
3639             fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3640             for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3641                 var path = font.glyphs[glyph];
3642                 fontcopy.glyphs[glyph] = {
3643                     w: path.w,
3644                     k: {},
3645                     d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3646                             return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3647                         }) + "z"
3648                 };
3649                 if (path.k) {
3650                     for (var k in path.k) if (path[has](k)) {
3651                         fontcopy.glyphs[glyph].k[k] = path.k[k];
3652                     }
3653                 }
3654             }
3655         }
3656         return font;
3657     };
3658     paperproto.getFont = function (family, weight, style, stretch) {
3659         stretch = stretch || "normal";
3660         style = style || "normal";
3661         weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3662         if (!R.fonts) {
3663             return;
3664         }
3665         var font = R.fonts[family];
3666         if (!font) {
3667             var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3668             for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3669                 if (name.test(fontName)) {
3670                     font = R.fonts[fontName];
3671                     break;
3672                 }
3673             }
3674         }
3675         var thefont;
3676         if (font) {
3677             for (var i = 0, ii = font[length]; i < ii; i++) {
3678                 thefont = font[i];
3679                 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3680                     break;
3681                 }
3682             }
3683         }
3684         return thefont;
3685     };
3686     paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3687         origin = origin || "middle"; // baseline|middle
3688         letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3689         var out = this.set(),
3690             letters = Str(string)[split](E),
3691             shift = 0,
3692             path = E,
3693             scale;
3694         R.is(font, string) && (font = this.getFont(font));
3695         if (font) {
3696             scale = (size || 16) / font.face["units-per-em"];
3697             var bb = font.face.bbox.split(separator),
3698                 top = +bb[0],
3699                 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3700             for (var i = 0, ii = letters[length]; i < ii; i++) {
3701                 var prev = i && font.glyphs[letters[i - 1]] || {},
3702                     curr = font.glyphs[letters[i]];
3703                 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3704                 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3705             }
3706             out.scale(scale, scale, top, height).translate(x - top, y - height);
3707         }
3708         return out;
3709     };
3710
3711     R.format = function (token, params) {
3712         var args = R.is(params, array) ? [0][concat](params) : arguments;
3713         token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3714             return args[++i] == null ? E : args[i];
3715         }));
3716         return token || E;
3717     };
3718     R.ninja = function () {
3719         oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3720         return R;
3721     };
3722     R.el = elproto;
3723     R.st = Set[proto];
3724
3725     oldRaphael.was ? (win.Raphael = R) : (Raphael = R);
3726 })();/*!
3727  * g.Raphael 0.4.1 - Charting library, based on Raphaël
3728  *
3729  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
3730  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
3731  */
3732  
3733  
3734 (function () {
3735     var mmax = Math.max,
3736         mmin = Math.min;
3737     Raphael.fn.g = Raphael.fn.g || {};
3738     Raphael.fn.g.markers = {
3739         disc: "disc",
3740         o: "disc",
3741         flower: "flower",
3742         f: "flower",
3743         diamond: "diamond",
3744         d: "diamond",
3745         square: "square",
3746         s: "square",
3747         triangle: "triangle",
3748         t: "triangle",
3749         star: "star",
3750         "*": "star",
3751         cross: "cross",
3752         x: "cross",
3753         plus: "plus",
3754         "+": "plus",
3755         arrow: "arrow",
3756         "->": "arrow"
3757     };
3758     Raphael.fn.g.shim = {stroke: "none", fill: "#000", "fill-opacity": 0};
3759     Raphael.fn.g.txtattr = {font: "12px Arial, sans-serif"};
3760     Raphael.fn.g.colors = [];
3761     var hues = [.6, .2, .05, .1333, .75, 0];
3762     for (var i = 0; i < 10; i++) {
3763         if (i < hues.length) {
3764             Raphael.fn.g.colors.push("hsb(" + hues[i] + ", .75, .75)");
3765         } else {
3766             Raphael.fn.g.colors.push("hsb(" + hues[i - hues.length] + ", 1, .5)");
3767         }
3768     }
3769     Raphael.fn.g.text = function (x, y, text) {
3770         return this.text(x, y, text).attr(this.g.txtattr);
3771     };
3772     Raphael.fn.g.labelise = function (label, val, total) {
3773         if (label) {
3774             return (label + "").replace(/(##+(?:\.#+)?)|(%%+(?:\.%+)?)/g, function (all, value, percent) {
3775                 if (value) {
3776                     return (+val).toFixed(value.replace(/^#+\.?/g, "").length);
3777                 }
3778                 if (percent) {
3779                     return (val * 100 / total).toFixed(percent.replace(/^%+\.?/g, "").length) + "%";
3780                 }
3781             });
3782         } else {
3783             return (+val).toFixed(0);
3784         }
3785     };
3786
3787     Raphael.fn.g.finger = function (x, y, width, height, dir, ending, isPath) {
3788         // dir 0 for horisontal and 1 for vertical
3789         if ((dir && !height) || (!dir && !width)) {
3790             return isPath ? "" : this.path();
3791         }
3792         ending = {square: "square", sharp: "sharp", soft: "soft"}[ending] || "round";
3793         var path;
3794         height = Math.round(height);
3795         width = Math.round(width);
3796         x = Math.round(x);
3797         y = Math.round(y);
3798         switch (ending) {
3799             case "round":
3800             if (!dir) {
3801                 var r = ~~(height / 2);
3802                 if (width < r) {
3803                     r = width;
3804                     path = ["M", x + .5, y + .5 - ~~(height / 2), "l", 0, 0, "a", r, ~~(height / 2), 0, 0, 1, 0, height, "l", 0, 0, "z"];
3805                 } else {
3806                     path = ["M", x + .5, y + .5 - r, "l", width - r, 0, "a", r, r, 0, 1, 1, 0, height, "l", r - width, 0, "z"];
3807                 }
3808             } else {
3809                 r = ~~(width / 2);
3810                 if (height < r) {
3811                     r = height;
3812                     path = ["M", x - ~~(width / 2), y, "l", 0, 0, "a", ~~(width / 2), r, 0, 0, 1, width, 0, "l", 0, 0, "z"];
3813                 } else {
3814                     path = ["M", x - r, y, "l", 0, r - height, "a", r, r, 0, 1, 1, width, 0, "l", 0, height - r, "z"];
3815                 }
3816             }
3817             break;
3818             case "sharp":
3819             if (!dir) {
3820                 var half = ~~(height / 2);
3821                 path = ["M", x, y + half, "l", 0, -height, mmax(width - half, 0), 0, mmin(half, width), half, -mmin(half, width), half + (half * 2 < height), "z"];
3822             } else {
3823                 half = ~~(width / 2);
3824                 path = ["M", x + half, y, "l", -width, 0, 0, -mmax(height - half, 0), half, -mmin(half, height), half, mmin(half, height), half, "z"];
3825             }
3826             break;
3827             case "square":
3828             if (!dir) {
3829                 path = ["M", x, y + ~~(height / 2), "l", 0, -height, width, 0, 0, height, "z"];
3830             } else {
3831                 path = ["M", x + ~~(width / 2), y, "l", 1 - width, 0, 0, -height, width - 1, 0, "z"];
3832             }
3833             break;
3834             case "soft":
3835             if (!dir) {
3836                 r = mmin(width, Math.round(height / 5));
3837                 path = ["M", x + .5, y + .5 - ~~(height / 2), "l", width - r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r * 2, "a", r, r, 0, 0, 1, -r, r, "l", r - width, 0, "z"];
3838             } else {
3839                 r = mmin(Math.round(width / 5), height);
3840                 path = ["M", x - ~~(width / 2), y, "l", 0, r - height, "a", r, r, 0, 0, 1, r, -r, "l", width - 2 * r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r, "z"];
3841             }
3842         }
3843         if (isPath) {
3844             return path.join(",");
3845         } else {
3846             return this.path(path);
3847         }
3848     };
3849
3850     // Symbols
3851     Raphael.fn.g.disc = function (cx, cy, r) {
3852         return this.circle(cx, cy, r);
3853     };
3854     Raphael.fn.g.line = function (cx, cy, r) {
3855         return this.rect(cx - r, cy - r / 5, 2 * r, 2 * r / 5);
3856     };
3857     Raphael.fn.g.square = function (cx, cy, r) {
3858         r = r * .7;
3859         return this.rect(cx - r, cy - r, 2 * r, 2 * r);
3860     };
3861     Raphael.fn.g.triangle = function (cx, cy, r) {
3862         r *= 1.75;
3863         return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z"));
3864     };
3865     Raphael.fn.g.diamond = function (cx, cy, r) {
3866         return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]);
3867     };
3868     Raphael.fn.g.flower = function (cx, cy, r, n) {
3869         r = r * 1.25;
3870         var rout = r,
3871             rin = rout * .5;
3872         n = +n < 3 || !n ? 5 : n;
3873         var points = ["M", cx, cy + rin, "Q"],
3874             R;
3875         for (var i = 1; i < n * 2 + 1; i++) {
3876             R = i % 2 ? rout : rin;
3877             points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]);
3878         }
3879         points.push("z");
3880         return this.path(points.join(","));
3881     };
3882     Raphael.fn.g.star = function (cx, cy, r, r2, rays) {
3883         r2 = r2 || r * .382;
3884         rays = rays || 5;
3885         var points = ["M", cx, cy + r2, "L"],
3886             R;
3887         for (var i = 1; i < rays * 2; i++) {
3888             R = i % 2 ? r : r2;
3889             points = points.concat([(cx + R * Math.sin(i * Math.PI / rays)), (cy + R * Math.cos(i * Math.PI / rays))]);
3890         }
3891         points.push("z");
3892         return this.path(points.join(","));
3893     };
3894     Raphael.fn.g.cross = function (cx, cy, r) {
3895         r = r / 2.5;
3896         return this.path("M".concat(cx - r, ",", cy, "l", [-r, -r, r, -r, r, r, r, -r, r, r, -r, r, r, r, -r, r, -r, -r, -r, r, -r, -r, "z"]));
3897     };
3898     Raphael.fn.g.plus = function (cx, cy, r) {
3899         r = r / 2;
3900         return this.path("M".concat(cx - r / 2, ",", cy - r / 2, "l", [0, -r, r, 0, 0, r, r, 0, 0, r, -r, 0, 0, r, -r, 0, 0, -r, -r, 0, 0, -r, "z"]));
3901     };
3902     Raphael.fn.g.arrow = function (cx, cy, r) {
3903         return this.path("M".concat(cx - r * .7, ",", cy - r * .4, "l", [r * .6, 0, 0, -r * .4, r, r * .8, -r, r * .8, 0, -r * .4, -r * .6, 0], "z"));
3904     };
3905
3906     // Tooltips
3907     Raphael.fn.g.tag = function (x, y, text, angle, r) {
3908         angle = angle || 0;
3909         r = r == null ? 5 : r;
3910         text = text == null ? "$9.99" : text;
3911         var R = .5522 * r,
3912             res = this.set(),
3913             d = 3;
3914         res.push(this.path().attr({fill: "#000", stroke: "#000"}));
3915         res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
3916         res.update = function () {
3917             this.rotate(0, x, y);
3918             var bb = this[1].getBBox();
3919             if (bb.height >= r * 2) {
3920                 this[0].attr({path: ["M", x, y + r, "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2, "m", 0, -r * 2 -d, "a", r + d, r + d, 0, 1, 0, 0, (r + d) * 2, "L", x + r + d, y + bb.height / 2 + d, "l", bb.width + 2 * d, 0, 0, -bb.height - 2 * d, -bb.width - 2 * d, 0, "L", x, y - r - d].join(",")});
3921             } else {
3922                 var dx = Math.sqrt(Math.pow(r + d, 2) - Math.pow(bb.height / 2 + d, 2));
3923                 this[0].attr({path: ["M", x, y + r, "c", -R, 0, -r, R - r, -r, -r, 0, -R, r - R, -r, r, -r, R, 0, r, r - R, r, r, 0, R, R - r, r, -r, r, "M", x + dx, y - bb.height / 2 - d, "a", r + d, r + d, 0, 1, 0, 0, bb.height + 2 * d, "l", r + d - dx + bb.width + 2 * d, 0, 0, -bb.height - 2 * d, "L", x + dx, y - bb.height / 2 - d].join(",")});
3924             }
3925             this[1].attr({x: x + r + d + bb.width / 2, y: y});
3926             angle = (360 - angle) % 360;
3927             this.rotate(angle, x, y);
3928             angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]});
3929             return this;
3930         };
3931         res.update();
3932         return res;
3933     };
3934     Raphael.fn.g.popupit = function (x, y, set, dir, size) {
3935         dir = dir == null ? 2 : dir;
3936         size = size || 5;
3937         x = Math.round(x);
3938         y = Math.round(y);
3939         var bb = set.getBBox(),
3940             w = Math.round(bb.width / 2),
3941             h = Math.round(bb.height / 2),
3942             dx = [0, w + size * 2, 0, -w - size * 2],
3943             dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
3944             p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
3945                 "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
3946                 "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
3947                 "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
3948                 "l", -mmax(w - size, 0), 0, "z"].join(","),
3949             xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];
3950         set.translate(xy.x - w - bb.x, xy.y - h - bb.y);
3951         return this.path(p).attr({fill: "#000", stroke: "none"}).insertBefore(set.node ? set : set[0]);
3952     };
3953     Raphael.fn.g.popup = function (x, y, text, dir, size) {
3954         dir = dir == null ? 2 : dir > 3 ? 3 : dir;
3955         size = size || 5;
3956         text = text || "$9.99";
3957         var res = this.set(),
3958             d = 3;
3959         res.push(this.path().attr({fill: "#000", stroke: "#000"}));
3960         res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
3961         res.update = function (X, Y, withAnimation) {
3962             X = X || x;
3963             Y = Y || y;
3964             var bb = this[1].getBBox(),
3965                 w = bb.width / 2,
3966                 h = bb.height / 2,
3967                 dx = [0, w + size * 2, 0, -w - size * 2],
3968                 dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
3969                 p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
3970                     "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
3971                     "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
3972                     "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
3973                     "l", -mmax(w - size, 0), 0, "z"].join(","),
3974                 xy = [{x: X, y: Y + size * 2 + h}, {x: X - size * 2 - w, y: Y}, {x: X, y: Y - size * 2 - h}, {x: X + size * 2 + w, y: Y}][dir];
3975             xy.path = p;
3976             if (withAnimation) {
3977                 this.animate(xy, 500, ">");
3978             } else {
3979                 this.attr(xy);
3980             }
3981             return this;
3982         };
3983         return res.update(x, y);
3984     };
3985     Raphael.fn.g.flag = function (x, y, text, angle) {
3986         angle = angle || 0;
3987         text = text || "$9.99";
3988         var res = this.set(),
3989             d = 3;
3990         res.push(this.path().attr({fill: "#000", stroke: "#000"}));
3991         res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
3992         res.update = function (x, y) {
3993             this.rotate(0, x, y);
3994             var bb = this[1].getBBox(),
3995                 h = bb.height / 2;
3996             this[0].attr({path: ["M", x, y, "l", h + d, -h - d, bb.width + 2 * d, 0, 0, bb.height + 2 * d, -bb.width - 2 * d, 0, "z"].join(",")});
3997             this[1].attr({x: x + h + d + bb.width / 2, y: y});
3998             angle = 360 - angle;
3999             this.rotate(angle, x, y);
4000             angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]});
4001             return this;
4002         };
4003         return res.update(x, y);
4004     };
4005     Raphael.fn.g.label = function (x, y, text) {
4006         var res = this.set();
4007         res.push(this.rect(x, y, 10, 10).attr({stroke: "none", fill: "#000"}));
4008         res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"}));
4009         res.update = function () {
4010             var bb = this[1].getBBox(),
4011                 r = mmin(bb.width + 10, bb.height + 10) / 2;
4012             this[0].attr({x: bb.x - r / 2, y: bb.y - r / 2, width: bb.width + r, height: bb.height + r, r: r});
4013         };
4014         res.update();
4015         return res;
4016     };
4017     Raphael.fn.g.labelit = function (set) {
4018         var bb = set.getBBox(),
4019             r = mmin(20, bb.width + 10, bb.height + 10) / 2;
4020         return this.rect(bb.x - r / 2, bb.y - r / 2, bb.width + r, bb.height + r, r).attr({stroke: "none", fill: "#000"}).insertBefore(set.node ? set : set[0]);
4021     };
4022     Raphael.fn.g.drop = function (x, y, text, size, angle) {
4023         size = size || 30;
4024         angle = angle || 0;
4025         var res = this.set();
4026         res.push(this.path(["M", x, y, "l", size, 0, "A", size * .4, size * .4, 0, 1, 0, x + size * .7, y - size * .7, "z"]).attr({fill: "#000", stroke: "none", rotation: [22.5 - angle, x, y]}));
4027         angle = (angle + 90) * Math.PI / 180;
4028         res.push(this.text(x + size * Math.sin(angle), y + size * Math.cos(angle), text).attr(this.g.txtattr).attr({"font-size": size * 12 / 30, fill: "#fff"}));
4029         res.drop = res[0];
4030         res.text = res[1];
4031         return res;
4032     };
4033     Raphael.fn.g.blob = function (x, y, text, angle, size) {
4034         angle = (+angle + 1 ? angle : 45) + 90;
4035         size = size || 12;
4036         var rad = Math.PI / 180,
4037             fontSize = size * 12 / 12;
4038         var res = this.set();
4039         res.push(this.path().attr({fill: "#000", stroke: "none"}));
4040         res.push(this.text(x + size * Math.sin((angle) * rad), y + size * Math.cos((angle) * rad) - fontSize / 2, text).attr(this.g.txtattr).attr({"font-size": fontSize, fill: "#fff"}));
4041         res.update = function (X, Y, withAnimation) {
4042             X = X || x;
4043             Y = Y || y;
4044             var bb = this[1].getBBox(),
4045                 w = mmax(bb.width + fontSize, size * 25 / 12),
4046                 h = mmax(bb.height + fontSize, size * 25 / 12),
4047                 x2 = X + size * Math.sin((angle - 22.5) * rad),
4048                 y2 = Y + size * Math.cos((angle - 22.5) * rad),
4049                 x1 = X + size * Math.sin((angle + 22.5) * rad),
4050                 y1 = Y + size * Math.cos((angle + 22.5) * rad),
4051                 dx = (x1 - x2) / 2,
4052                 dy = (y1 - y2) / 2,
4053                 rx = w / 2,
4054                 ry = h / 2,
4055                 k = -Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * dy * dy - ry * ry * dx * dx) / (rx * rx * dy * dy + ry * ry * dx * dx)),
4056                 cx = k * rx * dy / ry + (x1 + x2) / 2,
4057                 cy = k * -ry * dx / rx + (y1 + y2) / 2;
4058             if (withAnimation) {
4059                 this.animate({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}, 500, ">");
4060             } else {
4061                 this.attr({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")});
4062             }
4063             return this;
4064         };
4065         res.update(x, y);
4066         return res;
4067     };
4068
4069     Raphael.fn.g.colorValue = function (value, total, s, b) {
4070         return "hsb(" + [mmin((1 - value / total) * .4, 1), s || .75, b || .75] + ")";
4071     };
4072
4073     Raphael.fn.g.snapEnds = function (from, to, steps) {
4074         var f = from,
4075             t = to;
4076         if (f == t) {
4077             return {from: f, to: t, power: 0};
4078         }
4079         function round(a) {
4080             return Math.abs(a - .5) < .25 ? ~~(a) + .5 : Math.round(a);
4081         }
4082         var d = (t - f) / steps,
4083             r = ~~(d),
4084             R = r,
4085             i = 0;
4086         if (r) {
4087             while (R) {
4088                 i--;
4089                 R = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
4090             }
4091             i ++;
4092         } else {
4093             while (!r) {
4094                 i = i || 1;
4095                 r = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
4096                 i++;
4097             }
4098             i && i--;
4099         }
4100         t = round(to * Math.pow(10, i)) / Math.pow(10, i);
4101         if (t < to) {
4102             t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i);
4103         }
4104         f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i);
4105         return {from: f, to: t, power: i};
4106     };
4107     Raphael.fn.g.axis = function (x, y, length, from, to, steps, orientation, labels, type, dashsize) {
4108         dashsize = dashsize == null ? 2 : dashsize;
4109         type = type || "t";
4110         steps = steps || 10;
4111         var path = type == "|" || type == " " ? ["M", x + .5, y, "l", 0, .001] : orientation == 1 || orientation == 3 ? ["M", x + .5, y, "l", 0, -length] : ["M", x, y + .5, "l", length, 0],
4112             ends = this.g.snapEnds(from, to, steps),
4113             f = ends.from,
4114             t = ends.to,
4115             i = ends.power,
4116             j = 0,
4117             text = this.set();
4118         d = (t - f) / steps;
4119         var label = f,
4120             rnd = i > 0 ? i : 0;
4121             dx = length / steps;
4122         if (+orientation == 1 || +orientation == 3) {
4123             var Y = y,
4124                 addon = (orientation - 1 ? 1 : -1) * (dashsize + 3 + !!(orientation - 1));
4125             while (Y >= y - length) {
4126                 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), Y + .5, "l", dashsize * 2 + 1, 0]));
4127                 text.push(this.text(x + addon, Y, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"}));
4128                 label += d;
4129                 Y -= dx;
4130             }
4131             if (Math.round(Y + dx - (y - length))) {
4132                 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), y - length + .5, "l", dashsize * 2 + 1, 0]));
4133                 text.push(this.text(x + addon, y - length, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"}));
4134             }
4135         } else {
4136             label = f;
4137             rnd = (i > 0) * i;
4138             addon = (orientation ? -1 : 1) * (dashsize + 9 + !orientation);
4139             var X = x,
4140                 dx = length / steps,
4141                 txt = 0,
4142                 prev = 0;
4143             while (X <= x + length) {
4144                 type != "-" && type != " " && (path = path.concat(["M", X + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
4145                 text.push(txt = this.text(X, y + addon, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr));
4146                 var bb = txt.getBBox();
4147                 if (prev >= bb.x - 5) {
4148                     text.pop(text.length - 1).remove();
4149                 } else {
4150                     prev = bb.x + bb.width;
4151                 }
4152                 label += d;
4153                 X += dx;
4154             }
4155             if (Math.round(X - dx - x - length)) {
4156                 type != "-" && type != " " && (path = path.concat(["M", x + length + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
4157                 text.push(this.text(x + length, y + addon, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr));
4158             }
4159         }
4160         var res = this.path(path);
4161         res.text = text;
4162         res.all = this.set([res, text]);
4163         res.remove = function () {
4164             this.text.remove();
4165             this.constructor.prototype.remove.call(this);
4166         };
4167         return res;
4168     };
4169
4170     Raphael.el.lighter = function (times) {
4171         times = times || 2;
4172         var fs = [this.attrs.fill, this.attrs.stroke];
4173         this.fs = this.fs || [fs[0], fs[1]];
4174         fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
4175         fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
4176         fs[0].b = mmin(fs[0].b * times, 1);
4177         fs[0].s = fs[0].s / times;
4178         fs[1].b = mmin(fs[1].b * times, 1);
4179         fs[1].s = fs[1].s / times;
4180         this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
4181     };
4182     Raphael.el.darker = function (times) {
4183         times = times || 2;
4184         var fs = [this.attrs.fill, this.attrs.stroke];
4185         this.fs = this.fs || [fs[0], fs[1]];
4186         fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
4187         fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
4188         fs[0].s = mmin(fs[0].s * times, 1);
4189         fs[0].b = fs[0].b / times;
4190         fs[1].s = mmin(fs[1].s * times, 1);
4191         fs[1].b = fs[1].b / times;
4192         this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
4193     };
4194     Raphael.el.original = function () {
4195         if (this.fs) {
4196             this.attr({fill: this.fs[0], stroke: this.fs[1]});
4197             delete this.fs;
4198         }
4199     };
4200 })();/*!
4201  * g.Raphael 0.4.1 - Charting library, based on Raphaël
4202  *
4203  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4204  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4205  */
4206 Raphael.fn.g.barchart = function (x, y, width, height, values, opts) {
4207     opts = opts || {};
4208     var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square",
4209         gutter = parseFloat(opts.gutter || "20%"),
4210         chart = this.set(),
4211         bars = this.set(),
4212         covers = this.set(),
4213         covers2 = this.set(),
4214         total = Math.max.apply(Math, values),
4215         stacktotal = [],
4216         paper = this,
4217         multi = 0,
4218         colors = opts.colors || this.g.colors,
4219         len = values.length;
4220     if (this.raphael.is(values[0], "array")) {
4221         total = [];
4222         multi = len;
4223         len = 0;
4224         for (var i = values.length; i--;) {
4225             bars.push(this.set());
4226             total.push(Math.max.apply(Math, values[i]));
4227             len = Math.max(len, values[i].length);
4228         }
4229         if (opts.stacked) {
4230             for (var i = len; i--;) {
4231                 var tot = 0;
4232                 for (var j = values.length; j--;) {
4233                     tot +=+ values[j][i] || 0;
4234                 }
4235                 stacktotal.push(tot);
4236             }
4237         }
4238         for (var i = values.length; i--;) {
4239             if (values[i].length < len) {
4240                 for (var j = len; j--;) {
4241                     values[i].push(0);
4242                 }
4243             }
4244         }
4245         total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
4246     }
4247     
4248     total = (opts.to) || total;
4249     var barwidth = width / (len * (100 + gutter) + gutter) * 100,
4250         barhgutter = barwidth * gutter / 100,
4251         barvgutter = opts.vgutter == null ? 20 : opts.vgutter,
4252         stack = [],
4253         X = x + barhgutter,
4254         Y = (height - 2 * barvgutter) / total;
4255     if (!opts.stretch) {
4256         barhgutter = Math.round(barhgutter);
4257         barwidth = Math.floor(barwidth);
4258     }
4259     !opts.stacked && (barwidth /= multi || 1);
4260     for (var i = 0; i < len; i++) {
4261         stack = [];
4262         for (var j = 0; j < (multi || 1); j++) {
4263             var h = Math.round((multi ? values[j][i] : values[i]) * Y),
4264                 top = y + height - barvgutter - h,
4265                 bar = this.g.finger(Math.round(X + barwidth / 2), top + h, barwidth, h, true, type).attr({stroke: "none", fill: colors[multi ? j : i]});
4266             if (multi) {
4267                 bars[j].push(bar);
4268             } else {
4269                 bars.push(bar);
4270             }
4271             bar.y = top;
4272             bar.x = Math.round(X + barwidth / 2);
4273             bar.w = barwidth;
4274             bar.h = h;
4275                                                 bar.index = i;
4276             bar.value = multi ? values[j][i] : values[i];
4277             if (!opts.stacked) {
4278                 X += barwidth;
4279             } else {
4280                 stack.push(bar);
4281             }
4282         }
4283         if (opts.stacked) {
4284             var cvr;
4285             covers2.push(cvr = this.rect(stack[0].x - stack[0].w / 2, y, barwidth, height).attr(this.g.shim));
4286             cvr.bars = this.set();
4287             var size = 0;
4288             for (var s = stack.length; s--;) {
4289                 stack[s].toFront();
4290             }
4291             for (var s = 0, ss = stack.length; s < ss; s++) {
4292                 var bar = stack[s],
4293                     cover,
4294                     h = (size + bar.value) * Y,
4295                     path = this.g.finger(bar.x, y + height - barvgutter - !!size * .5, barwidth, h, true, type, 1);
4296                 cvr.bars.push(bar);
4297                 size && bar.attr({path: path});
4298                 bar.h = h;
4299                 bar.y = y + height - barvgutter - !!size * .5 - h;
4300                 covers.push(cover = this.rect(bar.x - bar.w / 2, bar.y, barwidth, bar.value * Y).attr(this.g.shim));
4301                 cover.bar = bar;
4302                 cover.value = bar.value;
4303                 size += bar.value;
4304             }
4305             X += barwidth;
4306         }
4307         X += barhgutter;
4308     }
4309     covers2.toFront();
4310     X = x + barhgutter;
4311     if (!opts.stacked) {
4312         for (var i = 0; i < len; i++) {
4313             for (var j = 0; j < (multi || 1); j++) {
4314                 var cover;
4315                 covers.push(cover = this.rect(Math.round(X), y + barvgutter, barwidth, height - barvgutter).attr(this.g.shim));
4316                 cover.bar = multi ? bars[j][i] : bars[i];
4317                 cover.value = cover.bar.value;
4318                 X += barwidth;
4319             }
4320             X += barhgutter;
4321         }
4322     }
4323     chart.label = function (labels, isBottom, rotate) {
4324         labels = labels || [];
4325         isBottom = isBottom == undefined ? true : isBottom;
4326         rotate = rotate == undefined ? false : rotate;
4327         this.labels = paper.set();
4328         var L, l = -Infinity;
4329         if (opts.stacked) {
4330             for (var i = 0; i < len; i++) {
4331                 var tot = 0;
4332                 for (var j = 0; j < (multi || 1); j++) {
4333                     tot += multi ? values[j][i] : values[i];
4334                     if (j == 0) {
4335                         var label = paper.g.labelise(labels[j][i], tot, total);
4336                         L = paper.g.text(bars[j][i].x, isBottom ? y + height - barvgutter / 2 : bars[j][i].y - 10, label);
4337                         if (rotate) {
4338                                 L.rotate(90);
4339                         }
4340                         var bb = L.getBBox();
4341                         if (bb.x - 7 < l) {
4342                             L.remove();
4343                         } else {
4344                             this.labels.push(L);
4345                             l = bb.x + (rotate ? bb.height : bb.width);
4346                         }
4347                     }
4348                 }
4349             }
4350         } else {
4351             for (var i = 0; i < len; i++) {
4352                 for (var j = 0; j < (multi || 1); j++) {
4353                     // did not remove the loop because don't yet know whether to accept multi array input for arrays
4354                     var label = paper.g.labelise(multi ? labels[0] && labels[0][i] : labels[i], multi ? values[0][i] : values[i], total);
4355                      L = paper.g.text(bars[0][i].x, isBottom ? y + 5 + height - barvgutter / 2 : bars[0][i].y - 10, label);
4356                         if (rotate) {
4357                                 L.rotate(90);
4358                                 // If we rotated it, we need to move it as well. Still have to use the width
4359                                 // to get the "length" of the label, divided it in two and shift down.
4360                                 L.translate(0, (L.getBBox().width / 2));
4361                         }
4362                     var bb = L.getBBox();
4363 //                    if (bb.x - 7 < l) {
4364                     if (bb.x - (this.getBBox().width) < l) {
4365                         L.remove();
4366                     } else {
4367                         this.labels.push(L);
4368                         l = bb.x + (rotate ? bb.height : bb.width);
4369                     }
4370                 }
4371             }
4372         }
4373         return this;
4374     };
4375     chart.hover = function (fin, fout) {
4376         covers2.hide();
4377         covers.show();
4378         covers.mouseover(fin).mouseout(fout);
4379         return this;
4380     };
4381     chart.hoverColumn = function (fin, fout) {
4382         covers.hide();
4383         covers2.show();
4384         fout = fout || function () {};
4385         covers2.mouseover(fin).mouseout(fout);
4386         return this;
4387     };
4388     chart.click = function (f) {
4389         covers2.hide();
4390         covers.show();
4391         covers.click(f);
4392         return this;
4393     };
4394     chart.each = function (f) {
4395         if (!Raphael.is(f, "function")) {
4396             return this;
4397         }
4398         for (var i = covers.length; i--;) {
4399             f.call(covers[i]);
4400         }
4401         return this;
4402     };
4403     chart.eachColumn = function (f) {
4404         if (!Raphael.is(f, "function")) {
4405             return this;
4406         }
4407         for (var i = covers2.length; i--;) {
4408             f.call(covers2[i]);
4409         }
4410         return this;
4411     };
4412     chart.clickColumn = function (f) {
4413         covers.hide();
4414         covers2.show();
4415         covers2.click(f);
4416         return this;
4417     };
4418     chart.push(bars, covers, covers2);
4419     chart.bars = bars;
4420     chart.covers = covers;
4421     return chart;
4422 };
4423 Raphael.fn.g.hbarchart = function (x, y, width, height, values, opts) {
4424     opts = opts || {};
4425     var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square",
4426         gutter = parseFloat(opts.gutter || "20%"),
4427         chart = this.set(),
4428         bars = this.set(),
4429         covers = this.set(),
4430         covers2 = this.set(),
4431         total = Math.max.apply(Math, values),
4432         stacktotal = [],
4433         paper = this,
4434         multi = 0,
4435         colors = opts.colors || this.g.colors,
4436         len = values.length;
4437     if (this.raphael.is(values[0], "array")) {
4438         total = [];
4439         multi = len;
4440         len = 0;
4441         for (var i = values.length; i--;) {
4442             bars.push(this.set());
4443             total.push(Math.max.apply(Math, values[i]));
4444             len = Math.max(len, values[i].length);
4445         }
4446         if (opts.stacked) {
4447             for (var i = len; i--;) {
4448                 var tot = 0;
4449                 for (var j = values.length; j--;) {
4450                     tot +=+ values[j][i] || 0;
4451                 }
4452                 stacktotal.push(tot);
4453             }
4454         }
4455         for (var i = values.length; i--;) {
4456             if (values[i].length < len) {
4457                 for (var j = len; j--;) {
4458                     values[i].push(0);
4459                 }
4460             }
4461         }
4462         total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
4463     }
4464     
4465     total = (opts.to) || total;
4466     var barheight = Math.floor(height / (len * (100 + gutter) + gutter) * 100),
4467         bargutter = Math.floor(barheight * gutter / 100),
4468         stack = [],
4469         Y = y + bargutter,
4470         X = (width - 1) / total;
4471     !opts.stacked && (barheight /= multi || 1);
4472     for (var i = 0; i < len; i++) {
4473         stack = [];
4474         for (var j = 0; j < (multi || 1); j++) {
4475             var val = multi ? values[j][i] : values[i],
4476                 bar = this.g.finger(x, Y + barheight / 2, Math.round(val * X), barheight - 1, false, type).attr({stroke: "none", fill: colors[multi ? j : i]});
4477             if (multi) {
4478                 bars[j].push(bar);
4479             } else {
4480                 bars.push(bar);
4481             }
4482             bar.x = x + Math.round(val * X);
4483             bar.y = Y + barheight / 2;
4484             bar.w = Math.round(val * X);
4485             bar.h = barheight;
4486             bar.value = +val;
4487             if (!opts.stacked) {
4488                 Y += barheight;
4489             } else {
4490                 stack.push(bar);
4491             }
4492         }
4493         if (opts.stacked) {
4494             var cvr = this.rect(x, stack[0].y - stack[0].h / 2, width, barheight).attr(this.g.shim);
4495             covers2.push(cvr);
4496             cvr.bars = this.set();
4497             var size = 0;
4498             for (var s = stack.length; s--;) {
4499                 stack[s].toFront();
4500             }
4501             for (var s = 0, ss = stack.length; s < ss; s++) {
4502                 var bar = stack[s],
4503                     cover,
4504                     val = Math.round((size + bar.value) * X),
4505                     path = this.g.finger(x, bar.y, val, barheight - 1, false, type, 1);
4506                 cvr.bars.push(bar);
4507                 size && bar.attr({path: path});
4508                 bar.w = val;
4509                 bar.x = x + val;
4510                 covers.push(cover = this.rect(x + size * X, bar.y - bar.h / 2, bar.value * X, barheight).attr(this.g.shim));
4511                 cover.bar = bar;
4512                 size += bar.value;
4513             }
4514             Y += barheight;
4515         }
4516         Y += bargutter;
4517     }
4518     covers2.toFront();
4519     Y = y + bargutter;
4520     if (!opts.stacked) {
4521         for (var i = 0; i < len; i++) {
4522             for (var j = 0; j < (multi || 1); j++) {
4523                 var cover = this.rect(x, Y, width, barheight).attr(this.g.shim);
4524                 covers.push(cover);
4525                 cover.bar = multi ? bars[j][i] : bars[i];
4526                 cover.value = cover.bar.value;
4527                 Y += barheight;
4528             }
4529             Y += bargutter;
4530         }
4531     }
4532     chart.label = function (labels, isRight) {
4533         labels = labels || [];
4534         this.labels = paper.set();
4535         for (var i = 0; i < len; i++) {
4536             for (var j = 0; j < multi; j++) {
4537                 var  label = paper.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total);
4538                 var X = isRight ? bars[i * (multi || 1) + j].x - barheight / 2 + 3 : x + 5,
4539                     A = isRight ? "end" : "start",
4540                     L;
4541                 this.labels.push(L = paper.g.text(X, bars[i * (multi || 1) + j].y, label).attr({"text-anchor": A}).insertBefore(covers[0]));
4542                 if (L.getBBox().x < x + 5) {
4543                     L.attr({x: x + 5, "text-anchor": "start"});
4544                 } else {
4545                     bars[i * (multi || 1) + j].label = L;
4546                 }
4547             }
4548         }
4549         return this;
4550     };
4551     chart.hover = function (fin, fout) {
4552         covers2.hide();
4553         covers.show();
4554         fout = fout || function () {};
4555         covers.mouseover(fin).mouseout(fout);
4556         return this;
4557     };
4558     chart.hoverColumn = function (fin, fout) {
4559         covers.hide();
4560         covers2.show();
4561         fout = fout || function () {};
4562         covers2.mouseover(fin).mouseout(fout);
4563         return this;
4564     };
4565     chart.each = function (f) {
4566         if (!Raphael.is(f, "function")) {
4567             return this;
4568         }
4569         for (var i = covers.length; i--;) {
4570             f.call(covers[i]);
4571         }
4572         return this;
4573     };
4574     chart.eachColumn = function (f) {
4575         if (!Raphael.is(f, "function")) {
4576             return this;
4577         }
4578         for (var i = covers2.length; i--;) {
4579             f.call(covers2[i]);
4580         }
4581         return this;
4582     };
4583     chart.click = function (f) {
4584         covers2.hide();
4585         covers.show();
4586         covers.click(f);
4587         return this;
4588     };
4589     chart.clickColumn = function (f) {
4590         covers.hide();
4591         covers2.show();
4592         covers2.click(f);
4593         return this;
4594     };
4595     chart.push(bars, covers, covers2);
4596     chart.bars = bars;
4597     chart.covers = covers;
4598     return chart;
4599 };
4600 /*!
4601  * g.Raphael 0.4.1 - Charting library, based on Raphaël
4602  *
4603  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4604  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4605  */
4606 Raphael.fn.g.dotchart = function (x, y, width, height, valuesx, valuesy, size, opts) {
4607     function drawAxis(ax) {
4608         +ax[0] && (ax[0] = paper.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t"));
4609         +ax[1] && (ax[1] = paper.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t"));
4610         +ax[2] && (ax[2] = paper.g.axis(x + gutter, y + height - gutter + maxR, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t"));
4611         +ax[3] && (ax[3] = paper.g.axis(x + gutter - maxR, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t"));
4612     }
4613     opts = opts || {};
4614     var xdim = this.g.snapEnds(Math.min.apply(Math, valuesx), Math.max.apply(Math, valuesx), valuesx.length - 1),
4615         minx = xdim.from,
4616         maxx = xdim.to,
4617         gutter = opts.gutter || 10,
4618         ydim = this.g.snapEnds(Math.min.apply(Math, valuesy), Math.max.apply(Math, valuesy), valuesy.length - 1),
4619         miny = ydim.from,
4620         maxy = ydim.to,
4621         len = Math.max(valuesx.length, valuesy.length, size.length),
4622         symbol = this.g.markers[opts.symbol] || "disc",
4623         res = this.set(),
4624         series = this.set(),
4625         max = opts.max || 100,
4626         top = Math.max.apply(Math, size),
4627         R = [],
4628         paper = this,
4629         k = Math.sqrt(top / Math.PI) * 2 / max;
4630
4631     for (var i = 0; i < len; i++) {
4632         R[i] = Math.min(Math.sqrt(size[i] / Math.PI) * 2 / k, max);
4633     }
4634     gutter = Math.max.apply(Math, R.concat(gutter));
4635     var axis = this.set(),
4636         maxR = Math.max.apply(Math, R);
4637     if (opts.axis) {
4638         var ax = (opts.axis + "").split(/[,\s]+/);
4639         drawAxis(ax);
4640         var g = [], b = [];
4641         for (var i = 0, ii = ax.length; i < ii; i++) {
4642             var bb = ax[i].all ? ax[i].all.getBBox()[["height", "width"][i % 2]] : 0;
4643             g[i] = bb + gutter;
4644             b[i] = bb;
4645         }
4646         gutter = Math.max.apply(Math, g.concat(gutter));
4647         for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
4648             ax[i].remove();
4649             ax[i] = 1;
4650         }
4651         drawAxis(ax);
4652         for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
4653             axis.push(ax[i].all);
4654         }
4655         res.axis = axis;
4656     }
4657     var kx = (width - gutter * 2) / ((maxx - minx) || 1),
4658         ky = (height - gutter * 2) / ((maxy - miny) || 1);
4659     for (var i = 0, ii = valuesy.length; i < ii; i++) {
4660         var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol,
4661             X = x + gutter + (valuesx[i] - minx) * kx,
4662             Y = y + height - gutter - (valuesy[i] - miny) * ky;
4663         sym && R[i] && series.push(this.g[sym](X, Y, R[i]).attr({fill: opts.heat ? this.g.colorValue(R[i], maxR) : Raphael.fn.g.colors[0], "fill-opacity": opts.opacity ? R[i] / max : 1, stroke: "none"}));
4664     }
4665     var covers = this.set();
4666     for (var i = 0, ii = valuesy.length; i < ii; i++) {
4667         var X = x + gutter + (valuesx[i] - minx) * kx,
4668             Y = y + height - gutter - (valuesy[i] - miny) * ky;
4669         covers.push(this.circle(X, Y, maxR).attr(this.g.shim));
4670         opts.href && opts.href[i] && covers[i].attr({href: opts.href[i]});
4671         covers[i].r = +R[i].toFixed(3);
4672         covers[i].x = +X.toFixed(3);
4673         covers[i].y = +Y.toFixed(3);
4674         covers[i].X = valuesx[i];
4675         covers[i].Y = valuesy[i];
4676         covers[i].value = size[i] || 0;
4677         covers[i].dot = series[i];
4678     }
4679     res.covers = covers;
4680     res.series = series;
4681     res.push(series, axis, covers);
4682     res.hover = function (fin, fout) {
4683         covers.mouseover(fin).mouseout(fout);
4684         return this;
4685     };
4686     res.click = function (f) {
4687         covers.click(f);
4688         return this;
4689     };
4690     res.each = function (f) {
4691         if (!Raphael.is(f, "function")) {
4692             return this;
4693         }
4694         for (var i = covers.length; i--;) {
4695             f.call(covers[i]);
4696         }
4697         return this;
4698     };
4699     res.href = function (map) {
4700         var cover;
4701         for (var i = covers.length; i--;) {
4702             cover = covers[i];
4703             if (cover.X == map.x && cover.Y == map.y && cover.value == map.value) {
4704                 cover.attr({href: map.href});
4705             }
4706         }
4707     };
4708     return res;
4709 };
4710 /*!
4711  * g.Raphael 0.4.2 - Charting library, based on Raphaël
4712  *
4713  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4714  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4715  */
4716 Raphael.fn.g.linechart = function (x, y, width, height, valuesx, valuesy, opts) {
4717     function shrink(values, dim) {
4718         var k = values.length / dim,
4719             j = 0,
4720             l = k,
4721             sum = 0,
4722             res = [];
4723         while (j < values.length) {
4724             l--;
4725             if (l < 0) {
4726                 sum += values[j] * (1 + l);
4727                 res.push(sum / k);
4728                 sum = values[j++] * -l;
4729                 l += k;
4730             } else {
4731                 sum += values[j++];
4732             }
4733         }
4734         return res;
4735     }
4736     function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) {
4737         var l1 = (p2x - p1x) / 2,
4738             l2 = (p3x - p2x) / 2,
4739             a = Math.atan((p2x - p1x) / Math.abs(p2y - p1y)),
4740             b = Math.atan((p3x - p2x) / Math.abs(p2y - p3y));
4741         a = p1y < p2y ? Math.PI - a : a;
4742         b = p3y < p2y ? Math.PI - b : b;
4743         var alpha = Math.PI / 2 - ((a + b) % (Math.PI * 2)) / 2,
4744             dx1 = l1 * Math.sin(alpha + a),
4745             dy1 = l1 * Math.cos(alpha + a),
4746             dx2 = l2 * Math.sin(alpha + b),
4747             dy2 = l2 * Math.cos(alpha + b);
4748         return {
4749             x1: p2x - dx1,
4750             y1: p2y + dy1,
4751             x2: p2x + dx2,
4752             y2: p2y + dy2
4753         };
4754     }
4755     opts = opts || {};
4756     if (!this.raphael.is(valuesx[0], "array")) {
4757         valuesx = [valuesx];
4758     }
4759     if (!this.raphael.is(valuesy[0], "array")) {
4760         valuesy = [valuesy];
4761     }
4762     var gutter = opts.gutter || 10,
4763         len = Math.max(valuesx[0].length, valuesy[0].length),
4764         symbol = opts.symbol || "",
4765         colors = opts.colors || Raphael.fn.g.colors,
4766         that = this,
4767         columns = null,
4768         dots = null,
4769         chart = this.set(),
4770         path = [];
4771
4772     for (var i = 0, ii = valuesy.length; i < ii; i++) {
4773         len = Math.max(len, valuesy[i].length);
4774     }
4775     var shades = this.set();
4776     for (i = 0, ii = valuesy.length; i < ii; i++) {
4777         if (opts.shade) {
4778             shades.push(this.path().attr({stroke: "none", fill: colors[i], opacity: opts.nostroke ? 1 : .3}));
4779         }
4780         if (valuesy[i].length > width - 2 * gutter) {
4781             valuesy[i] = shrink(valuesy[i], width - 2 * gutter);
4782             len = width - 2 * gutter;
4783         }
4784         if (valuesx[i] && valuesx[i].length > width - 2 * gutter) {
4785             valuesx[i] = shrink(valuesx[i], width - 2 * gutter);
4786         }
4787     }
4788     var allx = Array.prototype.concat.apply([], valuesx),
4789         ally = Array.prototype.concat.apply([], valuesy),
4790         xdim = this.g.snapEnds(Math.min.apply(Math, allx), Math.max.apply(Math, allx), valuesx[0].length - 1);
4791         if(opts.clip) {
4792             var minx = opts.minx || xdim.from,
4793                 maxx = opts.maxx || xdim.to,
4794                 ydim = this.g.snapEnds(Math.min.apply(Math, ally), Math.max.apply(Math, ally), valuesy[0].length - 1),
4795                 miny = opts.miny || ydim.from,
4796                 maxy = opts.maxy || ydim.to;
4797         } else {
4798             var minx = opts.minx && Math.min(opts.minx, xdim.from) || xdim.from,
4799                 maxx = opts.maxx && Math.max(opts.maxx, xdimt.to) || xdim.to,
4800                 ydim = this.g.snapEnds(Math.min.apply(Math, ally), Math.max.apply(Math, ally), valuesy[0].length - 1),
4801                 miny = opts.miny && Math.min(opts.miny, ydim.from) || ydim.from,
4802                 maxy = opts.maxy && Math.max(opts.maxy, ydim.to) || ydim.to;
4803         }
4804         kx = (width - gutter * 2) / ((maxx - minx) || 1),
4805         ky = (height - gutter * 2) / ((maxy - miny) || 1);
4806
4807     var lines = this.set(),
4808         symbols = this.set(),
4809         line;
4810     for (i = 0, ii = valuesy.length; i < ii; i++) {
4811         if (!opts.nostroke) {
4812             lines.push(line = this.path().attr({
4813                 stroke: colors[i],
4814                 "stroke-width": opts.width || 2,
4815                 "stroke-linejoin": "round",
4816                 "stroke-linecap": "round",
4817                 "stroke-dasharray": opts.dash || ""
4818             }));
4819         }
4820         var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol,
4821             symset = this.set();
4822         path = [];
4823         for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
4824             var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
4825                 Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
4826             (Raphael.is(sym, "array") ? sym[j] : sym) && symset.push(this.g[Raphael.fn.g.markers[this.raphael.is(sym, "array") ? sym[j] : sym]](X, Y, (opts.width || 2) * 3).attr({fill: colors[i], stroke: "none"}));
4827             if (opts.smooth) {
4828                 if (j && j != jj - 1) {
4829                     var X0 = x + gutter + ((valuesx[i] || valuesx[0])[j - 1] - minx) * kx,
4830                         Y0 = y + height - gutter - (valuesy[i][j - 1] - miny) * ky,
4831                         X2 = x + gutter + ((valuesx[i] || valuesx[0])[j + 1] - minx) * kx,
4832                         Y2 = y + height - gutter - (valuesy[i][j + 1] - miny) * ky;
4833                     var a = getAnchors(X0, Y0, X, Y, X2, Y2);
4834                     path = path.concat([a.x1, a.y1, X, Y, a.x2, a.y2]);
4835                 }
4836                 if (!j) {
4837                     path = ["M", X, Y, "C", X, Y];
4838                 }
4839             } else {
4840                 path = path.concat([j ? "L" : "M", X, Y]);
4841             }
4842         }
4843         if (opts.smooth) {
4844             path = path.concat([X, Y, X, Y]);
4845         }
4846         symbols.push(symset);
4847         if (opts.shade) {
4848             shades[i].attr({path: path.concat(["L", X, y + height - gutter, "L",  x + gutter + ((valuesx[i] || valuesx[0])[0] - minx) * kx, y + height - gutter, "z"]).join(",")});
4849         }
4850         !opts.nostroke && line.attr({path: path.join(","), 'clip-rect': [x + gutter, y + gutter, width - 2 * gutter, height - 2 * gutter].join(",")});
4851     }
4852
4853     function createColumns(f) {
4854         // unite Xs together
4855         var Xs = [];
4856         for (var i = 0, ii = valuesx.length; i < ii; i++) {
4857             Xs = Xs.concat(valuesx[i]);
4858         }
4859         Xs.sort(function(a,b) { return a - b; });
4860         // remove duplicates
4861         var Xs2 = [],
4862             xs = [];
4863         for (i = 0, ii = Xs.length; i < ii; i++) {
4864             Xs[i] != Xs[i - 1] && Xs2.push(Xs[i]) && xs.push(x + gutter + (Xs[i] - minx) * kx);
4865         }
4866         Xs = Xs2;
4867         ii = Xs.length;
4868         var cvrs = f || that.set();
4869         for (i = 0; i < ii; i++) {
4870             var X = xs[i] - (xs[i] - (xs[i - 1] || x)) / 2,
4871                 w = ((xs[i + 1] || x + width) - xs[i]) / 2 + (xs[i] - (xs[i - 1] || x)) / 2,
4872                 C;
4873             f ? (C = {}) : cvrs.push(C = that.rect(X - 1, y, Math.max(w + 1, 1), height).attr({stroke: "none", fill: "#000", opacity: 0}));
4874             C.values = [];
4875             C.symbols = that.set();
4876             C.y = [];
4877             C.x = xs[i];
4878             C.axis = Xs[i];
4879             for (var j = 0, jj = valuesy.length; j < jj; j++) {
4880                 Xs2 = valuesx[j] || valuesx[0];
4881                 for (var k = 0, kk = Xs2.length; k < kk; k++) {
4882                     if (Xs2[k] == Xs[i]) {
4883                         C.values.push(valuesy[j][k]);
4884                         C.y.push(y + height - gutter - (valuesy[j][k] - miny) * ky);
4885                         C.symbols.push(chart.symbols[j][k]);
4886                     }
4887                 }
4888             }
4889             f && f.call(C);
4890         }
4891         !f && (columns = cvrs);
4892     }
4893     function createDots(f) {
4894         var cvrs = f || that.set(),
4895             C;
4896         for (var i = 0, ii = valuesy.length; i < ii; i++) {
4897             for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
4898                 var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
4899                     nearX = x + gutter + ((valuesx[i] || valuesx[0])[j ? j - 1 : 1] - minx) * kx,
4900                     Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
4901                 f ? (C = {}) : cvrs.push(C = that.circle(X, Y, Math.abs(nearX - X) / 2).attr({stroke: "none", fill: "#000", opacity: 0}));
4902                 C.x = X;
4903                 C.y = Y;
4904                 C.value = valuesy[i][j];
4905                 C.line = chart.lines[i];
4906                 C.shade = chart.shades[i];
4907                 C.symbol = chart.symbols[i][j];
4908                 C.symbols = chart.symbols[i];
4909                 C.axis = (valuesx[i] || valuesx[0])[j];
4910                 f && f.call(C);
4911             }
4912         }
4913         !f && (dots = cvrs);
4914     }
4915
4916     var axis = this.set();
4917     if (opts.axis) {
4918         var ax = (opts.axis + "").split(/[,\s]+/);
4919         +ax[0] && axis.push(this.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t"));
4920         +ax[1] && axis.push(this.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t"));
4921         +ax[2] && axis.push(this.g.axis(x + gutter, y + height - gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t"));
4922         +ax[3] && axis.push(this.g.axis(x + gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t"));
4923     }
4924
4925     chart.push(lines, shades, symbols, axis, columns, dots);
4926     chart.lines = lines;
4927     chart.shades = shades;
4928     chart.symbols = symbols;
4929     chart.axis = axis;
4930     chart.hoverColumn = function (fin, fout) {
4931         !columns && createColumns();
4932         columns.mouseover(fin).mouseout(fout);
4933         return this;
4934     };
4935     chart.clickColumn = function (f) {
4936         !columns && createColumns();
4937         columns.click(f);
4938         return this;
4939     };
4940     chart.hrefColumn = function (cols) {
4941         var hrefs = that.raphael.is(arguments[0], "array") ? arguments[0] : arguments;
4942         if (!(arguments.length - 1) && typeof cols == "object") {
4943             for (var x in cols) {
4944                 for (var i = 0, ii = columns.length; i < ii; i++) if (columns[i].axis == x) {
4945                     columns[i].attr("href", cols[x]);
4946                 }
4947             }
4948         }
4949         !columns && createColumns();
4950         for (i = 0, ii = hrefs.length; i < ii; i++) {
4951             columns[i] && columns[i].attr("href", hrefs[i]);
4952         }
4953         return this;
4954     };
4955     chart.hover = function (fin, fout) {
4956         !dots && createDots();
4957         dots.mouseover(fin).mouseout(fout);
4958         return this;
4959     };
4960     chart.click = function (f) {
4961         !dots && createDots();
4962         dots.click(f);
4963         return this;
4964     };
4965     chart.each = function (f) {
4966         createDots(f);
4967         return this;
4968     };
4969     chart.eachColumn = function (f) {
4970         createColumns(f);
4971         return this;
4972     };
4973     return chart;
4974 };
4975 /*!
4976  * g.Raphael 0.4.1 - Charting library, based on Raphaël
4977  *
4978  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4979  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4980  */
4981 Raphael.fn.g.piechart = function (cx, cy, r, values, opts) {
4982     opts = opts || {};
4983     var paper = this,
4984         sectors = [],
4985         covers = this.set(),
4986         chart = this.set(),
4987         series = this.set(),
4988         order = [],
4989         len = values.length,
4990         angle = 0,
4991         total = 0,
4992         others = 0,
4993         cut = 9,
4994         defcut = true;
4995
4996     var sum = 0;
4997     for (var i = 0; i < len; i++)
4998         sum += values[i];
4999     var single = false;
5000     var single_index = -1;
5001     for (var i = 0; i < len; i++)
5002         if (sum == values[i]) {
5003             single = true;
5004             single_index = i;
5005             break;
5006         }
5007     if (len == 1 || single == true) {
5008         for(var i = 0; i < len; i++) {
5009             var radius = 0.1;
5010             if (i == single_index) {
5011                 radius = r;
5012             }
5013             series.push(this.circle(cx, cy, radius).attr({fill: opts.colors && opts.colors[i] || this.g.colors[i], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth}));
5014             covers.push(this.circle(cx, cy, radius).attr({href: opts.href ? opts.href[i] : null}).attr(this.g.shim));
5015             values[i] = {value: values[i], order: i, valueOf: function () { return this.value; }};
5016             series[i].middle = {x: cx, y: cy};
5017             series[i].mangle = 180;
5018         }
5019         total = values[single_index];
5020     } else {
5021         function sector(cx, cy, r, startAngle, endAngle, fill) {
5022             var rad = Math.PI / 180,
5023                 x1 = cx + r * Math.cos(-startAngle * rad),
5024                 x2 = cx + r * Math.cos(-endAngle * rad),
5025                 xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
5026                 y1 = cy + r * Math.sin(-startAngle * rad),
5027                 y2 = cy + r * Math.sin(-endAngle * rad),
5028                 ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
5029                 res = ["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2, "z"];
5030             res.middle = {x: xm, y: ym};
5031             return res;
5032         }
5033         for (var i = 0; i < len; i++) {
5034             total += values[i];
5035             values[i] = {value: values[i], order: i, valueOf: function () { return this.value; }};
5036         }
5037         values.sort(function (a, b) {
5038             return b.value - a.value;
5039         });
5040         for (i = 0; i < len; i++) {
5041             if (defcut && values[i] * 360 / total <= 1.5) {
5042                 cut = i;
5043                 defcut = false;
5044             }
5045             if (i > cut) {
5046                 defcut = false;
5047                 values[cut].value += values[i];
5048                 values[cut].others = true;
5049                 others = values[cut].value;
5050             }
5051         }
5052         len = Math.min(cut + 1, values.length);
5053         others && values.splice(len) && (values[cut].others = true);
5054         for (i = 0; i < len; i++) {
5055             var valueOrder = values[i].order;
5056             var mangle = angle - 360 * values[i] / total / 2;
5057             if (!i) {
5058                 angle = 90 - mangle;
5059                 mangle = angle - 360 * values[i] / total / 2;
5060             }
5061             if (opts.init) {
5062                 var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
5063             }
5064             var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
5065             var p = this.path(opts.init ? ipath : path).attr({fill: opts.colors && opts.colors[valueOrder] || this.g.colors[valueOrder] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round"});
5066             p.value = values[i];
5067             p.middle = path.middle;
5068             p.mangle = mangle;
5069             sectors.push(p);
5070             series.push(p);
5071             opts.init && p.animate({path: path.join(",")}, (+opts.init - 1) || 1000, ">");
5072         }
5073         for (i = 0; i < len; i++) {
5074             p = paper.path(sectors[i].attr("path")).attr(this.g.shim);
5075             var valueOrder = values[i].order;
5076             opts.href && opts.href[valueOrder] && p.attr({href: opts.href[valueOrder]});
5077             //p.attr = function () {}; // this breaks translate!
5078             covers.push(p);
5079         }
5080     }
5081
5082     chart.hover = function (fin, fout) {
5083         fout = fout || function () {};
5084         var that = this;
5085         for (var i = 0; i < len; i++) {
5086             (function (sector, cover, j) {
5087                 var o = {
5088                     sector: sector,
5089                     cover: cover,
5090                     cx: cx,
5091                     cy: cy,
5092                     mx: sector.middle.x,
5093                     my: sector.middle.y,
5094                     mangle: sector.mangle,
5095                     r: r,
5096                     value: values[j],
5097                     total: total,
5098                     label: that.labels && that.labels[j]
5099                 };
5100                 cover.mouseover(function () {
5101                     fin.call(o);
5102                 }).mouseout(function () {
5103                     fout.call(o);
5104                 });
5105             })(series[i], covers[i], i);
5106         }
5107         return this;
5108     };
5109     // x: where label could be put
5110     // y: where label could be put
5111     // value: value to show
5112     // total: total number to count %
5113     chart.each = function (f) {
5114         var that = this;
5115         for (var i = 0; i < len; i++) {
5116             (function (sector, cover, j) {
5117                 var o = {
5118                     sector: sector,
5119                     cover: cover,
5120                     cx: cx,
5121                     cy: cy,
5122                     x: sector.middle.x,
5123                     y: sector.middle.y,
5124                     mangle: sector.mangle,
5125                     r: r,
5126                     value: values[j],
5127                     total: total,
5128                     label: that.labels && that.labels[j]
5129                 };
5130                 f.call(o);
5131             })(series[i], covers[i], i);
5132         }
5133         return this;
5134     };
5135     chart.click = function (f) {
5136         var that = this;
5137         for (var i = 0; i < len; i++) {
5138             (function (sector, cover, j) {
5139                 var o = {
5140                     sector: sector,
5141                     cover: cover,
5142                     cx: cx,
5143                     cy: cy,
5144                     mx: sector.middle.x,
5145                     my: sector.middle.y,
5146                     mangle: sector.mangle,
5147                     r: r,
5148                     value: values[j],
5149                     total: total,
5150                     label: that.labels && that.labels[j]
5151                 };
5152                 cover.click(function () { f.call(o); });
5153             })(series[i], covers[i], i);
5154         }
5155         return this;
5156     };
5157     chart.inject = function (element) {
5158         element.insertBefore(covers[0]);
5159     };
5160     var legend = function (labels, otherslabel, mark, dir) {
5161         var x = cx + r + r / 5,
5162             y = cy,
5163             h = y + 10;
5164         labels = labels || [];
5165         dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
5166         mark = paper.g.markers[mark && mark.toLowerCase()] || "disc";
5167         chart.labels = paper.set();
5168         for (var i = 0; i < len; i++) {
5169             var clr = series[i].attr("fill"),
5170                 j = values[i].order,
5171                 txt;
5172             values[i].others && (labels[j] = otherslabel || "Others");
5173             labels[j] = paper.g.labelise(labels[j], values[i], total);
5174             chart.labels.push(paper.set());
5175             chart.labels[i].push(paper.g[mark](x + 5, h, 5).attr({fill: clr, stroke: "none"}));
5176             chart.labels[i].push(txt = paper.text(x + 20, h, labels[j] || values[j]).attr(paper.g.txtattr).attr({fill: opts.legendcolor || "#000", "text-anchor": "start"}));
5177             covers[i].label = chart.labels[i];
5178             h += txt.getBBox().height * 1.2;
5179         }
5180         var bb = chart.labels.getBBox(),
5181             tr = {
5182                 east: [0, -bb.height / 2],
5183                 west: [-bb.width - 2 * r - 20, -bb.height / 2],
5184                 north: [-r - bb.width / 2, -r - bb.height - 10],
5185                 south: [-r - bb.width / 2, r + 10]
5186             }[dir];
5187         chart.labels.translate.apply(chart.labels, tr);
5188         chart.push(chart.labels);
5189     };
5190     if (opts.legend) {
5191         legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
5192     }
5193     chart.push(series, covers);
5194     chart.series = series;
5195     chart.covers = covers;
5196     
5197     var w = paper.width,
5198         h = paper.height,
5199         bb = chart.getBBox(),
5200         tr = [(w - bb.width)/2 - bb.x, (h - bb.height)/2 - bb.y];
5201     cx += tr[0];
5202     cy += tr[1];
5203     chart.translate.apply(chart, tr);
5204     return chart;
5205 };