/* 
**  Incorporate locally copied TimesPeople feed from nytimes.com
*/

if (!("console" in window)) {
    window.console = {};
}

var TimesPeople = {
	init: function() {
		TimesPeople.getFeed();
	},
	getFeed: function() {
		new Ajax.Request('../../data/timespeople.js',
		{
			method:'get',
			onSuccess: function(transport){
				console.log('success');
				var r = eval(transport.responseText) || false;
				if(r){
					TimesPeople.drawFeed(r);
				}
			},
			onFailure: function(){ 
			//	do nothing...
				$('timespeople').innerHTML = '';
			}
		});
	},
	drawFeed: function(r) {
		var html     = [];
		var column1  = [];
		var column2  = [];
		var fragment = [];

		var an = ['article'];
		
		for(var i=0; i<10; i++){

			hasThumb = (r[i]. object_thumbnail_url.length>36) ? true : false;
			anOrA    = r[i].object_type.include(an) ? ' an ' : ' a '

			fragment.push("<li>");
			if(hasThumb) {
				fragment.push("<span class='moreinfo' style='float:right'><a href='"+r[i].object_url+"' target='_blank'><img src='"+r[i].object_thumbnail_url+"' alt='"+r[i].object+"'/></a></span>");
			}

			fragment.push("michael "+r[i].verb + anOrA + r[i].object_type +": <a href='"+r[i].object_url+"' target='_blank'>"+r[i].object.truncate(50)+"</a>");
			fragment.push("<p>"+ r[i].object_desc +"</p>");
			fragment.push("</li>\n");
			
			if (i%2 == 0) {
				column2.push(fragment.join(''));
			} else {
				column1.push(fragment.join(''));
			}
			fragment = [];	//	clear
		}

		html.push("<h3>Latest from my TimesPeople Feed</h3>\n");
		html.push("<div id='aColumn' style='width: 50%; float:right'><ul>"+column1.join('')+"</ul>\n</div>\n");
		html.push("<div id='bColumn' style='width: 50%; '><ul>"+column2.join('')+"</ul>\n</div>\n");
        html.push("<div id='feedlinks'><a href='http://timespeople.nytimes.com/view/user/37963625/1/feed.js'>JSON</a> <a href='http://timespeople.nytimes.com/view/user/37963625/1/rss.xml'>XML</a></div>");
		$('timespeople').innerHTML = html.join('');
	}
}

TimesPeople.init();
