0.5.10 Fixed parsing of path string. Added obj.node as an alternative to obj[0].
[raphael] / raphael.js
1 /*
2  * Raphael 0.5.10 - JavaScript Vector Library
3  *
4  * Copyright (c) 2008 Dmitry Baranovskiy (raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7 var Raphael = (function (type) {
8         var r = function () {
9             return r._create.apply(r, arguments);
10         };
11         r.version = "0.5.10";
12         r.type = type;
13         var C = {};
14         function Matrix(m11, m12, m21, m22, dx, dy) {
15             this.m = [
16                 [m11 || 1, m12 || 0, 0],
17                 [m21 || 0, m22 || 1, 0],
18                 [dx || 0, dy || 0, 1],
19             ];
20         }
21
22         C._getX = C._getY = C._getW = C._getH = function (x) { return x; };
23
24         if (type == "VML") {
25             Matrix.prototype.toString = function () {
26                 return "progid:DXImageTransform.Microsoft.Matrix(M11=" + this.m[0][0] +
27                     ", M12=" + this.m[1][0] + ", M21=" + this.m[0][1] + ", M22=" + this.m[1][1] +
28                     ", Dx=" + this.m[2][0] + ", Dy=" + this.m[2][1] + ", sizingmethod='auto expand', filtertype='bilinear')";
29             };
30             var thePath = function (params, pathString, VML) {
31                 var g = document.createElement("rvml:group"), gl = g.style;
32                 gl.position = "absolute";
33                 gl.left = 0;
34                 gl.top = 0;
35                 gl.width = VML.width + "px";
36                 gl.height = VML.height + "px";
37                 var el = document.createElement("rvml:shape"), ol = el.style;
38                 ol.width = VML.width + "px";
39                 ol.height = VML.height + "px";
40                 el.path = "";
41                 if (params["class"]) {
42                     el.className = params["class"];
43                 }
44                 el.coordsize = this.coordsize;
45                 el.coordorigin = this.coordorigin;
46                 g.appendChild(el);
47                 VML.canvas.appendChild(g);
48                 var p = new Element(el, g, VML);
49                 setFillAndStroke(p, params);
50                 if (params.gradient) {
51                     addGrdientFill(p, params.gradient);
52                 }
53                 p.isAbsolute = true;
54                 p.type = "path";
55                 p.path = [];
56                 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
57                 p.Path = "";
58                 p.absolutely = function () {
59                     this.isAbsolute = true;
60                     return this;
61                 };
62                 p.relatively = function () {
63                     this.isAbsolute = false;
64                     return this;
65                 };
66                 p.redraw = function () {
67                     this.Path = "";
68                     var oldPath = this.path;
69                     this.path = [];
70                     for (var i = 0, ii = oldPath.length; i < ii; i++) {
71                         if (oldPath[i].type != "end") {
72                             this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
73                         } else {
74                             this.andClose();
75                         }
76                     }
77                     return this;
78                 };
79                 p.moveTo = function (x, y) {
80                     var d = this.isAbsolute?"m":"t";
81                     var _getX = this.isAbsolute ? VML._getX : VML._getW;
82                     var _getY = this.isAbsolute ? VML._getY : VML._getH;
83                     d += Math.round(_getX(parseFloat(x, 10))) + " " + Math.round(_getY(parseFloat(y, 10)));
84                     this[0].path = this.Path += d;
85                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(x, 10));
86                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(y, 10));
87                     this.last.isAbsolute = this.isAbsolute;
88                     this.path.push({type: "move", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
89                     return this;
90                 };
91                 p.lineTo = function (x, y) {
92                     var d = this.isAbsolute?"l":"r";
93                     var _getX = this.isAbsolute ? VML._getX : VML._getW;
94                     var _getY = this.isAbsolute ? VML._getY : VML._getH;
95                     d += Math.round(_getX(parseFloat(x, 10))) + " " + Math.round(_getY(parseFloat(y, 10)));
96                     this[0].path = this.Path += d;
97                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(x, 10));
98                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(y, 10));
99                     this.last.isAbsolute = this.isAbsolute;
100                     this.path.push({type: "line", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
101                     return this;
102                 };
103                 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
104                     // for more information of where this math came from visit:
105                     // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
106                     x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
107                     y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
108                     var x1 = this.last.x,
109                         y1 = this.last.y,
110                         x = (x1 - x2) / 2,
111                         y = (y1 - y2) / 2,
112                         k = (large_arc_flag == sweep_flag ? -1 : 1) *
113                             Math.sqrt((rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
114                         cx = k * rx * y / ry + (x1 + x2) / 2,
115                         cy = k * -ry * x / rx + (y1 + y2) / 2,
116                         d = sweep_flag ? (this.isAbsolute?"wa":"wr") : (this.isAbsolute?"at":"ar"),
117                         _getX = this.isAbsolute ? VML._getX : VML._getW,
118                         _getY = this.isAbsolute ? VML._getY : VML._getH,
119                         left = Math.round(cx - rx),
120                         top = Math.round(cy - ry);
121                     d += [left, top, Math.round(left + rx * 2), Math.round(top + ry * 2), Math.round(x1), Math.round(y1), Math.round(_getX(parseFloat(x2, 10))), Math.round(_getX(parseFloat(y2, 10)))].join(", ");
122                     this[0].path = this.Path += d;
123                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(x2, 10));
124                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(y2, 10));
125                     this.last.isAbsolute = this.isAbsolute;
126                     this.path.push({type: "arc", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
127                     return this;
128                 };
129                 p.cplineTo = function (x1, y1, w1) {
130                     if (!w1) {
131                         return this.lineTo(x1, y1);
132                     } else {
133                         var p = {};
134                         p._getX = this.isAbsolute ? VML._getX : VML._getW;
135                         p._getY = this.isAbsolute ? VML._getY : VML._getH;
136                         var x = Math.round(p._getX(Math.round(parseFloat(x1, 10) * 100) / 100));
137                         var y = Math.round(p._getY(Math.round(parseFloat(y1, 10) * 100) / 100));
138                         var w = Math.round(VML._getW(Math.round(parseFloat(w1, 10) * 100) / 100));
139                         var d = this.isAbsolute?"c":"v";
140                         var attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y];
141                         d += attr.join(" ") + " ";
142                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
143                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
144                         this.last.bx = attr[2];
145                         this.last.by = attr[3];
146                         this[0].path = this.Path += d;
147                         this.path.push({type: "cpline", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
148                         return this;
149                     }
150                 };
151                 p.curveTo = function () {
152                     var d = this.isAbsolute?"c":"v";
153                     var _getX = this.isAbsolute ? VML._getX : VML._getW;
154                     var _getY = this.isAbsolute ? VML._getY : VML._getH;
155                     if (arguments.length == 6) {
156                         this.last.bx = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(arguments[2], 10));
157                         this.last.by = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(arguments[3], 10));
158                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(arguments[4], 10));
159                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(arguments[5], 10));
160                         d += Math.round(_getX(parseFloat(arguments[0], 10))) + " " +
161                              Math.round(_getY(parseFloat(arguments[1], 10))) + " " +
162                              Math.round(_getX(parseFloat(arguments[2], 10))) + " " +
163                              Math.round(_getY(parseFloat(arguments[3], 10))) + " " +
164                              Math.round(_getX(parseFloat(arguments[4], 10))) + " " +
165                              Math.round(_getY(parseFloat(arguments[5], 10))) + " ";
166                         this.last.isAbsolute = this.isAbsolute;
167                     }
168                     if (arguments.length == 4) {
169                         var bx = this.last.x * 2 - this.last.bx;
170                         var by = this.last.y * 2 - this.last.by;
171                         this.last.bx = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(arguments[0], 10));
172                         this.last.by = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(arguments[1], 10));
173                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + _getX(parseFloat(arguments[2], 10));
174                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + _getY(parseFloat(arguments[3], 10));
175                         d += [Math.round(bx), Math.round(by),
176                              Math.round(_getX(parseFloat(arguments[0], 10))),
177                              Math.round(_getY(parseFloat(arguments[1], 10))),
178                              Math.round(_getX(parseFloat(arguments[2], 10))),
179                              Math.round(_getY(parseFloat(arguments[3], 10)))].join(" ");
180                     }
181                     this[0].path = this.Path += d;
182                     this.path.push({type: "curve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
183                     return this;
184                 };
185                 p.addRoundedCorner = function (r, dir) {
186                     var R = .5522 * r, rollback = this.isAbsolute, o = this;
187                     if (rollback) {
188                         this.relatively();
189                         rollback = function () {
190                             o.absolutely();
191                         };
192                     } else {
193                         rollback = function () {};
194                     }
195                     var actions = {
196                         l: function () {
197                             return {
198                                 u: function () {
199                                     o.curveTo(-R, 0, -r, -(r - R), -r, -r);
200                                 },
201                                 d: function () {
202                                     o.curveTo(-R, 0, -r, r - R, -r, r);
203                                 }
204                             };
205                         },
206                         r: function () {
207                             return {
208                                 u: function () {
209                                     o.curveTo(R, 0, r, -(r - R), r, -r);
210                                 },
211                                 d: function () {
212                                     o.curveTo(R, 0, r, r - R, r, r);
213                                 }
214                             };
215                         },
216                         u: function () {
217                             return {
218                                 r: function () {
219                                     o.curveTo(0, -R, -(R - r), -r, r, -r);
220                                 },
221                                 l: function () {
222                                     o.curveTo(0, -R, R - r, -r, -r, -r);
223                                 }
224                             };
225                         },
226                         d: function () {
227                             return {
228                                 r: function () {
229                                     o.curveTo(0, R, -(R - r), r, r, r);
230                                 },
231                                 l: function () {
232                                     o.curveTo(0, R, R - r, r, -r, r);
233                                 }
234                             };
235                         }
236                     };
237                     actions[dir.charAt(0)]()[dir.charAt(1)]();
238                     rollback();
239                     return o;
240                 };
241                 p.andClose = function () {
242                     this[0].path = (this.Path += "x e");
243                     return this;
244                 };
245                 if (typeof pathString == "string") {
246                     p.absolutely();
247                     C.pathfinder(p, pathString);
248                 }
249                 return p;
250             };
251             var setFillAndStroke = function (o, params) {
252                 var s = o[0].style;
253                 o.attrs = o.attrs || {};
254                 for (var par in params) {
255                     o.attrs[par] = params[par];
256                 }
257                 params["font-family"] && (s.fontFamily = params["font-family"]);
258                 params["font-size"] && (s.fontSize = params["font-size"]);
259                 params["font"] && (s.font = params["font"]);
260                 params["font-weight"] && (s.fontWeight = params["font-weight"]);
261                 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
262                     o = o.shape || o[0];
263                     var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
264                     if ("fill-opacity" in params || "opacity" in params) {
265                         fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
266                     }
267                     if (params.fill) {
268                         fill.on = true;
269                     }
270                     if (fill.on == undefined || params.fill == "none") {
271                         fill.on = false;
272                     }
273                     if (fill.on && params.fill) {
274                         fill.color = params.fill;
275                     }
276                     o.appendChild(fill);
277                     var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
278                     if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
279                         stroke.on = true;
280                     }
281                     if (params.stroke == "none" || typeof stroke.on == "undefined") {
282                         stroke.on = false;
283                     }
284                     if (stroke.on && params.stroke) {
285                         stroke.color = params.stroke;
286                     }
287                     stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
288                     params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
289                     stroke.miterlimit = params["stroke-miterlimit"] || 8;
290                     params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
291                     params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
292                     if (params["stroke-dasharray"]) {
293                         var dashes = params["stroke-dasharray"].replace(" ", ",").split(","),
294                             dashesn = [],
295                             str = stroke.weight;
296                         for (var i = 0, ii = dashes.length; i < ii; i++) {
297                             var res = dashes[i] / str;
298                             if (!isNaN(res)) {
299                                 dashesn.push(res);
300                             }
301                         };
302                         stroke.dashstyle = dashesn.join(" ");
303                     }
304                     o.appendChild(stroke);
305                 }
306             };
307             var addGrdientFill = function (o, gradient) {
308                 o.attrs = o.attrs || {};
309                 o.attrs.gradient = gradient;
310                 o = o.shape || o[0];
311                 var fill = o.getElementsByTagName("fill");
312                 if (fill.length) {
313                     fill = fill[0];
314                 } else {
315                     fill = document.createElement("rvml:fill");
316                 }
317                 if (gradient.dots.length) {
318                     fill.on = true;
319                     fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientradial";
320                     if (typeof gradient.dots[0].color != "undefined") {
321                         fill.color = gradient.dots[0].color || "#000";
322                     }
323                     if (typeof gradient.dots[0].opacity != "undefined") {
324                         fill.opacity = gradient.dots[0].opacity;
325                     }
326                     if (typeof gradient.dots[gradient.dots.length - 1].opacity != "undefined") {
327                         fill.opacity2 = gradient.dots[gradient.dots.length - 1].opacity;
328                     }
329                     if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
330                         fill.color2 = gradient.dots[gradient.dots.length - 1].color || "#000";
331                     }
332                     var colors = "";
333                     for (var i = 1, ii = gradient.dots.length - 1; i < ii; i++) {
334                         colors += gradient.dots[i].offset + " " + gradient.dots[i].color;
335                         if (i != ii - 1) {
336                             colors += ",";
337                         }
338                     };
339                     if (colors) {
340                         fill.colors = colors;
341                     }
342                     if (gradient.vector) {
343                         var angle = Math.round(Math.atan((parseInt(gradient.vector[3], 10) - parseInt(gradient.vector[1], 10)) / (parseInt(gradient.vector[2], 10) - parseInt(gradient.vector[0], 10))) * 57.29) + 180;
344                         fill.angle = angle + 90;
345                     }
346                     if (gradient.type.toLowerCase() == "radial") {
347                         fill.focusposition = "0.5, 0.5";
348                         fill.focussize = "0, 0";
349                         fill.method = "none";
350                     }
351                 }
352             };
353             var Element = function (node, group, vml) {
354                 var Rotation = 0,
355                     RotX = 0,
356                     RotY = 0,
357                     Scale = 1;
358                 this[0] = node;
359                 this.node = node;
360                 this.X = 0;
361                 this.Y = 0;
362                 this.attrs = {};
363                 this.Group = group;
364                 this.vml = vml;
365                 this.rotate = function (deg) {
366                     if (deg == undefined) {
367                         return Rotation;
368                     }
369                     Rotation += deg;
370                     this.Group.style.rotation = Rotation;
371                     return this;
372                 };
373             };
374             Element.prototype.setBox = function (params) {
375                 var gs = this.Group.style,
376                     os = this[0].style;
377                 for (var i in params) {
378                     this.attrs[i] = params[i];
379                 }
380                 var attr = this.attrs, x, y, w, h;
381                 switch (this.type) {
382                     case "circle": 
383                         x = attr.cx - attr.r;
384                         y = attr.cy - attr.r;
385                         w = h = attr.r * 2;
386                         break;
387                     case "ellipse":
388                         x = attr.cx - attr.rx;
389                         y = attr.cy - attr.ry;
390                         w = attr.rx * 2;
391                         h = attr.ry * 2;
392                         break;
393                     case "rect":
394                     case "image":
395                         x = attr.x;
396                         y = attr.y;
397                         w = attr.w;
398                         h = attr.h;
399                         break;
400                     case "text":
401                         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("");
402                         return;
403                     default:
404                         return;
405                 }
406                 var left = this.vml.width / 2 - w / 2,
407                     top = this.vml.height / 2 - h / 2;
408                 gs.position = "absolute";
409                 gs.left = x - left + "px";
410                 gs.top = y - top + "px";
411                 this.X = x - left;
412                 this.Y = y - top;
413                 this.W = w;
414                 this.H = h;
415                 gs.width = this.vml.width + "px";
416                 gs.height = this.vml.height + "px";
417                 os.position = "absolute";
418                 os.top = top + "px";
419                 os.left = left + "px";
420                 os.width = w + "px";
421                 os.height = h + "px";
422             };
423             Element.prototype.hide = function () {
424                 this.Group.style.display = "none";
425                 return this;
426             };
427             Element.prototype.show = function () {
428                 this.Group.style.display = "block";
429                 return this;
430             };
431             Element.prototype.translate = function (x, y) {
432                 if (x == undefined && y == undefined) {
433                     return {x: this.X, y: this.Y};
434                 }
435                 this.X += x;
436                 this.Y += y;
437                 this.Group.style.left = this.X + "px";
438                 this.Group.style.top = this.Y + "px";
439                 return this;
440             };
441             // depricated
442             Element.prototype.matrix = function (xx, xy, yx, yy, dx, dy) {
443                 tMatrix = new Matrix(xx, xy, yx, yy, dx, dy);
444                 this.Group.style.filter = tMatrix;
445                 return this;
446             };
447             Element.prototype.scale = function (x, y) {
448                 if (x == undefined && y == undefined) {
449                     return ;
450                     // TODO
451                 }
452                 y = y || x;
453                 if (x != 0 && !(x == 1 && y == 1)) {
454                     var dirx = Math.round(x / Math.abs(x)),
455                         diry = Math.round(y / Math.abs(y)),
456                         s = this[0].style;
457                     if (dirx != 1 || diry != 1) {
458                         s.filter = new Matrix(dirx, 0, 0, diry, 0, 0);
459                     }
460                     var width = parseInt(s.width, 10) * x * dirx;
461                     var height = parseInt(s.height, 10) * y * diry;
462                     var left = parseInt(s.left, 10);
463                     var top = parseInt(s.top, 10);
464                     s.left = this.X = left + this.W / 2 - width / 2;
465                     s.top = this.Y = top + this.H / 2 - height / 2;
466                     s.width = this.W = width;
467                     s.height = this.H = height;
468                 }
469                 return this;
470             };
471             Element.prototype.getBBox = function () {
472                 return {
473                     x: this.Group.offsetLeft,
474                     y: this.Group.offsetTop,
475                     width: this.Group.offsetWidth,
476                     height: this.Group.offsetHeight
477                 };
478             };
479             Element.prototype.remove = function () {
480                 this[0].parentNode.removeChild(this[0]);
481                 this.Group.parentNode.removeChild(this.Group);
482                 this.shape && this.shape.parentNode.removeChild(this.shape);
483             };
484             Element.prototype.attr = function () {
485                 if (arguments.length == 1 && typeof arguments[0] == "string") {
486                     return this.attrs[arguments[0]];
487                 }
488                 if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
489                     var values = {};
490                     for (var i = 0, ii = arguments[0].length; i < ii; i++) {
491                         values[arguments[0][i]] = this.attrs[arguments[0][i]];
492                     };
493                     return values;
494                 }
495                 if (this[0].tagName.toLowerCase() == "group") {
496                     var children = this[0].childNodes;
497                     this.attrs = this.attrs || {};
498                     if (arguments.length == 2) {
499                         this.attrs[arguments[0]] = arguments[1];
500                     } else if (arguments.length == 1 || typeof arguments[0] == "object") {
501                         for (var j in arguments[0]) {
502                             this.attrs[j] = arguments[0][j];
503                         }
504                     }
505                     for (var i = 0, ii = children.length; i < ii; i++) {
506                         this.attr.apply(new item(children[i], this[0], this.vml), arguments);
507                     }
508                 } else {
509                     var params;
510                     if (arguments.length == 2) {
511                         params = {};
512                         params[arguments[0]] = arguments[1];
513                     }
514                     if (arguments.length == 1 && typeof arguments[0] == "object") {
515                         params = arguments[0];
516                     }
517                     if (params) {
518                         setFillAndStroke(this, params);
519                         this.setBox(params);
520                         if (params.gradient) {
521                             addGrdientFill(this, params.gradient);
522                         }
523                         if (params.text && this.type == "text") {
524                             this[0].string = params.text;
525                         }
526                         if (params.id) {
527                             this[0].id = params.id;
528                         }
529                     }
530                 }
531                 return this;
532             };
533             Element.prototype.toFront = function () {
534                 this.Group.parentNode.appendChild(this.Group);
535                 return this;
536             };
537             Element.prototype.toBack = function () {
538                 if (this.Group.parentNode.firstChild != this.Group) {
539                     this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
540                 }
541                 return this;
542             };
543             var theCircle = function (vml, x, y, r) {
544                 var g = document.createElement("rvml:group");
545                 var o = document.createElement("rvml:oval");
546                 g.appendChild(o);
547                 vml.canvas.appendChild(g);
548                 var res = new Element(o, g, vml);
549                 setFillAndStroke(res, {stroke: "#000", fill: "none"});
550                 res.setBox({x: x - r, y: y - r, w: r * 2, h: r * 2});
551                 res.attrs.cx = x;
552                 res.attrs.cy = y;
553                 res.attrs.r = r;
554                 res.type = "circle";
555                 return res;
556             };
557             var theRect = function (vml, x, y, w, h, r) {
558                 var g = document.createElement("rvml:group");
559                 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
560                 if (r) {
561                     o.arcsize = r / (Math.min(w, h));
562                 }
563                 g.appendChild(o);
564                 vml.canvas.appendChild(g);
565                 var res = new Element(o, g, vml);
566                 setFillAndStroke(res, {stroke: "#000"});
567                 res.setBox({x: x, y: y, w: w, h: h});
568                 res.attrs.x = x;
569                 res.attrs.y = y;
570                 res.attrs.w = w;
571                 res.attrs.h = h;
572                 res.attrs.r = r;
573                 res.type = "rect";
574                 return res;
575             };
576             var theEllipse = function (vml, x, y, rx, ry) {
577                 var g = document.createElement("rvml:group");
578                 var o = document.createElement("rvml:oval");
579                 g.appendChild(o);
580                 vml.canvas.appendChild(g);
581                 var res = new Element(o, g, vml);
582                 setFillAndStroke(res, {stroke: "#000"});
583                 res.setBox({x: x - rx, y: y - ry, w: rx * 2, h: ry * 2});
584                 res.attrs.cx = x;
585                 res.attrs.cy = y;
586                 res.attrs.rx = rx;
587                 res.attrs.ry = ry;
588                 res.type = "ellipse";
589                 return res;
590             };
591             var theImage = function (vml, src, x, y, w, h) {
592                 var g = document.createElement("rvml:group");
593                 var o = document.createElement("rvml:image");
594                 o.src = src;
595                 g.appendChild(o);
596                 vml.canvas.appendChild(g);
597                 var res = new Element(o, g, vml);
598                 res.type = "image";
599                 res.setBox({x: x, y: y, w: w, h: h});
600                 res.attrs.x = x;
601                 res.attrs.y = y;
602                 res.attrs.w = w;
603                 res.attrs.h = h;
604                 return res;
605             };
606             var theText = function (vml, x, y, text) {
607                 // @TODO: setTheBox
608                 var g = document.createElement("rvml:group"), gs = g.style;
609                 var el = document.createElement("rvml:shape"), ol = el.style;
610                 var path = document.createElement("rvml:path"), ps = path.style;
611                 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
612                 path.textpathok = true;
613                 ol.width = vml.width;
614                 ol.height = vml.height;
615                 gs.position = "absolute";
616                 gs.left = 0;
617                 gs.top = 0;
618                 gs.width = vml.width;
619                 gs.height = vml.height;
620                 var o = document.createElement("rvml:textpath");
621                 o.string = text;
622                 o.on = true;
623                 o.coordsize = vml.coordsize;
624                 o.coordorigin = vml.coordorigin;
625                 el.appendChild(o);
626                 el.appendChild(path);
627                 g.appendChild(el);
628                 vml.canvas.appendChild(g);
629                 var res = new Element(o, g, vml);
630                 res.shape = el;
631                 res.textpath = path;
632                 res.type = "text";
633                 res.attrs.x = x;
634                 res.attrs.y = y;
635                 res.attrs.w = 1;
636                 res.attrs.h = 1;
637                 return res;
638             };
639             var theGroup = function (vml) {
640                 var el = document.createElement("rvml:group"), els = el.style;
641                 els.position = "absolute";
642                 els.left = 0;
643                 els.top = 0;
644                 els.width = vml.width;
645                 els.height = vml.height;
646                 if (vml.canvas) {
647                     vml.canvas.appendChild(el);
648                 }
649                 var res = new Element(el, el, vml);
650                 for (var f in vml) {
651                     if (f.charAt(0) != "_" && typeof vml[f] == "function") {
652                         res[f] = (function (f) {
653                             return function () {
654                                 var e = vml[f].apply(vml, arguments);
655                                 el.appendChild(e[0].parentNode);
656                                 return e;
657                             };
658                         })(f);
659                     }
660                 }
661                 res.type = "group";
662                 return res;
663             };
664             r._create = function () {
665                 // container, width, height
666                 // x, y, width, height
667                 var container, width, height;
668                 if (typeof arguments[0] == "string") {
669                     container = document.getElementById(arguments[0]);
670                     width = arguments[1];
671                     height = arguments[2];
672                 }
673                 if (typeof arguments[0] == "object") {
674                     container = arguments[0];
675                     width = arguments[1];
676                     height = arguments[2];
677                 }
678                 if (typeof arguments[0] == "number") {
679                     container = 1;
680                     x = arguments[0];
681                     y = arguments[1];
682                     width = arguments[2];
683                     height = arguments[3];
684                 }
685                 if (!container) {
686                     throw new Error("VML container not found.");
687                 }
688                 if (!document.namespaces["rvml"]) {
689                     document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
690                     document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
691                 }
692                 var c = document.createElement("div"),
693                     r = C.canvas = document.createElement("rvml:group"),
694                     cs = c.style, rs = r.style;
695                 C.width = width;
696                 C.height = height;
697                 width = width || "320px";
698                 height = height || "200px";
699                 cs.clip = "rect(0 " + width + " " + height + " 0)";
700                 cs.position = "absolute";
701                 rs.width  = width;
702                 rs.height = height;
703                 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
704                 r.coordorigin = "0 0";
705
706                 var b = document.createElement("rvml:rect"), bs = b.style;
707                 bs.left = bs.top = 0;
708                 bs.width  = rs.width;
709                 bs.height = rs.height;
710                 b.filled = b.stroked = "f";
711
712                 r.appendChild(b);
713                 c.appendChild(r);
714                 if (container == 1) {
715                     document.body.appendChild(c);
716                     cs.position = "absolute";
717                     cs.left = x + "px";
718                     cs.top = y + "px";
719                     cs.width = width;
720                     cs.height = height;
721                     container = {
722                         style: {
723                             width: width,
724                             height: height
725                         }
726                     };
727                 } else {
728                     cs.width = container.style.width = width;
729                     cs.height = container.style.height = height;
730                     if (container.firstChild) {
731                         container.insertBefore(c, container.firstChild);
732                     } else {
733                         container.appendChild(c);
734                     }
735                 }
736                 for (var prop in C) {
737                     container[prop] = C[prop];
738                 }
739                 container.clear = function () {
740                     var todel = [];
741                     for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
742                         if (r.childNodes[i] != b) {
743                             todel.push(r.childNodes[i]);
744                         }
745                     }
746                     for (i = 0, ii = todel.length; i < ii; i++) {
747                         r.removeChild(todel[i]);
748                     }
749                 };
750                 return container;
751             };
752             C.remove = function () {
753                 C.canvas.parentNode.parentNode.removeChild(C.canvas.parentNode);
754             };
755         }
756         if (type == "SVG") {
757             Matrix.prototype.toString = function () {
758                 return "matrix(" + this.m[0][0] +
759                     ", " + this.m[1][0] + ", " + this.m[0][1] + ", " + this.m[1][1] +
760                     ", " + this.m[2][0] + ", " + this.m[2][1] + ")";
761             };
762             var thePath = function (params, pathString, SVG) {
763                 var el = document.createElementNS(SVG.svgns, "path");
764                 el.setAttribute("fill", "none");
765                 if (params) {
766                     for (var attr in params) {
767                         if (params.gradient) {
768                             addGrdientFill(el, params.gradient, SVG);
769                         } else {
770                             el.setAttribute(attr, params[attr]);
771                         }
772                     }
773                 }
774                 if (SVG.canvas) {
775                     SVG.canvas.appendChild(el);
776                 }
777                 var p = new Element(el, SVG);
778                 for (var attr in params) {
779                     p.attrs[attr] = params[attr];
780                 }
781                 p.isAbsolute = true;
782                 p.path = [];
783                 p.last = {x: 0, y: 0, bx: 0, by: 0};
784                 p.absolutely = function () {
785                     this.isAbsolute = true;
786                     return this;
787                 };
788                 p.relatively = function () {
789                     this.isAbsolute = false;
790                     return this;
791                 };
792                 p.redraw = function () {
793                     this[0].setAttribute("d", "M0 0");
794                     var oldPath = this.path;
795                     this.path = [];
796                     for (var i = 0, ii = oldPath.length; i < ii; i++) {
797                         if (oldPath[i].type != "end") {
798                             this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
799                         } else {
800                             this.andClose();
801                         }
802                     }
803                     return this;
804                 };
805                 p.moveTo = function (x, y) {
806                     var d = this.isAbsolute?"M":"m";
807                     var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
808                     var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
809                     d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
810                     var oldD = this[0].getAttribute("d") || "";
811                     this[0].setAttribute("d", oldD + d);
812                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + SVG._getX(parseFloat(x, 10));
813                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + SVG._getY(parseFloat(y, 10));
814                     this.path.push({type: "move", arg: arguments, pos: this.isAbsolute});
815                     return this;
816                 };
817                 p.lineTo = function (x, y) {
818                     this.last.x = (this.isAbsolute ? 0 : this.last.x) + SVG._getX(parseFloat(x, 10));
819                     this.last.y = (this.isAbsolute ? 0 : this.last.y) + SVG._getY(parseFloat(y, 10));
820                     var d = this.isAbsolute?"L":"l";
821                     var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
822                     var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
823                     d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
824                     var oldD = this[0].getAttribute("d") || "";
825                     this[0].setAttribute("d", oldD + d);
826                     this.path.push({type: "line", arg: arguments, pos: this.isAbsolute});
827                     return this;
828                 };
829                 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
830                     var d = this.isAbsolute ? "A" : "a";
831                     var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
832                     var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
833                     d += [SVG._getW(parseFloat(rx, 10)), SVG._getH(parseFloat(ry, 10)), 0, large_arc_flag, sweep_flag, _getX(parseFloat(x, 10)), _getY(parseFloat(y, 10))].join(" ");
834                     var oldD = this[0].getAttribute("d") || "";
835                     this[0].setAttribute("d", oldD + d);
836                     this.last.x = SVG._getX(parseFloat(x, 10));
837                     this.last.y = SVG._getY(parseFloat(y, 10));
838                     this.path.push({type: "arc", arg: arguments, pos: this.isAbsolute});
839                     return this;
840                 };
841                 p.cplineTo = function (x1, y1, w1) {
842                     if (!w1) {
843                         return this.lineTo(x1, y1);
844                     } else {
845                         var p = {};
846                         p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
847                         p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
848                         var x = p._getX(Math.round(parseFloat(x1, 10) * 100) / 100);
849                         var y = p._getY(Math.round(parseFloat(y1, 10) * 100) / 100);
850                         var w = SVG._getW(Math.round(parseFloat(w1, 10) * 100) / 100);
851                         var d = this.isAbsolute?"C":"c";
852                         var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
853                         for (var i = 0, ii = attr.length; i < ii; i++) {
854                             d += attr[i] + " ";
855                         }
856                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
857                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
858                         this.last.bx = attr[2];
859                         this.last.by = attr[3];
860                         var oldD = this[0].getAttribute("d") || "";
861                         this[0].setAttribute("d", oldD + d);
862                         this.path.push({type: "cpline", arg: arguments, pos: this.isAbsolute});
863                         return this;
864                     }
865                 };
866                 p.curveTo = function () {
867                     var p = {};
868                     p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
869                     p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
870                     if (arguments.length == 6) {
871                         var d = this.isAbsolute?"C":"c";
872                         for (var i = 0, ii = arguments.length; i < ii; i++) {
873                             d += p[(i % 2 == 0) ? "_getX" : "_getY"](Math.round(parseFloat(arguments[i], 10) * 100) / 100) + " ";
874                         }
875                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + p._getX((parseFloat(arguments[4], 10) * 100) / 100);
876                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + p._getY((parseFloat(arguments[5], 10) * 100) / 100);
877                         this.last.bx = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
878                         this.last.by = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
879                     } else {
880                         if (arguments.length == 4) {
881                             var d = this.isAbsolute?"S":"s";
882                             for (var i = 0, ii = arguments.length; i < ii; i++) {
883                                 d += p[i % 2 == 0 ? "_getX" : "_getY"]((parseFloat(arguments[i], 10) * 100) / 100) + " ";
884                             }
885                         }
886                         this.last.x = (this.isAbsolute ? 0 : this.last.x) + p._getX((parseFloat(arguments[2], 10) * 100) / 100);
887                         this.last.y = (this.isAbsolute ? 0 : this.last.y) + p._getY((parseFloat(arguments[3], 10) * 100) / 100);
888                         this.last.bx = p._getX((parseFloat(arguments[0], 10) * 100) / 100);
889                         this.last.by = p._getY((parseFloat(arguments[1], 10) * 100) / 100);
890                     }
891                     var oldD = this[0].getAttribute("d") || "";
892                     this[0].setAttribute("d", oldD + d);
893                     this.path.push({type: "curve", arg: arguments, pos: this.isAbsolute});
894                     return this;
895                 };
896                 p.addRoundedCorner = function (r, dir) {
897                     var R = .5522 * r, rollback = this.isAbsolute, o = this;
898                     if (rollback) {
899                         this.relatively();
900                         rollback = function () {
901                             o.absolutely();
902                         };
903                     } else {
904                         rollback = function () {};
905                     }
906                     var actions = {
907                         l: function () {
908                             return {
909                                 u: function () {
910                                     o.curveTo(-R, 0, -r, -(r - R), -r, -r);
911                                 },
912                                 d: function () {
913                                     o.curveTo(-R, 0, -r, r - R, -r, r);
914                                 }
915                             };
916                         },
917                         r: function () {
918                             return {
919                                 u: function () {
920                                     o.curveTo(R, 0, r, -(r - R), r, -r);
921                                 },
922                                 d: function () {
923                                     o.curveTo(R, 0, r, r - R, r, r);
924                                 }
925                             };
926                         },
927                         u: function () {
928                             return {
929                                 r: function () {
930                                     o.curveTo(0, -R, -(R - r), -r, r, -r);
931                                 },
932                                 l: function () {
933                                     o.curveTo(0, -R, R - r, -r, -r, -r);
934                                 }
935                             };
936                         },
937                         d: function () {
938                             return {
939                                 r: function () {
940                                     o.curveTo(0, R, -(R - r), r, r, r);
941                                 },
942                                 l: function () {
943                                     o.curveTo(0, R, R - r, r, -r, r);
944                                 }
945                             };
946                         }
947                     };
948                     actions[dir[0]]()[dir[1]]();
949                     rollback();
950                     return o;
951                 };
952                 p.andClose = function () {
953                     var oldD = this[0].getAttribute("d") || "";
954                     this[0].setAttribute("d", oldD + "Z ");
955                     this.path.push({type: "end"});
956                     return this;
957                 };
958                 if (typeof pathString == "string") {
959                     p.absolutely();
960                     C.pathfinder(p, pathString);
961                 }
962                 return p;
963             };
964             var addGrdientFill = function (o, gradient, SVG) {
965                 var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
966                 el.id = "raphael-gradient-" + SVG.gradients++;
967                 if (gradient.vector && gradient.vector.length) {
968                     el.setAttribute("x1", gradient.vector[0]);
969                     el.setAttribute("y1", gradient.vector[1]);
970                     el.setAttribute("x2", gradient.vector[2]);
971                     el.setAttribute("y2", gradient.vector[3]);
972                 }
973                 SVG.defs.appendChild(el);
974                 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
975                     var stop = document.createElementNS(SVG.svgns, "stop");
976                     stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
977                     stop.setAttribute("stop-color", gradient.dots[i].color || "#fff");
978                     if (typeof gradient.dots[i].opacity != "undefined") {
979                         stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
980                     }
981                     el.appendChild(stop);
982                 };
983                 o.setAttribute("fill", "url(#" + el.id + ")");
984             };
985             var Element = function (node, svg) {
986                 var X = 0,
987                     Y = 0,
988                     Rotation = {deg: 0, x: 0, y: 0},
989                     ScaleX = 1,
990                     ScaleY = 1,
991                     tMatrix = null;
992                 this[0] = node;
993                 this.node = node;
994                 this.svg = svg;
995                 this.attrs = this.attrs || {};
996                 this.transformations = []; // rotate, translate, scale, matrix
997                 this.rotate = function (deg) {
998                     if (deg == undefined) {
999                         return Rotation.deg;
1000                     }
1001                     var bbox = this.getBBox();
1002                     Rotation.deg += deg;
1003                     if (Rotation.deg) {
1004                         this.transformations[0] = ("rotate(" + Rotation.deg + " " + (bbox.x + bbox.width / 2) + " " + (bbox.y + bbox.height / 2) + ")");
1005                     } else {
1006                         this.transformations[0] = "";
1007                     }
1008                     this[0].setAttribute("transform", this.transformations.join(" "));
1009                     return this;
1010                 };
1011                 this.translate = function (x, y) {
1012                     if (x == undefined && y == undefined) {
1013                         return {x: X, y: Y};
1014                     }
1015                     X += x;
1016                     Y += y;
1017                     if (X && Y) {
1018                         this.transformations[1] = "translate(" + X + "," + Y + ")";
1019                     } else {
1020                         this.transformations[1] = "";
1021                     }
1022                     this[0].setAttribute("transform", this.transformations.join(" "));
1023                     return this;
1024                 };
1025                 this.scale = function (x, y) {
1026                     if (x == undefined && y == undefined) {
1027                         return {x: ScaleX, y: ScaleY};
1028                     }
1029                     y = y || x;
1030                     if (x != 0 && !(x == 1 && y == 1)) {
1031                         ScaleX *= x;
1032                         ScaleY *= y;
1033                         if (!(ScaleX == 1 && ScaleY == 1)) {
1034                             var bbox = this.getBBox(),
1035                                 dx = bbox.x * (1 - ScaleX) + (bbox.width / 2 - bbox.width * ScaleX / 2),
1036                                 dy = bbox.y * (1 - ScaleY) + (bbox.height / 2 - bbox.height * ScaleY / 2);
1037                             this.transformations[2] = new Matrix(ScaleX, 0, 0, ScaleY, dx, dy);
1038                         } else {
1039                             this.transformations[2] = "";
1040                         }
1041                         this[0].setAttribute("transform", this.transformations.join(" "));
1042                     }
1043                     return this;
1044                 };
1045             };
1046             Element.prototype.hide = function () {
1047                 this[0].style.display = "none";
1048                 return this;
1049             };
1050             Element.prototype.show = function () {
1051                 this[0].style.display = "block";
1052                 return this;
1053             };
1054             // depricated
1055             Element.prototype.matrix = function (xx, xy, yx, yy, dx, dy) {
1056                 this.transformations[3] = new Matrix(xx, xy, yx, yy, dx, dy);
1057                 this[0].setAttribute("transform", this.transformations.join(" "));
1058                 return this;
1059             };
1060             Element.prototype.remove = function () {
1061                 this[0].parentNode.removeChild(this[0]);
1062             };
1063             Element.prototype.getBBox = function () {
1064                 return this[0].getBBox();
1065             };
1066             Element.prototype.attr = function () {
1067                 if (arguments.length == 1 && typeof arguments[0] == "string") {
1068                     return this[0].getAttribute(arguments[0]);
1069                 }
1070                 if (arguments.length == 1 && arguments[0] instanceof Array) {
1071                     var values = {};
1072                     for (var j in arguments[0]) {
1073                         values[arguments[0][j]] = this.attrs[arguments[0][j]];
1074                     }
1075                     return values;
1076                 }
1077                 if (arguments.length == 2) {
1078                     var att = arguments[0],
1079                         value = arguments[1];
1080                     this[att] = value;
1081                     this.attrs[att] = value;
1082                     switch (att) {
1083                         case "rx":
1084                         case "cx":
1085                         case "x":
1086                             this[0].setAttribute(att, this.svg._getX(value));
1087                             break;
1088                         case "ry":
1089                         case "cy":
1090                         case "y":
1091                             this[0].setAttribute(att, this.svg._getY(value));
1092                             break;
1093                         case "width":
1094                             this[0].setAttribute(att, this.svg._getW(value));
1095                             break;
1096                         case "height":
1097                             this[0].setAttribute(att, this.svg._getH(value));
1098                             break;
1099                         case "gradient":
1100                             addGrdientFill(this[0], value, this.svg);
1101                             break;
1102                         case "stroke-dasharray":
1103                             this[0].setAttribute(att, value.replace(" ", ","));
1104                             break;
1105                         case "text":
1106                             if (this.type == "text") {
1107                                 this[0].removeChild(this[0].firstChild);
1108                                 this[0].appendChild(document.createTextNode(value));
1109                             }
1110                             break;
1111                         default :
1112                             var cssrule = att.replace(/(\-.)/g, function (w) {
1113                                 return w.substring(1).toUpperCase();
1114                             });
1115                             this[0].style[cssrule] = value;
1116                             // Need following line for Firefox
1117                             this[0].setAttribute(att, value);
1118                             break;
1119                     }
1120                 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1121                     var params = arguments[0];
1122                     for (var attr in params) {
1123                         this.attrs[attr] = params[attr];
1124                         if (attr == "stroke-dasharray") {
1125                             this[0].setAttribute(attr, params[attr].replace(" ", ","));
1126                         } else if (attr == "text" && this.type == "text") {
1127                             this[0].childNodes.length && this[0].removeChild(this[0].firstChild);
1128                             this[0].appendChild(document.createTextNode(params.text));
1129                         } else {
1130                             var cssrule = attr.replace(/(\-.)/g, function (w) {
1131                                 return w.substring(1).toUpperCase();
1132                             });
1133                             this[0].style[cssrule] = params[attr];
1134                             // Need following line for Firefox
1135                             this[0].setAttribute(attr, params[attr]);
1136                         }
1137                     }
1138                     if (params.gradient) {
1139                         this.attrs.gradient = params.gradient;
1140                         addGrdientFill(this[0], params.gradient, this.svg);
1141                     }
1142                 }
1143                 return this;
1144             };
1145             Element.prototype.toFront = function () {
1146                 this[0].parentNode.appendChild(this[0]);
1147                 return this;
1148             };
1149             Element.prototype.toBack = function () {
1150                 if (this[0].parentNode.firstChild != this[0]) {
1151                     this[0].parentNode.insertBefore(this[0], this[0].parentNode.firstChild);
1152                 }
1153                 return this;
1154             };
1155             var theCircle = function (svg, x, y, r) {
1156                 var el = document.createElementNS(svg.svgns, "circle");
1157                 el.setAttribute("cx", svg._getX(x));
1158                 el.setAttribute("cy", svg._getY(y));
1159                 el.setAttribute("r", r);
1160                 el.setAttribute("fill", "none");
1161                 el.setAttribute("stroke", "#000");
1162                 if (svg.canvas) {
1163                     svg.canvas.appendChild(el);
1164                 }
1165                 var res = new Element(el, svg);
1166                 res.attrs = res.attrs || {};
1167                 res.attrs.cx = x;
1168                 res.attrs.cy = y;
1169                 res.attrs.r = r;
1170                 res.attrs.stroke = "#000";
1171                 res.type = "circle";
1172                 return res;
1173             };
1174             var theRect = function (svg, x, y, w, h, r) {
1175                 var el = document.createElementNS(svg.svgns, "rect");
1176                 el.setAttribute("x", svg._getX(x));
1177                 el.setAttribute("y", svg._getY(y));
1178                 el.setAttribute("width", svg._getW(w));
1179                 el.setAttribute("height", svg._getH(h));
1180                 if (r) {
1181                     el.setAttribute("rx", r);
1182                     el.setAttribute("ry", r);
1183                 }
1184                 el.setAttribute("fill", "none");
1185                 el.setAttribute("stroke", "#000");
1186                 if (svg.canvas) {
1187                     svg.canvas.appendChild(el);
1188                 }
1189                 var res = new Element(el, svg);
1190                 res.attrs = res.attrs || {};
1191                 res.attrs.x = x;
1192                 res.attrs.y = y;
1193                 res.attrs.width = w;
1194                 res.attrs.height = h;
1195                 res.attrs.stroke = "#000";
1196                 if (r) {
1197                     res.attrs.rx = res.attrs.ry = r;
1198                 }
1199                 res.type = "rect";
1200                 return res;
1201             };
1202             var theEllipse = function (svg, x, y, rx, ry) {
1203                 var el = document.createElementNS(svg.svgns, "ellipse");
1204                 el.setAttribute("cx", svg._getX(x));
1205                 el.setAttribute("cy", svg._getY(y));
1206                 el.setAttribute("rx", svg._getW(rx));
1207                 el.setAttribute("ry", svg._getH(ry));
1208                 el.setAttribute("fill", "none");
1209                 el.setAttribute("stroke", "#000");
1210                 if (svg.canvas) {
1211                     svg.canvas.appendChild(el);
1212                 }
1213                 var res = new Element(el, svg);
1214                 res.attrs = res.attrs || {};
1215                 res.attrs.cx = x;
1216                 res.attrs.cy = y;
1217                 res.attrs.rx = rx;
1218                 res.attrs.ry = ry;
1219                 res.attrs.stroke = "#000";
1220                 res.type = "ellipse";
1221                 return res;
1222             };
1223             var theImage = function (svg, src, x, y, w, h) {
1224                 var el = document.createElementNS(svg.svgns, "image");
1225                 el.setAttribute("x", svg._getX(x));
1226                 el.setAttribute("y", svg._getY(y));
1227                 el.setAttribute("width", svg._getW(w));
1228                 el.setAttribute("height", svg._getH(h));
1229                 el.setAttributeNS(svg.xlink, "href", src);
1230                 if (svg.canvas) {
1231                     svg.canvas.appendChild(el);
1232                 }
1233                 var res = new Element(el, svg);
1234                 res.attrs = res.attrs || {};
1235                 res.attrs.x = x;
1236                 res.attrs.y = y;
1237                 res.attrs.width = w;
1238                 res.attrs.height = h;
1239                 res.type = "image";
1240                 return res;
1241             };
1242             var theText = function (svg, x, y, text) {
1243                 var el = document.createElementNS(svg.svgns, "text");
1244                 el.setAttribute("x", x);
1245                 el.setAttribute("y", y);
1246                 el.setAttribute("text-anchor", "middle");
1247                 el.setAttribute("fill", "#000");
1248                 if (text) {
1249                     el.appendChild(document.createTextNode(text));
1250                 }
1251                 if (svg.canvas) {
1252                     svg.canvas.appendChild(el);
1253                 }
1254                 var res = new Element(el, svg);
1255                 res.attrs = res.attrs || {};
1256                 res.attrs.x = x;
1257                 res.attrs.y = y;
1258                 res.attrs.fill = "#000";
1259                 res.type = "text";
1260                 return res;
1261             };
1262             var theGroup = function (svg) {
1263                 var el = document.createElementNS(svg.svgns, "g");
1264                 if (svg.canvas) {
1265                     svg.canvas.appendChild(el);
1266                 }
1267                 var i = new Element(el, svg);
1268                 for (var f in svg) {
1269                     if (f[0] != "_" && typeof svg[f] == "function") {
1270                         i[f] = (function (f) {
1271                             return function () {
1272                                 var e = svg[f].apply(svg, arguments);
1273                                 el.appendChild(e[0]);
1274                                 return e;
1275                             };
1276                         })(f);
1277                     }
1278                 }
1279                 i.type = "group";
1280                 return i;
1281             };
1282             r._create = function () {
1283                 // container, width, height
1284                 // x, y, width, height
1285                 if (typeof arguments[0] == "string") {
1286                     var container = document.getElementById(arguments[0]);
1287                     var width = arguments[1];
1288                     var height = arguments[2];
1289                 }
1290                 if (typeof arguments[0] == "object") {
1291                     var container = arguments[0];
1292                     var width = arguments[1];
1293                     var height = arguments[2];
1294                 }
1295                 if (typeof arguments[0] == "number") {
1296                     var container = 1,
1297                         x = arguments[0],
1298                         y = arguments[1],
1299                         width = arguments[2],
1300                         height = arguments[3];
1301                 }
1302                 if (!container) {
1303                     throw new Error("SVG container not found.");
1304                 }
1305                 C.canvas = document.createElementNS(C.svgns, "svg");
1306                 C.canvas.setAttribute("width", width || 320);
1307                 C.width = width || 320;
1308                 C.canvas.setAttribute("height", height || 200);
1309                 C.height = height || 200;
1310                 if (container == 1) {
1311                     document.body.appendChild(C.canvas);
1312                     C.canvas.style.position = "absolute";
1313                     C.canvas.style.left = x + "px";
1314                     C.canvas.style.top = y + "px";
1315                 } else {
1316                     if (container.firstChild) {
1317                         container.insertBefore(C.canvas, container.firstChild);
1318                     } else {
1319                         container.appendChild(C.canvas);
1320                     }
1321                 }
1322                 container = {
1323                     canvas: C.canvas,
1324                     clear: function () {
1325                         while (this.canvas.firstChild) {
1326                             this.canvas.removeChild(this.canvas.firstChild);
1327                         }
1328                         this.defs = document.createElementNS(C.svgns, "defs");
1329                         this.gradients = 0;
1330                         this.canvas.appendChild(this.defs);
1331                     }
1332                 };
1333                 for (var prop in C) {
1334                     if (prop != "create") {
1335                         container[prop] = C[prop];
1336                     }
1337                 }
1338                 container.clear();
1339                 return container;
1340             };
1341             C.remove = function () {
1342                 C.canvas.parentNode.removeChild(C.canvas);
1343             };
1344             C.svgns = "http://www.w3.org/2000/svg";
1345             C.xlink = "http://www.w3.org/1999/xlink";
1346         }
1347         if (type == "VML" || type == "SVG") {
1348             C.circle = function (x, y, r) {
1349                 return theCircle(this, x, y, r);
1350             };
1351             C.rect = function (x, y, w, h, r) {
1352                 return theRect(this, x, y, w, h, r);
1353             };
1354             C.ellipse = function (x, y, rx, ry) {
1355                 return theEllipse(this, x, y, rx, ry);
1356             };
1357             C.path = function (params, pathString) {
1358                 return thePath(params, pathString, this);
1359             };
1360             C.image = function (src, x, y, w, h) {
1361                 return theImage(this, src, x, y, w, h);
1362             };
1363             C.text = function (x, y, text) {
1364                 return theText(this, x, y, text);
1365             };
1366             C.group = function () {
1367                 return theGroup(this);
1368             };
1369             C.linerect = function (x, y, w, h, r) {
1370                 if (r && parseInt(r, 10)) {
1371                     return this.path({stroke: "#000"}).moveTo(x + r, y).lineTo(x + w - r, y).addRoundedCorner(r, "rd").lineTo(x + w, y + h - r).addRoundedCorner(r, "dl").lineTo(x + r, y + h).addRoundedCorner(r, "lu").lineTo(x, y + r).addRoundedCorner(r, "ur").andClose();
1372                 }
1373                 return this.path({stroke: "#000"}).moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).andClose();
1374             };
1375             C.drawGrid = function (x, y, w, h, wv, hv, color) {
1376                 color = color || "#000";
1377                 var p = this.path({stroke: color, "stroke-width": 1})
1378                         .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y);
1379                 for (var i = 1; i < hv; i++) {
1380                     p.moveTo(x, y + i * Math.round(h / hv)).lineTo(x + w, y + i * Math.round(h / hv));
1381                 }
1382                 for (var i = 1; i < wv; i++) {
1383                     p.moveTo(x + i * Math.round(w / wv), y).lineTo(x + i * Math.round(w / wv), y + h);
1384                 }
1385                 return p;
1386             };
1387             C.setGrid = function (xmin, ymin, xmax, ymax, w, h) {
1388                 var xc = (xmax - xmin) / w;
1389                 var yc = (ymax - ymin) / h;
1390                 this._getX = function (x) {
1391                     return xmin + x * xc;
1392                 };
1393                 this._getY = function (y) {
1394                     return ymin + y * yc;
1395                 };
1396                 this._getW = function (w) {
1397                     return w * xc;
1398                 };
1399                 this._getH = function (h) {
1400                     return h * yc;
1401                 };
1402             };
1403             C.clearGrid = function () {
1404                 this._getX = this._getY = this._getW = this._getH = function (x) { return x; };
1405             };
1406             C.safari = function () {
1407                 if (r.type == "SVG") {
1408                     var rect = C.rect(-C.width, -C.height, C.width * 3, C.height * 3).attr({stroke: "none"});
1409                     setTimeout(function () {rect.remove();}, 0);
1410                 }
1411             };
1412             Element.prototype.animateTo = function (x, y, ms, callback) {
1413                 clearTimeout(this.animation_in_progress);
1414                 if ("cx" in this.attrs || "x" in this.attrs) {
1415                     var is_round = ("cx" in this.attrs),
1416                         X = this.attrs.cx || this.attrs.x,
1417                         Y = this.attrs.cy || this.attrs.y;
1418                     if (x == X && y == Y) {
1419                         return this;
1420                     }
1421                     var dy = y - Y,
1422                         dx = x - X,
1423                         coeff = dy / dx,
1424                         plus = Y - coeff * X,
1425                         alpha = Math.atan(this.coeff);
1426                     this.xs = this.step * Math.cos(alpha);
1427                     if (x < X) {
1428                         this.xs = -this.xs;
1429                     }
1430                     var start = new Date(),
1431                         that = this;
1432                     (function () {
1433                         var time = (new Date()).getTime() - start.getTime();
1434                         if (time < ms) {
1435                             var x1 = X + time * dx / ms;
1436                             var y1 = x1 * coeff + plus;
1437                             that.attr(is_round ? {cx: x1, cy: y1} : {x: x1, y: y1});
1438                             that.animation_in_progress = setTimeout(arguments.callee, 1);
1439                             C.safari();
1440                         } else {
1441                             that.attr(is_round ? {cx: x, cy: y} : {x: x, y: y});
1442                             C.safari();
1443                             callback && callback.call(that);
1444                         }
1445                     })();
1446                 }
1447                 return this;
1448             };
1449             C.pathfinder = function (p, path) {
1450                 var commands = {
1451                     M: function (x, y) {
1452                         this.moveTo(x, y);
1453                     },
1454                     m: function (x, y) {
1455                         this.moveTo(this.last.x + x, this.last.y + y);
1456                     },
1457                     C: function (x1, y1, x2, y2, x3, y3) {
1458                         this.curveTo(x1, y1, x2, y2, x3, y3);
1459                     },
1460                     c: function (x1, y1, x2, y2, x3, y3) {
1461                         this.curveTo(this.last.x + x1, this.last.y + y1, this.last.x + x2, this.last.y + y2, this.last.x + x3, this.last.y + y3);
1462                     },
1463                     S: function (x1, y1, x2, y2) {
1464                         p.curveTo(x1, y1, x2, y2);
1465                     },
1466                     s: function (x1, y1, x2, y2) {
1467                         this.curveTo(this.last.x + x1, this.last.y + y1, this.last.x + x2, this.last.y + y2);
1468                     },
1469                     L: function (x, y) {
1470                         p.lineTo(x, y);
1471                     },
1472                     l: function (x, y) {
1473                         this.lineTo(this.last.x + x, this.last.y + y);
1474                     },
1475                     H: function (x) {
1476                         this.lineTo(x, this.last.y);
1477                     },
1478                     h: function (x) {
1479                         this.lineTo(this.last.x + x, this.last.y);
1480                     },
1481                     V: function (y) {
1482                         this.lineTo(this.last.x, y);
1483                     },
1484                     v: function (y) {
1485                         this.lineTo(this.last.x, this.last.y + y);
1486                     },
1487                     A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
1488                         this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
1489                     },
1490                     a: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
1491                         this.arcTo(this.last.x + rx, this.last.y + ry, largearcflag, sweethisflag, this.last.x + x, this.last.y + y);
1492                     },
1493                     z: function () {
1494                         this.andClose();
1495                     }
1496                 };
1497                 path.replace(/([mzlhvcsqta])\s*((-?\d*\.?\d*\s*,?\s*)+)/ig, function (a, b, c) {
1498                     var params = [];
1499                     c.replace(/(-?\d*\.?\d*)\s*,?\s*/ig, function (a, b) {
1500                         b && params.push(+b);
1501                     });
1502                     while (params.length >= commands[b].length) {
1503                         commands[b].apply(p, params.splice(0, commands[b].length));
1504                         if (!commands[b].length) {
1505                             break;
1506                         };
1507                     }
1508                 });
1509             };
1510             return r;
1511         } else {
1512             return function () {};
1513         }
1514     })((!(window.SVGPreserveAspectRatio && window.SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN == 2)) ? "VML" : "SVG");
1515
1516
1517 Raphael.vml = !(Raphael.svg = (Raphael.type == "SVG"));
1518 if (Raphael.vml && window.CanvasRenderingContext2D) {
1519     Raphael.type = "Canvas only";
1520     Raphael.vml = Raphael.svg = false;
1521 }
1522 Raphael.toString = function () {
1523     return "Your browser supports " + this.type + ".\nYou are running " + unescape("Rapha%EBl%20") + this.version;
1524 };
1525 // generic utilities
1526 Raphael.hsb2rgb = function (hue, saturation, brightness) {
1527     if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
1528         brightness = hue.b;
1529         saturation = hue.s;
1530         hue = hue.h;
1531     }
1532     var red,
1533         green,
1534         blue;
1535     if (brightness == 0) {
1536         return {r: 0, g: 0, b: 0, hex: "#000"};
1537     } else {
1538         var i = Math.floor(hue * 6),
1539             f = (hue * 6) - i,
1540             p = brightness * (1 - saturation),
1541             q = brightness * (1 - (saturation * f)),
1542             t = brightness * (1 - (saturation * (1 - f)));
1543         [
1544             function () {red = brightness; green = t; blue = p;},
1545             function () {red = q; green = brightness; blue = p;},
1546             function () {red = p; green = brightness; blue = t;},
1547             function () {red = p; green = q; blue = brightness;},
1548             function () {red = t; green = p; blue = brightness;},
1549             function () {red = brightness; green = p; blue = q;},
1550             function () {red = brightness; green = t; blue = p;},
1551         ][i]();
1552     }
1553     var rgb = {r: red, g: green, b: blue};
1554     red *= 255;
1555     green *= 255;
1556     blue *= 255;
1557     var r = Math.round(red).toString(16);
1558     if (r.length == 1) {
1559         r = "0" + r;
1560     }
1561     var g = Math.round(green).toString(16);
1562     if (g.length == 1) {
1563         g = "0" + g;
1564     }
1565     var b = Math.round(blue).toString(16);
1566     if (b.length == 1) {
1567         b = "0" + b;
1568     }
1569     rgb.hex = "#" + r + g + b;
1570     return rgb;
1571 };
1572 Raphael.rgb2hsb = function (red, green, blue) {
1573     if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
1574         blue = red.b;
1575         green = red.g;
1576         red = red.r;
1577     }
1578     if (typeof red == "string" && red.charAt(0) == "#") {
1579         if (red.length == 4) {
1580             blue = parseInt(red.substring(3), 16);
1581             green = parseInt(red.substring(2, 3), 16);
1582             red = parseInt(red.substring(1, 2), 16);
1583         } else {
1584             blue = parseInt(red.substring(5), 16);
1585             green = parseInt(red.substring(3, 5), 16);
1586             red = parseInt(red.substring(1, 3), 16);
1587         }
1588     }
1589     if (red > 1 || green > 1 || blue > 1) {
1590         red /= 255;
1591         green /= 255;
1592         blue /= 255;
1593     }
1594     var max = Math.max(red, green, blue),
1595         min = Math.min(red, green, blue),
1596         hue,
1597         saturation,
1598         brightness = max;
1599     if (min == max) {
1600         return {h: 0, s: 0, b: max};
1601     } else {
1602         var delta = (max - min);
1603         saturation = delta / max;
1604         if (red == max) {
1605             hue = (green - blue) / delta;
1606         } else if (green == max) {
1607             hue = 2 + ((blue - red) / delta);
1608         } else {
1609             hue = 4 + ((red - green) / delta);
1610         }
1611         hue /= 6;
1612         if (hue < 0) {
1613             hue += 1;
1614         }
1615         if (hue > 1) {
1616             hue -= 1;
1617         }
1618     }
1619     return {h: hue, s: saturation, b: brightness};
1620 };
1621 Raphael.getColor = function (value) {
1622     var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
1623     var rgb = this.hsb2rgb(start.h, start.s, start.b);
1624     start.h += .075;
1625     if (start.h > 1) {
1626         start.h = 0;
1627         start.s -= .2;
1628         if (start.s <= 0) {
1629             arguments.callee.start = {h: 0, s: 1, b: start.b};
1630         }
1631     }
1632     return rgb.hex;
1633 };
1634 Raphael.getColor.reset = function () {
1635     this.start = undefined;
1636 };