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