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