/**
 * Layer showing guideposts'n'stuff
 */

OpenLayers.Layer.GuidePost = OpenLayers.Class(OpenLayers.Layer.Vector, {
/**
 * Features that shows neighbouring posts
 */
curfeats : null,

/**
 * Constructor: OpenLayers.Layer.GuidePost
 */
initialize: function(name, options) {
  // style map for the different modi
  var gpstyle = new OpenLayers.StyleMap(
             {"default" :   { pointRadius : 4,
                              fillColor: "#ffcc66",
                              strokeColor: "#ff9933",
                              graphicZIndex: 10,
                              strokeWidth: 2
                          },
              "temporary" : { fillColor: "#ffa21c",
                              strokeColor: "#ff7214",
                              pointRadius : 6,
                              label : "${name}(${ele}m)",
                              labelAlign : "rm",
                              labelXOffset : -8,
                              labelYOffset : 8,
                              graphicZIndex: 10,
                              fontWeight : "bold"
                            },
              "neighbour" : {
                              label : "${time}(${rel_ele}m)",
                              labelAlign : "lm",
                              labelXOffset : 8,
                              fontWeight : "bold",
                              graphicZIndex: 20,
                              graphic : false,
                              fontSize : "0.8em"
              },
              "select" :   { pointRadius : 4,
                             fillColor: "#152eec",
                             strokeColor: "#001eb3",
                             graphicZIndex: 20,
                             strokeWidth: 2
                           }
                        
             });


  var options = OpenLayers.Util.extend({
              strategies: [new OpenLayers.Strategy.Fixed()],
              protocol: new OpenLayers.Protocol.HTTP({
                         url: "hiking/posts.josn",
                         format: new OpenLayers.Format.GeoJSON()
                        }),
              isBaseLayer: false,
              rendererOptions: {zIndexing: true},
              styleMap: gpstyle
            }, options);
  var newArguments = [name, options];
  OpenLayers.Layer.Vector.prototype.initialize.apply(this, newArguments);
},

loadNeighbours: function(e) {
  var  prot = new OpenLayers.Protocol.HTTP({
                               format: new OpenLayers.Format.GeoJSON(),
                               callback: this.addNeighbours,
                               scope: this});
  prot.read({url: "/hikingposts/" + e.attributes.id + ".josn"});
},

addNeighbours: function(resp) {
  var features = resp.features;
  if (features && features.length > 0) {
     var remote = new OpenLayers.Projection("EPSG:4326");;
     var local = this.map.getProjectionObject();
     if(!local.equals(remote)) {
       var geom;
       for(var i=0, len=features.length; i<len; ++i) {
         geom = features[i].geometry;
         if(geom) {
           geom.transform(remote, local);
         }
       }
     }
     for(var i=0, len=features.length; i<len; ++i) {
       features[i].renderIntent = 'neighbour';
     }
     this.curfeat = features;
     this.addFeatures(features);
     this.redraw();
  }
},  

releaseNeighbours: function(e) {
  this.freeNeighbours();
},

freeNeighbours: function() {
  if (this.curfeat != null) {
    this.destroyFeatures(this.curfeat);
    this.redraw();
    this.curfeat = null;
  }
},

afterAdd: function() {
  // execute parent function
  OpenLayers.Layer.Vector.prototype.afterAdd.apply(this, arguments);
  // add layer for hovering
  hover = new OpenLayers.Control.SelectFeature(this, {
                hover: true,
                highlightOnly: true,
                renderIntent: "temporary"
            });
  this.map.addControl(hover);
  hover.activate();
  // add layer for selecting
  sel = new OpenLayers.Control.SelectFeature(this, {
                renderIntent: "select",
                onSelect : this.loadNeighbours,
                onUnselect : this.releaseNeighbours,
                scope : this
            });
  this.map.addControl(sel);
  sel.activate();

},
  
  
CLASS_NAME: "OpenLayers.Layer.GuidePost"
/* end of OpenLayers.Layer.GuidePost */
});
