/**
* BSkyB Community Discovery JS
* Gets and sets the Community Discovery DHTML
* Requires: Pluck SiteLife DAAPI
* About: Pluck Community functions
*/

var skyCommunityDiscovery = {};

//writes latest blogs components using DiscoverContentAction DAAPI request object
skyCommunityDiscovery.writeBlogComponent = function(responseBatch) {
	stringHTML = "";
	discContLength = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent.length;
	var halfOfComponents = parseInt(discContLength)/2;
	var halfComps = Math.ceil(halfOfComponents);
	var lastBlogItem = "";
	if(document.getElementById('home')) {
		stringHTML += "<div id=\"blogsContainerLeft\" class=\"blogsContainerLeft\">";
	}
	for(x=0; x<discContLength; x++) {
		discoveredContent = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent[x];
		if(discoveredContent.PostAuthor.IsBlocked != "True") {
			componentImage = discoveredContent.PostAuthor.AvatarPhotoUrl;
			title = discoveredContent.PostTitle;
			truncTitle = pluckUtils.truncateString(title, 68);
			blogAuthor = discoveredContent.PostAuthor.UserKey.Key;			
			displayName = discoveredContent.PostAuthor.DisplayName;
			siteOfOrigin = discoveredContent.SiteOfOrigin;
			date = skyCommunity.formatDateStamp(discoveredContent.PostDate);
			url = discoveredContent.Url;
			body = discoveredContent.PostBody;
			//check to see what type of blog post we have
			blogType = "public";
			if(url.indexOf('/public?') != -1) {
				blogType = "public";
			}
			if(url.indexOf('/profile.html?') != -1) {
				blogType = "persona";
			}
	
			pluckUtils.siteOfOrigin(siteOfOrigin);			
			profileDomain += blogAuthor;
			if(document.getElementById('home')) {
				if(x == (halfComps-1) || x == (discContLength-1)) {
					lastBlogItem = true;
				}
			} else {
				if(x == (discContLength-1)) {
					lastBlogItem = true;
				}
			}
			
			skyCommunityDiscovery.setBlogComponent(lastBlogItem);
			
			if(document.getElementById('home')) {
				lastBlogItem = false;
	
				if(x == (halfComps-1)) {
					stringHTML += "<\/div><div id=\"blogsContainerRight\" class=\"blogsContainerRight\">";
				}
			
				if(x == (discContLength-1)) {
					stringHTML += "<\/div>";
				}
			}
		}
	}
	return stringHTML;
};

//writes latest blogs components using DiscoverContentAction DAAPI request object
skyCommunityDiscovery.writeSmallBlogComponent = function(responseBatch) {
	stringHTML = "";
	discContLength = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent.length;
	var lastBlogItem = "";
	
	for(x=0; x<discContLength; x++) {
		discoveredContent = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent[x];
		if(discoveredContent.PostAuthor.IsBlocked != "True") {
			componentImage = discoveredContent.PostAuthor.AvatarPhotoUrl;
			title = discoveredContent.PostTitle;
			truncTitle = pluckUtils.truncateString(title, 68);
			blogAuthor = discoveredContent.PostAuthor.UserKey.Key;			
			displayName = discoveredContent.PostAuthor.DisplayName;
			siteOfOrigin = discoveredContent.SiteOfOrigin;
			date = skyCommunity.formatDateStamp(discoveredContent.PostDate);
			url = discoveredContent.Url;
			body = discoveredContent.PostBody;
			//check to see what type of blog post we have
			blogType = "public";
			if(url.indexOf('/public?') != -1) {
				blogType = "public";
			}
			if(url.indexOf('/profile.html?') != -1) {
				blogType = "persona";
			}
	
			pluckUtils.siteOfOrigin(siteOfOrigin);			
			profileDomain += blogAuthor;

			if(x == (discContLength-1)) {
				lastBlogItem = true;
			}
			
			skyCommunityDiscovery.setBlogComponent(lastBlogItem);
		}
	}
	return stringHTML;
};

//sets html for blogs component in community home page and blogs page
skyCommunityDiscovery.setBlogComponent = function(lastBlogItem) {
	if(lastBlogItem == true) {stringHTML += "<div class='componentItem' style='border-bottom:none;'><div class='componentImage'><a href='" + profileDomain + "'><img src='" + componentImage + "' alt='" + displayName + "' title='" + displayName + "' \/><\/a><\/div>";}
	else {stringHTML += "<div class='componentItem'><div class='componentImage'><a href='" + profileDomain + "'><img src='" + componentImage + "' alt='" + displayName + "' title='" + displayName + "' \/><\/a><\/div>";}
	stringHTML += "<div class='componentInner'><div class='title'><a href='" + url + "'>" + truncTitle + "<\/a><\/div>";
	stringHTML += "<div class='author'><a href='" + profileDomain + "'>" + displayName + "<\/a><\/div>";
	stringHTML += "<div class='date'>" + date + "<\/div>";
	if(blogType == "public") {
		if(document.getElementById('home')) {
			stringHTML += "<div class='siteName'>" + siteName + "<\/div><div class='linkContainer'><a class='readLink' href='" + url + "' title='Read Blog'>Read Blog<\/a><\/div>";
		} else {
			stringHTML += "<div class='siteName'>" + siteName + "<\/div>";
		}
	} else {
		if(document.getElementById('home')) {
			stringHTML += "<div class='linkContainer'><a class='readLink' href='" + url + "' title='Read Blog'>Read Blog<\/a><\/div>";
		}
	}
	if(!document.getElementById('home')) {
		stringHTML += "</div><div class='componentInnerRight'><div class='body'>" + pluckUtils.truncateString(body, 230) + "<\/div>";
		stringHTML += "<div class='linkContainer'><a class='readLink' href='" + url + "' title='Read Blog'>Read Blog<\/a><\/div>"
	}
	stringHTML += "<\/div><\/div>";
};

