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