﻿// ----------------------------------------
// SHOW RECENT POSTS
//First Post with picture
// ----------------------------------------
// This functions takes a blogger-feed in JSON
// format and displays it.
//
// Version: 4.0
// Date:    2009.4.20
// Author:  lmo_0731@yahoo.com
// URL:    lm0.blogspot.com
// ----------------------------------------
function getImageWidth(image) {
	pic = new Image();
	pic.src=image;
	var w=pic.width;
	var h=pic.height;
	if(w>h) return 150;
	else return 110;
}
function getimg(strvar){
	var l=strvar.length;
	var str=strvar.substr(0,l-181);
	var S=""; l-=181;
	var d=str.indexOf('<img');
	if(d==-1) return S;
	var b=0;
	var k=1;
	while(d>b)
	{
		b=str.indexOf('src',b+1); if(b==-1) return S;
	}
	while(str.charAt(b)!='='&&b<l) b++; b++;
	while(str.charAt(b)==' '&&b<l) b++;
	var c=str.charAt(b); b++;
	while(str.charAt(b)!=c) 
	{
		S+=str.charAt(b);
		b++;
	}
	return S;
}
function showrecentposts(json) {
	var entry;
	var wpics=1;
	for (var i = 0; i < numposts; i++) {
		document.write("<div class='hr'></div>");
		var entry = json.feed.entry[i];
		var posttitle = entry.title.$t;
		var posturl;
		if (i == json.feed.entry.length) break;
		for (var k = 0; k < entry.link.length; k++) {
			if (entry.link[k].rel == 'alternate') {
				posturl = entry.link[k].href;
				break;
			}
		}
		var postalt=posttitle;
		if(posttitle.length>25) 
		{
			posttitle = posttitle.substring(0, 25);
			var quoteEndT = posttitle.lastIndexOf(" ");
			posttitle = posttitle.substring(0,quoteEndT);
			posttitle +="...";
		}
		posttitle = "<a title='" + postalt + "' href='" + posturl + "'a/>" + posttitle + "</a>";
		var readmorelink = "(унших)";
		readmorelink = "<a title='" + postalt + "' href='" + posturl + "'a/>" +readmorelink+ "</a>"; //readmorelink = readmorelink.link(posturl);
		var postdate = entry.published.$t;
		var cdyear = postdate.substring(0,4);
		var cdmonth = postdate.substring(5,7);
		var cdday = postdate.substring(8,10);
		var monthnames = new Array();
		monthnames[1] = "1/";
		monthnames[2] = "2/";
		monthnames[3] = "3/";
		monthnames[4] = "4/";
		monthnames[5] = "5/";
		monthnames[6] = "6/";
		monthnames[7] = "7/";
		monthnames[8] = "8/";
		monthnames[9] = "9/";
		monthnames[10] = "10/";
		monthnames[11] = "11/";
		monthnames[12] = "12/";
		if ("content" in entry) {
			var postcontent = entry.content.$t;}
		else
		if ("summary" in entry) {
			var postcontent = entry.summary.$t;}
		else var postcontent = "";
		var imgurl="";
		if(i<wpics) 
		{
			imgurl=getimg(postcontent);
			img='<img title="'+postalt+'" longdesc="' +imgurl+'" style="width: 110px; float: left" src="'+imgurl+'"/>'
			//img=img.link(imgurl);
		}
		var re = /<\S[^>]*>/g; 
		postcontent = postcontent.replace(re, " ");
		if (showpostdate == true) document.write( '<text class="date-header">' + monthnames[parseInt(cdmonth,10)] + '' + cdday +"</text>  ");
		document.write('<b>'+posttitle+'</b><br/>');
		if (showpostsummary == true) {
			if(i<wpics) document.write("<br/>");
			if(imgurl!=''&&i<wpics) document.write(img);
			if (postcontent.length < numchars) {
				document.write(postcontent);
			}
			else {
				var bigchar=260;
				if(imgurl=="") bigchar=460;
				if(i<wpics) postcontent = postcontent.substring(0, bigchar);
				else postcontent = postcontent.substring(0, numchars);
				var quoteEnd = postcontent.lastIndexOf(" ");
				postcontent = postcontent.substring(0,quoteEnd);
				
				document.write(postcontent + '...' + readmorelink);
			}
		}
	}
}
