/***********************************************
** scripts for handling Pluck bogs via DAAPI
***********************************************/
/*
**  render a list of blogs based on an array
**  of ids.  The rendering will be broken into
**  featured blogs (group blogs) and community
**  blogs (public blogs.)  User blogs are not
**  supported at this time.
**
**  arBlogList:  an array of blog id's to show
**	uiId:  the id of the div in which the output
**		   should be placed
**  returns:  void
**
**  ASSUMPTIONS:
**		prototype.js is included prior to call
**		serverUrl is set prior to call
**		uiId is a valid div tag
**		blnByType is a boolean that determins if the list should be grouped or just displayed as a single list
*/
var blogListUiId = null;
var blogListByType = false;
var blogList = null;
function renderBlogsFromIdList(arBlogList, uiId, blnByType) {
	blogListUiId = uiId;		/* will use in callback function */
	blogListByType = blnByType	/* true to show the list grouped by blog type, false otherwise */
	blogList = arBlogList;
	/* build up a DAAPI request for all of the blogs in the arrary */
	requestBatch = new RequestBatch();
	for (var i=0; i<arBlogList.length; i++)
		requestBatch.AddToRequest(new BlogKey(arBlogList[i][0]));
	requestBatch.BeginRequest(serverUrl, renderBlogsCallback);
}
/* Same as renderBlogsFromIdList, but using a responseBatch that's already loaded */
function renderBlogsFromResponseBatch(responseBatch, arBlogList, uiId, blnByType) {
	blogListUiId = uiId;
	blogListByType = blnByType;
	blogList = arBlogList;
	renderBlogsCallback(responseBatch);
}
/* Callback handler for renderBlogsFromIdList */
function renderBlogsCallback(responseBatch) {
	var featuredHTML = "";
	var communityHTML = "";
	var response;
	var i;
	for (i=0; i<responseBatch.Responses.length; i++) {
		response = responseBatch.Responses[i];
		var blog = response.Blog;
		if (blog!=null) {
			if (blogListByType) {
				var blogType = getBlogType(blog.BlogKey.Key);
				if (blogType == "group")
					featuredHTML += renderBlogEntryForList(blog);
				else if (blogType == "public")
					communityHTML += renderBlogEntryForList(blog);
			} else {
				featuredHTML += renderBlogEntryForList(blog);
			}
		}
	}
	if (blogListByType) {
		if (featuredHTML != "")
			featuredHTML = "<div class='blogHeader'>Featured Blogs</div>\n" + featuredHTML;
		if (communityHTML != "")
			communityHTML = "<div class='blogHeader'>Community Blogs</div>\n" + communityHTML;
	}
	document.getElementById(blogListUiId).innerHTML = (featuredHTML + communityHTML);
}
function getBlogType(blogId) {
	var i;
	for (i=0; i<blogList.length; i++) {
		if (blogList[i][0] == blogId)
			return blogList[i][1].toLowerCase();
	}
	return false; 
}
/* Render a blog entry for display in a list */
function renderBlogEntryForList(blog) {
	var s = "<div class='blogEntry'>\n";
	s += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	s += "<tr>";
	s += "<td align=\"left\" valign=\"top\" style=\"padding-right: 9px;\">";
	s += "<a href='"+blog.BlogUrl+"'><img src='"+blog.AvatarUrl+"'></a>";
	s += "</td>\n";		/* end blogAvatar */
	s += "<td align=\"left\" valign=\"top\">";
	s += "<div class='blogTitle'><a href='"+blog.BlogUrl+"'>"+blog.Title+"</a></div>\n";
	s += "<div class='blogTagline'>"+blog.Tagline+"</div>\n";
	s += "</td>\n";		/* end blogAvatar */
	s += "</tr>\n";		/* end blogAvatar */
	s += "</table>\n";		/* end blogAvatar */
	s += "</div>\n";			/* end blogInfo */
	s += "\n";		/* end blogEntry */
	return s;
}

