// JavaScript Document

// updates the page content with the selected blog entry
function fnSetBlogEntry (nx)
{
	fnBuildBlogNav(nx, false);
	
	var txtFile = new XMLHttpRequest();
	var allText = "";
	
	txtFile.open("GET", "/includes/blog/" + jsEntries[nx].filename, true);
	
	txtFile.onreadystatechange = function() {
		if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
			if (txtFile.status === 200) {  // Makes sure it's found the file.
		  		allText = txtFile.responseText;
					
				allText = "<div align='center'>" + 
							"<div align='left' style='width:95%;'>" + fnBuildNextPrevLinks(nx) +
								"<div class='blogTitle'>" + jsEntries[nx].title + "<span class='blogDate'>" + jsEntries[nx].longdate + "</span></div>" +
								"<div class='blogCategory'><b>Category: </b>" + 
									"<a style='cursor:pointer; color:#003366;' " +
										"onclick='javascript:fnSetCategoryContent(fnGetCategoryWithID(" + jsEntries[nx].categoryID + "));'>" +
										jsEntries[nx].category + "</a>" +
								"</div>" +
								allText + fnGetToTopLink() + fnBuildNextPrevLinks(nx) +
							"</div>" +
						"</div>";
				
				document.getElementById('blog').innerHTML = allText;//txtFile.responseText;
		  		//lines = txtFile.responseText.split("\n"); // Will separate each line into an array
			}
	  	}
	}			
			
	txtFile.send(null);
	/*var innerHTML = "<iframe id='myiframe' width='100%' " +
		"onload='if (window.parent && window.parent.autoIframe) {window.parent.autoIframe(\"myiframe\");}' " +
		"scrolling='no' style='border:0px;' src='includes/blog/" + filename + "'></iframe>";*/
}

// Builds the navigation at the top of the blog
function fnBuildBlogNav(nx, bypage)
{
	var html = "Posts:";
	var entriesPerPage = 5;
	
	if (jsEntries)
	{
		if (!bypage)
			curEntryInx = nx;
		
		var arLen = jsEntries.length;
		var numPages = Math.round((arLen / entriesPerPage) + .5); //roundup
		var linkStyle = "cursor:pointer; color:#003366; text-decoration:underline; font-size:10px;"
		
		if (bypage)
			curEntryPage = nx;
		else if (nx != -1)
			curEntryPage = numPages - (Math.floor(nx / entriesPerPage));
			//curEntryPage = Math.floor(nx / entriesPerPage) + 1;
				
		//first build the current page
		//var firstNxOnPage = (curEntryPage * entriesPerPage) - entriesPerPage;
		//for (var i=firstNxOnPage; i<arLen && i<(entriesPerPage*curEntryPage); i++)
		var firstNxOnPage = entriesPerPage * (numPages - curEntryPage);
		for (var i=firstNxOnPage; i<arLen && i<(firstNxOnPage + entriesPerPage); i++)
		{
			if (curEntryInx == i)
				html += "<div class='pageNavItemPassive'>" + jsEntries[i].shortdate + " " + jsEntries[i].title + "</div>";
			else			
				html += "<a class='pageNavItem' style='cursor:pointer' onclick='javascript:fnSetBlogEntry(" + jsEntries[i].inx + ");'>" +
					jsEntries[i].shortdate + " " + jsEntries[i].title + "</a>";
		}
		
		//build the page links
		html += "<div style='padding-top:8px; font-size:10px; color:#333333' align='center' width='100%'>";
		
		if (curEntryPage == 1)
			html += "&lt;&lt; [&larr; older]";
		else
		{
			var prevPage = curEntryPage-1;
			html += "<a style='" + linkStyle + "' onclick='javascript:fnBuildBlogNav(1, true);' title='oldest'>&lt;&lt;</a> " +
					"<a style='" + linkStyle + "' onclick='javascript:fnBuildBlogNav(" + prevPage + ", true);' title='next oldest posts'>[&larr; older]</a>";
		}
		
		html += " " + curEntryPage + " of " + numPages + " ";
		
		if (curEntryPage == numPages)
			html += "[newer &rarr;] &gt;&gt;";
		else
		{
			var nextPage = curEntryPage+1;
			html += "<a style='" + linkStyle + "' onclick='javascript:fnBuildBlogNav(" + nextPage + ", true);' title='next newest posts'>[newer &rarr;]</a> " +
					"<a style='" + linkStyle + "' onclick='javascript:fnBuildBlogNav(" + numPages + ", true);' title='newest'>&gt;&gt;</a>";
		}
		
		html += "</div>";
	}
	else
	{
		alert ("no entries!");
	}	
	document.getElementById('blogNav').innerHTML = html;
	
	//Next, call routine to build category nave
	if (nx != -1 && !bypage)
		fnBuildCategoryNav(-1);
}

