$("div.business").each(function() {
	// yelp will take phone # in any format--no need to strip out spaces or formatting
	var phone = $(this).find(".phone").text();
	// set the id of the outer div since the context "this" changes in the getjson call
	var bid = $(this).attr("id");
	var isDetailsView = (bid.toString() == "details");
	var isSmallView = ($("#" + bid).is(".small"));
	$.getJSON(
		"http://api.yelp.com/phone_search?phone=" + phone + "&ywsid=h2tLISuhQnuB8hfJfKWRbw&callback=?",
		function(data) {
			var gotBusiness = ((data.message.code == 0) && (data.businesses.length > 0));
			// the small view only gets the small stars and a count
			if ((gotBusiness) && (isSmallView)) {
				$("#" + bid).children(".name").append(
					"<br /><a target='_blank' href='"
					+ data.businesses[0].url
					+ "' title='Yelp Reviews' class='yelpRatingSmall'><img src='"
					+ data.businesses[0].rating_img_url_small 
					+ "' /> ("
					+ data.businesses[0].review_count
					+ ")</a>"
				);
			} 
			// the regular and detail views both get larger ratings and a link to the yelp reviews as per yelp's api
			else if (gotBusiness) {
				$("#" + bid).children(".name").append(
					"<a target='_blank' href='"
					+ data.businesses[0].url
					+ "' title='Yelp Reviews' class='yelpRating'><img src='"
					+ data.businesses[0].rating_img_url 
					+ "' /> ("
					+ data.businesses[0].review_count
					+ ")</a>"
				);

				// add in the reviews before the final link to yelp
				if (isDetailsView) {

					var today = new Date();
					var month = (today.getMonth().toString().length == 1) ? "0" + (today.getMonth() + 1).toString() : (today.getMonth() + 1).toString();
					var day = (today.getDate().toString().length == 1) ? "0" + today.getDate().toString() : today.getDate().toString();
					var datestring = today.getFullYear().toString() + month + day;
					
					$("#" + bid).children(".description").append(
						"<div class='clear'></div><div class='yelpTrends'><h3>Yelp Ratings Distribution & Trends</h3><img src='"
						+ "http://static.px.yelp.com/biz_details_graphs/static/"
						+ datestring
						+ "/dist/"
						+ data.businesses[0].id
						+ "' title='Ratings Distribution' /><img src='"
						+ "http://static.px.yelp.com/biz_details_graphs/static/"
						+ datestring
						+ "/trend/" 
						+ data.businesses[0].id
						+ "' title='Trend' /></div>"
					);

					$("#" + bid).children(".description").append(
						"<div class='yelpReviews'><h3>Top Reviews "
						+ "(<a target='_blank' href='"
						+ data.businesses[0].url
						+ "' title='Yelp Reviews'>See all "
						+ data.businesses[0].review_count
						+ " reviews</a>)</h3>"
					);
					$.each(data.businesses[0].reviews, function(i,review) {
						$("#" + bid).children(".description").append(
							"<p class='yelpReview'>"
							+ "<img src='" + review.rating_img_url_small + "' alt='' title='" + review.rating + "' /> "
							+	"<span class='smallDate'>from</span> <a href='" + review.url + "' target='_blank'>" + review.user_name + "</a> "
							+ "<span class='smallDate'>on " + review.date	+ "</span><br />"
							+ review.text_excerpt
							+ "</p>"
						);
					});
					$("#" + bid).children(".description").append(
						"</div>"
					);

				}
				
				if (!isDetailsView) {
					$("#" + bid).children(".description").append(
						"<p class='yelpLink'><a target='_blank' href='"
						+ data.businesses[0].url
						+ "' title='Yelp Reviews'>See all "
						+ data.businesses[0].review_count
						+ " Yelp reviews</a></p>"
					);
				}
				
			}

			// if it's the details page and no reviews, suggest that they submit the business
			else if ((!gotBusiness) && (isDetailsView)) {
				$("#" + bid).children(".description").append(
					"<p class='yelpLink'>Yelp can't find this business. Would you like to <a target='_blank' href='http://www.yelp.com/writeareview/newbiz?search_loc=Philadelphia%2C+PA'>add it</a>?</p>"
				);
			}
	});
});