//writes latest photos component using DiscoverContentAction DAAPI request object
skyCommunityDiscovery.writePhotoComponent = function(responseBatch) {
	stringHTML = "";
	discContLength = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent.length;
	var halfOfComponents = parseInt(discContLength)/2;
	var halfComps = Math.ceil(halfOfComponents);
	if(discContLength != 0) {
		stringHTML += "<div class='imageComponentTop'>";
		for(x=0; x<discContLength; x++) {
			discoveredContent = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent[x];
			componentImage = discoveredContent.Image.Small;
			title = discoveredContent.Title;
			displayName = discoveredContent.Author.DisplayName;
			date = pluckUtils.formatDateStamp(discoveredContent.CreatedOn);
			url = discoveredContent.PhotoUrl;
			
			skyCommunityDiscovery.setPhotoComponent();
	
			if(x == (4)) {
				stringHTML += "<\/div><div class=\"imageComponentBottom\">";
			}
			if(x == (discContLength-1)) {
				stringHTML += "<\/div>";
			}
		}
	}	
	return stringHTML;
};

//sets html for photo component in community home page
skyCommunityDiscovery.setPhotoComponent = function() {
	stringHTML += "<div class='imageComponentContainer'><div class='componentImage' onmouseover='skyCommunityDiscovery.onPhotoImage(this);' onmouseout='skyCommunityDiscovery.offPhotoImage(this);'><a href='" + url + "'><img src='" + componentImage + "' alt='" + title + ", " + displayName + "' title='" + title + ", " + displayName + "' \/><\/a><\/div>";
	stringHTML += "<div class='imageTitle'><a href='" + url + "'>" + pluckUtils.truncateString(title, 30) + "<\/a><\/div>";
	stringHTML += "<div class='imageAuthor'>" + displayName + "<\/div><\/div>";
};
//onmouseover of photo image
skyCommunityDiscovery.onPhotoImage = function(ele) {
	ele.style.border = "10px solid #6D8FB7";
};
//onmouseout of photo image
skyCommunityDiscovery.offPhotoImage = function(ele) {
	ele.style.border = "10px solid #B0C6DC";
};

//writes latest personas component using DiscoverContentAction DAAPI request object
skyCommunityDiscovery.writePersonaComponent = function(responseBatch, isSingle) {
	stringHTML = "";
	discContLength = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent.length;
	var halfOfComponents = parseInt(discContLength)/2;
	var halfComps = Math.ceil(halfOfComponents);
	var lastPersonaItem = "";
	if(isSingle === false) {
		stringHTML += "<div id=\"membersContainerLeft\" class=\"membersContainerLeft\">";
	}
	for(x=0; x<discContLength; x++) {
		discoveredContent = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent[x];
		if(discoveredContent.IsBlocked != "True") {
			personaAvatar = discoveredContent.AvatarPhotoUrl;
			personaName = discoveredContent.DisplayName;
			personaAbout = discoveredContent.AboutMe;
			personaUrl = discoveredContent.PersonaUrl;
			
			if(isSingle === false) {			
				if(x == (halfComps-1) || x == (discContLength-1)) {
					lastPersonaItem = true;
				}
			} else {
				if(x == (discContLength-1)) {
					lastPersonaItem = true;
				}
			}
			
			skyCommunityDiscovery.setPersonaComponent(lastPersonaItem);
			
			if(isSingle === false) {
				lastPersonaItem = false;
	
				if(x == (halfComps-1)) {
					stringHTML += "<\/div><div id=\"membersContainerRight\" class=\"membersContainerRight\">";
				}
			
				if(x == (discContLength-1)) {
					stringHTML += "<\/div>";
				}
			}
		}
	}
	return stringHTML;
};

//sets html for persona (members) component in community home page and members page
skyCommunityDiscovery.setPersonaComponent = function(lastPersonaItem) {
	if(lastPersonaItem == true) {stringHTML += "<div class='personaComponent noBorder'>";}
	else {stringHTML += "<div class='personaComponent'>";}
	stringHTML += "<div class='personaAvatar'><a href='" + personaUrl + "' title='" + personaName + "'><img src='" + personaAvatar + "' alt='" + personaName + "' title='" + personaName +"' \/><\/a><\/div>";
	stringHTML += "<div class='personaInfo'><div class='personaName'><a href='" + personaUrl +"' title='" + personaName + "'>" + personaName + "<\/a><\/div>";
	stringHTML += "<div class='personaAbout'>" + pluckUtils.truncateString(personaAbout, 80) + "<\/div>";
	stringHTML += "<div class='linkContainer'><a class='readLink' title='See Full Profile' href='" + personaUrl + "'>See Full Profile<\/a><\/div><\/div>";
	stringHTML += "<\/div>";
};

//sets the recent discussions posts component
skyCommunityDiscovery.setRecentDiscussionsComponent = function(lastDiscussion) {
	stringHTML += "<div class='Summary_ForumsContainer'><div class='Summary_ForumsTitle'>";
	stringHTML += "<a href='" + discussionUrl + "'>" + discussionTitle + "<\/a><\/div>";
	if(lastDiscussion === true) {
		stringHTML += "<div id='Summary_Forums_In' class='Summary_ForumsIn_Last'>By: <a href='" + posterUrl + "'>" + poster + "<\/a> on " + pluckUtils.formatDateStamp(postDateTime) + "<\/div><\/div>";
	} else {
		stringHTML += "<div id='Summary_Forums_In' class='Summary_ForumsIn'>By: <a href='" + posterUrl + "'>" + poster + "<\/a> on " + pluckUtils.formatDateStamp(postDateTime) + "<\/div><\/div>";
	}
}
