﻿function yelpReview() {
 this.apiKey = '0Z5E7zU2-Dv2CnNDccNr7A';
 this.container = '';
 this.category = 'Restaurants';
 this.map = null;
 this.longitude = -82.5306527; // Sarasots
 this.latitude = 27.3364347; // Sarasota
 this.radius = 5.0;
 this.limit = 3;
 this.callback = 'displayReviews';
 this.yelpURL = '';
}

yelpReview.prototype={
 setContainer:function(containerName) {
  this.container=containerName;
 }, 
 getContainer:function() {
  return this.container;
 },
 constructYelpURL:function() {
  if (this.map != null) {
   var mapBounds = this.map.getBounds(); 
  
   var yelpURL = "http://api.yelp.com/" + 
                 "business_review_search?" + 
                 "callback=" + this.callback + 
                 "&term=" + this.category + 
                 "&limit=" + this.limit +
                 "&tl_lat=" + mapBounds.getSouthWest().lat() + 
                 "&tl_long=" + mapBounds.getSouthWest().lng() + 
                 "&br_lat=" + mapBounds.getNorthEast().lat() + 
                 "&br_long=" + mapBounds.getNorthEast().lng() +                 
                 "&ywsid=" + this.apiKey; 
  } else {
   var yelpURL = "http://api.yelp.com/" + 
                 "business_review_search?" + 
                 "callback=" + this.callback + 
                 "&term=" + this.category + 
                 "&limit=" + this.limit +
                 "&lat=" + this.latitude + 
                 "&long=" + this.longitude + 
                 "&radius=" + this.radius + 
                 "&ywsid=" + this.apiKey;   
  }

  this.yelpURL = encodeURI(yelpURL); 
 },
 getReviewsByCity:function(category, map, longitude, latitude, radius, limit, callback) {
  this.category = category;
  this.map = map;
  this.limit = limit;
  this.callback = callback;
  this.constructYelpURL();
  
  var yelpScript = document.createElement('script'); 
  yelpScript.src = this.yelpURL; 
  yelpScript.type = 'text/javascript'; 
  
  var documentHead = document.getElementsByTagName('head').item(0); 
  documentHead.appendChild(yelpScript); 
  
  return false;   
 },
 displayReviews:function(data) {
  var divHTML = "";

  if(data.message.text == "OK") { 
   if (data.businesses.length == 0) { 
    divHTML = "";

    divHTML += "<div class=\"dotted clearfix\">";
    divHTML += " <div class=\"info\">";
    divHTML += "  <p><strong>No reviews were found, please check back later.</strong></p>";
    divHTML += " </div>";
    divHTML += "</div>";
    divHTML += "<div class=\"more clearfix\">";
    divHTML += " <span style=\"float: left;\"><img src=\"/images/yelp.png\" /></span>";
    divHTML += "</div>";
    
   } else {
    for(var i=0; i<data.businesses.length; i++) { 
     divHTML = "";
     
     business = data.businesses[i]; 
     
     divHTML += "<div class=\"dotted clearfix\">";
     divHTML += " <div class=\"info\">";
     divHTML += "  <p><strong><a href=\"" + business.url + "\" target=\"_blank\">" + business.name + "</a></strong></p>";
     
     if (business.categories.length > 0) {
      divHTML += "  <p>Category: <a href=\"" + business.categories[0].search_url + "\" target=\"_blank\">" + business.categories[0].name + "</a></p>"
     }
     
     divHTML += " </div>";
     divHTML += " <div class=\"starreview\">";
     divHTML += "  <img src=\"" + business.rating_img_url_small + "\"/>";
     divHTML += "  <p><a href=\"" + business.url + "\" target=\"_blank\">" + business.review_count + " reviews</a></p>";
     divHTML += " </div>";
     divHTML += "</div>";
     
     $('#' + this.container).append(divHTML);
    }
    
    divHTML = "<div class=\"more\">";
    divHTML += " <span style=\"float: left;\"><img src=\"/images/yelp.png\" /></span>";
    divHTML += " <a href=\"" + business.nearby_url + "\" target=\"_blank\">&raquo; View more</a>";
    divHTML += "</div>";
   }
  } else {
   divHTML = "";

   divHTML += "<div class=\"dotted clearfix\">";
   divHTML += " <div class=\"info\">";
   divHTML += "  <p><strong>No reviews were found at this time, please check back later.</strong></p>";
   divHTML += " </div>";
   divHTML += "</div>";
   divHTML += "<div class=\"more clearfix\">";
   divHTML += " <span style=\"float: left;\"><img src=\"/images/yelp.png\" /></span>";
   divHTML += "</div>";   
  }
  
  $('#' + this.container).append(divHTML);
 }
}
