var drivericon = "<img alt='Has a driver' title='Has a driver' src='/images/dude14-driver-red.png' align='top'/>";
var passengericon = "<img alt='Needs a driver' title='Needs a driver' src='/images/dude14-blank.gif' align='top'/>";

// A class representing a single Trip Search result returned by the
// Goloco Ajax.
function TripResult(result, use_origin) {
  this.trip_ = result;
  if (this.trip_.driver)  {
	this.myicon = drivericon
	this.drive_text= "(willing to drive)"
  } else {
	this.myicon = passengericon 
	this.drive_text= "(looking for a ride)"
  }

  this.lat = result.destination.lat
  this.lng = result.destination.lng
  var points = [ new GLatLng(result.origin.lat, result.origin.lng), new GLatLng(this.lat, this.lng)]
  
  this.show_spider = false;
  if (use_origin) {
	this.use_origin = true
	this.show_spider = true;
	this.spider_line = new GPolyline(points, "green", 3)
  	this.line = new GPolyline(points, "blue", 4, 1)
  } else {
	this.line = new GPolyline(points, "green", 4, .8)
  }

  gMap.addOverlay(this.marker({"icon":redcaricon, zIndexProcess:this.destinationZ.bind(this)}));
}
TripResult.prototype.destinationZ = function() {return (GOverlay.getZIndex(this.lat) - 10000)}
TripResult.prototype.originZ = function() {return (GOverlay.getZIndex(this.trip_.origin.lat) - 10000)}
TripResult.prototype.showLine = function(spider) {
	if (spider == true) {
		gMap.addOverlay(this.spider_line)
	} else {
		gMap.addOverlay(this.line);
	}
	gMap.addOverlay(this.startMarker());
}
TripResult.prototype.hideLine = function() {
	gMap.removeOverlay(this.line);
	//gMap.removeOverlay(this.spider_line);
	gMap.removeOverlay(this.startMarker());
}
TripResult.prototype.find_marker = function() { 
	var from = new GLatLng(gCurrentFrom.lat, gCurrentFrom.lng);
	var marker = this.marker()
	var startmarker = this.startMarker()
	var uppermarker = marker
	if (marker.getLatLng().lat() < startmarker.getLatLng().lat()) uppermarker = startmarker
	
	bounds = new GLatLngBounds();
	bounds.extend(this.startMarker().getPoint());
    bounds.extend(this.marker().getPoint());
	var z = parseFloat(map.getBoundsZoomLevel(bounds))
	var currentzoom = parseFloat(map.getZoom())
	if (currentzoom < (z-1)  ||  currentzoom >= z) {
		// only rezoom if we are at a radically different zoom
		map.setCenter(bounds.getCenter(), z - 1);		
	}	
	this.showLine();
	uppermarker.openInfoWindowHtml(this.trip_.bubble, {noCloseOnClick: true})
	this.showLine();

	var query = "@"+this.trip_.origin.lat+", " +this.trip_.origin.lng + " to @" + this.trip_.destination.lat+", " +this.trip_.destination.lng
	//gDirections.load(query, {preserveViewport: true})
	
};

TripResult.prototype.hideRoute = function() {
	//Hide the directions and show the route, if we are in group view,
	// otherwise just hide the route.
	//gDirections.clear()
		this.hideLine()
}
// Returns the GMap marker for this trip, creating it with the given
// icon if it has not already been created.
TripResult.prototype.marker = function(opt_icon) {
  if (this.marker_) return this.marker_;
  var marker = new GMarker(new GLatLng(parseFloat(this.lat),
                                     parseFloat(this.lng)),
                           opt_icon);
  GEvent.bind(marker, "click", this, this.find_marker.bind(this));
  GEvent.addListener(marker, 'infowindowbeforeclose', this.hideRoute.bind(this))
  this.marker_ = marker;
  return marker;
}
// Returns the GMap marker for this trip, creating it with the given
// icon if it has not already been created.
TripResult.prototype.startMarker = function(opt_icon) {
  if (this.startMarker_) return this.startMarker_;
  var startMarker = new GMarker(new GLatLng(parseFloat(this.trip_.origin.lat),
                                     parseFloat(this.trip_.origin.lng)),
                           {"icon":tripResultIcon, zIndexProcess:this.originZ.bind(this)});
  GEvent.bind(startMarker, "click", this, this.find_marker.bind(this));
  GEvent.addListener(startMarker, 'infowindowbeforeclose', this.hideRoute.bind(this))

  this.startMarker_ = startMarker;
  return startMarker;
}
TripResult.prototype.remove_markers = function () {
	if (this.marker_) 
		gMap.removeOverlay(this.marker_)	
	if (this.line)
		gMap.removeOverlay(this.line)
	if (this.spider_line)
		gMap.removeOverlay(this.spider_line)
	if (this.startMarker_) 
		gMap.removeOverlay(this.startMarker_)	
}
// Returns the HTML we display for a result before it has been "saved"
TripResult.prototype.unselectedHtml = function() {
	var container = document.createElement("div");
	var holder = document.createElement("div");
	holder.className = "tripdetails"
  	holder.innerHTML = this.trip_.date + '<br/>' +
	'from: ' + this.trip_.origin.description +'<br/>' +
	'to: <span style="font-weight:bold">'+this.trip_.destination.description+'</span>' + '<br/>' +
	'with: ' + this.trip_.leader.full_name +" "+ this.myicon + this.drive_text + '<br/>' +
	'<div class="bluebutton"><a href="#" onclick="show_trip(\''+this.trip_.slug+'\'); return false;">view trip</a></div>' + '<br/>'
	var image = document.createElement("div");
	image.innerHTML = this.trip_.image
	image.className = "tripimage"
	container.appendChild(holder)
	container.appendChild(image)
	return container
  return container;
}