// Returns a html string for the selected blog entry's footer
function fnBuildNextPrevLinks (nx)
{
	var html = "<div style='padding-top:10px;'>" +
		"<div style='width:100%; padding-bottom:6px; padding-top:2px; border:1px solid #006699; border-left:none; border-right:none; height:22px; font-size:10px;'>" +
		"<div style='width:100%; font-size:9px; font-family:font-family:Sylfaen, Georgia, Times, serif;' align='center'>recent entry nav</div>";

	var linkStyle = "cursor:pointer; color:#003366;"
	
	if (nx != (jsEntries.length - 1))
	{
		//previous entry link
		var prevNx = nx+1;
		html += "<div style='width:45%; float:left;' align='left'>" +
			"<a style='" + linkStyle + "' onclick='javascript:fnSetBlogEntry(" + prevNx + ");' " +
				"title='" + jsEntries[prevNx].shortdate + " " + jsEntries[prevNx].title + "'>" +
				"[&larr;Prev] " + jsEntries[prevNx].shortdate + " " + jsEntries[prevNx].title.substring(0,15) + "...</a>" +
		"</div>"
	}
		
	if (nx > 0)
	{
		//next entry link
		var nextNx = nx-1;
		html += "<div style='width:45%; float:right;' align='right'>" +
			"<a style='" + linkStyle + "' onclick='javascript:fnSetBlogEntry(" + nextNx + ");' " +
				"title='" + jsEntries[nextNx].shortdate + " " + jsEntries[nextNx].title + "'>" + 
				jsEntries[nextNx].shortdate + " " + jsEntries[nextNx].title.substring(0,15) + "... [Next&rarr;]</a>" +
		"</div>"
	}
	
	return html + "</div></div>";
}

// Loops through our entries in attempt to return the index of the entry with the passed-in ID
function fnGetEntryWithID(id)
{
	if (!jsEntries)
		return -1;
	
	var arrLength = jsEntries.length;
	
	for (var i=0; i<(arrLength-1); i++)
	{
		if (jsEntries[i].id == id)
			return i;
	}
	
	return -1;
}
function fnGetCategoryWithID(id)
{
	if (!jsCategories)
		return -1;
		
	var arrLen = jsCategories.length;
	
	for (var i=0; i<(arrLen-1); i++)
	{
		if (jsCategories[i].id == id)
			return i;
	}
	
	return -1;
}

// Builds the category navigation at the top of the blog
function fnBuildCategoryNav(nx)
{
	var html = "Categories:";
	
	if (jsCategories)
	{
		var arLen = jsCategories.length;
		var linkStyle = "cursor:pointer; color:#003366; text-decoration:underline; font-size:10px;"
		
		//build the list
		for (var i=0; i<arLen; i++)
		{
			if (nx == i)
				html += "<div class='pageNavItemPassive'>" + jsCategories[i].name + "</div>";
			else
				html += "<a class='pageNavItem' style='cursor:pointer' onclick='javascript:fnSetCategoryContent(" + jsCategories[i].inx + ");'>" +
					jsCategories[i].name + "</a>";
		}
	}
	else
	{
		html += "<div>no categories</div>";
	}	
	document.getElementById('categoryNav').innerHTML = html;
}

// Loads entry titles under the selected category
function fnSetCategoryContent(nx)
{
	fnBuildCategoryNav(nx);
	fnBuildBlogNav(-1, false);
	
	var html = "<div style='font-size:16px; font-family:Sylfaen, Georgia, Times, serif'>" +
				"<div style='padding:14px; padding-bottom:0px; padding-top:8px;'>Query Result";
	
	if (!jsCategories[nx])
		html += "</div><div>Category was not found, or categories are not loaded.</div>";
	else if (!jsEntries)
		html += "</div><div>Blog entries were not found, or are not loaded.</div>";
	else {
		html += " where category is <b>" + jsCategories[nx].name + "</b>:</div>";
		
		var categoryID = jsCategories[nx].id
		var arLen = jsEntries.length;
		var count = 0;
		var resultsHtml = "";
		
		//loop through blog entries, finding any within the passed-in category
		for (var i=0; i<arLen; i++)
		{
			if (jsEntries[i].categoryID == categoryID)
			{
				resultsHtml += "<div style='padding:2px; padding-left:14px;'>&rarr; " +
					"<a style='cursor:pointer; color:#003366;' onclick='javascript:fnSetBlogEntry(" + i + ");'>" +
						jsEntries[i].shortdate + " " + jsEntries[i].title + "</a></div>";
				
				count += 1;
			}
		}
		html += "<div style='padding-bottom:10px; padding-left:30px; font-size:14px;'>" + count + " result(s)</div>";
		html += resultsHtml;
	}
	document.getElementById('blog').innerHTML = html + "</div>";
}