code: function (layer, idx) {
          var base = this.getLayerIndex(layer);
          if (idx < 0) {
              idx = 0;
          } else if (idx > this.layers.length) {
              idx = this.layers.length;
          }
          if (base != idx) {
              this.layers.splice(base, 1);
              this.layers.splice(idx, 0, layer);
              for (var i=0, len=this.layers.length; i<len; i++) {
                  this.setLayerZIndex(this.layers[i], i);
              }
              this.events.triggerEvent("changelayer", {
                  layer: layer, property: "order"
              });
              if(this.allOverlays) {
                  if(idx === 0) {
                      this.setBaseLayer(layer);
                  } else if(this.baseLayer !== this.layers[0]) {
                      this.setBaseLayer(this.layers[0]);
                  }
              }
          }
      }
docs: APIMethod: setLayerIndex
  Move the given layer to the specified (zero-based) index in the layer
  list, changing its z-index in the map display. Use
  map.getLayerIndex() to find out the current index of a layer. Note
  that this cannot (or at least should not) be effectively used to
  raise base layers above overlays.
  
  Parameters:
  layer - {<OpenLayers.Layer>}
  idx - {int}