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