$(function(){
	// make the header images cycle - requires jquery.cycle plugin
	$('#header').cycle({
		fx: 'fade',
		timeout: 10000,
		next:	'#header',
		pause: 1
	});
	// make any lightbox links turn on prettyphoto - requires jquery.prettyphoto plugin
	$('a.lightbox').prettyPhoto({
		animationSpeed: 'normal',
		padding: 50,
		opacity: 0.65,
		showTitle: true,
		allowresize: false,
		counter_separator_label: '/',
		theme: 'light_rounded'
	});

	$('.announcement')
		.each(
			function() {
				var current = $(this);
				current.attr('boxh', current.height());
		})
		.css({height:180,overflow:'hidden'})
		.after('<a href=# class=show>+ more</a>');
	$('a.show').live('click', function(ev){
		ev.preventDefault();
		var boxh = $(this).prev('.announcement').attr('boxh');
		$(this).fadeOut();
		$(this).prev('.announcement').animate({height: boxh},'slow');
	});
	
	// wire up the yelp api selectors - if you copy this code, don't forget to replace your own yelp api key or yelp will gorge your eyeballs out
	$("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 + " .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 + " > .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 + " .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 + " .description").append(
							"<div class='yelpReviews'><h3>Top Reviews "
							+ "(<a target='_blank' href='"
							+ data.businesses[0].url
							+ "' title='Yelp Reviews'>Read all "
							+ data.businesses[0].review_count
							+ " reviews</a> or <a target='_blank' href='http://www.yelp.com/writeareview/biz/"
							+ data.businesses[0].id
							+ "'>write a review now</a>)</h3>"
						);
						$.each(data.businesses[0].reviews, function(i,review) {
							$("#" + bid + " .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 + " .description").append(
							"</div>"
						);

					}
					
					if (!isDetailsView) {
						$("#" + bid + " .description").append(
							"<p class='yelpLink'><a target='_blank' href='"
							+ data.businesses[0].url
							+ "' title='Yelp Reviews'>Read all "
							+ data.businesses[0].review_count
							+ " reviews</a> or <a target='_blank' href='http://www.yelp.com/writeareview/biz/"
							+ data.businesses[0].id
							+ "'>write a review now</a></p>"
						);
					}
					
				}

				// if it's the details page and no reviews, suggest that they submit the business
				else if ((!gotBusiness) && (isDetailsView)) {
					$("#" + bid + " .description").append(
						"<br /><br /><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>"
					);
				}
		});
	});
});
