

////////////////////////////////////////////////////
//YELPLOCALRESULT STUFF

var numDirectionsFinished = 0;
var totalTime = 0;

// A class representing a single Local Search result from YELP
function YelpLocalResult(id, lat, lng, name, address1, address2, city, state, zip, phoneNum, ratingImageUrl, avgRating, url, reviewCount) {
	this.id_ = id;
	this.lat_ = lat;
	this.lng_ = lng;
	this.name_ = name;
	this.address1_ = address1;
	this.address2_ = address2;
	this.city_ = city;
	this.state_ = state;
	this.zip_ = zip;
	this.phoneNum_ = phoneNum;
	
	//yelp specific
	this.ratingImageUrl_ = ratingImageUrl;
	this.avgRating_ = avgRating;
	this.url_ = url;
	this.reviewCount_ = reviewCount;
	
	//other search stuff
	this.distTot_ = 0; // in meters
	this.timeTot_ = 0; // in seconds
	
	this.marker_;
	
	this.directions_ = [];
}

// Returns the ID for this YelpLocalResult
YelpLocalResult.prototype.getId = function() {
	return this.id_;
}

// Returns the GPoint for this YelpLocalResult
YelpLocalResult.prototype.getPoint = function() {
	return new GPoint(parseFloat(this.lng_), parseFloat(this.lat_));
}

// Returns the GPoint for this YelpLocalResult
YelpLocalResult.prototype.getLatLng = function() {
	return new GLatLng(parseFloat(this.lat_), parseFloat(this.lng_));
}

// Returns the average rating for this YelpLocalResult
YelpLocalResult.prototype.getAvgRating = function() {
	return this.avgRating_;
}

// Returns the average distance for this YelpLocalResult
YelpLocalResult.prototype.getAvgDistance = function(num) {
	return this.distTot_/num;
}

// Returns the average time for this YelpLocalResult
YelpLocalResult.prototype.getAvgTime = function(num) {
	return this.timeTot_/num;
}

YelpLocalResult.prototype.addTimeAndDist = function(index) {
	this.distTot_ += this.directions_[index].getRoute(0).getDistance().meters;
	this.timeTot_ += this.directions_[index].getRoute(0).getDuration().seconds;
}

// Returns the review count for this YelpLocalResult
YelpLocalResult.prototype.getReviewCount = function() {
	return this.reviewCount_;
}

// Returns the GMap marker for this result, creating it with the given icon if it has not already been created
YelpLocalResult.prototype.marker = function(icon) {
	if (this.marker_) return this.marker_;
	var marker = new GMarker(new GLatLng(parseFloat(this.lat_),
								parseFloat(this.lng_)),
								icon);
	GEvent.bind(marker, "click", this, function() {
		openInfoWindow(this.id_);
		//marker.openInfoWindow(this.getInfoHtml(icon));
	});
	GEvent.bind(marker, "infowindowclose", this, function() {
		infoWindowClosed();
	});
	this.marker_ = marker;
	return marker;
}

