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