var WMBLOGPOSTS = {
	settings: {
		target: '_blank',
		dateFormat: 'DD.MM.YYYY',
		maxPosts: 10
	},
	
	init: function (d) {
		var posts = [], post, i = 0, pp, row, ppfix = this.settings.PermalinkPrefix;
		for (i; i < d.rows.length; i++) {
			row = d.rows[i].value;
			// create the PermalinkPrefix for this post
			pp = ppfix.replace('%year%',row.pdate.substr(0,4)).replace('%monthnum%',row.pdate.substr(5,2)).replace('%day%',row.pdate.substr(8,2));
			// create the post
			post = {
				url: 'http://' + this.settings.domainName + pp + row.url,
				pdate: row.pdate,
				title: row.title
			}
			posts.push(post);
		}
		this.settings.posts = posts;
		this.render();
	},
	
	load: function () {
		var script= document.createElement('script');
		script.src = 'http://blogapi.webmart.de/posts/' + this.settings.id;
		script.type ="text/javascript";
		document.getElementsByTagName('head')[0].appendChild(script);
	},
	
	render: function () {
		var p = this.settings.posts, i = 0, o = '', formatteddate, postdiv = document.createElement('div');
		postdiv.id = 'blogposts_' + this.settings.id;
		for (i; i < Math.min(p.length,this.settings.maxPosts); i++) {
			formatteddate=this.settings.dateFormat.replace('YYYY',p[i].pdate.substr(0,4)).replace('MM',p[i].pdate.substr(5,2)).replace('DD',p[i].pdate.substr(8,2));
			o += '<div><a title="' + p[i].title + '" target="' + this.settings.target + '" class="blogpost" href="'
			+ p[i].url + '">' + p[i].title +  '</a>';
			if (formatteddate.length > 3) {
				o += ' <span>' + formatteddate + '</span>';
			}
			o += '</div>'; 
		};
		document.getElementById('WMBLOGPOSTS').innerHTML=o;
	}
};