YelpLocalResult.prototype.getResultsHtml = function(icon) {
	var item = "";
	try {
		item = "<table><tr><td valign = 'top'>";
		item += "<a href='javascript:void(0);' onclick='javascript:openInfoWindow("+this.id_+"); return false;'><img src='"+icon.image+"'/></a></td><td><strong><font color='blue'><a href='javascript:void(0);' onclick='javascript:openInfoWindow("+this.id_+"); return false;'>"+this.name_+"</a></font></strong><br/>";
		item += '<img class="ratingsimage" src="'+this.ratingImageUrl_+'"/>&nbsp;<a href="'+this.url_+'" target="_blank">based&nbsp;on&nbsp;';
		item += this.reviewCount_+'&nbsp;reviews</a><br/>';

		if(this.address1_.length) {
			item += this.address1_+"<br>";
		}
		if(this.address2_.length) {
			item += this.address2_+"<br>";
		}
		item += this.city_+",&nbsp;"+this.state_+"&nbsp;"+this.zip_+"<br>";
		if(this.phoneNum_.length) {
			item += this.phoneNum_+"<br>";
		}
		
		//avg distance and time
		//item += "avg distance: " + Math.round(this.distTot_/entryLocations.length) + " meters<br>avg time: " + Math.round(this.timeTot_/entryLocations.length/60*100)/100 + " minutes<br>";
		
		//Point Markers needs to be a global
		for(var i = 0 ; i < entryLocations.length ; i++) {
			var link = "http://www.google.com/maps?source=uds&daddr="+this.address1_+"+"+this.city_+","+this.state_+"&saddr=" + entryLocations[i].entryString_;
			//var latlngstring = entryLocations[i].getPoint().toString();
			//link += "=" + latlngstring.substring(1, (latlngstring.length)-1);
			link = link.replace(/#/g, "%23");
			item += "<a href='" + link + "' target='_blank'>from <img style='border: none;' src='images/marker_" + (i+1) + ".png'/></a>&nbsp;";
		}
		item += "<br><br></td></tr></table>";
	} catch(e) {
		// BA TODO Better Error
		alert(e.name);
	}
	return item;
}

YelpLocalResult.prototype.getInfoHtml = function(icon) {
	var item = "";
	try {
		item = "<table><tr><td valign = 'top'>";
		item += "<img src='"+icon.image+"'/></td><td><font color='blue'><strong>"+this.name_+"</strong></font><br>";
		item += '<img class="ratingsimage" src="'+this.ratingImageUrl_+'"/>&nbsp;<a href="'+this.url_+'" target="_blank">based&nbsp;on&nbsp;';
		item += this.reviewCount_+'&nbsp;reviews</a><br/>';

		if(this.address1_.length) {
			item += this.address1_+"<br>";
		}
		if(this.address2_.length) {
			item += this.address2_+"<br>";
		}
		item += this.city_+",&nbsp;"+this.state_+"&nbsp;"+this.zip_+"<br>";
		if(this.phoneNum_.length) {
			item += this.phoneNum_+"<br>";
		}
		
		//avg distance and time
		//item += "avg distance: " + Math.round(this.distTot_/entryLocations.length) + " meters<br>avg time: " + Math.round(this.timeTot_/entryLocations.length/60*100)/100 + " seconds<br>";
		
		for(var i = 0 ; i < entryLocations.length ; i++) {
			var link = "http://www.google.com/maps?source=uds&daddr="+this.address1_+"+"+this.city_+","+this.state_+"&saddr=" + entryLocations[i].entryString_;
			//var latlngstring = entryLocations[i].getPoint().toString();
			//link += "=" + latlngstring.substring(1, (latlngstring.length)-1);
			link = link.replace(/#/g, "%23");
			item += "<a href='" + link + "' target='_blank'>from <img style='border: none;' src='images/marker_" + (i+1) + ".png'/></a>&nbsp;";
		}
		item += "<br></td></tr></table>";
	} catch(e) {
		alert(e.name);
	}
	return item;
}

YelpLocalResult.prototype.calculateDistanceAndTime = function(resultId, entryId, entryLocation) {
	this.directions_[entryId] = new GDirections();
	GEvent.addListener(this.directions_[entryId],"error", function() {
		alert("Directions Failed: "+this.directions_[entryId].getStatus().code);
	});
	GEvent.addListener(this.directions_[entryId],"load", function() {
		setTimeout('doneWithDirections('+resultId+', '+entryId+')', 1);
	});
	
	var query = "from:" + entryLocation.getLatLng().lat() + "," + entryLocation.getLatLng().lng() + " to:" + this.getLatLng().lat()+","+this.getLatLng().lng();
	this.directions_[entryId].load(query);
}

function findLocalResult(id) {
	for(var i = 0; i < yLocalResults.length; i++) {
		if(yLocalResults[i].getId() == id) {
			return i;
		}
	}
}

//END YELPLOCALRESULT
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// GOOGLERESULT
// A class representing a single Local Search result from Google
function GoogleLocalResult(id, lat, lng, name, address, city, state, phoneNum, icon) {
	this.id_ = id;
	this.lat_ = lat;
	this.lng_ = lng;
	this.name_ = name;
	this.address_ = address;
	this.city_ = city;
	this.state_ = state;
	this.phoneNum_ = phoneNum;
	
	//other search stuff
	this.distTot_ = 0; // in meters
	this.timeTot_ = 0; // in seconds
	
	this.marker_ = this.marker(icon);
	
	this.directions_ = [];
}

// Returns the GPoint for this GoogleLocalResult
GoogleLocalResult.prototype.getPoint = function() {
	return new GPoint(parseFloat(this.lng_), parseFloat(this.lat_));
}

// Returns the GPoint for this GoogleLocalResult
GoogleLocalResult.prototype.getLatLng = function() {
	return new GLatLng(parseFloat(this.lat_), parseFloat(this.lng_));
}

// Returns the GMap marker for this result, creating it with the given icon if it has not already been created
GoogleLocalResult.prototype.marker = function(icon) {
	if (this.marker_) return this.marker_;
	var marker = new GMarker(new GLatLng(parseFloat(this.lat_),
								parseFloat(this.lng_)),
								icon);
	GEvent.bind(marker, "click", this, function() {
		openInfoWindow(this.id_);
		//marker.openInfoWindow(this.getInfoHtml(icon));
	});
	GEvent.bind(marker, "infowindowclose", this, function() {
		infoWindowClosed();
	});
	this.marker_ = marker;
	return marker;
}

GoogleLocalResult.prototype.getResultsHtml = function(icon) {
	var item = "";
	try {
		item = "<table><tr><td valign = 'top'>";
		item += "<a href='javascript:void(0);' onclick='javascript:openInfoWindow("+this.id_+"); return false;'><img src='"+icon.image+"'/></a></td><td><strong><font color='blue'><a href='javascript:void(0);' onclick='javascript:openInfoWindow("+this.id_+"); return false;'>"+this.name_+"</a></font></strong><br/>";

		item += this.address_+"<br>";
		item += this.city_+",&nbsp;"+this.state_+"<br>";
		
		if(this.phoneNum_) {
			item += this.phoneNum_+"<br>";
		}
		
		//avg distance and time
		//item += "avg distance: " + Math.round(this.distTot_/entryLocations.length) + " meters<br>avg time: " + Math.round(this.timeTot_/entryLocations.length/60*100)/100 + " minutes<br>";
		
		//Point Markers needs to be a global
		for(var i = 0 ; i < entryLocations.length ; i++) {
			var link = "http://www.google.com/maps?source=uds&daddr="+this.address_+"+"+this.city_+","+this.state_+"&saddr=" + entryLocations[i].entryString_;
			//var latlngstring = entryLocations[i].getPoint().toString();
			//link += "=" + latlngstring.substring(1, (latlngstring.length)-1);
			link = link.replace(/#/g, "%23");
			item += "<a href='" + link + "' target='_blank'>from <img style='border: none;' src='images/marker_" + (i+1) + ".png'/></a>&nbsp;";
		}
		item += "<br><br></td></tr></table>";
	} catch(e) {
		// BA TODO Better Error
		alert(e.name);
	}
	return item;
}

GoogleLocalResult.prototype.getInfoHtml = function(icon) {
	var item = "";
	try {
		item = "<table><tr><td valign = 'top'>";
		item += "<img src='"+icon.image+"'/></td><td><font color='blue'><strong>"+this.name_+"</strong></font><br>";
		
		item += this.address_+"<br>";
		item += this.city_+",&nbsp;"+this.state_+"<br>";
		if(this.phoneNum_) {
			item += this.phoneNum_+"<br>";
		}
		
		//avg distance and time
		//item += "avg distance: " + Math.round(this.distTot_/entryLocations.length) + " meters<br>avg time: " + Math.round(this.timeTot_/entryLocations.length/60*100)/100 + " seconds<br>";
		
		for(var i = 0 ; i < entryLocations.length ; i++) {
			var link = "http://www.google.com/maps?source=uds&daddr="+this.address_+"+"+this.city_+","+this.state_+"&saddr=" + entryLocations[i].entryString_;
			//var latlngstring = entryLocations[i].getPoint().toString();
			//link += "=" + latlngstring.substring(1, (latlngstring.length)-1);
			link = link.replace(/#/g, "%23");
			item += "<a href='" + link + "' target='_blank'>from <img style='border: none;' src='images/marker_" + (i+1) + ".png'/></a>&nbsp;";
		}
		item += "<br></td></tr></table>";
	} catch(e) {
		alert(e.name);
	}
	return item;
}


///////////////////////////////////////////////////
//Directions Call Back
function doneWithDirections(resultId, entryId) {
	yLocalResults[resultId].addTimeAndDist(entryId);
	numDirectionsFinished++;
}
//END DIRECTIONS CALL BACK
///////////////////////////////////////////////////

///////////////////////////////////////////////////
//SORTING METHODS
function localResultsSortByReview(a, b) {
	if(a.getAvgRating() == b.getAvgRating()) {
		return b.getReviewCount() - a.getReviewCount();
	}
	else {
		return b.getAvgRating() - a.getAvgRating();
	}
}

function localResultsSortByDistance(a, b) {
	return a.getAvgDistance(entryLocations.length) - b.getAvgDistance(entryLocations.length);
}

function localResultsSortByTime(a, b) {
	return a.getAvgTime(entryLocations.length) - b.getAvgTime(entryLocations.length);
}
//END SORTING METHODS
///////////////////////////////////////////////////

