Added “text” attribute for text nodes + IE fixes
[raphael] / raphael.js
1 function Raphael() {
2     return (function (r, args) {
3         r.version = "0.5.4b";
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[0].style.display = "none";
367                 };
368                 this.show = function () {
369                     this[0].style.display = "block";
370                 };
371                 this.rotate = function (deg) {
372                     Rotation += deg;
373                     this.Group.style.rotation = Rotation;
374                     return this;
375                 };
376                 this.translate = function (x, y) {
377                     this.X += x;
378                     this.Y += y;
379                     this.Group.style.left = this.X + "px";
380                     this.Group.style.top = this.Y + "px";
381                     return this;
382                 };
383                 // depricated
384                 this.matrix = function (xx, xy, yx, yy, dx, dy) {
385                     tMatrix = new Matrix(xx, xy, yx, yy, dx, dy);
386                     this.Group.style.filter = tMatrix;
387                     return this;
388                 };
389                 this.scale = function (x, y) {
390                     y = y || x;
391                     if (x != 0 && !(x == 1 && y == 1)) {
392                         var dirx = Math.round(x / Math.abs(x)),
393                             diry = Math.round(y / Math.abs(y));
394                         if (dirx != 1 || diry != 1) {
395                             this[0].style.filter = new Matrix(dirx, 0, 0, diry, 0, 0);
396                         }
397                         var width = parseInt(this[0].style.width, 10) * x * dirx;
398                         var height = parseInt(this[0].style.height, 10) * y * diry;
399                         var left = parseInt(this[0].style.left, 10);
400                         var top = parseInt(this[0].style.top, 10);
401                         this[0].style.left = this.X = left + this.W / 2 - width / 2;
402                         this[0].style.top = this.Y = top + this.H / 2 - height / 2;
403                         this[0].style.width = this.W = width;
404                         this[0].style.height = this.H = height;
405                     }
406                     return this;
407                 };
408                 this.getBBox = function () {
409                     return {
410                         x: this.Group.offsetLeft,
411                         y: this.Group.offsetTop,
412                         width: this.Group.offsetWidth,
413                         height: this.Group.offsetHeight
414                     };
415                 };
416                 this.remove = function () {
417                     this[0].parentNode.removeChild(this[0]);
418                     this.Group.parentNode.removeChild(this.Group);
419                     this.shape && this.shape.parentNode.removeChild(this.shape);
420                 };
421                 this.attr = function () {
422                     if (arguments.length == 1 && typeof arguments[0] == "string") {
423                         return this[0].attrs[arguments[0]];
424                     }
425                     if (this[0].attrs && arguments.length == 1 && arguments[0] instanceof Array) {
426                         var values = {};
427                         for (var i = 0, ii = arguments[0].length; i < ii; i++) {
428                             values[arguments[0][i]] = this[0].attrs[arguments[0][i]];
429                         };
430                         return values;
431                     }
432                     if (this[0].tagName.toLowerCase() == "group") {
433                         var children = this[0].childNodes;
434                         this[0].attrs = this[0].attrs || {};
435                         if (arguments.length == 2) {
436                             this[0].attrs[arguments[0]] = arguments[1];
437                         } else if (arguments.length = 1 || typeof arguments[0] == "object") {
438                             for (var j in arguments[0]) {
439                                 this[0].attrs[j] = arguments[0][j];
440                             }
441                         }
442                         for (var i = 0, ii = children.length; i < ii; i++) {
443                             this.attr.apply(new item(children[i], this[0], vml), arguments);
444                         }
445                     } else {
446                         if (arguments.length == 2) {
447                             var att = arguments[0],
448                                 value = arguments[1];
449                             switch (att) {
450                                 case "r":
451                                     this[0].style.width = this[0].style.height = value * 2 + "px";
452                                     this[0].style.left = vml._getX(this.cx) - value + "px";
453                                     this[0].style.top = vml._getY(this.cy) - value + "px";
454                                     this.r = value;
455                                     break;
456                                 case "rx":
457                                     this[0].style.width = value * 2 + "px";
458                                     this[0].style.left = vml._getX(this.cx) - value + "px";
459                                     this.rx = value;
460                                     break;
461                                 case "ry":
462                                     this[0].style.height = value * 2 + "px";
463                                     this[0].style.top = vml._getY(this.cy) - value + "px";
464                                     this.ry = value;
465                                     break;
466                                 case "cx":
467                                     if (this.r || this.rx) {
468                                         this[0].style.left = vml._getX(value) - (this.r || vml._getW(this.rx)) + "px";
469                                         this.cx = value;
470                                     }
471                                     break;
472                                 case "x":
473                                     this[0].style.left = vml._getX(value) + "px";
474                                     break;
475                                 case "cy":
476                                     if (this.r || this.ry) {
477                                         this[0].style.top = vml._getY(value) - (this.r || vml._getH(this.ry)) + "px";
478                                         this.cy = value;
479                                     }
480                                     break;
481                                 case "y":
482                                     this[0].style.top = vml._getY(value) + "px";
483                                     break;
484                                 case "fill":
485                                 case "fill-opacity":
486                                 case "joinstyle":
487                                 case "opacity":
488                                 case "stroke":
489                                 case "stroke-dasharray":
490                                 case "stroke-opacity":
491                                 case "stroke-width":
492                                     var params = {};
493                                     params[att] = value;
494                                     setFillAndStroke(this, params);
495                                     break;
496                                 case "font":
497                                 case "font-family":
498                                 case "font-size":
499                                 case "font-weight":
500                                 case "height":
501                                 case "width":
502                                     this[0].style[att] = value;
503                                     break;
504                                 case "id":
505                                     this[0].id = value;
506                                     break;
507                                 case "text":
508                                     if (this.type == "text") {
509                                         this[0].string = value;
510                                     }
511                                     break;
512                                 case "gradient":
513                                     addGrdientFill(this, value);
514                             }
515                         }
516                         if (arguments.length == 1 && typeof arguments[0] == "object") {
517                             var params = arguments[0];
518                             setFillAndStroke(this, params);
519                             if (params.gradient) {
520                                 addGrdientFill(this, params.gradient);
521                             }
522                             if (params.text && this.type == "text") {
523                                 this[0].string = params.text;
524                             }
525                             if (params.id) {
526                                 this[0].id = params.id;
527                             }
528                         }
529                     }
530                     return this;
531                 };
532                 this.toFront = function () {
533                     this.Group.parentNode.appendChild(this.Group);
534                 };
535                 this.toBack = function () {
536                     if (this.Group.parentNode.firstChild != this.Group) {
537                         this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
538                     }
539                 };
540             };
541             var theCircle = function (vml, x, y, r) {
542                 var g = document.createElement("rvml:group");
543                 var o = document.createElement("rvml:oval");
544                 g.appendChild(o);
545                 vml.canvas.appendChild(g);
546                 var res = new Element(o, g, vml);
547                 setFillAndStroke(res, {stroke: "#000"});
548                 setTheBox(vml, res, x - r, y - r, r * 2, r * 2);
549                 o.attrs.cx = x;
550                 o.attrs.cy = y;
551                 o.attrs.r = r;
552                 res.type = "circle";
553                 return res;
554             };
555             var theRect = function (vml, x, y, w, h, r) {
556                 var g = document.createElement("rvml:group");
557                 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
558                 if (r) {
559                     o.arcsize = r / (Math.min(w, h));
560                 }
561                 g.appendChild(o);
562                 vml.canvas.appendChild(g);
563                 var res = new Element(o, g, vml);
564                 setFillAndStroke(res, {stroke: "#000"});
565                 setTheBox(vml, res, x, y, w, h);
566                 o.attrs.x = x;
567                 o.attrs.y = y;
568                 o.attrs.w = w;
569                 o.attrs.h = h;
570                 o.attrs.r = r;
571                 res.type = "rect";
572                 return res;
573             };
574             var theEllipse = function (vml, x, y, rx, ry) {
575                 var g = document.createElement("rvml:group");
576                 var o = document.createElement("rvml:oval");
577                 g.appendChild(o);
578                 vml.canvas.appendChild(g);
579                 var res = new Element(o, g, vml);
580                 setFillAndStroke(res, {stroke: "#000"});
581                 setTheBox(vml, res, x - rx, y - ry, rx * 2, ry * 2);
582                 o.attrs.cx = x;
583                 o.attrs.cy = y;
584                 o.attrs.rx = rx;
585                 o.attrs.ry = ry;
586                 res.type = "ellipse";
587                 return res;
588             };
589             var theImage = function (vml, src, x, y, w, h) {
590                 var g = document.createElement("rvml:group");
591                 var o = document.createElement("rvml:image");
592                 o.src = src;
593                 g.appendChild(o);
594                 vml.canvas.appendChild(g);
595                 var res = new Element(o, g, vml);
596                 setTheBox(vml, res, x, y, w, h);
597                 res.type = "image";
598                 return res;
599             };
600             var theText = function (vml, x, y, text) {
601                 // @TODO: setTheBox
602                 var g = document.createElement("rvml:group"), gs = g.style;
603                 var el = document.createElement("rvml:shape"), ol = el.style;
604                 var path = document.createElement("rvml:path"), ps = path.style;
605                 path.v = ["m", Math.round(x), ", ", Math.round(y), "l", Math.round(x) + 1, ", ", Math.round(y)].join("");
606                 path.textpathok = true;
607                 ol.width = vml.width;
608                 ol.height = vml.height;
609                 gs.position = "absolute";
610                 gs.left = 0;
611                 gs.top = 0;
612                 gs.width = vml.width;
613                 gs.height = vml.height;
614                 var o = document.createElement("rvml:textpath");
615                 o.string = text;
616                 o.on = true;
617                 o.coordsize = vml.coordsize;
618                 o.coordorigin = vml.coordorigin;
619                 el.appendChild(o);
620                 el.appendChild(path);
621                 // g.appendChild(el);
622                 vml.canvas.appendChild(el);
623                 var res = new Element(o, el, vml);
624                 res.shape = el;
625                 res.type = "text";
626                 return res;
627             };
628             var theGroup = function (vml) {
629                 var el = document.createElement("rvml:group"), els = el.style;
630                 els.position = "absolute";
631                 els.left = 0;
632                 els.top = 0;
633                 els.width = vml.width;
634                 els.height = vml.height;
635                 if (vml.canvas) {
636                     vml.canvas.appendChild(el);
637                 }
638                 var res = new Element(el, el, vml);
639                 for (var f in vml) {
640                     if (f.charAt(0) != "_" && typeof vml[f] == "function") {
641                         res[f] = (function (f) {
642                             return function () {
643                                 var e = vml[f].apply(vml, arguments);
644                                 el.appendChild(e[0].parentNode);
645                                 return e;
646                             };
647                         })(f);
648                     }
649                 }
650                 res.type = "group";
651                 return res;
652             };
653             r._create = function () {
654                 // container, width, height
655                 // x, y, width, height
656                 if (typeof arguments[0] == "string") {
657                     var container = document.getElementById(arguments[0]);
658                     var width = arguments[1];
659                     var height = arguments[2];
660                 }
661                 if (typeof arguments[0] == "object") {
662                     var container = arguments[0];
663                     var width = arguments[1];
664                     var height = arguments[2];
665                 }
666                 if (typeof arguments[0] == "number") {
667                     var container = 1,
668                         x = arguments[0],
669                         y = arguments[1],
670                         width = arguments[2],
671                         height = arguments[3];
672                 }
673                 if (!container) {
674                     throw new Error("VML container not found.");
675                 }
676                 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
677                 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
678                 var c = document.createElement("div"),
679                     r = C.canvas = document.createElement("rvml:group"),
680                     cs = c.style, rs = r.style;
681                 C.width = width;
682                 C.height = height;
683                 width = width || "320px";
684                 height = height || "200px";
685                 cs.clip = "rect(0 " + width + " " + height + " 0)";
686                 cs.position = "absolute";
687                 rs.width  = width;
688                 rs.height = height;
689                 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
690                 r.coordorigin = "0 0";
691
692                 var b = document.createElement("rvml:rect"), bs = b.style;
693                 bs.left = bs.top = 0;
694                 bs.width  = rs.width;
695                 bs.height = rs.height;
696                 b.filled = b.stroked = "f";
697
698                 r.appendChild(b);
699                 c.appendChild(r);
700                 if (container == 1) {
701                     document.body.appendChild(c);
702                     cs.position = "absolute";
703                     cs.left = x + "px";
704                     cs.top = y + "px";
705                     cs.width = width;
706                     cs.height = height;
707                     container = {
708                         style: {
709                             width: width,
710                             height: height
711                         }
712                     };
713                 } else {
714                     cs.width = container.style.width = width;
715                     cs.height = container.style.height = height;
716                     if (container.firstChild) {
717                         container.insertBefore(c, container.firstChild);
718                     } else {
719                         container.appendChild(c);
720                     }
721                 }
722                 for (var prop in C) {
723                     container[prop] = C[prop];
724                 }
725                 container.clear = function () {
726                     var todel = [];
727                     for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
728                         if (r.childNodes[i] != b) {
729                             todel.push(r.childNodes[i]);
730                         }
731                     }
732                     for (i = 0, ii = todel.length; i < ii; i++) {
733                         r.removeChild(todel[i]);
734                     }
735                 };
736                 return container;
737             };
738         }
739         if (r.svg) {
740             Matrix.prototype.toString = function () {
741                 return "matrix(" + this.m[0][0] +
742                     ", " + this.m[1][0] + ", " + this.m[0][1] + ", " + this.m[1][1] +
743                     ", " + this.m[2][0] + ", " + this.m[2][1] + ")";
744             };
745             var thePath = function (params, pathString, SVG) {
746                 var el = document.createElementNS(SVG.svgns, "path");
747                 el.setAttribute("fill", "none");
748                 if (params) {
749                     for (var attr in params) {
750                         if (params.gradient) {
751                             addGrdientFill(el, params.gradient, SVG);
752                         } else {
753                             el.setAttribute(attr, params[attr]);
754                         }
755                     }
756                 }
757                 if (SVG.canvas) {
758                     SVG.canvas.appendChild(el);
759                 }
760                 var p = new Element(el, SVG);
761                 for (var attr in params) {
762                     p.attrs[attr] = params[attr];
763                 }
764                 p.isAbsolute = true;
765                 p.path = [];
766                 p.last = {x: 0, y: 0, bx: 0, by: 0};
767                 p.absolutely = function () {
768                     this.isAbsolute = true;
769                     return this;
770                 };
771                 p.relatively = function () {
772                     this.isAbsolute = false;
773                     return this;
774                 };
775                 p.redraw = function () {
776                     this[0].setAttribute("d", "M0 0");
777                     var oldPath = this.path;
778                     this.path = [];
779                     for (var i = 0, ii = oldPath.length; i < ii; i++) {
780                         if (oldPath[i].type != "end") {
781                             this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
782                         } else {
783                             this.andClose();
784                         }
785                     };
786                 };
787                 p.moveTo = function (x, y) {
788                     var d = this.isAbsolute?"M":"m";
789                     var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
790                     var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
791                     d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
792                     var oldD = this[0].getAttribute("d") || "";
793                     this[0].setAttribute("d", oldD + d);
794                     this.last.x = SVG._getX(parseFloat(x, 10));
795                     this.last.y = SVG._getY(parseFloat(y, 10));
796                     this.path.push({type: "move", arg: arguments, pos: this.isAbsolute});
797                     return this;
798                 };
799                 p.lineTo = function (x, y) {
800                     var d = this.isAbsolute?"L":"l";
801                     var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
802                     var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
803                     d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
804                     var oldD = this[0].getAttribute("d") || "";
805                     this[0].setAttribute("d", oldD + d);
806                     this.last.x = SVG._getX(parseFloat(x, 10));
807                     this.last.y = SVG._getY(parseFloat(y, 10));
808                     this.path.push({type: "line", arg: arguments, pos: this.isAbsolute});
809                     return this;
810                 };
811                 p.cplineTo = function (x1, y1, w1) {
812                     if (!w1) {
813                         return this.lineTo(x1, y1);
814                     } else {
815                         var p = {};
816                         p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
817                         p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
818                         var x = p._getX(Math.round(parseFloat(x1, 10) * 100) / 100);
819                         var y = p._getY(Math.round(parseFloat(y1, 10) * 100) / 100);
820                         var w = SVG._getW(Math.round(parseFloat(w1, 10) * 100) / 100);
821                         var d = this.isAbsolute?"C":"c";
822                         var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
823                         for (var i = 0, ii = attr.length; i < ii; i++) {
824                             d += attr[i] + " ";
825                         }
826                         this.last.x = attr[4];
827                         this.last.y = attr[5];
828                         this.last.bx = attr[2];
829                         this.last.by = attr[3];
830                         var oldD = this[0].getAttribute("d") || "";
831                         this[0].setAttribute("d", oldD + d);
832                         this.path.push({type: "cpline", arg: arguments, pos: this.isAbsolute});
833                         return this;
834                     }
835                 };
836                 p.curveTo = function () {
837                     var p = {};
838                     p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
839                     p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
840                     if (arguments.length == 6) {
841                         var d = this.isAbsolute?"C":"c";
842                         for (var i = 0, ii = arguments.length; i < ii; i++) {
843                             d += p[(i % 2 == 0) ? "_getX" : "_getY"](Math.round(parseFloat(arguments[i], 10) * 100) / 100) + " ";
844                         }
845                         this.last.x = p._getX((parseFloat(arguments[4], 10) * 100) / 100);
846                         this.last.y = p._getY((parseFloat(arguments[5], 10) * 100) / 100);
847                         this.last.bx = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
848                         this.last.by = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
849                     } else {
850                         if (arguments.length == 4) {
851                             var d = this.isAbsolute?"S":"s";
852                             for (var i = 0, ii = arguments.length; i < ii; i++) {
853                                 d += p[i % 2 == 0 ? "_getX" : "_getY"]((parseFloat(arguments[i], 10) * 100) / 100) + " ";
854                             }
855                         }
856                         this.last.x = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
857                         this.last.y = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
858                         this.last.bx = p._getX((parseFloat(arguments[0], 10) * 100) / 100);
859                         this.last.by = p._getY((parseFloat(arguments[1], 10) * 100) / 100);
860                     }
861                     var oldD = this[0].getAttribute("d") || "";
862                     this[0].setAttribute("d", oldD + d);
863                     this.path.push({type: "curve", arg: arguments, pos: this.isAbsolute});
864                     return this;
865                 };
866                 p.addRoundedCorner = function (r, dir) {
867                     var R = .5522 * r, rollback = this.isAbsolute, o = this;
868                     if (rollback) {
869                         this.relatively();
870                         rollback = function () {
871                             o.absolutely();
872                         };
873                     } else {
874                         rollback = function () {};
875                     }
876                     var actions = {
877                         l: function () {
878                             return {
879                                 u: function () {
880                                     o.curveTo(-R, 0, -r, -(r - R), -r, -r);
881                                 },
882                                 d: function () {
883                                     o.curveTo(-R, 0, -r, r - R, -r, r);
884                                 }
885                             };
886                         },
887                         r: function () {
888                             return {
889                                 u: function () {
890                                     o.curveTo(R, 0, r, -(r - R), r, -r);
891                                 },
892                                 d: function () {
893                                     o.curveTo(R, 0, r, r - R, r, r);
894                                 }
895                             };
896                         },
897                         u: function () {
898                             return {
899                                 r: function () {
900                                     o.curveTo(0, -R, -(R - r), -r, r, -r);
901                                 },
902                                 l: function () {
903                                     o.curveTo(0, -R, R - r, -r, -r, -r);
904                                 }
905                             };
906                         },
907                         d: function () {
908                             return {
909                                 r: function () {
910                                     o.curveTo(0, R, -(R - r), r, r, r);
911                                 },
912                                 l: function () {
913                                     o.curveTo(0, R, R - r, r, -r, r);
914                                 }
915                             };
916                         }
917                     };
918                     actions[dir[0]]()[dir[1]]();
919                     rollback();
920                     return o;
921                 };
922                 p.andClose = function () {
923                     var oldD = this[0].getAttribute("d") || "";
924                     this[0].setAttribute("d", oldD + "Z ");
925                     this.path.push({type: "end"});
926                     return this;
927                 };
928                 if (typeof pathString == "string") {
929                     pathString = pathString.replace(/([mzlhvcsqta])/ig, ",$1,").replace(/([^,])\-/ig, "$1,-");
930                     path = pathString.split(",");
931                     var i = 1, ii = path.length;
932                     while (i < ii) {
933                         switch (path[i]) {
934                             case "M":
935                                 p.absolutely().moveTo(path[++i], path[++i]);
936                                 break;
937                             case "m":
938                                 p.relatively().moveTo(path[++i], path[++i]);
939                                 break;
940                             case "C":
941                                 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
942                                 break;
943                             case "c":
944                                 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
945                                 break;
946                             case "s":
947                                 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i]);
948                                 break;
949                             case "S":
950                                 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i]);
951                                 break;
952                             case "L":
953                                 p.absolutely().lineTo(path[++i], path[++i]);
954                                 break;
955                             case "l":
956                                 p.relatively().lineTo(path[++i], path[++i]);
957                                 break;
958                             case "H":
959                                 p.absolutely().lineTo(path[++i], 0);
960                                 break;
961                             case "h":
962                                 p.relatively().lineTo(path[++i], 0);
963                                 break;
964                             case "V":
965                                 p.absolutely().lineTo(0, path[++i]);
966                                 break;
967                             case "v":
968                                 p.relatively().lineTo(0, path[++i]);
969                                 break;
970                             case "z":
971                                 p.andClose();
972                                 break;
973                         }
974                         i++;
975                     }
976                 }
977                 return p;
978             };
979             var addGrdientFill = function (o, gradient, SVG) {
980                 var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
981                 el.id = "raphael-gradient-" + SVG.gradients++;
982                 if (gradient.vector && gradient.vector.length) {
983                     el.setAttribute("x1", gradient.vector[0]);
984                     el.setAttribute("y1", gradient.vector[1]);
985                     el.setAttribute("x2", gradient.vector[2]);
986                     el.setAttribute("y2", gradient.vector[3]);
987                 }
988                 SVG.defs.appendChild(el);
989                 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
990                     var stop = document.createElementNS(SVG.svgns, "stop");
991                     stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
992                     stop.setAttribute("stop-color", gradient.dots[i].color || "#fff");
993                     if (typeof gradient.dots[i].opacity != "undefined") {
994                         stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
995                     }
996                     el.appendChild(stop);
997                 };
998                 o.setAttribute("fill", "url(#" + el.id + ")");
999             };
1000             var Element = function (node, svg) {
1001                 var X = 0,
1002                     Y = 0,
1003                     Rotation = {deg: 0, x: 0, y: 0},
1004                     Scale = 1,
1005                     tMatrix = null;
1006                 this[0] = node;
1007                 this.attrs = this.attrs || {};
1008                 this.transformations = [];
1009                 this.hide = function () {
1010                     this[0].style.display = "none";
1011                 };
1012                 this.show = function () {
1013                     this[0].style.display = "block";
1014                 };
1015                 this.rotate = function (deg) {
1016                     var bbox = this.getBBox();
1017                     this.transformations.push("rotate(" + deg + " " + (bbox.x + bbox.width / 2) + " " + (bbox.y + bbox.height / 2) + ")");
1018                     this[0].setAttribute("transform", this.transformations.join(" "));
1019                     return this;
1020                 };
1021                 this.translate = function (x, y) {
1022                     this.transformations.push("translate(" + x + "," + y + ")");
1023                     this[0].setAttribute("transform", this.transformations.join(" "));
1024                     return this;
1025                 };
1026                 this.scale = function (x, y) {
1027                     y = y || x;
1028                     if (x != 0 && !(x == 1 && y == 1)) {
1029                         var bbox = this.getBBox(),
1030                             dx = bbox.x * (1 - x) + (bbox.width / 2 - bbox.width * x / 2),
1031                             dy = bbox.y * (1 - y) + (bbox.height / 2 - bbox.height * y / 2);
1032                         this.transformations.push(new Matrix(x, 0, 0, y, dx, dy));
1033                         this[0].setAttribute("transform", this.transformations.join(" "));
1034                     }
1035                     return this;
1036                 };
1037                 this.matrix = function (xx, xy, yx, yy, dx, dy) {
1038                     this.transformations.push(new Matrix(xx, xy, yx, yy, dx, dy));
1039                     this[0].setAttribute("transform", this.transformations.join(" "));
1040                     return this;
1041                 };
1042                 this.remove = function () {
1043                     this[0].parentNode.removeChild(this[0]);
1044                 };
1045                 this.getBBox = function () {
1046                     return this[0].getBBox();
1047                 };
1048                 this.attr = function () {
1049                     if (arguments.length == 1 && typeof arguments[0] == "string") {
1050                         return this.attrs[arguments[0]];
1051                     }
1052                     if (arguments.length == 1 && arguments[0] instanceof Array) {
1053                         var values = {};
1054                         for (var j in arguments[0]) {
1055                             values[arguments[0][j]] = this.attrs[arguments[0][j]];
1056                         }
1057                         return values;
1058                     }
1059                     if (arguments.length == 2) {
1060                         var att = arguments[0],
1061                             value = arguments[1];
1062                         this[att] = value;
1063                         this.attrs[att] = value;
1064                         switch (att) {
1065                             case "rx":
1066                             case "cx":
1067                             case "x":
1068                                 this[0].setAttribute(att, svg._getX(value));
1069                                 break;
1070                             case "ry":
1071                             case "cy":
1072                             case "y":
1073                                 this[0].setAttribute(att, svg._getY(value));
1074                                 break;
1075                             case "width":
1076                                 this[0].setAttribute(att, svg._getW(value));
1077                                 break;
1078                             case "height":
1079                                 this[0].setAttribute(att, svg._getH(value));
1080                                 break;
1081                             case "gradient":
1082                                 addGrdientFill(this[0], params.gradient, svg);
1083                                 break;
1084                             case "stroke-dasharray":
1085                                 this[0].setAttribute(att, value.replace(" ", ","));
1086                                 break;
1087                             case "text":
1088                                 if (this.type == "text") {
1089                                     this[0].removeChild(this[0].firstChild);
1090                                     this[0].appendChild(document.createTextNode(value));
1091                                 }
1092                                 break;
1093                             default :
1094                                 var cssrule = att.replace(/(\-.)/g, function (w) {
1095                                     return w.substring(1).toUpperCase();
1096                                 });
1097                                 this[0].style[cssrule] = value;
1098                                 // Need following line for Firefox
1099                                 this[0].setAttribute(att, value);
1100                         }
1101                     } else if (arguments.length = 1 && typeof arguments[0] == "object") {
1102                         var params = arguments[0];
1103                         for (var attr in params) {
1104                             this.attrs[attr] = params[attr];
1105                             if (attr == "stroke-dasharray") {
1106                                 this[0].setAttribute(attr, params[attr].replace(" ", ","));
1107                             } else if (attr == "text" && this.type == "text") {
1108                                 this[0].removeChild(this[0].firstChild);
1109                                 this[0].appendChild(document.createTextNode(params[attr]));
1110                             } else {
1111                                 var cssrule = attr.replace(/(\-.)/g, function (w) {
1112                                     return w.substring(1).toUpperCase();
1113                                 });
1114                                 this[0].style[cssrule] = params[attr];
1115                                 // Need following line for Firefox
1116                                 this[0].setAttribute(attr, params[attr]);
1117                             }
1118                         }
1119                         if (params.gradient) {
1120                             this[0].attrs.gradient = params.gradient;
1121                             addGrdientFill(this[0], params.gradient, svg);
1122                         }
1123                     }
1124                     return this;
1125                 };
1126                 this.toFront = function () {
1127                     this[0].parentNode.appendChild(this[0]);
1128                 };
1129                 this.toBack = function () {
1130                     if (this[0].parentNode.firstChild != this[0]) {
1131                         this[0].parentNode.insertBefore(this[0], this[0].parentNode.firstChild);
1132                     }
1133                 };
1134             };
1135             var theCircle = function (svg, x, y, r) {
1136                 var el = document.createElementNS(svg.svgns, "circle");
1137                 el.setAttribute("cx", svg._getX(x));
1138                 el.setAttribute("cy", svg._getY(y));
1139                 el.setAttribute("r", r);
1140                 el.setAttribute("fill", "none");
1141                 el.setAttribute("stroke", "#000");
1142                 el.attrs = el.attrs || {};
1143                 el.attrs.cx = x;
1144                 el.attrs.cy = y;
1145                 el.attrs.r = r;
1146                 el.attrs.stroke = "#000";
1147                 if (svg.canvas) {
1148                     svg.canvas.appendChild(el);
1149                 }
1150                 var res = new Element(el, svg);
1151                 res.type = "circle";
1152                 return res;
1153             };
1154             var theRect = function (svg, x, y, w, h, r) {
1155                 var el = document.createElementNS(svg.svgns, "rect");
1156                 el.setAttribute("x", svg._getX(x));
1157                 el.setAttribute("y", svg._getY(y));
1158                 el.setAttribute("width", svg._getW(w));
1159                 el.setAttribute("height", svg._getH(h));
1160                 el.attrs = el.attrs || {};
1161                 el.attrs.x = x;
1162                 el.attrs.y = y;
1163                 el.attrs.width = w;
1164                 el.attrs.height = h;
1165                 if (r) {
1166                     el.setAttribute("rx", r);
1167                     el.setAttribute("ry", r);
1168                     el.attrs.rx = el.attrs.ry = r;
1169                 }
1170                 el.setAttribute("fill", "none");
1171                 el.setAttribute("stroke", "#000");
1172                 el.attrs.stroke = "#000";
1173                 if (svg.canvas) {
1174                     svg.canvas.appendChild(el);
1175                 }
1176                 var res = new Element(el, svg);
1177                 res.type = "rect";
1178                 return res;
1179             };
1180             var theEllipse = function (svg, x, y, rx, ry) {
1181                 var el = document.createElementNS(svg.svgns, "ellipse");
1182                 el.setAttribute("cx", svg._getX(x));
1183                 el.setAttribute("cy", svg._getY(y));
1184                 el.setAttribute("rx", svg._getW(rx));
1185                 el.setAttribute("ry", svg._getH(ry));
1186                 el.setAttribute("fill", "none");
1187                 el.setAttribute("stroke", "#000");
1188                 el.attrs = el.attrs || {};
1189                 el.attrs.cx = x;
1190                 el.attrs.cy = y;
1191                 el.attrs.rx = rx;
1192                 el.attrs.ry = ry;
1193                 el.attrs.stroke = "#000";
1194                 if (svg.canvas) {
1195                     svg.canvas.appendChild(el);
1196                 }
1197                 var res = new Element(el, svg);
1198                 res.type = "ellipse";
1199                 return res;
1200             };
1201             var theImage = function (svg, src, x, y, w, h) {
1202                 var el = document.createElementNS(svg.svgns, "image");
1203                 el.setAttribute("x", svg._getX(x));
1204                 el.setAttribute("y", svg._getY(y));
1205                 el.setAttribute("width", svg._getW(w));
1206                 el.setAttribute("height", svg._getH(h));
1207                 el.setAttributeNS(svg.xlink, "href", src);
1208                 if (svg.canvas) {
1209                     svg.canvas.appendChild(el);
1210                 }
1211                 var res = new Element(el, svg);
1212                 res.type = "image";
1213                 return res;
1214             };
1215             var theText = function (svg, x, y, text) {
1216                 var el = document.createElementNS(svg.svgns, "text");
1217                 el.setAttribute("x", x);
1218                 el.setAttribute("y", y);
1219                 el.setAttribute("text-anchor", "middle");
1220                 el.setAttribute("fill", "#000");
1221                 el.attrs = el.attrs || {};
1222                 el.attrs.x = x;
1223                 el.attrs.y = y;
1224                 el.attrs.fill = "#000";
1225                 if (text) {
1226                     el.appendChild(document.createTextNode(text));
1227                 }
1228                 if (svg.canvas) {
1229                     svg.canvas.appendChild(el);
1230                 }
1231                 var res = new Element(el, svg);
1232                 res.type = "text";
1233                 return res;
1234             };
1235             var theGroup = function (svg) {
1236                 var el = document.createElementNS(svg.svgns, "g");
1237                 if (svg.canvas) {
1238                     svg.canvas.appendChild(el);
1239                 }
1240                 var i = new Element(el, svg);
1241                 for (var f in svg) {
1242                     if (f[0] != "_" && typeof svg[f] == "function") {
1243                         i[f] = (function (f) {
1244                             return function () {
1245                                 var e = svg[f].apply(svg, arguments);
1246                                 el.appendChild(e[0]);
1247                                 return e;
1248                             };
1249                         })(f);
1250                     }
1251                 }
1252                 i.type = "group";
1253                 return i;
1254             };
1255             r._create = function () {
1256                 // container, width, height
1257                 // x, y, width, height
1258                 if (typeof arguments[0] == "string") {
1259                     var container = document.getElementById(arguments[0]);
1260                     var width = arguments[1];
1261                     var height = arguments[2];
1262                 }
1263                 if (typeof arguments[0] == "object") {
1264                     var container = arguments[0];
1265                     var width = arguments[1];
1266                     var height = arguments[2];
1267                 }
1268                 if (typeof arguments[0] == "number") {
1269                     var container = 1,
1270                         x = arguments[0],
1271                         y = arguments[1],
1272                         width = arguments[2],
1273                         height = arguments[3];
1274                 }
1275                 if (!container) {
1276                     throw new Error("SVG container not found.");
1277                 }
1278                 C.canvas = document.createElementNS(C.svgns, "svg");
1279                 C.canvas.setAttribute("width", width || 320);
1280                 C.width = width || 320;
1281                 C.canvas.setAttribute("height", height || 200);
1282                 C.height = height || 200;
1283                 if (container == 1) {
1284                     document.body.appendChild(C.canvas);
1285                     C.canvas.style.position = "absolute";
1286                     C.canvas.style.left = x + "px";
1287                     C.canvas.style.top = y + "px";
1288                 } else {
1289                     if (container.firstChild) {
1290                         container.insertBefore(C.canvas, container.firstChild);
1291                     } else {
1292                         container.appendChild(C.canvas);
1293                     }
1294                 }
1295                 container = {
1296                     canvas: C.canvas,
1297                     clear: function () {
1298                         while (this.canvas.firstChild) {
1299                             this.canvas.removeChild(this.canvas.firstChild);
1300                         }
1301                         this.defs = document.createElementNS(C.svgns, "defs");
1302                         this.gradients = 0;
1303                         this.canvas.appendChild(this.defs);
1304                     }
1305                 };
1306                 for (var prop in C) {
1307                     if (prop != "create") {
1308                         container[prop] = C[prop];
1309                     }
1310                 }
1311                 container.clear();
1312                 return container;
1313             };
1314             C.svgns = "http://www.w3.org/2000/svg";
1315             C.xlink = "http://www.w3.org/1999/xlink";
1316         }
1317         if (r.vml || r.svg) {
1318             C.circle = function (x, y, r) {
1319                 return theCircle(this, x, y, r);
1320             };
1321             C.rect = function (x, y, w, h, r) {
1322                 return theRect(this, x, y, w, h, r);
1323             };
1324             C.ellipse = function (x, y, rx, ry) {
1325                 return theEllipse(this, x, y, rx, ry);
1326             };
1327             C.path = function (params, pathString) {
1328                 return thePath(params, pathString, this);
1329             };
1330             C.image = function (src, x, y, w, h) {
1331                 return theImage(this, src, x, y, w, h);
1332             };
1333             C.text = function (x, y, text) {
1334                 return theText(this, x, y, text);
1335             };
1336             C.group = function () {
1337                 return theGroup(this);
1338             };
1339             C.linerect = function (x, y, w, h, r) {
1340                 if (r && parseInt(r, 10)) {
1341                     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();
1342                 }
1343                 return this.path({stroke: "#000"}).moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).andClose();
1344             };
1345             C.drawGrid = function (x, y, w, h, wv, hv, color) {
1346                 color = color || "#000";
1347                 var res = this.group();
1348                 var params = {stroke: color, "stroke-width": "1px", "stroke-opacity": .3};
1349                 res.rect(x, y, w, h).attr(params);
1350                 for (var i = 1; i < hv; i++) {
1351                     var p = res.path(params);
1352                     p.moveTo(x, y + i * Math.round(h / hv)).lineTo(x + w, y + i * Math.round(h / hv));
1353                 }
1354                 for (var i = 1; i < wv; i++) {
1355                     res.path(params).moveTo(x + i * Math.round(w / wv), y).lineTo(x + i * Math.round(w / wv), y + h);
1356                 }
1357                 return res;
1358             };
1359             C.setGrid = function (xmin, ymin, xmax, ymax, w, h) {
1360                 var xc = (xmax - xmin) / w;
1361                 var yc = (ymax - ymin) / h;
1362                 this._getX = function (x) {
1363                     return xmin + x * xc;
1364                 };
1365                 this._getY = function (y) {
1366                     return ymin + y * yc;
1367                 };
1368                 this._getW = function (w) {
1369                     return w * xc;
1370                 };
1371                 this._getH = function (h) {
1372                     return h * yc;
1373                 };
1374             };
1375             C.clearGrid = function () {
1376                 this._getX = this._getY = this._getW = this._getH = function (x) { return x; };
1377             };
1378             C.safari = function () {
1379                 if (this.type == "SVG") {
1380                     var rect = C.rect(0, 0, C.width, C.height).attr("stroke-width", 0);
1381                     setTimeout(function () {rect.remove();}, 0);
1382                 }
1383             };
1384             Raphael = function () {
1385                 return r._create.apply(r, arguments);
1386             };
1387             return r._create.apply(r, args);
1388         } else {
1389             return null;
1390         }
1391     })(arguments.callee, arguments);
1392 }
1393
1394
1395 Raphael.type = (!(window.SVGPreserveAspectRatio && window.SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN == 2) && !(window.CanvasRenderingContext2D)) ? "VML" : "SVG";
1396 Raphael.vml = !(Raphael.svg = (Raphael.type == "SVG"));
1397 if (!(window.SVGPreserveAspectRatio && window.SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN == 2) && window.CanvasRenderingContext2D) {
1398     Raphael.type = "Canvas only";
1399     Raphael.vml = Raphael.svg = false;
1400 }
1401 Raphael.toString = function () {
1402     return "You browser supports " + this.type;
1403 };