1define(["./raphael.core"], function(R) {
2    if (R && !R.vml) {
3        return;
4    }
5
6    var has = "hasOwnProperty",
7        Str = String,
8        toFloat = parseFloat,
9        math = Math,
10        round = math.round,
11        mmax = math.max,
12        mmin = math.min,
13        abs = math.abs,
14        fillString = "fill",
15        separator = /[, ]+/,
16        eve = R.eve,
17        ms = " progid:DXImageTransform.Microsoft",
18        S = " ",
19        E = "",
20        map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
21        bites = /([clmz]),?([^clmz]*)/gi,
22        blurregexp = / progid:\S+Blur\([^\)]+\)/g,
23        val = /-?[^,\s-]+/g,
24        cssDot = "position:absolute;left:0;top:0;width:1px;height:1px;behavior:url(#default#VML)",
25        zoom = 21600,
26        pathTypes = {path: 1, rect: 1, image: 1},
27        ovalTypes = {circle: 1, ellipse: 1},
28        path2vml = function (path) {
29            var total =  /[ahqstv]/ig,
30                command = R._pathToAbsolute;
31            Str(path).match(total) && (command = R._path2curve);
32            total = /[clmz]/g;
33            if (command == R._pathToAbsolute && !Str(path).match(total)) {
34                var res = Str(path).replace(bites, function (all, command, args) {
35                    var vals = [],
36                        isMove = command.toLowerCase() == "m",
37                        res = map[command];
38                    args.replace(val, function (value) {
39                        if (isMove && vals.length == 2) {
40                            res += vals + map[command == "m" ? "l" : "L"];
41                            vals = [];
42                        }
43                        vals.push(round(value * zoom));
44                    });
45                    return res + vals;
46                });
47                return res;
48            }
49            var pa = command(path), p, r;
50            res = [];
51            for (var i = 0, ii = pa.length; i < ii; i++) {
52                p = pa[i];
53                r = pa[i][0].toLowerCase();
54                r == "z" && (r = "x");
55                for (var j = 1, jj = p.length; j < jj; j++) {
56                    r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
57                }
58                res.push(r);
59            }
60            return res.join(S);
61        },
62        compensation = function (deg, dx, dy) {
63            var m = R.matrix();
64            m.rotate(-deg, .5, .5);
65            return {
66                dx: m.x(dx, dy),
67                dy: m.y(dx, dy)
68            };
69        },
70        setCoords = function (p, sx, sy, dx, dy, deg) {
71            var _ = p._,
72                m = p.matrix,
73                fillpos = _.fillpos,
74                o = p.node,
75                s = o.style,
76                y = 1,
77                flip = "",
78                dxdy,
79                kx = zoom / sx,
80                ky = zoom / sy;
81            s.visibility = "hidden";
82            if (!sx || !sy) {
83                return;
84            }
85            o.coordsize = abs(kx) + S + abs(ky);
86            s.rotation = deg * (sx * sy < 0 ? -1 : 1);
87            if (deg) {
88                var c = compensation(deg, dx, dy);
89                dx = c.dx;
90                dy = c.dy;
91            }
92            sx < 0 && (flip += "x");
93            sy < 0 && (flip += " y") && (y = -1);
94            s.flip = flip;
95            o.coordorigin = (dx * -kx) + S + (dy * -ky);
96            if (fillpos || _.fillsize) {
97                var fill = o.getElementsByTagName(fillString);
98                fill = fill && fill[0];
99                o.removeChild(fill);
100                if (fillpos) {
101                    c = compensation(deg, m.x(fillpos[0], fillpos[1]), m.y(fillpos[0], fillpos[1]));
102                    fill.position = c.dx * y + S + c.dy * y;
103                }
104                if (_.fillsize) {
105                    fill.size = _.fillsize[0] * abs(sx) + S + _.fillsize[1] * abs(sy);
106                }
107                o.appendChild(fill);
108            }
109            s.visibility = "visible";
110        };
111    R.toString = function () {
112        return  "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
113    };
114    var addArrow = function (o, value, isEnd) {
115        var values = Str(value).toLowerCase().split("-"),
116            se = isEnd ? "end" : "start",
117            i = values.length,
118            type = "classic",
119            w = "medium",
120            h = "medium";
121        while (i--) {
122            switch (values[i]) {
123                case "block":
124                case "classic":
125                case "oval":
126                case "diamond":
127                case "open":
128                case "none":
129                    type = values[i];
130                    break;
131                case "wide":
132                case "narrow": h = values[i]; break;
133                case "long":
134                case "short": w = values[i]; break;
135            }
136        }
137        var stroke = o.node.getElementsByTagName("stroke")[0];
138        stroke[se + "arrow"] = type;
139        stroke[se + "arrowlength"] = w;
140        stroke[se + "arrowwidth"] = h;
141    },
142    setFillAndStroke = function (o, params) {
143        // o.paper.canvas.style.display = "none";
144        o.attrs = o.attrs || {};
145        var node = o.node,
146            a = o.attrs,
147            s = node.style,
148            xy,
149            newpath = pathTypes[o.type] && (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.cx != a.cx || params.cy != a.cy || params.rx != a.rx || params.ry != a.ry || params.r != a.r),
150            isOval = ovalTypes[o.type] && (a.cx != params.cx || a.cy != params.cy || a.r != params.r || a.rx != params.rx || a.ry != params.ry),
151            res = o;
152
153
154        for (var par in params) if (params[has](par)) {
155            a[par] = params[par];
156        }
157        if (newpath) {
158            a.path = R._getPath[o.type](o);
159            o._.dirty = 1;
160        }
161        params.href && (node.href = params.href);
162        params.title && (node.title = params.title);
163        params.target && (node.target = params.target);
164        params.cursor && (s.cursor = params.cursor);
165        "blur" in params && o.blur(params.blur);
166        if (params.path && o.type == "path" || newpath) {
167            node.path = path2vml(~Str(a.path).toLowerCase().indexOf("r") ? R._pathToAbsolute(a.path) : a.path);
168            o._.dirty = 1;
169            if (o.type == "image") {
170                o._.fillpos = [a.x, a.y];
171                o._.fillsize = [a.width, a.height];
172                setCoords(o, 1, 1, 0, 0, 0);
173            }
174        }
175        "transform" in params && o.transform(params.transform);
176        if (isOval) {
177            var cx = +a.cx,
178                cy = +a.cy,
179                rx = +a.rx || +a.r || 0,
180                ry = +a.ry || +a.r || 0;
181            node.path = R.format("ar{0},{1},{2},{3},{4},{1},{4},{1}x", round((cx - rx) * zoom), round((cy - ry) * zoom), round((cx + rx) * zoom), round((cy + ry) * zoom), round(cx * zoom));
182            o._.dirty = 1;
183        }
184        if ("clip-rect" in params) {
185            var rect = Str(params["clip-rect"]).split(separator);
186            if (rect.length == 4) {
187                rect[2] = +rect[2] + (+rect[0]);
188                rect[3] = +rect[3] + (+rect[1]);
189                var div = node.clipRect || R._g.doc.createElement("div"),
190                    dstyle = div.style;
191                dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
192                if (!node.clipRect) {
193                    dstyle.position = "absolute";
194                    dstyle.top = 0;
195                    dstyle.left = 0;
196                    dstyle.width = o.paper.width + "px";
197                    dstyle.height = o.paper.height + "px";
198                    node.parentNode.insertBefore(div, node);
199                    div.appendChild(node);
200                    node.clipRect = div;
201                }
202            }
203            if (!params["clip-rect"]) {
204                node.clipRect && (node.clipRect.style.clip = "auto");
205            }
206        }
207        if (o.textpath) {
208            var textpathStyle = o.textpath.style;
209            params.font && (textpathStyle.font = params.font);
210            params["font-family"] && (textpathStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, E) + '"');
211            params["font-size"] && (textpathStyle.fontSize = params["font-size"]);
212            params["font-weight"] && (textpathStyle.fontWeight = params["font-weight"]);
213            params["font-style"] && (textpathStyle.fontStyle = params["font-style"]);
214        }
215        if ("arrow-start" in params) {
216            addArrow(res, params["arrow-start"]);
217        }
218        if ("arrow-end" in params) {
219            addArrow(res, params["arrow-end"], 1);
220        }
221        if (params.opacity != null ||
222            params.fill != null ||
223            params.src != null ||
224            params.stroke != null ||
225            params["stroke-width"] != null ||
226            params["stroke-opacity"] != null ||
227            params["fill-opacity"] != null ||
228            params["stroke-dasharray"] != null ||
229            params["stroke-miterlimit"] != null ||
230            params["stroke-linejoin"] != null ||
231            params["stroke-linecap"] != null) {
232            var fill = node.getElementsByTagName(fillString),
233                newfill = false;
234            fill = fill && fill[0];
235            !fill && (newfill = fill = createNode(fillString));
236            if (o.type == "image" && params.src) {
237                fill.src = params.src;
238            }
239            params.fill && (fill.on = true);
240            if (fill.on == null || params.fill == "none" || params.fill === null) {
241                fill.on = false;
242            }
243            if (fill.on && params.fill) {
244                var isURL = Str(params.fill).match(R._ISURL);
245                if (isURL) {
246                    fill.parentNode == node && node.removeChild(fill);
247                    fill.rotate = true;
248                    fill.src = isURL[1];
249                    fill.type = "tile";
250                    var bbox = o.getBBox(1);
251                    fill.position = bbox.x + S + bbox.y;
252                    o._.fillpos = [bbox.x, bbox.y];
253
254                    R._preload(isURL[1], function () {
255                        o._.fillsize = [this.offsetWidth, this.offsetHeight];
256                    });
257                } else {
258                    fill.color = R.getRGB(params.fill).hex;
259                    fill.src = E;
260                    fill.type = "solid";
261                    if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill, fill)) {
262                        a.fill = "none";
263                        a.gradient = params.fill;
264                        fill.rotate = false;
265                    }
266                }
267            }
268            if ("fill-opacity" in params || "opacity" in params) {
269                var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
270                opacity = mmin(mmax(opacity, 0), 1);
271                fill.opacity = opacity;
272                if (fill.src) {
273                    fill.color = "none";
274                }
275            }
276            node.appendChild(fill);
277            var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
278            newstroke = false;
279            !stroke && (newstroke = stroke = createNode("stroke"));
280            if ((params.stroke && params.stroke != "none") ||
281                params["stroke-width"] ||
282                params["stroke-opacity"] != null ||
283                params["stroke-dasharray"] ||
284                params["stroke-miterlimit"] ||
285                params["stroke-linejoin"] ||
286                params["stroke-linecap"]) {
287                stroke.on = true;
288            }
289            (params.stroke == "none" || params.stroke === null || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
290            var strokeColor = R.getRGB(params.stroke);
291            stroke.on && params.stroke && (stroke.color = strokeColor.hex);
292            opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
293            var width = (toFloat(params["stroke-width"]) || 1) * .75;
294            opacity = mmin(mmax(opacity, 0), 1);
295            params["stroke-width"] == null && (width = a["stroke-width"]);
296            params["stroke-width"] && (stroke.weight = width);
297            width && width < 1 && (opacity *= width) && (stroke.weight = 1);
298            stroke.opacity = opacity;
299
300            params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
301            stroke.miterlimit = params["stroke-miterlimit"] || 8;
302            params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
303            if ("stroke-dasharray" in params) {
304                var dasharray = {
305                    "-": "shortdash",
306                    ".": "shortdot",
307                    "-.": "shortdashdot",
308                    "-..": "shortdashdotdot",
309                    ". ": "dot",
310                    "- ": "dash",
311                    "--": "longdash",
312                    "- .": "dashdot",
313                    "--.": "longdashdot",
314                    "--..": "longdashdotdot"
315                };
316                stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
317            }
318            newstroke && node.appendChild(stroke);
319        }
320        if (res.type == "text") {
321            res.paper.canvas.style.display = E;
322            var span = res.paper.span,
323                m = 100,
324                fontSize = a.font && a.font.match(/\d+(?:\.\d*)?(?=px)/);
325            s = span.style;
326            a.font && (s.font = a.font);
327            a["font-family"] && (s.fontFamily = a["font-family"]);
328            a["font-weight"] && (s.fontWeight = a["font-weight"]);
329            a["font-style"] && (s.fontStyle = a["font-style"]);
330            fontSize = toFloat(a["font-size"] || fontSize && fontSize[0]) || 10;
331            s.fontSize = fontSize * m + "px";
332            res.textpath.string && (span.innerHTML = Str(res.textpath.string).replace(/</g, "&#60;").replace(/&/g, "&#38;").replace(/\n/g, "<br>"));
333            var brect = span.getBoundingClientRect();
334            res.W = a.w = (brect.right - brect.left) / m;
335            res.H = a.h = (brect.bottom - brect.top) / m;
336            // res.paper.canvas.style.display = "none";
337            res.X = a.x;
338            res.Y = a.y + res.H / 2;
339
340            ("x" in params || "y" in params) && (res.path.v = R.format("m{0},{1}l{2},{1}", round(a.x * zoom), round(a.y * zoom), round(a.x * zoom) + 1));
341            var dirtyattrs = ["x", "y", "text", "font", "font-family", "font-weight", "font-style", "font-size"];
342            for (var d = 0, dd = dirtyattrs.length; d < dd; d++) if (dirtyattrs[d] in params) {
343                res._.dirty = 1;
344                break;
345            }
346
347            // text-anchor emulation
348            switch (a["text-anchor"]) {
349                case "start":
350                    res.textpath.style["v-text-align"] = "left";
351                    res.bbx = res.W / 2;
352                break;
353                case "end":
354                    res.textpath.style["v-text-align"] = "right";
355                    res.bbx = -res.W / 2;
356                break;
357                default:
358                    res.textpath.style["v-text-align"] = "center";
359                    res.bbx = 0;
360                break;
361            }
362            res.textpath.style["v-text-kern"] = true;
363        }
364        // res.paper.canvas.style.display = E;
365    },
366    addGradientFill = function (o, gradient, fill) {
367        o.attrs = o.attrs || {};
368        var attrs = o.attrs,
369            pow = Math.pow,
370            opacity,
371            oindex,
372            type = "linear",
373            fxfy = ".5 .5";
374        o.attrs.gradient = gradient;
375        gradient = Str(gradient).replace(R._radial_gradient, function (all, fx, fy) {
376            type = "radial";
377            if (fx && fy) {
378                fx = toFloat(fx);
379                fy = toFloat(fy);
380                pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
381                fxfy = fx + S + fy;
382            }
383            return E;
384        });
385        gradient = gradient.split(/\s*\-\s*/);
386        if (type == "linear") {
387            var angle = gradient.shift();
388            angle = -toFloat(angle);
389            if (isNaN(angle)) {
390                return null;
391            }
392        }
393        var dots = R._parseDots(gradient);
394        if (!dots) {
395            return null;
396        }
397        o = o.shape || o.node;
398        if (dots.length) {
399            o.removeChild(fill);
400            fill.on = true;
401            fill.method = "none";
402            fill.color = dots[0].color;
403            fill.color2 = dots[dots.length - 1].color;
404            var clrs = [];
405            for (var i = 0, ii = dots.length; i < ii; i++) {
406                dots[i].offset && clrs.push(dots[i].offset + S + dots[i].color);
407            }
408            fill.colors = clrs.length ? clrs.join() : "0% " + fill.color;
409            if (type == "radial") {
410                fill.type = "gradientTitle";
411                fill.focus = "100%";
412                fill.focussize = "0 0";
413                fill.focusposition = fxfy;
414                fill.angle = 0;
415            } else {
416                // fill.rotate= true;
417                fill.type = "gradient";
418                fill.angle = (270 - angle) % 360;
419            }
420            o.appendChild(fill);
421        }
422        return 1;
423    },
424    Element = function (node, vml) {
425        this[0] = this.node = node;
426        node.raphael = true;
427        this.id = R._oid++;
428        node.raphaelid = this.id;
429        this.X = 0;
430        this.Y = 0;
431        this.attrs = {};
432        this.paper = vml;
433        this.matrix = R.matrix();
434        this._ = {
435            transform: [],
436            sx: 1,
437            sy: 1,
438            dx: 0,
439            dy: 0,
440            deg: 0,
441            dirty: 1,
442            dirtyT: 1
443        };
444        !vml.bottom && (vml.bottom = this);
445        this.prev = vml.top;
446        vml.top && (vml.top.next = this);
447        vml.top = this;
448        this.next = null;
449    };
450    var elproto = R.el;
451
452    Element.prototype = elproto;
453    elproto.constructor = Element;
454    elproto.transform = function (tstr) {
455        if (tstr == null) {
456            return this._.transform;
457        }
458        var vbs = this.paper._viewBoxShift,
459            vbt = vbs ? "s" + [vbs.scale, vbs.scale] + "-1-1t" + [vbs.dx, vbs.dy] : E,
460            oldt;
461        if (vbs) {
462            oldt = tstr = Str(tstr).replace(/\.{3}|\u2026/g, this._.transform || E);
463        }
464        R._extractTransform(this, vbt + tstr);
465        var matrix = this.matrix.clone(),
466            skew = this.skew,
467            o = this.node,
468            split,
469            isGrad = ~Str(this.attrs.fill).indexOf("-"),
470            isPatt = !Str(this.attrs.fill).indexOf("url(");
471        matrix.translate(1, 1);
472        if (isPatt || isGrad || this.type == "image") {
473            skew.matrix = "1 0 0 1";
474            skew.offset = "0 0";
475            split = matrix.split();
476            if ((isGrad && split.noRotation) || !split.isSimple) {
477                o.style.filter = matrix.toFilter();
478                var bb = this.getBBox(),
479                    bbt = this.getBBox(1),
480                    dx = bb.x - bbt.x,
481                    dy = bb.y - bbt.y;
482                o.coordorigin = (dx * -zoom) + S + (dy * -zoom);
483                setCoords(this, 1, 1, dx, dy, 0);
484            } else {
485                o.style.filter = E;
486                setCoords(this, split.scalex, split.scaley, split.dx, split.dy, split.rotate);
487            }
488        } else {
489            o.style.filter = E;
490            skew.matrix = Str(matrix);
491            skew.offset = matrix.offset();
492        }
493        if (oldt !== null) { // empty string value is true as well
494            this._.transform = oldt;
495            R._extractTransform(this, oldt);
496        }
497        return this;
498    };
499    elproto.rotate = function (deg, cx, cy) {
500        if (this.removed) {
501            return this;
502        }
503        if (deg == null) {
504            return;
505        }
506        deg = Str(deg).split(separator);
507        if (deg.length - 1) {
508            cx = toFloat(deg[1]);
509            cy = toFloat(deg[2]);
510        }
511        deg = toFloat(deg[0]);
512        (cy == null) && (cx = cy);
513        if (cx == null || cy == null) {
514            var bbox = this.getBBox(1);
515            cx = bbox.x + bbox.width / 2;
516            cy = bbox.y + bbox.height / 2;
517        }
518        this._.dirtyT = 1;
519        this.transform(this._.transform.concat([["r", deg, cx, cy]]));
520        return this;
521    };
522    elproto.translate = function (dx, dy) {
523        if (this.removed) {
524            return this;
525        }
526        dx = Str(dx).split(separator);
527        if (dx.length - 1) {
528            dy = toFloat(dx[1]);
529        }
530        dx = toFloat(dx[0]) || 0;
531        dy = +dy || 0;
532        if (this._.bbox) {
533            this._.bbox.x += dx;
534            this._.bbox.y += dy;
535        }
536        this.transform(this._.transform.concat([["t", dx, dy]]));
537        return this;
538    };
539    elproto.scale = function (sx, sy, cx, cy) {
540        if (this.removed) {
541            return this;
542        }
543        sx = Str(sx).split(separator);
544        if (sx.length - 1) {
545            sy = toFloat(sx[1]);
546            cx = toFloat(sx[2]);
547            cy = toFloat(sx[3]);
548            isNaN(cx) && (cx = null);
549            isNaN(cy) && (cy = null);
550        }
551        sx = toFloat(sx[0]);
552        (sy == null) && (sy = sx);
553        (cy == null) && (cx = cy);
554        if (cx == null || cy == null) {
555            var bbox = this.getBBox(1);
556        }
557        cx = cx == null ? bbox.x + bbox.width / 2 : cx;
558        cy = cy == null ? bbox.y + bbox.height / 2 : cy;
559
560        this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
561        this._.dirtyT = 1;
562        return this;
563    };
564    elproto.hide = function () {
565        !this.removed && (this.node.style.display = "none");
566        return this;
567    };
568    elproto.show = function () {
569        !this.removed && (this.node.style.display = E);
570        return this;
571    };
572    // Needed to fix the vml setViewBox issues
573    elproto.auxGetBBox = R.el.getBBox;
574    elproto.getBBox = function(){
575      var b = this.auxGetBBox();
576      if (this.paper && this.paper._viewBoxShift)
577      {
578        var c = {};
579        var z = 1/this.paper._viewBoxShift.scale;
580        c.x = b.x - this.paper._viewBoxShift.dx;
581        c.x *= z;
582        c.y = b.y - this.paper._viewBoxShift.dy;
583        c.y *= z;
584        c.width  = b.width  * z;
585        c.height = b.height * z;
586        c.x2 = c.x + c.width;
587        c.y2 = c.y + c.height;
588        return c;
589      }
590      return b;
591    };
592    elproto._getBBox = function () {
593        if (this.removed) {
594            return {};
595        }
596        return {
597            x: this.X + (this.bbx || 0) - this.W / 2,
598            y: this.Y - this.H,
599            width: this.W,
600            height: this.H
601        };
602    };
603    elproto.remove = function () {
604        if (this.removed || !this.node.parentNode) {
605            return;
606        }
607        this.paper.__set__ && this.paper.__set__.exclude(this);
608        R.eve.unbind("raphael.*.*." + this.id);
609        R._tear(this, this.paper);
610        this.node.parentNode.removeChild(this.node);
611        this.shape && this.shape.parentNode.removeChild(this.shape);
612        for (var i in this) {
613            this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
614        }
615        this.removed = true;
616    };
617    elproto.attr = function (name, value) {
618        if (this.removed) {
619            return this;
620        }
621        if (name == null) {
622            var res = {};
623            for (var a in this.attrs) if (this.attrs[has](a)) {
624                res[a] = this.attrs[a];
625            }
626            res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
627            res.transform = this._.transform;
628            return res;
629        }
630        if (value == null && R.is(name, "string")) {
631            if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
632                return this.attrs.gradient;
633            }
634            var names = name.split(separator),
635                out = {};
636            for (var i = 0, ii = names.length; i < ii; i++) {
637                name = names[i];
638                if (name in this.attrs) {
639                    out[name] = this.attrs[name];
640                } else if (R.is(this.paper.customAttributes[name], "function")) {
641                    out[name] = this.paper.customAttributes[name].def;
642                } else {
643                    out[name] = R._availableAttrs[name];
644                }
645            }
646            return ii - 1 ? out : out[names[0]];
647        }
648        if (this.attrs && value == null && R.is(name, "array")) {
649            out = {};
650            for (i = 0, ii = name.length; i < ii; i++) {
651                out[name[i]] = this.attr(name[i]);
652            }
653            return out;
654        }
655        var params;
656        if (value != null) {
657            params = {};
658            params[name] = value;
659        }
660        value == null && R.is(name, "object") && (params = name);
661        for (var key in params) {
662            eve("raphael.attr." + key + "." + this.id, this, params[key]);
663        }
664        if (params) {
665            for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
666                var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
667                this.attrs[key] = params[key];
668                for (var subkey in par) if (par[has](subkey)) {
669                    params[subkey] = par[subkey];
670                }
671            }
672            // this.paper.canvas.style.display = "none";
673            if (params.text && this.type == "text") {
674                this.textpath.string = params.text;
675            }
676            setFillAndStroke(this, params);
677            // this.paper.canvas.style.display = E;
678        }
679        return this;
680    };
681    elproto.toFront = function () {
682        !this.removed && this.node.parentNode.appendChild(this.node);
683        this.paper && this.paper.top != this && R._tofront(this, this.paper);
684        return this;
685    };
686    elproto.toBack = function () {
687        if (this.removed) {
688            return this;
689        }
690        if (this.node.parentNode.firstChild != this.node) {
691            this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
692            R._toback(this, this.paper);
693        }
694        return this;
695    };
696    elproto.insertAfter = function (element) {
697        if (this.removed) {
698            return this;
699        }
700        if (element.constructor == R.st.constructor) {
701            element = element[element.length - 1];
702        }
703        if (element.node.nextSibling) {
704            element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
705        } else {
706            element.node.parentNode.appendChild(this.node);
707        }
708        R._insertafter(this, element, this.paper);
709        return this;
710    };
711    elproto.insertBefore = function (element) {
712        if (this.removed) {
713            return this;
714        }
715        if (element.constructor == R.st.constructor) {
716            element = element[0];
717        }
718        element.node.parentNode.insertBefore(this.node, element.node);
719        R._insertbefore(this, element, this.paper);
720        return this;
721    };
722    elproto.blur = function (size) {
723        var s = this.node.runtimeStyle,
724            f = s.filter;
725        f = f.replace(blurregexp, E);
726        if (+size !== 0) {
727            this.attrs.blur = size;
728            s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
729            s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
730        } else {
731            s.filter = f;
732            s.margin = 0;
733            delete this.attrs.blur;
734        }
735        return this;
736    };
737
738    R._engine.path = function (pathString, vml) {
739        var el = createNode("shape");
740        el.style.cssText = cssDot;
741        el.coordsize = zoom + S + zoom;
742        el.coordorigin = vml.coordorigin;
743        var p = new Element(el, vml),
744            attr = {fill: "none", stroke: "#000"};
745        pathString && (attr.path = pathString);
746        p.type = "path";
747        p.path = [];
748        p.Path = E;
749        setFillAndStroke(p, attr);
750        vml.canvas && vml.canvas.appendChild(el);
751        var skew = createNode("skew");
752        skew.on = true;
753        el.appendChild(skew);
754        p.skew = skew;
755        p.transform(E);
756        return p;
757    };
758    R._engine.rect = function (vml, x, y, w, h, r) {
759        var path = R._rectPath(x, y, w, h, r),
760            res = vml.path(path),
761            a = res.attrs;
762        res.X = a.x = x;
763        res.Y = a.y = y;
764        res.W = a.width = w;
765        res.H = a.height = h;
766        a.r = r;
767        a.path = path;
768        res.type = "rect";
769        return res;
770    };
771    R._engine.ellipse = function (vml, x, y, rx, ry) {
772        var res = vml.path(),
773            a = res.attrs;
774        res.X = x - rx;
775        res.Y = y - ry;
776        res.W = rx * 2;
777        res.H = ry * 2;
778        res.type = "ellipse";
779        setFillAndStroke(res, {
780            cx: x,
781            cy: y,
782            rx: rx,
783            ry: ry
784        });
785        return res;
786    };
787    R._engine.circle = function (vml, x, y, r) {
788        var res = vml.path(),
789            a = res.attrs;
790        res.X = x - r;
791        res.Y = y - r;
792        res.W = res.H = r * 2;
793        res.type = "circle";
794        setFillAndStroke(res, {
795            cx: x,
796            cy: y,
797            r: r
798        });
799        return res;
800    };
801    R._engine.image = function (vml, src, x, y, w, h) {
802        var path = R._rectPath(x, y, w, h),
803            res = vml.path(path).attr({stroke: "none"}),
804            a = res.attrs,
805            node = res.node,
806            fill = node.getElementsByTagName(fillString)[0];
807        a.src = src;
808        res.X = a.x = x;
809        res.Y = a.y = y;
810        res.W = a.width = w;
811        res.H = a.height = h;
812        a.path = path;
813        res.type = "image";
814        fill.parentNode == node && node.removeChild(fill);
815        fill.rotate = true;
816        fill.src = src;
817        fill.type = "tile";
818        res._.fillpos = [x, y];
819        res._.fillsize = [w, h];
820        node.appendChild(fill);
821        setCoords(res, 1, 1, 0, 0, 0);
822        return res;
823    };
824    R._engine.text = function (vml, x, y, text) {
825        var el = createNode("shape"),
826            path = createNode("path"),
827            o = createNode("textpath");
828        x = x || 0;
829        y = y || 0;
830        text = text || "";
831        path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
832        path.textpathok = true;
833        o.string = Str(text);
834        o.on = true;
835        el.style.cssText = cssDot;
836        el.coordsize = zoom + S + zoom;
837        el.coordorigin = "0 0";
838        var p = new Element(el, vml),
839            attr = {
840                fill: "#000",
841                stroke: "none",
842                font: R._availableAttrs.font,
843                text: text
844            };
845        p.shape = el;
846        p.path = path;
847        p.textpath = o;
848        p.type = "text";
849        p.attrs.text = Str(text);
850        p.attrs.x = x;
851        p.attrs.y = y;
852        p.attrs.w = 1;
853        p.attrs.h = 1;
854        setFillAndStroke(p, attr);
855        el.appendChild(o);
856        el.appendChild(path);
857        vml.canvas.appendChild(el);
858        var skew = createNode("skew");
859        skew.on = true;
860        el.appendChild(skew);
861        p.skew = skew;
862        p.transform(E);
863        return p;
864    };
865    R._engine.setSize = function (width, height) {
866        var cs = this.canvas.style;
867        this.width = width;
868        this.height = height;
869        width == +width && (width += "px");
870        height == +height && (height += "px");
871        cs.width = width;
872        cs.height = height;
873        cs.clip = "rect(0 " + width + " " + height + " 0)";
874        if (this._viewBox) {
875            R._engine.setViewBox.apply(this, this._viewBox);
876        }
877        return this;
878    };
879    R._engine.setViewBox = function (x, y, w, h, fit) {
880        R.eve("raphael.setViewBox", this, this._viewBox, [x, y, w, h, fit]);
881        var paperSize = this.getSize(),
882            width = paperSize.width,
883            height = paperSize.height,
884            H, W;
885        if (fit) {
886            H = height / h;
887            W = width / w;
888            if (w * H < width) {
889                x -= (width - w * H) / 2 / H;
890            }
891            if (h * W < height) {
892                y -= (height - h * W) / 2 / W;
893            }
894        }
895        this._viewBox = [x, y, w, h, !!fit];
896        this._viewBoxShift = {
897            dx: -x,
898            dy: -y,
899            scale: paperSize
900        };
901        this.forEach(function (el) {
902            el.transform("...");
903        });
904        return this;
905    };
906    var createNode;
907    R._engine.initWin = function (win) {
908            var doc = win.document;
909            if (doc.styleSheets.length < 31) {
910                doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
911            } else {
912                // no more room, add to the existing one
913                // http://msdn.microsoft.com/en-us/library/ms531194%28VS.85%29.aspx
914                doc.styleSheets[0].addRule(".rvml", "behavior:url(#default#VML)");
915            }
916            try {
917                !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
918                createNode = function (tagName) {
919                    return doc.createElement('<rvml:' + tagName + ' class="rvml">');
920                };
921            } catch (e) {
922                createNode = function (tagName) {
923                    return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
924                };
925            }
926        };
927    R._engine.initWin(R._g.win);
928    R._engine.create = function () {
929        var con = R._getContainer.apply(0, arguments),
930            container = con.container,
931            height = con.height,
932            s,
933            width = con.width,
934            x = con.x,
935            y = con.y;
936        if (!container) {
937            throw new Error("VML container not found.");
938        }
939        var res = new R._Paper,
940            c = res.canvas = R._g.doc.createElement("div"),
941            cs = c.style;
942        x = x || 0;
943        y = y || 0;
944        width = width || 512;
945        height = height || 342;
946        res.width = width;
947        res.height = height;
948        width == +width && (width += "px");
949        height == +height && (height += "px");
950        res.coordsize = zoom * 1e3 + S + zoom * 1e3;
951        res.coordorigin = "0 0";
952        res.span = R._g.doc.createElement("span");
953        res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
954        c.appendChild(res.span);
955        cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
956        if (container == 1) {
957            R._g.doc.body.appendChild(c);
958            cs.left = x + "px";
959            cs.top = y + "px";
960            cs.position = "absolute";
961        } else {
962            if (container.firstChild) {
963                container.insertBefore(c, container.firstChild);
964            } else {
965                container.appendChild(c);
966            }
967        }
968        res.renderfix = function () {};
969        return res;
970    };
971    R.prototype.clear = function () {
972        R.eve("raphael.clear", this);
973        this.canvas.innerHTML = E;
974        this.span = R._g.doc.createElement("span");
975        this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
976        this.canvas.appendChild(this.span);
977        this.bottom = this.top = null;
978    };
979    R.prototype.remove = function () {
980        R.eve("raphael.remove", this);
981        this.canvas.parentNode.removeChild(this.canvas);
982        for (var i in this) {
983            this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
984        }
985        return true;
986    };
987
988    var setproto = R.st;
989    for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
990        setproto[method] = (function (methodname) {
991            return function () {
992                var arg = arguments;
993                return this.forEach(function (el) {
994                    el[methodname].apply(el, arg);
995                });
996            };
997        })(method);
998    }
999});
1000