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