/*
Dynamic Barefoot Feeds
*/
$(document).ready(function(){
	
	// Recipes Feed
	if($(".panel_recipe > .panel_copy").length>0){
		var wineName = "";		
		var $WINENAMEDIV = $(".wine_name");
		if($WINENAMEDIV.length>0){
			wineName = $WINENAMEDIV.html();
		}
		
		$(".panel_recipe > .panel_copy").html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/barefoot-recipe/list/all/" + wineName + "?callback=?",
			function(data){
			  $(".panel_recipe > .panel_copy").html("");
			  $.each(data.recipes, function(i,item){
			 /* var htmlString = "<div class=\"recipe_container\"><div class=\"left\">";
					htmlString += "<a href=\"" + item.link +"\">" + item.title +"</a><br/>";
					htmlString += "By " + item.member.username + "</div>";
					htmlString += "<div class=\"right\"><!--Rating: ?--></div></div>";*/						
				var htmlString = "<div class=\"recipe_container\"><div>";
					htmlString += "<a href=\"" + item.link +"\">" + item.title +"</a><br/>";
					htmlString += "By " + item.member.username + "</div>";
					htmlString += "</div>";
				if (i < 2){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";		 				
					htmlString += "<p><div style=\"text-align:center;\">";
					htmlString += "<a href=\"http://republic.barefootwine.com/recipes/\">&gt; more recipes</a></div></p>";
					
				}				  
				$(htmlString).appendTo(".panel_recipe > .panel_copy");
									
				if ( i == 2 ) return false; // only return top 3
			  });
			});
	}
	
	// Photos Feed
	var $PANEL_PHOTOS = $(".panel_photos > .panel_copy");
	if($PANEL_PHOTOS.length>0){
		$PANEL_PHOTOS.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/gallery/latest-submissions/?callback=?",
			function(data){
			  $PANEL_PHOTOS.html("");
			  $.each(data.photos, function(i,item){				
											
				    var htmlString = "<div class=\"photo_container\"><div class=\"left\">";
					htmlString += "<!-- PHOTO BORDER --><div class=\"photo_outter\"><div class=\"photo_inner\">";
					htmlString += "<a href=\"" + item.link +"\">" + "<img border=\"0\" class=\"block\" src=\"" + item.image.medium.src + "\"/>" +"</a></div></div></div>";
					htmlString += "<div class=\"right\"><a href=\"" +item.link + "\">" + item.gallery + "</a><br/>";						
					/*htmlString += "<b>??? Views</b><br/>";						
					htmlString += "Lorem ipsum dolor sit amet adipiscing elit. Vestibulum...";*/						
					htmlString += "</div></div>";						
					
                            
                            
				if (i < 2){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";		 				
					htmlString += "<p><div style=\"text-align:center;\">";
					htmlString += "<a href=\"http://republic.barefootwine.com/galleries/\">&gt; more photos</a></div></p>";
					
				}				  
				$(htmlString).appendTo($PANEL_PHOTOS);
									
				if ( i == 2 ) return false; // only return top 3
			  });
			});
	}

	// Testimonials Feed
	var $TESTIMONIALS = $(".panel_testimonials > .panel_copy");
	if($TESTIMONIALS.length>0){
		$TESTIMONIALS.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/barefoot-comments/?callback=?",
			function(data){
			  $TESTIMONIALS.html("");
			  $.each(data.comments, function(i,item){				
		
				    var htmlString = "<div class=\"copy\"><p>" + item.comment + "</p></div>";
					htmlString += "<div class=\"panel_title\"><a href=\""+item.member.link+"\">" + item.member.username + "</a></div>";
				                            
                            
				if (i < 0){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";		 				
				}				  
				
				htmlString += "<p><a href=\"http://republic.barefootwine.com/reviews/\">&gt; read more testimonials</a></p>";
				
				$(htmlString).appendTo($TESTIMONIALS);3
									
				if ( i == 0 ) return false; // only return top 1
			  });
			});
	}

	// Jen Wall Blog Feed
	var $JENBLOG = $(".panel_jenwall > .panel_copy");
	if($JENBLOG.length>0){
		$JENBLOG.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/blog/list/republic-blog/all/10//JenWall/?callback=?",
			function(data){
			  $JENBLOG.html("<div class=\"panel_title\">Winemaker<br/>Barefoot Cellars</div><div class=\"hr\"></div>");
			  $.each(data.posts, function(i,item){				                                        

					var htmlString = "";
				    htmlString += "";
					htmlString += "<div class=\"blog_entries\"><div class=\"left\">Blog Entry:</div>";
					htmlString += "<div class=\"right\">";
					htmlString += "<a href=\"" + item.link +"\">" + item.title +"</a><br/>";
					htmlString += "<p>By <a href=\"" + item.member.link +"\">" + item.member.username +"</a> on " + item.created +"</p>";
					htmlString += "</div></div>";											                            
                            
				if (i < 1){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";	
					htmlString += "<p><div style=\"text-align:center;\">";
					htmlString += "<a href=\"http://republic.barefootwine.com/blog/\">&gt; more blog posts</a></div></p>";
				}				  
				$(htmlString).appendTo($JENBLOG);
									
				if ( i == 1 ) return false; // only return top 2
			  });
			});
	}


	// Wine Comments Feed
	var $COMMENTS = $(".wine_comments_container");
	if($COMMENTS.length>0){
		var wineName = "";		
		var $WINENAMEDIV = $(".wine_name");
		if($WINENAMEDIV.length>0){
			wineName = $WINENAMEDIV.html();
			// remove comment when copying to CMS
		}
		$("<div id='loadingDiv' class='wine_comments'>Loading...</div>").appendTo($COMMENTS);
		$.getJSON("http://republic.barefootwine.com/json/review/list/" + wineName + "/?callback=?",
			function(data){
			  $("#loadingDiv").remove();
			  $.each(data.reviews, function(i,item){              		           

				var htmlString = "<div class=\"wine_comments\">";
					htmlString += "<div class=\"title\">\""+item.title+"\"</div>";
					htmlString += item.description + "<a href=\""+item.link+"\">more</a>";
					htmlString += "<p>Posted " + item.created + " by <a href=\"" + item.member.link + "\">" + item.member.username + "</a></p></div>";	
					
				if (i < 2){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";		 				
				}				  
				$(htmlString).appendTo($COMMENTS);
									
				if ( i == 2 ) return false; // only return top 3
			  });
			});
	}
	
	// Homepage Recipes Feed
	var $RECIPE_FEATURE = $("#random_feature3 .copy"); 
	if($RECIPE_FEATURE.length>0){
		$RECIPE_FEATURE.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/barefoot-recipe/list/all/?callback=?",
			function(data){
			  $RECIPE_FEATURE.html("");
			  $.each(data.recipes, function(i,item){
																							
				var htmlString = "<div class=\"feature_title\">";
					htmlString += "<a href=\"" + item.link +"\">" + item.title +"</a>";
					htmlString += "<br>By <a href=\"" + item.member.link + "\">" + item.member.username + "</a></div>";
					htmlString += "<br>";
					
					if(i == 1){
						htmlString += "<a href=\"http://republic.barefootwine.com/recipes\">&gt; more recipes</a>";
					}
				$(htmlString).appendTo($RECIPE_FEATURE);
				if ( i == 1 ) return false; // only return top 2
			  });
			});
	}
	
	// News Homepage Feed
	var $NEWS_FEATURE = $("#random_feature1 .copy"); 
	if($NEWS_FEATURE.length>0){
		$NEWS_FEATURE.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/blog/list/republic-blog/all/10/5/?callback=?",
			function(data){
			  $NEWS_FEATURE.html("");
			  $.each(data.posts, function(i,item){
					
				var htmlString = "<b>" + item.title + "</b> ";
					htmlString += "<a href=\"" + item.link +"\">&gt; read article</a>";
					htmlString += "<br><br>" ;
				
				if(i == 1){
					htmlString += "<a href=\"http://republic.barefootwine.com/blog/index/republic-blog/barefoot-news/\">&gt; read more news</a>";
				}
				
				$(htmlString).appendTo($NEWS_FEATURE);
				if ( i == 1 ) return false; // only return top 2
			  });
			});
	}
	
	// Testimonials Homepage Feed
	var $TESTIMONIALSHOME = $("#random_feature4 .copy"); 
	if($TESTIMONIALSHOME.length>0){
		$TESTIMONIALSHOME.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/barefoot-comments/?callback=?",
			function(data){
			  $TESTIMONIALSHOME.html("");
			  $.each(data.comments, function(i,item){				
		            var commentString = item.comment;
					if(commentString.length > 115){
						commentString = commentString.substr(0,115);
						commentString = commentString.replace(/\w+$/, '');
						commentString += "...";
					}
					
				    var htmlString = "<div class=\"feature_title\">" + commentString + "</div>";
					htmlString += "-" + item.member.username + "";
				
				htmlString += "<p><a href=\"http://republic.barefootwine.com/reviews/\">&gt; read more testimonials</a></p>";
				
				$(htmlString).appendTo($TESTIMONIALSHOME);
									
				if ( i == 0 ) return false; // only return top 1
			  });
			});
	}
	
	// Jen Wall's Main Blog Feed
	var $JENMAINBLOG = $(".jensblog");
	if($JENMAINBLOG.length>0){
		$JENMAINBLOG.html("Loading...");
		$.getJSON("http://republic.barefootwine.com/json/blog/list/republic-blog/all/10//JenWall/?callback=?",
			function(data){
			  /*$JENMAINBLOG.html("<div class=\"title\">lars</div><div class=\"hr\"></div>");*/
			  $JENMAINBLOG.html("")
			  $.each(data.posts, function(i,item){				                                        

					var htmlString = "";
				    htmlString += "";
					htmlString += "<div class=\"title\"><a href=\"" +  item.link + "\">" + item.title + "</a></div>";
					htmlString += "<div class=\"copy\">" + item.body + " <br/><a href=\"" + item.link + "\">read more...</a>";
					htmlString += "<p>Posted " + item.created + " by ";
					htmlString += "<a href=\"" + item.member.link + "\">" + item.member.username + "</a>";
					htmlString += " | <a href=\"" + item.link + "#comments\"> " + item.comments + " Comments</a>";
					if(item.tags != null){
					htmlString += "<br />Filed under: " + splitString(item.tags);
					}
					htmlString += "</p></div>";
			
				if (i < 1){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";		 				
				}				  
				$(htmlString).appendTo($JENMAINBLOG);
									
				if ( i == 1 ) return false; // only return top 2
			  });
			});
	}	
	
	// Barefooters / Photos Feed
	var $BAREFOOTERS_PHOTOS = $(".panel_barefooters > .panel_copy");
	if($BAREFOOTERS_PHOTOS.length>0){
		$BAREFOOTERS_PHOTOS.html("Loading...");
		//http://barefoot.dnsalias.com/json/member/list/0/1000/username/99/ 
		//$.getJSON("http://barefoot.dnsalias.com/json/member/list/?callback=?",
		$.getJSON("http://republic.barefootwine.com/json/member/list/0/1000/username/99/?callback=?",
			function(data){
			  $BAREFOOTERS_PHOTOS.html("");
			  $.each(data.members, function(i,item){				
											
				    var htmlString = "<div class=\"photo_container\"><div class=\"left\">";
					htmlString += "<!-- PHOTO BORDER --><div class=\"photo_outter\"><div class=\"photo_inner\">";
					htmlString += "<a href=\"" + item.link + "\">";
					htmlString += "<img src=\"" + item.photo.medium.src + "\" border=\"0\" class=\"block\" /></a>";
					
					htmlString += "</div></div>";
					htmlString += "<!-- END: PHOTO BORDER --></div>";
					htmlString += "<div class=\"right\">";
					htmlString += "<a href=\"" + item.link + "\">" + item.username + "</a><br />";
					htmlString += "<b>I Barefoot in</b><br />" + item.barefooter_region + "</div></div><!-- END: PHOTO -->";
					
                            
				if (i < 1){
					htmlString += "<div class=\"hr\"></div>";
				}else{
					htmlString += "<div class=\"clear\"></div>";
					htmlString += "<p><div style=\"text-align:center;\">";
					htmlString += "<a href=\"http://republic.barefootwine.com/barefooters/\">&gt; more barefooters</a></div></p>";
					
					
				}				  
				$(htmlString).appendTo($BAREFOOTERS_PHOTOS);
									
				if ( i == 1 ) return false; // only return top 3
			  });
			});
	}

	
	
});
		
function splitString(str){
		if(str !=""){
			var newStr = "";
			for (i=0;i<str.length;i++){
				newStr += str[i];
				if(i < str.length - 1){
					newStr += ", "	
				}
			}
		}
		return newStr;	
}