var StopIcon = Class.create({
  /**
  *constructor for StopIcon. Will create a stop icon and plot it on map with hover over text
  *@param stop_id - the stop id, used for ajax requests
  *@param map - the google maps map
  *@param lat - the latitude
  *param lon - the longitude
  *@param busPlotter - the bus plotter which creates the bus icon
  */
  initialize: function(map, stop_name, stop_id, lat, lon, busPlotter, icon){
    this.stop_id = stop_id;
    this.stop_name = stop_name;
    this.icon = icon;
    this.map = map;
    this.busPlotter = busPlotter;
    this.mapLocation = new GLatLng(lat, lon);
    this.setMarker();
    this.setInfoEvents();
  },
  
  
  //creates a new Gmarker and sets it to the current busLocation using the current stopIcon.
  setMarker: function(){
    if(this.icon != null){
      this.marker = new GMarker(this.mapLocation, {'icon': this.icon});
    }else{
      this.marker = new GMarker(this.mapLocation, {});
    }  
      this.map.addOverlay(this.marker);
  },
  
  /**
  * updates the info box from the stopResponse. 
  * @param stopResponse - a string with the format for info box
  */
  updateInfo: function(stopResponse){
    this.infoBox.updateText(this.stop_name + ':<br/>scheduled times for stop.<br/>click bus num to view bus.<br/><br/>' + stopResponse)
  },
  
  /* creates an InfoWindow and binds it with the bus marker, callse updateInfo to set the text.*/
  setInfoEvents: function(){
    this.infoBox = new InfoOverlay("");
  	GEvent.bind(this.marker, "click", this, this.onClick);
  },

  onClick: function(){
    this.infoBox.getDiv().setStyle({height: '150px'})
    this.infoBox.updateText(this.stop_name + ':<br/><br/>Please Wait....')
    this.marker.openInfoWindow(this.infoBox.getDiv(), {'noCloseOnClick': true});
    var thisContainer = this;
    new Ajax.Request("./stops/" + this.stop_id, {method: 'get', evalScripts: false, onComplete: function(response){
      var stopResponse = eval("(" + response.transport.responseText + ")");
      try{
        thisContainer.updateInfo(stopResponse);
      }catch(err){
        console.log(err);
        $('connection_lost').show();
      }
    }});
  }  
});