$(function(){

	$(".prettydate").prettyDate();

	// 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'
	});

	if ($("#dnn_ctr444_Feedback_cboCategory").length > 0) {
		var $options = $("#dnn_ctr444_Feedback_cboCategory option");
		var afterhash = location.hash.substring(location.hash.indexOf("#") + 1);
		$options.each(function() {
			this.selected = (this.text.toLowerCase().replace(" ", "").indexOf(afterhash) != -1);
		});
	};
	
	 if ($("#dnn_ctr769_Feedback_txtBody").length > 0) {
	 	$("#dnn_ctr769_Feedback_txtBody").val("Exact location of issue (including Zip code):\n\nDescription of issue:\n");
	};

	// 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>"
					);
				}
		});
	});
});

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < -1 || day_diff >= 60 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == -1 && "Today" ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 60 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text(date);
		});
	};