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