/* coding: utf-8 */

var SvgStyle = function (svg, bbox) {
    this.svg = svg;
    this.bb = bbox;
};

SvgStyle.prototype.setTop = function (id) {
    var g = this.svg.getElementById(id),
        p = g.parentNode;
    p.removeChild(g);
    p.appendChild(g);
};

SvgStyle.prototype.setStrokeWidth = function (id, num) {
    var g = this.svg.getElementById(id),
        chd = g.childNodes,
        len = chd.length;
    for (var i = 0; i < len; i++) {
        chd[i].setAttribute('vector-effect', 'non-scaling-stroke');
    }
    g.setAttribute('stroke-width', num);
};

SvgStyle.prototype.setRadius = function (id, num, originalScale) {
    var els = this.svg.getElementById(id).getElementsByTagName('ellipse'),
        len = els.length,
        rx = num,
        ry = num / this.bb.vaspOption;
    if (! originalScale) {
        rx *= this.bb.scale1px;
        ry *= this.bb.scale1px;
    }
    for (var i = 0; i < len; i++) {
        var e = els[i];
        e.setAttribute('rx', rx);
        e.setAttribute('ry', ry);
    }
};

SvgStyle.prototype.css = function (text) {
    var ss = this.svg.parentNode.styleSheets;
    if (ss.length === 0) {
        this.svg.appendChild(document.createElementNS(
            'http://www.w3.org/2000/svg', 'style'));
    }
    ss[0].insertRule(text, 0);
};