Add rotation around given point.
[raphael] / raphael.js
1 /*
2  * Raphael 0.7.dev - JavaScript Vector Library
3  *
4  * Copyright (c) 2008 – 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7
8
9 var Raphael = (function () {
10     var create,
11         R = function () {
12             return create.apply(R, arguments);
13         };
14     R.version = "0.7.dev";
15     R.type = (window.SVGAngle ? "SVG" : "VML");
16     R.svg = !(R.vml = R.type == "VML");
17     R.idGenerator = 0;
18     var paper = {};
19     R.fn = {};
20     var availableAttrs = {cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '16px "Arial"', "font-family": '"Arial"', "font-size": "16", gradient: 0, height: 0, opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, translation: "0 0", width: 0, x: 0, y: 0},
21         availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "number", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"};
22
23     R.toString = function () {
24         return  "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
25                 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
26     };
27     // colour utilities
28     var hsb2rgb = function (hue, saturation, brightness) {
29         if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
30             brightness = hue.b;
31             saturation = hue.s;
32             hue = hue.h;
33         }
34         var red,
35             green,
36             blue;
37         if (brightness == 0) {
38             return {r: 0, g: 0, b: 0, hex: "#000"};
39         }
40         if (hue > 1 || saturation > 1 || brightness > 1) {
41             hue /= 255;
42             saturation /= 255;
43             brightness /= 255;
44         }
45         var i = Math.floor(hue * 6),
46             f = (hue * 6) - i,
47             p = brightness * (1 - saturation),
48             q = brightness * (1 - (saturation * f)),
49             t = brightness * (1 - (saturation * (1 - f)));
50         red = [brightness, q, p, p, t, brightness, brightness][i];
51         green = [t, brightness, brightness, q, p, p, t][i];
52         blue = [p, p, t, brightness, brightness, q, p][i];
53         var rgb = {r: red, g: green, b: blue};
54         red *= 255;
55         green *= 255;
56         blue *= 255;
57         var r = Math.round(red).toString(16);
58         if (r.length == 1) {
59             r = "0" + r;
60         }
61         var g = Math.round(green).toString(16);
62         if (g.length == 1) {
63             g = "0" + g;
64         }
65         var b = Math.round(blue).toString(16);
66         if (b.length == 1) {
67             b = "0" + b;
68         }
69         rgb.hex = "#" + r + g + b;
70         return rgb;
71     };
72     var rgb2hsb = function (red, green, blue) {
73         if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
74             blue = red.b;
75             green = red.g;
76             red = red.r;
77         }
78         if (typeof red == "string" && red.charAt(0) == "#") {
79             if (red.length == 4) {
80                 blue = parseInt(red.substring(3), 16);
81                 green = parseInt(red.substring(2, 3), 16);
82                 red = parseInt(red.substring(1, 2), 16);
83             } else {
84                 blue = parseInt(red.substring(5), 16);
85                 green = parseInt(red.substring(3, 5), 16);
86                 red = parseInt(red.substring(1, 3), 16);
87             }
88         }
89         if (red > 1 || green > 1 || blue > 1) {
90             red /= 255;
91             green /= 255;
92             blue /= 255;
93         }
94         var max = Math.max(red, green, blue),
95             min = Math.min(red, green, blue),
96             hue,
97             saturation,
98             brightness = max;
99         if (min == max) {
100             return {h: 0, s: 0, b: max};
101         } else {
102             var delta = (max - min);
103             saturation = delta / max;
104             if (red == max) {
105                 hue = (green - blue) / delta;
106             } else if (green == max) {
107                 hue = 2 + ((blue - red) / delta);
108             } else {
109                 hue = 4 + ((red - green) / delta);
110             }
111             hue /= 6;
112             if (hue < 0) {
113                 hue += 1;
114             }
115             if (hue > 1) {
116                 hue -= 1;
117             }
118         }
119         return {h: hue, s: saturation, b: brightness};
120     };
121     var getRGB = function (colour) {
122         if (!colour) {
123             return {r: 0, g: 0, b: 0, hex: "#000"};
124         }
125         if (colour == "none") {
126             return {r: -1, g: -1, b: -1, hex: "none"};
127         }
128         var red, green, blue,
129             rgb = colour.match(/^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hsb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i);
130         if (rgb) {
131             if (rgb[2]) {
132                 blue = parseInt(rgb[2].substring(5), 16);
133                 green = parseInt(rgb[2].substring(3, 5), 16);
134                 red = parseInt(rgb[2].substring(1, 3), 16);
135             }
136             if (rgb[3]) {
137                 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
138                 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
139                 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
140             }
141             if (rgb[4]) {
142                 rgb = rgb[4].split(/\s*,\s*/);
143                 red = parseFloat(rgb[0], 10);
144                 green = parseFloat(rgb[1], 10);
145                 blue = parseFloat(rgb[2], 10);
146             }
147             if (rgb[5]) {
148                 rgb = rgb[5].split(/\s*,\s*/);
149                 red = parseFloat(rgb[0], 10) * 2.55;
150                 green = parseFloat(rgb[1], 10) * 2.55;
151                 blue = parseFloat(rgb[2], 10) * 2.55;
152             }
153             if (rgb[6]) {
154                 rgb = rgb[6].split(/\s*,\s*/);
155                 red = parseFloat(rgb[0], 10);
156                 green = parseFloat(rgb[1], 10);
157                 blue = parseFloat(rgb[2], 10);
158                 return hsb2rgb(red, green, blue);
159             }
160             if (rgb[7]) {
161                 rgb = rgb[7].split(/\s*,\s*/);
162                 red = parseFloat(rgb[0], 10) * 2.55;
163                 green = parseFloat(rgb[1], 10) * 2.55;
164                 blue = parseFloat(rgb[2], 10) * 2.55;
165                 return hsb2rgb(red, green, blue);
166             }
167             var rgb = {r: red, g: green, b: blue};
168             var r = Math.round(red).toString(16);
169             (r.length == 1) && (r = "0" + r);
170             var g = Math.round(green).toString(16);
171             (g.length == 1) && (g = "0" + g);
172             var b = Math.round(blue).toString(16);
173             (b.length == 1) && (b = "0" + b);
174             rgb.hex = "#" + r + g + b;
175             return rgb;
176         } else {
177             return {r: -1, g: -1, b: -1, hex: "none"};
178         }
179     };
180     R.getColor = function (value) {
181         var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
182         var rgb = hsb2rgb(start.h, start.s, start.b);
183         start.h += .075;
184         if (start.h > 1) {
185             start.h = 0;
186             start.s -= .2;
187             if (start.s <= 0) {
188                 arguments.callee.start = {h: 0, s: 1, b: start.b};
189             }
190         }
191         return rgb.hex;
192     };
193     R.getColor.reset = function () {
194         this.start = undefined;
195     };
196     // path utilities
197     R.parsePathString = function (pathString) {
198         var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
199             data = [],
200             toString = function () {
201                 var res = "";
202                 for (var i = 0, ii = this.length; i < ii; i++) {
203                     res += this[i][0] + this[i].join(",").substring(2);
204                 }
205                 return res;
206             };
207         if (pathString.toString.toString() == toString.toString()) {
208             return pathString;
209         }
210         pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*\s*,?\s*)+)/ig, function (a, b, c) {
211             var params = [], name = b.toLowerCase();
212             c.replace(/(-?\d*\.?\d*)\s*,?\s*/ig, function (a, b) {
213                 b && params.push(+b);
214             });
215             while (params.length >= paramCounts[name]) {
216                 data.push([b].concat(params.splice(0, paramCounts[name])));
217                 if (!paramCounts[name]) {
218                     break;
219                 };
220             }
221         });
222         data.toString = toString;
223         return data;
224     };
225     var pathDimensions = function (path) {
226         var pathArray = path;
227         if (typeof path == "string") {
228             pathArray = Raphael.parsePathString(path);
229         }
230         pathArray = pathToAbsolute(pathArray);
231         var x = [], y = [], length = 0;
232         for (var i = 0, ii = pathArray.length; i < ii; i++) {
233             switch (pathArray[i][0]) {
234                 case "Z":
235                     break;
236                 case "A":
237                     x.push(pathArray[i][pathArray[i].length - 2]);
238                     y.push(pathArray[i][pathArray[i].length - 1]);
239                     break;
240                 default:
241                     for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
242                         if (j % 2) {
243                             x.push(pathArray[i][j]);
244                         } else {
245                             y.push(pathArray[i][j]);
246                         }
247                     }
248             }
249         }
250         var minx = Math.min.apply(Math, x),
251             miny = Math.min.apply(Math, y);
252         return {
253             x: minx,
254             y: miny,
255             width: Math.max.apply(Math, x) - minx,
256             height: Math.max.apply(Math, y) - miny,
257             X: x,
258             Y: y
259         };
260     };
261     var pathToRelative = function (pathArray) {
262         var res = [];
263         if (typeof pathArray == "string") {
264             pathArray = this.parsePathString(pathArray);
265         }
266         var x = 0, y = 0, start = 0;
267         if (pathArray[0][0] == "M") {
268             x = pathArray[0][1];
269             y = pathArray[0][2];
270             start++;
271             res.push(pathArray[0]);
272         }
273         for (var i = start, ii = pathArray.length; i < ii; i++) {
274             res[i] = [];
275             if (pathArray[i][0] != pathArray[i][0].toLowerCase()) {
276                 res[i][0] = pathArray[i][0].toLowerCase();
277                 switch (res[i][0]) {
278                     case "a":
279                         res[i][1] = pathArray[i][1];
280                         res[i][2] = pathArray[i][2];
281                         res[i][3] = 0;
282                         res[i][4] = pathArray[i][4];
283                         res[i][5] = pathArray[i][5];
284                         res[i][6] = +(pathArray[i][6] - x).toFixed(3);
285                         res[i][7] = +(pathArray[i][7] - y).toFixed(3);
286                         break;
287                     case "v":
288                         res[i][1] = +(pathArray[i][1] - y).toFixed(3);
289                         break;
290                     default:
291                         for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
292                             res[i][j] = +(pathArray[i][j] - ((j % 2) ? x : y)).toFixed(3);
293                         }
294                 }
295             } else {
296                 res[i] = pathArray[i];
297             }
298             switch (res[i][0]) {
299                 case "z":
300                     break;
301                 case "h": 
302                     x += res[i][res[i].length - 1];
303                     break;
304                 case "v":
305                     y += res[i][res[i].length - 1];
306                     break;
307                 default:
308                     x += res[i][res[i].length - 2];
309                     y += res[i][res[i].length - 1];
310             }
311         }
312         res.toString = pathArray.toString;
313         return res;
314     };
315     var pathToAbsolute = function (pathArray) {
316         var res = [];
317         if (typeof pathArray == "string") {
318             pathArray = R.parsePathString(pathArray);
319         }
320         var x = 0, y = 0, start = 0;
321         if (pathArray[0][0] == "M") {
322             x = +pathArray[0][1];
323             y = +pathArray[0][2];
324             start++;
325             res[0] = pathArray[0];
326         }
327         for (var i = start, ii = pathArray.length; i < ii; i++) {
328             res[i] = [];
329             if (pathArray[i][0] != pathArray[i][0].toUpperCase()) {
330                 res[i][0] = pathArray[i][0].toUpperCase();
331                 switch (res[i][0]) {
332                     case "A":
333                         res[i][1] = pathArray[i][1];
334                         res[i][2] = pathArray[i][2];
335                         res[i][3] = 0;
336                         res[i][4] = pathArray[i][4];
337                         res[i][5] = pathArray[i][5];
338                         res[i][6] = +(pathArray[i][6] + x).toFixed(3);
339                         res[i][7] = +(pathArray[i][7] + y).toFixed(3);
340                         break;
341                     case "V":
342                         res[i][1] = +pathArray[i][1] + y;
343                         break;
344                     default:
345                         for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
346                             res[i][j] = +pathArray[i][j] + ((j % 2) ? x : y);
347                         }
348                 }
349             } else {
350                 res[i] = pathArray[i];
351             }
352             switch (res[i][0]) {
353                 case "Z":
354                     break;
355                 case "H": 
356                     x = res[i][1];
357                     break;
358                 case "V":
359                     y = res[i][1];
360                     break;
361                 default:
362                     x = res[i][res[i].length - 2];
363                     y = res[i][res[i].length - 1];
364             }
365         }
366         res.toString = pathArray.toString;
367         return res;
368     };
369     var pathEqualiser = function (path1, path2) {
370         var data = [pathToAbsolute(this.parsePathString(path1)), pathToAbsolute(this.parsePathString(path2))],
371             attrs = [{x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}, {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}],
372             processPath = function (path, d) {
373                 if (!path) {
374                     return ["U"];
375                 }
376                 switch (path[0]) {
377                     case "M":
378                         d.X = path[1];
379                         d.Y = path[2];
380                         break;
381                     case "S":
382                         var nx = d.x + (d.x - (d.bx || d.x));
383                         var ny = d.y + (d.y - (d.by || d.y));
384                         path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
385                         break;
386                     case "T":
387                         var nx = d.x + (d.x - (d.bx || d.x));
388                         var ny = d.y + (d.y - (d.by || d.y));
389                         path = ["Q", nx, ny, path[1], path[2]];
390                         break;
391                     case "H":
392                         path = ["L", path[1], d.y];
393                         break;
394                     case "V":
395                         path = ["L", d.x, path[1]];
396                         break;
397                     case "Z":
398                         path = ["L", d.X, d.Y];
399                         break;
400                 }
401                 return path;
402             },
403             edgeCases = function (a, b, i) {
404                 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
405                     data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
406                     attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
407                     attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
408                     attrs[a].x = data[a][i][data[a][i].length - 2];
409                     attrs[a].y = data[a][i][data[a][i].length - 1];
410                     return true;
411                 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
412                     data[a][i] = ["C", attrs[a].x, attrs[a].y, data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
413                 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
414                     data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
415                 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
416                     var x = data[b][i][data[b][i].length - 2];
417                     var y = data[b][i][data[b][i].length - 1];
418                     data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
419                     data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
420                     i++;
421                     attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
422                     attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
423                     attrs[b].x = data[b][i][data[b][i].length - 2];
424                     attrs[b].y = data[b][i][data[b][i].length - 1];
425                     return true;
426                 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
427                     var x = data[b][i][data[b][i].length - 2];
428                     var y = data[b][i][data[b][i].length - 1];
429                     data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
430                     data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
431                     i++;
432                     attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
433                     attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
434                     attrs[b].x = data[b][i][data[b][i].length - 2];
435                     attrs[b].y = data[b][i][data[b][i].length - 1];
436                     return true;
437                 } else if (data[a][i][0] == "U") {
438                     data[a][i][0] = data[b][i][0];
439                     for (var j = 1, jj = data[b][i].length; j < jj; j++) {
440                         data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
441                     }
442                 }
443                 return false;
444             };
445         for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
446             data[0][i] = processPath(data[0][i], attrs[0]);
447             data[1][i] = processPath(data[1][i], attrs[1]);
448             if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
449                 continue;
450             }
451             attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
452             attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
453             attrs[0].x = data[0][i][data[0][i].length - 2];
454             attrs[0].y = data[0][i][data[0][i].length - 1];
455             attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
456             attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
457             attrs[1].x = data[1][i][data[1][i].length - 2];
458             attrs[1].y = data[1][i][data[1][i].length - 1];
459         }
460         return data;
461     };
462
463     // SVG
464     if (R.svg) {
465         var thePath = function (params, pathString, SVG) {
466             var el = document.createElementNS(SVG.svgns, "path");
467             el.setAttribute("fill", "none");
468             if (SVG.canvas) {
469                 SVG.canvas.appendChild(el);
470             }
471             var p = new Element(el, SVG);
472             p.isAbsolute = true;
473             p.type = "path";
474             p.last = {x: 0, y: 0, bx: 0, by: 0};
475             p.absolutely = function () {
476                 this.isAbsolute = true;
477                 return this;
478             };
479             p.relatively = function () {
480                 this.isAbsolute = false;
481                 return this;
482             };
483             p.moveTo = function (x, y) {
484                 var d = this.isAbsolute?"M":"m";
485                 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
486                 var oldD = this[0].getAttribute("d") || "";
487                 (oldD == "M0,0") && (oldD = "");
488                 this[0].setAttribute("d", oldD + d);
489                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
490                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
491                 this.attrs.path = oldD + d;
492                 return this;
493             };
494             p.lineTo = function (x, y) {
495                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
496                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
497                 var d = this.isAbsolute?"L":"l";
498                 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
499                 var oldD = this[0].getAttribute("d") || "";
500                 this[0].setAttribute("d", oldD + d);
501                 this.attrs.path = oldD + d;
502                 return this;
503             };
504             p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
505                 var d = this.isAbsolute ? "A" : "a";
506                 d += [parseFloat(rx, 10).toFixed(3), parseFloat(ry, 10).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x, 10).toFixed(3), parseFloat(y, 10).toFixed(3)].join(" ");
507                 var oldD = this[0].getAttribute("d") || "";
508                 this[0].setAttribute("d", oldD + d);
509                 this.last.x = parseFloat(x, 10);
510                 this.last.y = parseFloat(y, 10);
511                 this.attrs.path = oldD + d;
512                 return this;
513             };
514             p.cplineTo = function (x1, y1, w1) {
515                 if (!w1) {
516                     return this.lineTo(x1, y1);
517                 } else {
518                     var p = {};
519                     var x = parseFloat(x1, 10);
520                     var y = parseFloat(y1, 10);
521                     var w = parseFloat(w1, 10);
522                     var d = this.isAbsolute?"C":"c";
523                     var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
524                     for (var i = 0, ii = attr.length; i < ii; i++) {
525                         d += attr[i].toFixed(3) + " ";
526                     }
527                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
528                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
529                     this.last.bx = attr[2];
530                     this.last.by = attr[3];
531                     var oldD = this[0].getAttribute("d") || "";
532                     this[0].setAttribute("d", oldD + d);
533                     this.attrs.path = oldD + d;
534                     return this;
535                 }
536             };
537             p.curveTo = function () {
538                 var p = {},
539                     command = [0, 1, 2, 3, "s", 5, "c"];
540
541                 var d = command[arguments.length];
542                 if (this.isAbsolute) {
543                     d = d.toUpperCase();
544                 }
545                 for (var i = 0, ii = arguments.length; i < ii; i++) {
546                     d += parseFloat(arguments[i], 10).toFixed(3) + " ";
547                 }
548                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
549                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
550                 this.last.bx = parseFloat(arguments[arguments.length - 4], 10);
551                 this.last.by = parseFloat(arguments[arguments.length - 3], 10);
552                 var oldD = this.node.getAttribute("d") || "";
553                 this.node.setAttribute("d", oldD + d);
554                 this.attrs.path = oldD + d;
555                 return this;
556             };
557             p.qcurveTo = function () {
558                 var p = {},
559                     command = [0, 1, "t", 3, "q"];
560
561                 var d = command[arguments.length];
562                 if (this.isAbsolute) {
563                     d = d.toUpperCase();
564                 }
565                 for (var i = 0, ii = arguments.length; i < ii; i++) {
566                     d += parseFloat(arguments[i], 10).toFixed(3) + " ";
567                 }
568                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
569                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
570                 if (arguments.length != 2) {
571                     this.last.qx = parseFloat(arguments[arguments.length - 4], 10);
572                     this.last.qy = parseFloat(arguments[arguments.length - 3], 10);
573                 }
574                 var oldD = this.node.getAttribute("d") || "";
575                 this.node.setAttribute("d", oldD + d);
576                 this.attrs.path = oldD + d;
577                 return this;
578             };
579             p.addRoundedCorner = function (r, dir) {
580                 var R = .5522 * r, rollback = this.isAbsolute, o = this;
581                 if (rollback) {
582                     this.relatively();
583                     rollback = function () {
584                         o.absolutely();
585                     };
586                 } else {
587                     rollback = function () {};
588                 }
589                 var actions = {
590                     l: function () {
591                         return {
592                             u: function () {
593                                 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
594                             },
595                             d: function () {
596                                 o.curveTo(-R, 0, -r, r - R, -r, r);
597                             }
598                         };
599                     },
600                     r: function () {
601                         return {
602                             u: function () {
603                                 o.curveTo(R, 0, r, -(r - R), r, -r);
604                             },
605                             d: function () {
606                                 o.curveTo(R, 0, r, r - R, r, r);
607                             }
608                         };
609                     },
610                     u: function () {
611                         return {
612                             r: function () {
613                                 o.curveTo(0, -R, -(R - r), -r, r, -r);
614                             },
615                             l: function () {
616                                 o.curveTo(0, -R, R - r, -r, -r, -r);
617                             }
618                         };
619                     },
620                     d: function () {
621                         return {
622                             r: function () {
623                                 o.curveTo(0, R, -(R - r), r, r, r);
624                             },
625                             l: function () {
626                                 o.curveTo(0, R, R - r, r, -r, r);
627                             }
628                         };
629                     }
630                 };
631                 actions[dir[0]]()[dir[1]]();
632                 rollback();
633                 return o;
634             };
635             p.andClose = function () {
636                 var oldD = this[0].getAttribute("d") || "";
637                 this[0].setAttribute("d", oldD + "Z ");
638                 this.attrs.path = oldD + "Z ";
639                 return this;
640             };
641             if (pathString) {
642                 p.attrs.path = "" + pathString;
643                 p.absolutely();
644                 paper.pathfinder(p, p.attrs.path);
645             }
646             if (params) {
647                 setFillAndStroke(p, params);
648             }
649             return p;
650         };
651         var addGrdientFill = function (o, gradient, SVG) {
652             var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
653             el.id = "raphael-gradient-" + Raphael.idGenerator++;
654             if (gradient.vector && gradient.vector.length) {
655                 el.setAttribute("x1", gradient.vector[0]);
656                 el.setAttribute("y1", gradient.vector[1]);
657                 el.setAttribute("x2", gradient.vector[2]);
658                 el.setAttribute("y2", gradient.vector[3]);
659             }
660             SVG.defs.appendChild(el);
661             for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
662                 var stop = document.createElementNS(SVG.svgns, "stop");
663                 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
664                 stop.setAttribute("stop-color", getRGB(gradient.dots[i].color).hex || "#fff");
665                 if (typeof gradient.dots[i].opacity != "undefined") {
666                     stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
667                 }
668                 el.appendChild(stop);
669             };
670             o.setAttribute("fill", "url(#" + el.id + ")");
671         };
672         var updatePosition = function (o) {
673             if (o.pattern) {
674                 var bbox = o.node.getBBox();
675                 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
676             }
677         };
678         var setFillAndStroke = function (o, params) {
679             var dasharray = {
680                 "-": [3, 1],
681                 ".": [1, 1],
682                 "-.": [3, 1, 1, 1],
683                 "-..": [3, 1, 1, 1, 1, 1],
684                 ". ": [1, 3],
685                 "- ": [4, 3],
686                 "--": [8, 3],
687                 "- .": [4, 3, 1, 3],
688                 "--.": [8, 3, 1, 3],
689                 "--..": [8, 3, 1, 3, 1, 3]
690             },
691             addDashes = function (o, value) {
692                 value = dasharray[value.toString().toLowerCase()];
693                 if (value) {
694                     var width = o.attrs["stroke-width"] || "1",
695                         butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
696                         dashes = [];
697                     for (var i = 0, ii = value.length; i < ii; i++) {
698                         dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
699                     }
700                     value = dashes.join(",");
701                     o.node.setAttribute("stroke-dasharray", value);
702                 }
703             };
704             for (var att in params) {
705                 var value = params[att];
706                 o.attrs[att] = value;
707                 switch (att) {
708                     case "path":
709                         if (o.type == "path") {
710                             o.node.setAttribute("d", "M0,0");
711                             paper.pathfinder(o, value);
712                         }
713                     case "rx":
714                     case "cx":
715                     case "x":
716                         o.node.setAttribute(att, value);
717                         updatePosition(o);
718                         break;
719                     case "ry":
720                     case "cy":
721                     case "y":
722                         o.node.setAttribute(att, value);
723                         updatePosition(o);
724                         break;
725                     case "width":
726                         o.node.setAttribute(att, value);
727                         break;
728                     case "height":
729                         o.node.setAttribute(att, value);
730                         break;
731                     case "gradient":
732                         addGrdientFill(o.node, value, o.svg);
733                         break;
734                     case "stroke-width":
735                         o.node.style.strokeWidth = value;
736                         // Need following line for Firefox
737                         o.node.setAttribute(att, value);
738                         if (o.attrs["stroke-dasharray"]) {
739                             addDashes(o, o.attrs["stroke-dasharray"]);
740                         }
741                         break;
742                     case "stroke-dasharray":
743                         addDashes(o, value);
744                         break;
745                     case "text":
746                         if (o.type == "text") {
747                             o.node.childNodes.length && o.node.removeChild(o.node.firstChild);
748                             o.node.appendChild(document.createTextNode(value));
749                         }
750                         break;
751                     case "rotation":
752                         o.rotate(value, true);
753                         break;
754                     case "translation":
755                         var xy = value.split(/[, ]+/);
756                         o.translate(xy[0], xy[1]);
757                         break;
758                     case "scale":
759                         var xy = value.split(/[, ]+/);
760                         o.scale(xy[0], xy[1]);
761                         break;
762                     case "fill":
763                         var isURL = value.match(/^url\(([^\)]+)\)$/i);
764                         if (isURL) {
765                             var el = document.createElementNS(o.svg.svgns, "pattern");
766                             var ig = document.createElementNS(o.svg.svgns, "image");
767                             el.id = "raphael-pattern-" + Raphael.idGenerator++;
768                             el.setAttribute("x", 0);
769                             el.setAttribute("y", 0);
770                             el.setAttribute("patternUnits", "userSpaceOnUse");
771                             ig.setAttribute("x", 0);
772                             ig.setAttribute("y", 0);
773                             ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
774                             el.appendChild(ig);
775
776                             var img = document.createElement("img");
777                             img.style.position = "absolute";
778                             img.style.top = "-9999em";
779                             img.style.left = "-9999em";
780                             img.onload = function () {
781                                 el.setAttribute("width", this.offsetWidth);
782                                 el.setAttribute("height", this.offsetHeight);
783                                 ig.setAttribute("width", this.offsetWidth);
784                                 ig.setAttribute("height", this.offsetHeight);
785                                 document.body.removeChild(this);
786                                 paper.safari();
787                             };
788                             document.body.appendChild(img);
789                             img.src = isURL[1];
790                             o.svg.defs.appendChild(el);
791                             o.node.style.fill = "url(#" + el.id + ")";
792                             o.node.setAttribute("fill", "url(#" + el.id + ")");
793                             o.pattern = el;
794                             updatePosition(o);
795                             break;
796                         }
797                     case "stroke":
798                         o.node.style[att] = getRGB(value).hex;
799                         // Need following line for Firefox
800                         o.node.setAttribute(att, getRGB(value).hex);
801                         break;
802                     default :
803                         var cssrule = att.replace(/(\-.)/g, function (w) {
804                             return w.substring(1).toUpperCase();
805                         });
806                         o.node.style[cssrule] = value;
807                         // Need following line for Firefox
808                         o.node.setAttribute(att, value);
809                         break;
810                 }
811             }
812         };
813         var Element = function (node, svg) {
814             var X = 0,
815                 Y = 0;
816             this[0] = node;
817             this.node = node;
818             this.svg = svg;
819             this.attrs = this.attrs || {};
820             this.transformations = []; // rotate, translate, scale
821             this._ = {
822                 tx: 0,
823                 ty: 0,
824                 rt: {deg: 0, x: 0, y: 0},
825                 sx: 1,
826                 sy: 1
827             };
828         };
829         Element.prototype.translate = function (x, y) {
830             if (x == undefined && y == undefined) {
831                 return {x: this._.tx, y: this._.ty};
832             }
833             this._.tx += +x;
834             this._.ty += +y;
835             switch (this.type) {
836                 case "circle":
837                 case "ellipse":
838                     this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
839                     break;
840                 case "rect":
841                 case "image":
842                 case "text":
843                     this.attr({x: this.attrs.x + x, y: this.attrs.y + y});
844                     break;
845                 case "path":
846                     var path = pathToRelative(this.attrs.path);
847                     path[0][1] += +x;
848                     path[0][2] += +y;
849                     this.attr({path: path.join(" ")});
850                 break;
851             }
852             return this;
853         };
854         Element.prototype.rotate = function (deg, isAbsolute, cx, cy) {
855             if (deg == undefined) {
856                 return this._.rt.deg;
857             }
858             var bbox = this.getBBox();
859             if (isAbsolute) {
860                 this._.rt.deg = deg;
861             } else {
862                 this._.rt.deg += deg;
863             }
864
865             cx = cx || bbox.x + bbox.width / 2;
866             cy = cy || bbox.y + bbox.height / 2;
867             if (this._.rt.deg) {
868                 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
869             } else {
870                 this.transformations[0] = "";
871             }
872             this.node.setAttribute("transform", this.transformations.join(" "));
873             return this;
874         };
875         Element.prototype.hide = function () {
876             this.node.style.display = "none";
877             return this;
878         };
879         Element.prototype.show = function () {
880             this.node.style.display = "block";
881             return this;
882         };
883         Element.prototype.remove = function () {
884             this.node.parentNode.removeChild(this.node);
885         };
886         Element.prototype.getBBox = function () {
887             return this.node.getBBox();
888         };
889         Element.prototype.attr = function () {
890             if (arguments.length == 1 && typeof arguments[0] == "string") {
891                 if (arguments[0] == "translation") {
892                     return this.translate();
893                 }
894                 return this.attrs[arguments[0]];
895             }
896             if (arguments.length == 1 && arguments[0] instanceof Array) {
897                 var values = {};
898                 for (var j in arguments[0]) {
899                     values[arguments[0][j]] = this.attrs[arguments[0][j]];
900                 }
901                 return values;
902             }
903             if (arguments.length == 2) {
904                 var params = {};
905                 params[arguments[0]] = arguments[1];
906                 setFillAndStroke(this, params);
907             } else if (arguments.length == 1 && typeof arguments[0] == "object") {
908                 setFillAndStroke(this, arguments[0]);
909             }
910             return this;
911         };
912         Element.prototype.toFront = function () {
913             this.node.parentNode.appendChild(this.node);
914             return this;
915         };
916         Element.prototype.toBack = function () {
917             if (this.node.parentNode.firstChild != this.node) {
918                 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
919             }
920             return this;
921         };
922         Element.prototype.insertAfter = function (element) {
923             if (element.node.nextSibling) {
924                 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
925             } else {
926                 element.node.parentNode.appendChild(this.node);
927             }
928             return this;
929         };
930         Element.prototype.insertBefore = function (element) {
931             element.node.parentNode.insertBefore(this.node, element.node);
932             return this;
933         };
934         var theCircle = function (svg, x, y, r) {
935             var el = document.createElementNS(svg.svgns, "circle");
936             el.setAttribute("cx", x);
937             el.setAttribute("cy", y);
938             el.setAttribute("r", r);
939             el.setAttribute("fill", "none");
940             el.setAttribute("stroke", "#000");
941             if (svg.canvas) {
942                 svg.canvas.appendChild(el);
943             }
944             var res = new Element(el, svg);
945             res.attrs = res.attrs || {};
946             res.attrs.cx = x;
947             res.attrs.cy = y;
948             res.attrs.r = r;
949             res.attrs.stroke = "#000";
950             res.type = "circle";
951             return res;
952         };
953         var theRect = function (svg, x, y, w, h, r) {
954             var el = document.createElementNS(svg.svgns, "rect");
955             el.setAttribute("x", x);
956             el.setAttribute("y", y);
957             el.setAttribute("width", w);
958             el.setAttribute("height", h);
959             if (r) {
960                 el.setAttribute("rx", r);
961                 el.setAttribute("ry", r);
962             }
963             el.setAttribute("fill", "none");
964             el.setAttribute("stroke", "#000");
965             if (svg.canvas) {
966                 svg.canvas.appendChild(el);
967             }
968             var res = new Element(el, svg);
969             res.attrs = res.attrs || {};
970             res.attrs.x = x;
971             res.attrs.y = y;
972             res.attrs.width = w;
973             res.attrs.height = h;
974             res.attrs.stroke = "#000";
975             if (r) {
976                 res.attrs.rx = res.attrs.ry = r;
977             }
978             res.type = "rect";
979             return res;
980         };
981         var theEllipse = function (svg, x, y, rx, ry) {
982             var el = document.createElementNS(svg.svgns, "ellipse");
983             el.setAttribute("cx", x);
984             el.setAttribute("cy", y);
985             el.setAttribute("rx", rx);
986             el.setAttribute("ry", ry);
987             el.setAttribute("fill", "none");
988             el.setAttribute("stroke", "#000");
989             if (svg.canvas) {
990                 svg.canvas.appendChild(el);
991             }
992             var res = new Element(el, svg);
993             res.attrs = res.attrs || {};
994             res.attrs.cx = x;
995             res.attrs.cy = y;
996             res.attrs.rx = rx;
997             res.attrs.ry = ry;
998             res.attrs.stroke = "#000";
999             res.type = "ellipse";
1000             return res;
1001         };
1002         var theImage = function (svg, src, x, y, w, h) {
1003             var el = document.createElementNS(svg.svgns, "image");
1004             el.setAttribute("x", x);
1005             el.setAttribute("y", y);
1006             el.setAttribute("width", w);
1007             el.setAttribute("height", h);
1008             el.setAttribute("preserveAspectRatio", "none");
1009             el.setAttributeNS(svg.xlink, "href", src);
1010             if (svg.canvas) {
1011                 svg.canvas.appendChild(el);
1012             }
1013             var res = new Element(el, svg);
1014             res.attrs = res.attrs || {};
1015             res.attrs.x = x;
1016             res.attrs.y = y;
1017             res.attrs.width = w;
1018             res.attrs.height = h;
1019             res.type = "image";
1020             return res;
1021         };
1022         var theText = function (svg, x, y, text) {
1023             var el = document.createElementNS(svg.svgns, "text");
1024             el.setAttribute("x", x);
1025             el.setAttribute("y", y);
1026             el.setAttribute("text-anchor", "middle");
1027             if (text) {
1028                 el.appendChild(document.createTextNode(text));
1029             }
1030             if (svg.canvas) {
1031                 svg.canvas.appendChild(el);
1032             }
1033             var res = new Element(el, svg);
1034             res.attrs = res.attrs || {};
1035             res.attrs.x = x;
1036             res.attrs.y = y;
1037             res.type = "text";
1038             setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000"});
1039             return res;
1040         };
1041         var theGroup = function (svg) {
1042             var el = document.createElementNS(svg.svgns, "g");
1043             if (svg.canvas) {
1044                 svg.canvas.appendChild(el);
1045             }
1046             var i = new Element(el, svg);
1047             for (var f in svg) {
1048                 if (f[0] != "_" && typeof svg[f] == "function") {
1049                     i[f] = (function (f) {
1050                         return function () {
1051                             var e = svg[f].apply(svg, arguments);
1052                             el.appendChild(e[0]);
1053                             return e;
1054                         };
1055                     })(f);
1056                 }
1057             }
1058             i.type = "group";
1059             return i;
1060         };
1061         create = function () {
1062             // container, width, height
1063             // x, y, width, height
1064             if (typeof arguments[0] == "string") {
1065                 var container = document.getElementById(arguments[0]);
1066                 var width = arguments[1];
1067                 var height = arguments[2];
1068             }
1069             if (typeof arguments[0] == "object") {
1070                 var container = arguments[0];
1071                 var width = arguments[1];
1072                 var height = arguments[2];
1073             }
1074             if (typeof arguments[0] == "number") {
1075                 var container = 1,
1076                     x = arguments[0],
1077                     y = arguments[1],
1078                     width = arguments[2],
1079                     height = arguments[3];
1080             }
1081             if (!container) {
1082                 throw new Error("SVG container not found.");
1083             }
1084             paper.canvas = document.createElementNS(paper.svgns, "svg");
1085             paper.canvas.setAttribute("width", width || 320);
1086             paper.width = width || 320;
1087             paper.canvas.setAttribute("height", height || 200);
1088             paper.height = height || 200;
1089             if (container == 1) {
1090                 document.body.appendChild(paper.canvas);
1091                 paper.canvas.style.position = "absolute";
1092                 paper.canvas.style.left = x + "px";
1093                 paper.canvas.style.top = y + "px";
1094             } else {
1095                 if (container.firstChild) {
1096                     container.insertBefore(paper.canvas, container.firstChild);
1097                 } else {
1098                     container.appendChild(paper.canvas);
1099                 }
1100             }
1101             container = {
1102                 canvas: paper.canvas,
1103                 clear: function () {
1104                     while (this.canvas.firstChild) {
1105                         this.canvas.removeChild(this.canvas.firstChild);
1106                     }
1107                     this.defs = document.createElementNS(paper.svgns, "defs");
1108                     this.canvas.appendChild(this.defs);
1109                 }
1110             };
1111             for (var prop in paper) {
1112                 if (prop != "create") {
1113                     container[prop] = paper[prop];
1114                 }
1115             }
1116             for (var prop in R.fn) {
1117                 if (!container[prop]) {
1118                     container[prop] = R.fn[prop];
1119                 }
1120             }
1121             container.clear();
1122             return container;
1123         };
1124         paper.remove = function () {
1125             this.canvas.parentNode.removeChild(this.canvas);
1126         };
1127         paper.svgns = "http://www.w3.org/2000/svg";
1128         paper.xlink = "http://www.w3.org/1999/xlink";
1129         paper.safari = function () {
1130             if (navigator.vendor == "Apple Computer, Inc.") {
1131                 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1132                 setTimeout(function () {rect.remove();}, 0);
1133             }
1134         };
1135     }
1136
1137     // VML
1138     if (R.vml) {
1139         thePath = function (params, pathString, VML) {
1140             var g = document.createElement("rvml:group"), gl = g.style;
1141             gl.position = "absolute";
1142             gl.left = 0;
1143             gl.top = 0;
1144             gl.width = VML.width + "px";
1145             gl.height = VML.height + "px";
1146             var el = document.createElement("rvml:shape"), ol = el.style;
1147             ol.width = VML.width + "px";
1148             ol.height = VML.height + "px";
1149             el.path = "";
1150             if (params["class"]) {
1151                 el.className = params["class"];
1152             }
1153             el.coordsize = this.coordsize;
1154             el.coordorigin = this.coordorigin;
1155             g.appendChild(el);
1156             VML.canvas.appendChild(g);
1157             var p = new Element(el, g, VML);
1158             p.isAbsolute = true;
1159             p.type = "path";
1160             p.path = [];
1161             p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1162             p.Path = "";
1163             p.absolutely = function () {
1164                 this.isAbsolute = true;
1165                 return this;
1166             };
1167             p.relatively = function () {
1168                 this.isAbsolute = false;
1169                 return this;
1170             };
1171             p.moveTo = function (x, y) {
1172                 var d = this.isAbsolute?"m":"t";
1173                 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1174                 this.node.path = this.Path += d;
1175                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1176                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1177                 this.last.isAbsolute = this.isAbsolute;
1178                 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1179                 return this;
1180             };
1181             p.lineTo = function (x, y) {
1182                 var d = this.isAbsolute?"l":"r";
1183                 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1184                 this[0].path = this.Path += d;
1185                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1186                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1187                 this.last.isAbsolute = this.isAbsolute;
1188                 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1189                 return this;
1190             };
1191             p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1192                 // for more information of where this math came from visit:
1193                 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1194                 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1195                 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1196                 var x1 = this.last.x,
1197                     y1 = this.last.y,
1198                     x = (x1 - x2) / 2,
1199                     y = (y1 - y2) / 2,
1200                     k = (large_arc_flag == sweep_flag ? -1 : 1) *
1201                         Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1202                     cx = k * rx * y / ry + (x1 + x2) / 2,
1203                     cy = k * -ry * x / rx + (y1 + y2) / 2,
1204                     d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1205                     left = Math.round(cx - rx),
1206                     top = Math.round(cy - ry);
1207                 d += [left, top, Math.round(left + rx * 2), Math.round(top + ry * 2), Math.round(x1), Math.round(y1), Math.round(parseFloat(x2, 10)), Math.round(parseFloat(y2, 10))].join(", ");
1208                 this.node.path = this.Path += d;
1209                 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2, 10);
1210                 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2, 10);
1211                 this.last.isAbsolute = this.isAbsolute;
1212                 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1213                 return this;
1214             };
1215             p.cplineTo = function (x1, y1, w1) {
1216                 if (!w1) {
1217                     return this.lineTo(x1, y1);
1218                 } else {
1219                     var x = Math.round(Math.round(parseFloat(x1, 10) * 100) / 100),
1220                         y = Math.round(Math.round(parseFloat(y1, 10) * 100) / 100),
1221                         w = Math.round(Math.round(parseFloat(w1, 10) * 100) / 100),
1222                         d = this.isAbsolute ? "c" : "v",
1223                         attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1224                         svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1225                     d += attr.join(" ") + " ";
1226                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1227                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1228                     this.last.bx = attr[2];
1229                     this.last.by = attr[3];
1230                     this.node.path = this.Path += d;
1231                     this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1232                     return this;
1233                 }
1234             };
1235             p.curveTo = function () {
1236                 var d = this.isAbsolute ? "c" : "v";
1237                 if (arguments.length == 6) {
1238                     this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1239                     this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1240                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4], 10);
1241                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5], 10);
1242                     d += [Math.round(parseFloat(arguments[0], 10)),
1243                          Math.round(parseFloat(arguments[1], 10)),
1244                          Math.round(parseFloat(arguments[2], 10)),
1245                          Math.round(parseFloat(arguments[3], 10)),
1246                          Math.round(parseFloat(arguments[4], 10)),
1247                          Math.round(parseFloat(arguments[5], 10))].join(" ") + " ";
1248                     this.last.isAbsolute = this.isAbsolute;
1249                     this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1250                 }
1251                 if (arguments.length == 4) {
1252                     var bx = this.last.x * 2 - this.last.bx;
1253                     var by = this.last.y * 2 - this.last.by;
1254                     this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1255                     this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1256                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1257                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1258                     d += [Math.round(bx), Math.round(by),
1259                          Math.round(parseFloat(arguments[0], 10)),
1260                          Math.round(parseFloat(arguments[1], 10)),
1261                          Math.round(parseFloat(arguments[2], 10)),
1262                          Math.round(parseFloat(arguments[3], 10))].join(" ") + " ";
1263                      this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1264                 }
1265                 this.node.path = this.Path += d;
1266                 return this;
1267             };
1268             p.qcurveTo = function () {
1269                 var d = "qb";
1270                 if (arguments.length == 4) {
1271                     this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1272                     this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1273                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1274                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1275                     d += [Math.round(this.last.qx),
1276                          Math.round(this.last.qy),
1277                          Math.round(this.last.x),
1278                          Math.round(this.last.y)].join(" ") + " ";
1279                     this.last.isAbsolute = this.isAbsolute;
1280                     this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1281                 }
1282                 if (arguments.length == 2) {
1283                     this.last.qx = this.last.x * 2 - this.last.qx;
1284                     this.last.qy = this.last.y * 2 - this.last.qy;
1285                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1286                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1287                     d += [Math.round(this.last.qx),
1288                          Math.round(this.last.qy),
1289                          Math.round(this.last.x),
1290                          Math.round(this.last.y)].join(" ") + " ";
1291                      this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1292                 }
1293                 this.node.path = this.Path += d;
1294                 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1295                 return this;
1296             };
1297             p.addRoundedCorner = function (r, dir) {
1298                 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1299                 if (rollback) {
1300                     this.relatively();
1301                     rollback = function () {
1302                         o.absolutely();
1303                     };
1304                 } else {
1305                     rollback = function () {};
1306                 }
1307                 var actions = {
1308                     l: function () {
1309                         return {
1310                             u: function () {
1311                                 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1312                             },
1313                             d: function () {
1314                                 o.curveTo(-R, 0, -r, r - R, -r, r);
1315                             }
1316                         };
1317                     },
1318                     r: function () {
1319                         return {
1320                             u: function () {
1321                                 o.curveTo(R, 0, r, -(r - R), r, -r);
1322                             },
1323                             d: function () {
1324                                 o.curveTo(R, 0, r, r - R, r, r);
1325                             }
1326                         };
1327                     },
1328                     u: function () {
1329                         return {
1330                             r: function () {
1331                                 o.curveTo(0, -R, -(R - r), -r, r, -r);
1332                             },
1333                             l: function () {
1334                                 o.curveTo(0, -R, R - r, -r, -r, -r);
1335                             }
1336                         };
1337                     },
1338                     d: function () {
1339                         return {
1340                             r: function () {
1341                                 o.curveTo(0, R, -(R - r), r, r, r);
1342                             },
1343                             l: function () {
1344                                 o.curveTo(0, R, R - r, r, -r, r);
1345                             }
1346                         };
1347                     }
1348                 };
1349                 actions[dir.charAt(0)]()[dir.charAt(1)]();
1350                 rollback();
1351                 return o;
1352             };
1353             p.andClose = function () {
1354                 this.node.path = (this.Path += "x e");
1355                 this.attrs.path += "z";
1356                 return this;
1357             };
1358             if (pathString) {
1359                 p.absolutely();
1360                 p.attrs.path = "";
1361                 paper.pathfinder(p, "" + pathString);
1362             }
1363             // p.setBox();
1364             setFillAndStroke(p, params);
1365             if (params.gradient) {
1366                 addGrdientFill(p, params.gradient);
1367             }
1368             return p;
1369         };
1370         var setFillAndStroke = function (o, params) {
1371             var s = o.node.style,
1372                 res = o;
1373             o.attrs = o.attrs || {};
1374             for (var par in params) {
1375                 o.attrs[par] = params[par];
1376             }
1377             if (params.path && o.type == "path") {
1378                 o.Path = "";
1379                 o.path = [];
1380                 paper.pathfinder(o, params.path);
1381             }
1382             if (params.rotation != null) {
1383                 o.Group.style.rotation = params.rotation;
1384             }
1385             if (params.translation) {
1386                 var xy = params.translation.split(/[, ]+/);
1387                 o.translate(xy[0], xy[1]);
1388             }
1389             if (params.scale) {
1390                 var xy = params.scale.split(/[, ]+/);
1391                 o.scale(xy[0], xy[1]);
1392             }
1393             if (o.type == "image" && params.opacity) {
1394                 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1395                 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1396             }
1397             params.font && (s.font = params.font);
1398             params["font-family"] && (s.fontFamily = params["font-family"]);
1399             params["font-size"] && (s.fontSize = params["font-size"]);
1400             params["font-weight"] && (s.fontWeight = params["font-weight"]);
1401             params["font-style"] && (s.fontStyle = params["font-style"]);
1402             if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
1403                 o = o.shape || o.node;
1404                 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
1405                 if ("fill-opacity" in params || "opacity" in params) {
1406                     fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1407                 }
1408                 if (params.fill) {
1409                     fill.on = true;
1410                 }
1411                 if (fill.on == undefined || params.fill == "none") {
1412                     fill.on = false;
1413                 }
1414                 if (fill.on && params.fill) {
1415                     var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1416                     if (isURL) {
1417                         fill.src = isURL[1];
1418                         fill.type = "tile";
1419                     } else {
1420                         fill.color = getRGB(params.fill).hex;
1421                         fill.src = "";
1422                         fill.type = "solid";
1423                     }
1424                 }
1425                 o.appendChild(fill);
1426                 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
1427                 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
1428                     stroke.on = true;
1429                 }
1430                 if (params.stroke == "none" || typeof stroke.on == "undefined") {
1431                     stroke.on = false;
1432                 }
1433                 if (stroke.on && params.stroke) {
1434                     stroke.color = getRGB(params.stroke).hex;
1435                 }
1436                 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1437                 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1438                 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1439                 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1440                 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
1441                 if (params["stroke-dasharray"]) {
1442                     var dasharray = {
1443                         "-": "shortdash",
1444                         ".": "shortdot",
1445                         "-.": "shortdashdot",
1446                         "-..": "shortdashdotdot",
1447                         ". ": "dot",
1448                         "- ": "dash",
1449                         "--": "longdash",
1450                         "- .": "dashdot",
1451                         "--.": "longdashdot",
1452                         "--..": "longdashdotdot"
1453                     };
1454                     stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1455                 }
1456                 o.appendChild(stroke);
1457             }
1458             if (res.type == "text") {
1459                 var span = document.createElement("span"),
1460                     s = span.style;
1461                 res.attrs.font && (s.font = res.attrs.font);
1462                 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1463                 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1464                 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1465                 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1466                 res.node.parentNode.appendChild(span);
1467                 span.innerText = res.node.string;
1468                 res.W = res.attrs.w = span.offsetWidth;
1469                 res.H = res.attrs.h = span.offsetHeight;
1470                 res.X = res.attrs.x - Math.round(res.W / 2);
1471                 res.Y = res.attrs.y - Math.round(res.H / 2);
1472                 res.node.parentNode.removeChild(span);
1473             }
1474         };
1475         var getAngle = function (a, b, c, d) {
1476             var angle = Math.round(Math.atan((parseFloat(c, 10) - parseFloat(a, 10)) / (parseFloat(d, 10) - parseFloat(b, 10))) * 57.29) || 0;
1477             if (!angle && parseFloat(a, 10) < parseFloat(b, 10)) {
1478                 angle = 180;
1479             }
1480             angle -= 180;
1481             if (angle < 0) {
1482                 angle += 360;
1483             }
1484             return angle;
1485         };
1486         var addGrdientFill = function (o, gradient) {
1487             o.attrs = o.attrs || {};
1488             o.attrs.gradient = gradient;
1489             o = o.shape || o[0];
1490             var fill = o.getElementsByTagName("fill");
1491             if (fill.length) {
1492                 fill = fill[0];
1493             } else {
1494                 fill = document.createElement("rvml:fill");
1495             }
1496             if (gradient.dots.length) {
1497                 fill.on = true;
1498                 fill.method = "none";
1499                 fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientTitle";
1500                 if (typeof gradient.dots[0].color != "undefined") {
1501                     fill.color = getRGB(gradient.dots[0].color).hex;
1502                 }
1503                 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1504                     fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1505                 }
1506                 var colors = [];
1507                 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1508                     if (gradient.dots[i].offset) {
1509                         colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1510                     }
1511                 };
1512                 var fillOpacity = typeof gradient.dots[0].opacity == "undefined" ? 1 : gradient.dots[0].opacity;
1513                 var fillOpacity2 = typeof gradient.dots[gradient.dots.length - 1].opacity == "undefined" ? 1 : gradient.dots[gradient.dots.length - 1].opacity;
1514                 if (colors) {
1515                     fill.colors.value = colors.join(",");
1516                     fillOpacity2 += fillOpacity;
1517                     fillOpacity = fillOpacity2 - fillOpacity;
1518                     fillOpacity2 -= fillOpacity;
1519                 }
1520                 fill.setAttribute("opacity", fillOpacity);
1521                 fill.setAttribute("opacity2", fillOpacity2);
1522                 if (gradient.vector) {
1523                     fill.angle = getAngle.apply(null, gradient.vector);
1524                 }
1525                 if (gradient.type.toLowerCase() == "radial") {
1526                     fill.focus = "100%";
1527                     fill.focusposition = "0.5 0.5";
1528                 }
1529             }
1530         };
1531         var Element = function (node, group, vml) {
1532             var Rotation = 0,
1533                 RotX = 0,
1534                 RotY = 0,
1535                 Scale = 1;
1536             this[0] = node;
1537             this.node = node;
1538             this.X = 0;
1539             this.Y = 0;
1540             this.attrs = {};
1541             this.Group = group;
1542             this.vml = vml;
1543             this._ = {
1544                 tx: 0,
1545                 ty: 0,
1546                 rt: 0,
1547                 sx: 1,
1548                 sy: 1
1549             };
1550         };
1551         Element.prototype.rotate = function (deg, isAbsolute, cx, cy) {
1552             if (deg == undefined) {
1553                 return this._.rt;
1554             }
1555             if (isAbsolute) {
1556                 this._.rt = deg;
1557             } else {
1558                 this._.rt += deg;
1559             }
1560             this.setBox(null, cx, cy);
1561             this.Group.style.rotation = this._.rt;
1562             return this;
1563         };
1564         Element.prototype.setBox = function (params, cx, cy) {
1565             var gs = this.Group.style,
1566                 os = (this.shape && this.shape.style) || this.node.style;
1567             for (var i in params) {
1568                 this.attrs[i] = params[i];
1569             }
1570             var attr = this.attrs, x, y, w, h;
1571             switch (this.type) {
1572                 case "circle": 
1573                     x = attr.cx - attr.r;
1574                     y = attr.cy - attr.r;
1575                     w = h = attr.r * 2;
1576                     break;
1577                 case "ellipse":
1578                     x = attr.cx - attr.rx;
1579                     y = attr.cy - attr.ry;
1580                     w = attr.rx * 2;
1581                     h = attr.ry * 2;
1582                     break;
1583                 case "rect":
1584                 case "image":
1585                     x = attr.x;
1586                     y = attr.y;
1587                     w = attr.width || 0;
1588                     h = attr.height || 0;
1589                     break;
1590                 case "text":
1591                     this.textpath.v = ["m", Math.round(attr.x), ", ", Math.round(attr.y - 2), "l", Math.round(attr.x) + 1, ", ", Math.round(attr.y - 2)].join("");
1592                     x = attr.x - Math.round(this.W / 2);
1593                     y = attr.y - this.H / 2;
1594                     w = this.W;
1595                     h = this.H;
1596                     break;
1597                 case "path":
1598                     if (!this.attrs.path) {
1599                         x = 0;
1600                         y = 0;
1601                         w = this.vml.width;
1602                         h = this.vml.height;
1603                     } else {
1604                         var dim = pathDimensions(this.attrs.path),
1605                         x = dim.x;
1606                         y = dim.y;
1607                         w = dim.width;
1608                         h = dim.height;
1609                     }
1610                     break;
1611                 default:
1612                     x = 0;
1613                     y = 0;
1614                     w = this.vml.width;
1615                     h = this.vml.height;
1616                     break;
1617             }
1618             cx = cx || x + w / 2;
1619             cy = cy || y + h / 2;
1620             var left = cx - this.vml.width / 2,
1621                 top = cy - this.vml.height / 2;
1622             if (this.type == "path" || this.type == "text") {
1623                 gs.left = left + "px";
1624                 gs.top = top + "px";
1625                 this.X = this.type == "text" ? x : -left;
1626                 this.Y = this.type == "text" ? y : -top;
1627                 this.W = w;
1628                 this.H = h;
1629                 os.left = -left + "px";
1630                 os.top = -top + "px";
1631                 // var left = Math.round(this.vml.width / 2 - w / 2 - x),
1632                 //     top = Math.round(this.vml.height / 2 - h / 2 - y);
1633                 // gs.left = - left + "px";
1634                 // gs.top = - top + "px";
1635                 // this.X = this.type == "text" ? x : left;
1636                 // this.Y = this.type == "text" ? y : top;
1637                 // this.W = w;
1638                 // this.H = h;
1639                 // os.top = top + "px";
1640                 // os.left = left + "px";
1641             } else {
1642                 gs.left = left + "px";
1643                 gs.top = top + "px";
1644                 this.X = x - left;
1645                 this.Y = y - top;
1646                 this.W = w;
1647                 this.H = h;
1648                 gs.width = this.vml.width + "px";
1649                 gs.height = this.vml.height + "px";
1650                 os.left = x - left + "px";
1651                 os.top = y - top + "px";
1652                 os.width = w + "px";
1653                 os.height = h + "px";
1654             }
1655         };
1656         Element.prototype.hide = function () {
1657             this.Group.style.display = "none";
1658             return this;
1659         };
1660         Element.prototype.show = function () {
1661             this.Group.style.display = "block";
1662             return this;
1663         };
1664         Element.prototype.translate = function (x, y) {
1665             if (x == undefined && y == undefined) {
1666                 return {x: this._.tx, y: this._.ty};
1667             }
1668             this._.tx += +x;
1669             this._.ty += +y;
1670             if (this.type == "path") {
1671                 var path = this.attrs.path;
1672                 path = pathToRelative(path);
1673                 path[0][1] += +x;
1674                 path[0][2] += +y;
1675                 this.attr({path: path.join(" ")});
1676             }
1677             this.setBox({x: this._.tx, y: this._.ty});
1678             return this;
1679         };
1680         Element.prototype.getBBox = function () {
1681             return {
1682                 x: this.X,
1683                 y: this.Y,
1684                 width: this.W,
1685                 height: this.H
1686             };
1687         };
1688         Element.prototype.remove = function () {
1689             this[0].parentNode.removeChild(this[0]);
1690             this.Group.parentNode.removeChild(this.Group);
1691             this.shape && this.shape.parentNode.removeChild(this.shape);
1692         };
1693         Element.prototype.attr = function () {
1694             if (arguments.length == 1 && typeof arguments[0] == "string") {
1695                 if (arguments[0] == "translation") {
1696                     return this.translate();
1697                 }
1698                 return this.attrs[arguments[0]];
1699             }
1700             if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
1701                 var values = {};
1702                 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1703                     values[arguments[0][i]] = this.attrs[arguments[0][i]];
1704                 };
1705                 return values;
1706             }
1707             if (this.node.tagName.toLowerCase() == "group") {
1708                 var children = this[0].childNodes;
1709                 this.attrs = this.attrs || {};
1710                 if (arguments.length == 2) {
1711                     this.attrs[arguments[0]] = arguments[1];
1712                 } else if (arguments.length == 1 || typeof arguments[0] == "object") {
1713                     for (var j in arguments[0]) {
1714                         this.attrs[j] = arguments[0][j];
1715                     }
1716                 }
1717                 for (var i = 0, ii = children.length; i < ii; i++) {
1718                     this.attr.apply(new item(children[i], this[0], this.vml), arguments);
1719                 }
1720             } else {
1721                 var params;
1722                 if (arguments.length == 2) {
1723                     params = {};
1724                     params[arguments[0]] = arguments[1];
1725                 }
1726                 if (arguments.length == 1 && typeof arguments[0] == "object") {
1727                     params = arguments[0];
1728                 }
1729                 if (params) {
1730                     if (params.gradient) {
1731                         addGrdientFill(this, params.gradient);
1732                     }
1733                     if (params.text && this.type == "text") {
1734                         this.node.string = params.text;
1735                     }
1736                     if (params.id) {
1737                         this.node.id = params.id;
1738                     }
1739                     setFillAndStroke(this, params);
1740                     // this.setBox(params);
1741                 }
1742             }
1743             return this;
1744         };
1745         Element.prototype.toFront = function () {
1746             this.Group.parentNode.appendChild(this.Group);
1747             return this;
1748         };
1749         Element.prototype.toBack = function () {
1750             if (this.Group.parentNode.firstChild != this.Group) {
1751                 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1752             }
1753             return this;
1754         };
1755         Element.prototype.insertAfter = function (element) {
1756             if (element.Group.nextSibling) {
1757                 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1758             } else {
1759                 element.Group.parentNode.appendChild(this.Group);
1760             }
1761             return this;
1762         };
1763         Element.prototype.insertBefore = function (element) {
1764             element.Group.parentNode.insertBefore(this.Group, element.Group);
1765             return this;
1766         };
1767         var theCircle = function (vml, x, y, r) {
1768             var g = document.createElement("rvml:group");
1769             var o = document.createElement("rvml:oval");
1770             g.appendChild(o);
1771             vml.canvas.appendChild(g);
1772             var res = new Element(o, g, vml);
1773             res.type = "circle";
1774             setFillAndStroke(res, {stroke: "#000", fill: "none"});
1775             res.attrs.cx = x;
1776             res.attrs.cy = y;
1777             res.attrs.r = r;
1778             res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1779             return res;
1780         };
1781         var theRect = function (vml, x, y, w, h, r) {
1782             var g = document.createElement("rvml:group");
1783             var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
1784             if (r) {
1785                 o.arcsize = r / (Math.min(w, h));
1786             }
1787             g.appendChild(o);
1788             vml.canvas.appendChild(g);
1789             var res = new Element(o, g, vml);
1790             res.type = "rect";
1791             setFillAndStroke(res, {stroke: "#000"});
1792             res.attrs.x = x;
1793             res.attrs.y = y;
1794             res.attrs.w = w;
1795             res.attrs.h = h;
1796             res.attrs.r = r;
1797             res.setBox({x: x, y: y, width: w, height: h});
1798             return res;
1799         };
1800         var theEllipse = function (vml, x, y, rx, ry) {
1801             var g = document.createElement("rvml:group");
1802             var o = document.createElement("rvml:oval");
1803             g.appendChild(o);
1804             vml.canvas.appendChild(g);
1805             var res = new Element(o, g, vml);
1806             res.type = "ellipse";
1807             setFillAndStroke(res, {stroke: "#000"});
1808             res.attrs.cx = x;
1809             res.attrs.cy = y;
1810             res.attrs.rx = rx;
1811             res.attrs.ry = ry;
1812             res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
1813             return res;
1814         };
1815         var theImage = function (vml, src, x, y, w, h) {
1816             var g = document.createElement("rvml:group");
1817             var o = document.createElement("rvml:image");
1818             o.src = src;
1819             g.appendChild(o);
1820             vml.canvas.appendChild(g);
1821             var res = new Element(o, g, vml);
1822             res.type = "image";
1823             res.attrs.x = x;
1824             res.attrs.y = y;
1825             res.attrs.w = w;
1826             res.attrs.h = h;
1827             res.setBox({x: x, y: y, width: w, height: h});
1828             return res;
1829         };
1830         var theText = function (vml, x, y, text) {
1831             var g = document.createElement("rvml:group"), gs = g.style;
1832             var el = document.createElement("rvml:shape"), ol = el.style;
1833             var path = document.createElement("rvml:path"), ps = path.style;
1834             path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
1835             path.textpathok = true;
1836             ol.width = vml.width;
1837             ol.height = vml.height;
1838             gs.position = "absolute";
1839             gs.left = 0;
1840             gs.top = 0;
1841             gs.width = vml.width;
1842             gs.height = vml.height;
1843             var o = document.createElement("rvml:textpath");
1844             o.string = text;
1845             o.on = true;
1846             o.coordsize = vml.coordsize;
1847             o.coordorigin = vml.coordorigin;
1848             el.appendChild(o);
1849             el.appendChild(path);
1850             g.appendChild(el);
1851             vml.canvas.appendChild(g);
1852             var res = new Element(o, g, vml);
1853             res.shape = el;
1854             res.textpath = path;
1855             res.type = "text";
1856             res.attrs.x = x;
1857             res.attrs.y = y;
1858             res.attrs.w = 1;
1859             res.attrs.h = 1;
1860             setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000"});
1861             return res;
1862         };
1863         create = function () {
1864             // container, width, height
1865             // x, y, width, height
1866             var container, width, height;
1867             if (typeof arguments[0] == "string") {
1868                 container = document.getElementById(arguments[0]);
1869                 width = arguments[1];
1870                 height = arguments[2];
1871             }
1872             if (typeof arguments[0] == "object") {
1873                 container = arguments[0];
1874                 width = arguments[1];
1875                 height = arguments[2];
1876             }
1877             if (typeof arguments[0] == "number") {
1878                 container = 1;
1879                 x = arguments[0];
1880                 y = arguments[1];
1881                 width = arguments[2];
1882                 height = arguments[3];
1883             }
1884             if (!container) {
1885                 throw new Error("VML container not found.");
1886             }
1887             if (!document.namespaces["rvml"]) {
1888                 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
1889                 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
1890             }
1891             var c = document.createElement("div"),
1892                 d = document.createElement("div"),
1893                 r = paper.canvas = document.createElement("rvml:group"),
1894                 cs = c.style, rs = r.style;
1895             paper.width = width;
1896             paper.height = height;
1897             width = width || "320px";
1898             height = height || "200px";
1899             cs.clip = "rect(0 " + width + " " + height + " 0)";
1900             cs.top = "-2px";
1901             cs.left = "-2px";
1902             cs.position = "absolute";
1903             rs.position = "absolute";
1904             d.style.position = "relative";
1905             rs.width  = width;
1906             rs.height = height;
1907             r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
1908             r.coordorigin = "0 0";
1909
1910             var b = document.createElement("rvml:rect"), bs = b.style;
1911             bs.left = bs.top = 0;
1912             bs.width  = rs.width;
1913             bs.height = rs.height;
1914             b.filled = b.stroked = "f";
1915
1916             r.appendChild(b);
1917             c.appendChild(r);
1918             d.appendChild(c);
1919             if (container == 1) {
1920                 document.body.appendChild(d);
1921                 cs.position = "absolute";
1922                 cs.left = x + "px";
1923                 cs.top = y + "px";
1924                 cs.width = width;
1925                 cs.height = height;
1926                 container = {
1927                     style: {
1928                         width: width,
1929                         height: height
1930                     }
1931                 };
1932             } else {
1933                 cs.width = container.style.width = width;
1934                 cs.height = container.style.height = height;
1935                 if (container.firstChild) {
1936                     container.insertBefore(d, container.firstChild);
1937                 } else {
1938                     container.appendChild(d);
1939                 }
1940             }
1941             for (var prop in paper) {
1942                 container[prop] = paper[prop];
1943             }
1944             for (var prop in R.fn) {
1945                 if (!container[prop]) {
1946                     container[prop] = R.fn[prop];
1947                 }
1948             }
1949             container.clear = function () {
1950                 var todel = [];
1951                 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
1952                     if (r.childNodes[i] != b) {
1953                         todel.push(r.childNodes[i]);
1954                     }
1955                 }
1956                 for (i = 0, ii = todel.length; i < ii; i++) {
1957                     r.removeChild(todel[i]);
1958                 }
1959             };
1960             return container;
1961         };
1962         paper.remove = function () {
1963             this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
1964         };
1965         paper.safari = function () {};
1966     }
1967
1968     // rest
1969
1970     // Events
1971     var addEvent = (function () {
1972         if (document.addEventListener) {
1973             return function (obj, type, fn) {
1974                 obj.addEventListener(type, fn, false);
1975             };
1976         } else if (document.attachEvent) {
1977             return function (obj, type, fn) {
1978                 var f = function (e) {
1979                     fn.call(this, e || window.event);
1980                 };
1981                 obj.attachEvent("on" + type, f);
1982             };
1983         }
1984     })();
1985     var events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
1986     for (var i = events.length; i--;) {
1987         (function (eventName) {
1988             Element.prototype[eventName] = function (fn) {
1989                 addEvent(this.node, eventName, fn);
1990                 return this;
1991             };
1992         })(events[i]);
1993     }
1994
1995     // Set
1996     var Set = function (itemsArray) {
1997         this.items = [];
1998         if (itemsArray && itemsArray.constructor == Array) {
1999             for (var i = itemsArray.length; i--;) {
2000                 if (itemsArray[i].constructor == Element) {
2001                     this.items[this.items.length] = itemsArray[i];
2002                 }
2003             }
2004         }
2005     };
2006     Set.prototype.add = function (item) {
2007         if (item && item.constructor == Element) {
2008             this.items[this.items.length] = item;
2009         }
2010         return this;
2011     };
2012     Set.prototype.remove = function (item) {
2013         if (item && item.constructor == Element) {
2014             for (var i = this.items.length; i--;) {
2015                 if (this.items[i] == item) {
2016                     this.items.splice(i, 1);
2017                     return this;
2018                 }
2019             }
2020         }
2021         return this;
2022     };
2023     for (var method in Element.prototype) {
2024         Set.prototype[method] = (function (methodname) {
2025             return function () {
2026                 for (var i = this.items.length; i--;) {
2027                     this.items[i][methodname].apply(this.items[i], arguments);
2028                 }
2029                 return this;
2030             };
2031         })(method);
2032     }
2033     Set.prototype.getBBox = function () {
2034         var x = [], y = [], w = [], h = [];
2035         for (var i = this.items.length; i--;) {
2036             var box = this.items[i].getBBox();
2037             x.push(box.x);
2038             y.push(box.y);
2039             w.push(box.x + box.width);
2040             h.push(box.y + box.height);
2041         }
2042         x = Math.min(x);
2043         y = Math.min(y);
2044         return {
2045             x: x,
2046             y: y,
2047             w: Math.max(w) - x,
2048             h: Math.max(h) - y
2049         };
2050     };
2051
2052     paper.circle = function (x, y, r) {
2053         return theCircle(this, x, y, r);
2054     };
2055     paper.rect = function (x, y, w, h, r) {
2056         return theRect(this, x, y, w, h, r);
2057     };
2058     paper.ellipse = function (x, y, rx, ry) {
2059         return theEllipse(this, x, y, rx, ry);
2060     };
2061     paper.path = function (params, pathString) {
2062         return thePath(params, pathString, this);
2063     };
2064     paper.image = function (src, x, y, w, h) {
2065         return theImage(this, src, x, y, w, h);
2066     };
2067     paper.text = function (x, y, text) {
2068         return theText(this, x, y, text);
2069     };
2070     paper.group = function () {
2071         return this;
2072     };
2073     paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2074         color = color || "#000";
2075         var p = this.path({stroke: color, "stroke-width": 1})
2076                 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y),
2077             rowHeight = h / hv,
2078             columnWidth = w / wv;
2079         for (var i = 1; i < hv; i++) {
2080             p.moveTo(x, y + i * rowHeight).lineTo(x + w, y + i * rowHeight);
2081         }
2082         for (var i = 1; i < wv; i++) {
2083             p.moveTo(x + i * columnWidth, y).lineTo(x + i * columnWidth, y + h);
2084         }
2085         return p;
2086     };
2087     paper.pathfinder = function (p, path) {
2088         var commands = {
2089             M: function (x, y) {
2090                 this.moveTo(x, y);
2091             },
2092             C: function (x1, y1, x2, y2, x3, y3) {
2093                 this.curveTo(x1, y1, x2, y2, x3, y3);
2094             },
2095             Q: function (x1, y1, x2, y2) {
2096                 this.qcurveTo(x1, y1, x2, y2);
2097             },
2098             T: function (x, y) {
2099                 this.qcurveTo(x, y);
2100             },
2101             S: function (x1, y1, x2, y2) {
2102                 p.curveTo(x1, y1, x2, y2);
2103             },
2104             L: function (x, y) {
2105                 p.lineTo(x, y);
2106             },
2107             H: function (x) {
2108                 this.lineTo(x, this.last.y);
2109             },
2110             V: function (y) {
2111                 this.lineTo(this.last.x, y);
2112             },
2113             A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2114                 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2115             },
2116             Z: function () {
2117                 this.andClose();
2118             }
2119         };
2120
2121         path = pathToAbsolute(path);
2122         for (var i = 0, ii = path.length; i < ii; i++) {
2123             var b = path[i].shift();
2124             commands[b].apply(p, path[i]);
2125         }
2126     };
2127     paper.set = function (itemsArray) {
2128         return new Set(itemsArray);
2129     };
2130     Element.prototype.stop = function () {
2131         clearTimeout(this.animation_in_progress);
2132     };
2133     Element.prototype.scale = function (x, y) {
2134         if (x == undefined && y == undefined) {
2135             return {x: this._.sx, y: this._.sy};
2136         }
2137         y = y || x;
2138         var dx, dy, cx, cy;
2139         if (x != 0 && !(x == 1 && y == 1)) {
2140             var dirx = Math.round(x / Math.abs(x)),
2141                 diry = Math.round(y / Math.abs(y)),
2142                 s = this.node.style;
2143             dx = this.attr("x");
2144             dy = this.attr("y");
2145             cx = this.attr("cx");
2146             cy = this.attr("cy");
2147             if (dirx != 1 || diry != 1) {
2148                 if (this.transformations) {
2149                     this.transformations[2] = "scale(" + [dirx, diry] + ")";
2150                     this.node.setAttribute("transform", this.transformations.join(" "));
2151                     dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2152                     dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2153                     cx = this.attr("cx") * dirx;
2154                     cy = this.attr("cy") * diry;
2155                 } else {
2156                     this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2157                         ", M12=0, M21=0, M22=" + diry +
2158                         ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2159                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2160                 }
2161             } else {
2162                 if (this.transformations) {
2163                     this.transformations[2] = "";
2164                     this.node.setAttribute("transform", this.transformations.join(" "));
2165                 } else {
2166                     this.node.filterMatrix = "";
2167                     s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2168                 }
2169             }
2170             switch (this.type) {
2171                 case "rect":
2172                 case "image":
2173                     this.attr({
2174                         width: this.attrs.width * x * dirx / this._.sx,
2175                         height: this.attrs.height * y * diry / this._.sy,
2176                         x: dx,
2177                         y: dy
2178                     });
2179                     break;
2180                 case "circle":
2181                 case "ellipse":
2182                     this.attr({
2183                         rx: this.attrs.rx * x * dirx / this._.sx,
2184                         ry: this.attrs.ry * y * diry / this._.sy,
2185                         r: this.attrs.r * x * diry / this._.sx,
2186                         cx: cx,
2187                         cy: cy
2188                     });
2189                     break;
2190                 case "path":
2191                     var path = pathToRelative(Raphael.parsePathString(this.attr("path"))), 
2192                         skip = true,
2193                         dim = pathDimensions(this.attrs.path),
2194                         dx = -dim.width * (x - 1) / 2,
2195                         dy = -dim.height * (y - 1) / 2;
2196                     for (var i = 0, ii = path.length; i < ii; i++) {
2197                         if (path[i][0].toUpperCase() == "M" && skip) {
2198                             continue;
2199                         } else {
2200                             skip = false;
2201                         }
2202                         if (path[i][0].toUpperCase() == "A") {
2203                             path[i][path[i].length - 2] *= x * dirx;
2204                             path[i][path[i].length - 1] *= y * diry;
2205                         } else {
2206                             for (var j = 1, jj = path[i].length; j < jj; j++) {
2207                                 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2208                             }
2209                         }
2210                     }
2211                     var dim2 = pathDimensions(path),
2212                         dx = dim.x + dim.width / 2 - dim2.x - dim2.width / 2,
2213                         dy = dim.y + dim.height / 2 - dim2.y - dim2.height / 2;
2214                     path = pathToRelative(path);
2215                     path[0][1] += dx;
2216                     path[0][2] += dy;
2217                     
2218                     this.attr({path: path.join(" ")});
2219             }
2220         }
2221         this._.sx = x;
2222         this._.sy = y;
2223         return this;
2224     };
2225     Element.prototype.animate = function (params, ms, callback) {
2226         clearTimeout(this.animation_in_progress);
2227         var from = {}, to = {}, diff = {}, t = {x: 0, y: 0};
2228         for (var attr in params) {
2229             if (attr in availableAnimAttrs) {
2230                 from[attr] = this.attr(attr);
2231                 if (typeof from[attr] == "undefined") {
2232                     from[attr] = availableAttrs[attr];
2233                 }
2234                 to[attr] = params[attr];
2235                 switch (availableAnimAttrs[attr]) {
2236                     case "number":
2237                         diff[attr] = (to[attr] - from[attr]) / ms;
2238                         break;
2239                     case "colour":
2240                         from[attr] = getRGB(from[attr]);
2241                         var toColour = getRGB(to[attr]);
2242                         diff[attr] = {
2243                             r: (toColour.r - from[attr].r) / ms,
2244                             g: (toColour.g - from[attr].g) / ms,
2245                             b: (toColour.b - from[attr].b) / ms
2246                         };
2247                         break;
2248                     case "path":
2249                         var pathes = pathEqualiser(from[attr], to[attr]);
2250                         from[attr] = pathes[0];
2251                         to[attr] = pathes[1];
2252                         diff[attr] = [];
2253                         for (var i = 0, ii = from[attr].length; i < ii; i++) {
2254                             diff[attr][i] = [0];
2255                             for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2256                                 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2257                             }
2258                         }
2259                         break;
2260                     case "csv":
2261                         var values = params[attr].split(/[, ]+/);
2262                         if (attr == "translation") {
2263                             from[attr] = [0, 0];
2264                             diff[attr] = [values[0] / ms, values[1] / ms];
2265                         } else {
2266                             from[attr] = from[attr].split(/[, ]+/);
2267                             diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2268                         }
2269                         to[attr] = values;
2270                 }
2271             }
2272         }
2273         var start = new Date(),
2274             prev = 0,
2275             that = this;
2276         (function () {
2277             var time = (new Date()).getTime() - start.getTime(),
2278                 set = {},
2279                 now;
2280             if (time < ms) {
2281                 for (var attr in from) {
2282                     switch (availableAnimAttrs[attr]) {
2283                         case "number":
2284                             now = +from[attr] + time * diff[attr];
2285                             break;
2286                         case "colour":
2287                             now = "rgb(" + [
2288                                 Math.round(from[attr].r + time * diff[attr].r),
2289                                 Math.round(from[attr].g + time * diff[attr].g),
2290                                 Math.round(from[attr].b + time * diff[attr].b)
2291                             ].join(",") + ")";
2292                             break;
2293                         case "path":
2294                             now = [];
2295                             for (var i = 0, ii = from[attr].length; i < ii; i++) {
2296                                 now[i] = [from[attr][i][0]];
2297                                 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2298                                     now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2299                                 }
2300                                 now[i] = now[i].join(" ");
2301                             }
2302                             now = now.join(" ");
2303                             break;
2304                         case "csv":
2305                             if (attr == "translation") {
2306                                 var x = diff[attr][0] * (time - prev),
2307                                     y = diff[attr][1] * (time - prev);
2308                                 t.x += x;
2309                                 t.y += y;
2310                                 now = [x, y].join(" ");
2311                             } else {
2312                                 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2313                             }
2314                             break;
2315                     }
2316                     if (attr == "font-size") {
2317                         set[attr] = now + "px";
2318                     } else {
2319                         set[attr] = now;
2320                     }
2321                 }
2322                 that.attr(set);
2323                 that.animation_in_progress = setTimeout(arguments.callee, 0);
2324                 paper.safari();
2325             } else {
2326                 if (t.x || t.y) {
2327                     that.translate(-t.x, -t.y);
2328                 }
2329                 that.attr(params);
2330                 clearTimeout(that.animation_in_progress);
2331                 paper.safari();
2332                 (typeof callback == "function") && callback.call(that);
2333             }
2334             prev = time;
2335         })();
2336         return this;
2337     };
2338     return R;
2339 })();