var RoutePlotter = Class.create({
  initialize: function(map, busPlotter, routes, links){
    this.map = map;
    this.busPlotter = busPlotter;
    this.routes = $H(routes);
    if(links != null)
      this.links = $A(links);
    
    this.setRoutes();
    this.busPlotter.setRoutePlotter(this);
  },
  
  setRoutes: function(){
    var map = this.map
    this.routes.each(function(route){
      route.value[0].hide();
      map.addOverlay(route.value[0]);
    });
  },
  
  toggleRoute: function(routeId, link){
    routeId = routeId.replace(/d/, '');
    var thisRoute = this.routes.get(routeId);
    if(thisRoute != null){
      if(thisRoute[1]){
        thisRoute[0].hide();
        this.busPlotter.removeSelectedRoute(routeId);
      }else{
        thisRoute[0].show();
        coordCount = thisRoute[0].getVertexCount();
    		approxCenter = Math.floor(coordCount/2);
    		coord = thisRoute[0].getVertex(approxCenter);
    		this.map.panTo(coord);
        this.busPlotter.setRouteSelected(routeId);
      }
      thisRoute[1] = !thisRoute[1] //opposite boolean set
    }else if(routeId == "ALL"){
      this.routes.each(function(poly){
        if(poly.value[1]){
          poly.value[0].hide();
          poly.value[1] = false;
        }
      });
      this.busPlotter.setRouteSelected(routeId);
    }else{
      console.log("invalid input");
    }
    if(link != null){
      this.replaceText(link);
    }
  },
  
  is_set: function(routeId){
    return this.routes.get(routeId)[1];
  },
  
  replaceText: function(link){
    if(this.routes.get($(link).id)){
      if(this.routes.get($(link).id)[1]){
        $(link).update("<a>Hide Route " + $(link).id + "</a>");
      }else{
        $(link).update("<a>Show Route " + $(link).id + "</a>");
      }
    }else if($(link).id == "ALL"){
      this.links.each(function(link){
        if($(link).id != "ALL")
          $(link).update("<a>Show Route " + $(link).id + "</a>");
      });
    }
  }
});