/**
 * Lists blog entries from the specified JSON feed
 * by creating a new 'ul' element in the DOM.  Each
 * bullet is the title of one blog entry, and contains
 * a hyperlink to that entry's URL.
 *
 * @param {JSON} json is the JSON object pulled from the Blogger service.
 */
function listEntries(json) {	var monthnames	=	new Array();
	monthnames[1]	=	"Янв";
	monthnames[2]	=	"Фев";
	monthnames[3]	=	"Март";
	monthnames[4]	=	"Апр";
	monthnames[5]	=	"Май";
	monthnames[6]	=	"Июнь";
	monthnames[7]	=	"Июль";
	monthnames[8]	=	"Авг";
	monthnames[9]	=	"Сент";
	monthnames[10]	=	"Окт";
	monthnames[11]	=	"Нояб";
	monthnames[12]	=	"Дек";
	// Clear any information displayed under the "data" div.
	removeOldResults();
	var ul = document.createElement('ul');
	var xt = '';

//  for (var i = 0; i < json.feed.entry.length; i++) {	for (var i = 0; i < 5; i++) {		xt = '';		var entry = json.feed.entry[i];
		var alturl;

		for (var k = 0; k < entry.link.length; k++) {			if (entry.link[k].rel == 'alternate') {				alturl = entry.link[k].href;
				break;
			}
		}

		var li = document.createElement('li');
		var a = document.createElement('a');
		a.href = alturl;
		a.target = '_blank';

		var strDate = entry.published.$t;

		var cdyear	=	strDate.substring(0,4);
		var cdmonth	=	strDate.substring(5,7);
		var cdday	=	strDate.substring(8,10);

		xt = cdday + ' ' + monthnames[parseInt(cdmonth,10)];

		var txt = document.createTextNode(xt + ': ' + entry.title.$t);
		a.appendChild(txt);
		li.appendChild(a);
		ul.appendChild(li);
	}

	// Install the bullet list of blog posts.
	document.getElementById('data').appendChild(ul);
}

/**
 * Called when the user clicks the 'Search' button to
 * retrieve a blog's JSON feed.  Creates a new script
 * element in the DOM whose source is the JSON feed
 * 'query'.blogspot.com, and specifies that the callback
 * function is 'listEntries' (above).
 *
 * @param {String} query is the blog to be retrieved, specified
 *     as a prefix of ".blogspot.com".
 */
function search(query) {  // Delete any previous JSON script nodes.
  removeOldJSONScriptNodes();

  // Clear any old data to prepare to display the Loading... message.
  removeOldResults();
  // Show a "Loading..." indicator.
  var div = document.getElementById('data');
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('Loading...'));
  div.appendChild(p);
  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://' + query + '.blogspot.com/feeds/posts' +
                      '/default?alt=json-in-script&callback=listEntries&charset=windows-1251');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/**
 * Deletes any old script elements which have been created by previous calls
 * to search().
 */
function removeOldJSONScriptNodes() {	var jsonScript = document.getElementById('jsonScript');
	if (jsonScript) {		jsonScript.parentNode.removeChild(jsonScript);
	}
}

/**
 * Deletes pre-existing children of the data div from the page. The data div
 * may contain a "Loading..." message, or the results of a previous query.
 * This old data should be removed before displaying new data.
 */
function removeOldResults() {	var div = document.getElementById('data');
	if (div.firstChild) {		div.removeChild(div.firstChild);
	}
}
