/*	This file contains functions which return content of the most recent slott report blog
 *	To use: 
 *	-create an html element with id="blogtext" 
 *	-Use the function getJSON() to populate the element with text
 *
 *	Note: the default size of the id="blogtext" element is 7 lines with 28 characters per line
 */

var maxLines = 4; //number of lines in blog text area
var maxChars = 27; //number of characters per line

if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].attributes['id'].value == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

String.prototype.stripHTML = function() {
	return this.replace(/<.*?>/g, '');
}

/* Retrieves a JSON feed for theslottreport.blogspot.com */
function getJSON() {
  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://theslottreport.blogspot.com/feeds/posts/default?alt=json-in-script&callback=showBlog');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/* Retrieves a JSON feed for theslottreport.blogspot.com */
function searchJSON() {
  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://theslottreport.blogspot.com/feeds/posts/default?alt=json-in-script&callback=showBlogArray');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/* Retrieves a JSON feed for theslottreport.blogspot.com
 * Prints the JSON object to the screen (only in firefox)
 */
function debugJSON() {
  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://theslottreport.blogspot.com/feeds/posts/default?alt=json-in-script&callback=printObject');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/* Displays the content of the most recent slott report blog */
function showBlog(json) {
	var blogtext = json.feed.entry[0].content.$t;
	if (blogtext.indexOf('</style>') != -1) {
		blogtext = blogtext.substring(blogtext.indexOf('</style>') + 8)
	}
    var txt = truncateString(blogtext.stripHTML());
	
	/* Hack to un-autoupdate blogtext */
	if (txt == "The conclusion of tax season is right around the corner. Below are several investor-friendly tips around tax time from Ed Slott.You converted a traditional IRA to a Roth IRA during 2008, a ...") {
		txt = "You converted a traditional IRA to a Roth IRA during 2008, a year that saw equity prices rise significantly. You can do a Roth recharacterization -- a do-over in the IRA world.";
	}
	//alert(document.getElementById('blogtext'));
	document.getElementById('blogtext').appendChild(document.createTextNode(txt));
}

/* Truncates a string to the maximum length that will fit in the blog text area */
function truncateString(s) {
	var curLine=1,arrInd=0;
	var retStr="", lStr="";
	var strArray = s.split(" "); //Split the string into an array

	//While more words can fit, and we have words to add
	while (curLine <= maxLines && arrInd < strArray.length)
	{
		if ((lStr.length + strArray[arrInd].length) <= maxChars) { //Line is not at max char length
			lStr += strArray[arrInd] + " ";
			retStr += strArray[arrInd] + " ";
			arrInd++;
		} else { //Go to next line
			lStr = "";
			curLine++;
		}
	}
	return (retStr.substr(0,retStr.length - 5) + "...");
}

/* Displays a list of links to blogs containing search terms */
function showBlogArray(json) {
	var ul = document.createElement('ul');
	for (var i = 0; i < json.feed.entry.length; i++) {
		var alturl;
		for (var k = 0; k < json.feed.entry[i].link.length; k++) {
			if (json.feed.entry[i].link[k].rel == 'alternate') {
		        alturl = json.feed.entry[i].link[k].href;
				break;
		    }
		}
		
		var li = document.createElement('li');
		li.className="box";
		var blog_link = document.createElement('a');
		blog_link.href = alturl;
		blog_link.target = '_blank';
		blog_link.className = 'a2';
		var txt_content = json.feed.entry[i].content.$t.stripHTML() + " " + json.feed.entry[i].title.$t;
		var txt = document.createTextNode(json.feed.entry[i].title.$t);

		blog_link.appendChild(txt);
		li.appendChild(blog_link);

		if ((txt_content.toUpperCase().indexOf(search_terms.toUpperCase())) != -1){
			ul.appendChild(li);
		}
	}
	if (ext_found != 1) {
		document.getElementById('search_result_box').innerHTML = '';
		document.getElementById('height_div').innerHTML = '';
	}
	document.getElementById('blogtext').appendChild(ul);
}

/* Prints an object and its properties recursively
 */
function printObject(obj) {
	for (p in obj) {
		if ((typeof obj[p]) == "object") {
			document.write(p + " -> (object):<br>");
			printObjectInd(obj[p], 1);
		} else {
			document.write(p + " -> " + obj[p] + "<br>");
		}
	}
}

/* Prints an object and its properties recursively
 * Indents 
 */
function printObjectInd(obj, indent) {
	var i;
	for (p in obj) {
		for (i = 0; i < indent; i++) {
			document.write("...")
		}
		if ((typeof obj[p]) == "object") {
			document.write(p + " -> (object):<br>");
			printObjectInd(obj[p], (indent + 1));
		} else {
			document.write(p + " -> " + obj[p] + "<br>");
		}
	}
}