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