// add backward / forward functionality to elements SVG.extend(SVG.Element, { // get all siblings, including myself siblings: function() { return this.parent.children(); }, // get the curent position siblings position: function() { return this.siblings().indexOf(this); }, // get the next element next: function() { return this.siblings()[this.position() + 1]; }, // get the next element previous: function() { return this.siblings()[this.position() - 1]; }, // send given element one step forwards forward: function() { return this.parent.remove(this).put(this, this.position() + 1); }, // send given element one step backwards backward: function() { var i, p = this.parent.level(); i = this.position(); if (i > 1) p.remove(this).add(this, i - 1); return this; }, // send given element all the way to the front front: function() { return this.parent.remove(this).put(this); }, // send given element all the way to the back back: function() { var p = this.parent.level(); if (this.position() > 1) p.remove(this).add(this, 0); return this; } });