/*****
 * BSkyB Daily Archives
 * Requires: Pluck SiteLife
 * Dependencies: pluckBlogArchive.js
 * About: sky daily specific styles and structural blog archive functionality
 *****/

var skydailyBlogArchive = {};

//sets the blog archive links HTML
skydailyBlogArchive.setArchiveLinksHTML = function(blogPostMonthYearCounts) {
	var blogArchive = document.getElementById("blogArchive");
	var strArchiveLinks = "<dt id='archiveHeader'>Archive<\/dt>";

	for (var i = 0; i < blogPostMonthYearCounts.length; i++) {
		var strArchiveLink = "<dd><a class='archiveLink' href='#'>" + pluckUtils.toMonth(blogPostMonthYearCounts[i].MonthId) + " (" + blogPostMonthYearCounts[i].BlogPostCount + ")" + "<\/a><\/dd>";
 		strArchiveLinks += strArchiveLink;
 	}
 	blogArchive.innerHTML = strArchiveLinks;
 	var blogArchiveLinks =  blogArchive.getElementsByTagName('a');
 	return blogArchiveLinks;
};

//hide the comments (if archive blogs clicked from blog post page)
skydailyBlogArchive.hideCommentsHTML = function() {
	//hides comments code, if present, so page looks like blog archive landing page
 	if(document.getElementById('commentContainer')) {
	 	document.getElementById('commentContainer').style.display = "none";
 	}
 	if(document.getElementById('commentForm')) {
 		document.getElementById('commentForm').style.display = "none";
	}
 	if(document.getElementById('noComments')) {
 		document.getElementById('noComments').style.display = "none";
	}
};

//hide the archive links as none present
skydailyBlogArchive.hideArchiveLinksHTML = function() {
	document.getElementById("blogArchive").style.display = "none";
	document.getElementById("blogArchive").style.visibility = "hidden";
};

//sets the site specific archive breadcrumb	
skydailyBlogArchive.setArchiveBreadcrumb = function (year, month){
	if(document.getElementById('pageDate')) {document.getElementById('pageDate').innerHTML = month + " " + year;}
	if(document.getElementById('blogArchiveTitle')) {document.getElementById('blogArchiveTitle').innerHTML = month + " " + year;}
	if(document.getElementById('postTitle')) {document.getElementById('postTitle').innerHTML = month + " " + year;}
	skydailyBlog.showBreadCrumb();
};

//sets the site specific page title	
skydailyBlogArchive.setPageTitle = function(month){
	var archiveMonthText = "Archive for " + month;
	var documentTitle = archiveMonthText+" | "+pluckUtils.SITE_TITLE;
	document.title = documentTitle;
};

//build archived blog posts
skydailyBlogArchive.buildArchiveBlogPosts = function(_page) {
	var postHtml = "";

	for (var i = 0; i < _page.BlogPosts.length; i++) {
	
		var post = _page.BlogPosts[i];		
		var postUrl = pluckBlog.splitPostUrl(post.Url);
		var firstPostClass = "";
	
		//used to set the first blog posts css class
	    if(i === 0) {
	      firstPostClass = ' firstPost';
	    }
	
		// Add the class set in the above statement
		postHtml += "<div class='post" + firstPostClass + "'>";
		postHtml += "<h4 id='blogPostTitle-"+ i +"'><a href='"+ postUrl +"'>"+ post.PostTitle +"</a></h4>";
		postHtml += "<p class='articleUpdate'><span class='blogPostDate'>"+ skydailyUtils.formatDateStamp(post.PostDate) +"</span> - Written by <span class='blogPostAuthor'>" + pluckProfile.linkedDisplayName(post.PostAuthor) + "</span></p>";
		// Reset the variables
		firstPostClass = '';
	
		responsePostBody = post.PostBody;
		pluckBlogPost.amendBlogBodyHtml(responsePostBody);
		
		// Truncate posts for listing pages
		var truncatedString = pluckUtils.truncateString(varResponsePostBody, 255);
		p = '<div class="articleText"><p>' + truncatedString + '</p></div>';
		postHtml += p;
		varResponsePostBody = "";
		
		//add in the more link
		postHtml += '<div class="more-link"><a href="' + postUrl + '">' + 'More<\/a><\/div>';
		
		//add in the comments link			
		postHtml += "<div class='commentsLink'><a title='Comments' href='"+ postUrl +"#comment'>Comments ("+ post.NumberOfComments +")<\/div>";
		postHtml += "<\/div>";
	}
	
	//if blog post page
	if(document.getElementById('blogPostBody')) {
	    document.getElementById('blogPostBody').id = 'posts';
	}
	document.getElementById('posts').innerHTML = postHtml;
};

//sets pagination of the archived blog posts
skydailyBlogArchive.postPagination = function(_blogPostPage, globalYear) {
	if(document.getElementById('paging')) {
		var globalMonth = _blogPostPage.MonthId;
		numberOfBlogPosts = parseInt(_blogPostPage.NumberOfBlogPosts);
		numberPerPage = parseInt(_blogPostPage.NumberPerPage);
		if(numberOfBlogPosts > numberPerPage) {
			var currentPosts = parseInt(_blogPostPage.OnPage);
		 	var lastPage = Math.ceil(_blogPostPage.NumberOfBlogPosts/_blogPostPage.NumberPerPage);
	 		var prevPost = (currentPosts > 1) ? currentPosts-1 : currentPosts;
	 		var nextPost = (currentPosts < lastPage) ? currentPosts+1 : currentPosts;
	 		document.getElementById('paging').innerHTML = '';
	 		var pageNumbers = '';
	
			if (lastPage != 1) {
				for (var i = 1; i <= lastPage; i++) {
					if (i == _blogPostPage.OnPage) {
						pageNumbers += '<strong>' + i + '</strong>';
					} else {
						pageNumbers += '<span class="pagingLinks"><a href="#" onclick=\"pluckBlogArchive.getBlogArchiveContent(arguments[0], ' + globalYear + ', ' + globalMonth  + ', ' + i + ');">' + i + '</a></span>';
					}
				}
			}
	 		var nextBlogPosts = (currentPosts < lastPage) ? '<span class="pagingLinks"><a class="next" href="#" onclick="pluckBlogArchive.getBlogArchiveContent(arguments[0], ' + globalYear + ', ' + globalMonth  + ', ' + nextPost + ');">Next</a> &gt;</span>' : '<span class="pagingLinks">Next &gt;</span>';
			var prevBlogPosts = (currentPosts > 1) ? '<span class="pagingLinks">&lt; <a class="prev" href="#" onclick=\"pluckBlogArchive.getBlogArchiveContent(arguments[0], ' + globalYear + ', ' + globalMonth  + ', ' + prevPost + ');\">Prev</a></span>' : '<span class="pagingLinks">&lt; Prev</span>';
			document.getElementById('paging').innerHTML = prevBlogPosts + pageNumbers + nextBlogPosts;
		} else {
			document.getElementById('paging').innerHTML = ' ';
		}
		return;
	}
};