// A class representing a single Local Search result returned by the
// Google AJAX Search API.
function LocalResult(result, fromResult) {
  this.result_ = result;
  this.lat = result.lat
  this.lng = result.lng
  
  this.resultNode_ = this.listHtml();
  this.fromResult = fromResult; 
  var myicon;
  if (!fromResult) {
	  document.getElementById("searchwell").appendChild(this.resultNode_);
	  myicon = gSmallIcon;
	
  } else {
	  myicon = memarker_icon;
  }
  gMap.addOverlay(this.marker({"icon":myicon, zIndexProcess:this.markerIndex.bind(this)}));

}

LocalResult.prototype = new Cluster;

// Returns the GMap marker for this result, creating it with the given
// icon if it has not already been created.
LocalResult.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.showInfoWindow.bind(this));
  GEvent.bind(marker, "mouseover", this, function() {revealListItem(this.listHTML_)});
  GEvent.bind(marker, "mouseout", this, function() {revealListItem()});
  this.marker_ = marker;
  return marker;
}
LocalResult.prototype.remove_markers = function () {
	if (this.marker_) 
		gMap.removeOverlay(this.marker_)	
}

LocalResult.prototype.showInfoWindow = function () {
	this.marker_.openInfoWindow(this.unselectedHtml());
	this.showMyTrips();
	setCurrentCluster(this)
}
LocalResult.prototype.showPostWindow = function () {
	if (!this.posting || gMap.getInfoWindow().isHidden()) {
		this.marker_.openInfoWindow(this.postingForm());
		this.posting = true;
	}
	gSelectedResult = this;
}
// Returns the HTML we display for a result before it has been "saved"
LocalResult.prototype.unselectedHtml = function() {
  var container = document.createElement("div");
  container.className = "unselected";
  var addressDiv = document.createElement("div");
  addressDiv.innerHTML=" "+this.result_.title+"<br/>" + this.result_.streetAddress + ", " + this.result_.city
  var saveDiv = document.createElement("div");
  saveDiv.className = "bluebutton";
  if (this.fromResult) {
  	saveDiv.innerHTML = "<a href='#' onclick='return false'>GoLoco From Here</a>";
		 GEvent.bindDom(saveDiv, "click", this, function() {
		   gMap.closeInfoWindow();
           changeFrom(this.lat, this.lng, this.address())
		 });

  } 
  container.appendChild(addressDiv);
  container.appendChild(saveDiv);
  return container;
}
// Returns the HTML we display for a result in the List
LocalResult.prototype.listHtml = function() {
  var container = document.createElement("div");
  this.listHTML_ = container;
  container.className = "listResult";
  var addressDiv = document.createElement("div");
  addressDiv.innerHTML="<img src='/images/markers/target.png' width='20px' height='20px' style='vertical-align:middle'/> "+this.result_.title+"<br/>" + this.result_.streetAddress + ", " + this.result_.city

  var actionDiv = document.createElement("div");
  actionDiv.className = "bluebutton"
  var countDiv = document.createElement("a");
  this.count_ = countDiv;
//  countDiv.className = "countDiv";
  countDiv.innerHTML ="Searching <img src='/images/7-1.gif' height='10px' />";
  var saveDiv = document.createElement("a");
  saveDiv.innerHTML = "Post My Trip";
  var fromDiv = document.createElement("div");
  this.trip_listings = document.createElement("div");
  
  GEvent.bindDom(saveDiv, "click", this, function() {
    //gMap.closeInfoWindow();
    this.showPostWindow();
    //this.make_trip();
    gSelectedResults.push(this);
  });
  GEvent.bindDom(addressDiv, "click", this, this.showInfoWindow)
  GEvent.bindDom(countDiv, "click", this, this.showInfoWindow)
  container.appendChild(addressDiv);
  actionDiv.appendChild(saveDiv);
  actionDiv.appendChild(countDiv);
  container.appendChild(actionDiv);
  container.appendChild(this.trip_listings);
  return container;
}

// a little form that allows the collection of details for posting a trip
LocalResult.prototype.postingForm = function() {
  var container = document.createElement("div");
  container.innerHTML="From: "+gCurrentFrom.address+" <br/> To: " + this.result_.title 
  if (datechooser.parentNode)
  	datechooser.parentNode.removeChild(datechooser)
  container.appendChild(datechooser)
  return container;
}

LocalResult.prototype.make_trip = function(can_drive) {

	get_dates();
	
	$("trip_search_origin_latitude").value = gCurrentFrom.lat;
	$("trip_search_origin_longitude").value = gCurrentFrom.lng;
	$("trip_search_destination_latitude").value = this.lat;
	$("trip_search_destination_longitude").value = this.lng;
	$("hpa_destination").value = whereto.value;
	$("trip_search_destination_address").value = this.address();
	$("trip_search_origin_address").value = gCurrentFrom.address;
	if (can_drive) {
		$("trip_search_rider_type_either").checked = true;
		$("trip_search_rider_type_passenger").checked = false;
	} else {
		$("trip_search_rider_type_either").checked = false;
		$("trip_search_rider_type_passenger").checked = true;
	}
	new Ajax.Request('/trips', {asynchronous:true, evalScripts:true, parameters:Form.serialize($("trip_search_form")), onSuccess:this.postedTrip.bind(this)});
	var container = document.createElement("div");
	container.innerHTML = "I'm posting your trip now.  <p>Please hold on a second."
	gMap.closeInfoWindow();
	this.marker_.openInfoWindow(container)
//	$("trip_search_form").submit();
}
LocalResult.prototype.postedTrip = function(transport) {
	var container = document.createElement("div");
	container.innerHTML = transport.responseText
	gMap.closeInfoWindow();
    this.posting = false;
	this.marker_.openInfoWindow(container)
	
}
LocalResult.prototype.slug = function() {
    return this.address()
}
LocalResult.prototype.find_marker = function() {
	gMap.recenterOrPanToLatLng(new GPoint(parseFloat(this.lng), parseFloat(this.lat)))
	this.marker_.openInfoWindow(this.unselectedHtml());
};
LocalResult.prototype.address = function() {
	parts = []
	street = this.result_.streetAddress
	city = this.result_.city
	state = this.result_.region
	if (city.length) parts.push(city)
	if (state.length) parts.push(state)
	result = parts.join(", ")
	if (street.length && result != street) {
		result = [street, result].join(", ")
	}
	return result
}

LocalResult.prototype.markerIndex = function() {
	return (GOverlay.getZIndex(this.lat) - 10700)
}

LocalResult.prototype.lat = function() {
    return this.lat
}
LocalResult.prototype.lng = function() {
    return this.lng
}