
/* specific style based functions for populating data on only the index page of
 * the main site. */
//. 20091028 - bob

function localFetchHappening() {

	jQuery('#index-happening-now').html('<div class="loadingbar">Loading...</div>');

	jQuery.get(
		'/a/index/comments.api/' + getTime(),
		{ },
		function(json) {

			var html = new String;
			var odd = false;
			
			jQuery(json.data).each(function(){
				
				html +=
					'<li style="overflow:hidden;clear:both;"' + ((odd)?(' class="alt"'):('')) + '>'+
					'<img style="height:34px;width:34px;float:left;margin:3px 0px 4px 3px;" src="' + this.comment.avatarURL + '" />'+
					'<div class="post-link" style="float:right;width:205px;margin-bottom:4px;">'+
					'<a href="' + this.comment.profileURL + '" class="post">' + this.comment.username + '</a> '+
					'commented on '+
					'<a href="' + this.post.url + '" class="post">' + this.post.title + '</a><br />'+
					'</div>'+
					'</li>';
					
				odd = !odd;

				
				return;
			});
			
			jQuery('#index-happening-now').html('<ul class="post">' + html + '</ul>');

			return;
		},'json'
	);

	return;
}

function localFetchUsers() {

	jQuery.get(
		'/a/index/users.api/' + getTime(),
		{ },
		function(json) {

			var iter = 0;
			var html = new String;
/*
			jQuery(json.online).each(function(){
				html +=
					'<div class="user-icon" style="width:73px;">'+
					'<a href="' + this.profileURL + '">'+
					'<img src="' + this.avatarURL + '" alt="" style="width:65px;height:65px;" />'+
					'</a><br />'+
					'<a href="' + this.profileURL + '">' + bCommon.breakLineWrap(this.username) + '</a>' +
					'</div>';
			});
			jQuery('#index-user-online').html(html);
			jQuery('#index-user-online-link').show();
*/
			
			html = '';
			json.newest = json.newest.slice(0,8);
			jQuery(json.newest).each(function(){
				html +=
					'<div class="user-icon" style="width:73px;">'+
					'<a href="' + this.profileURL + '">'+
					'<img src="' + this.avatarURL + '" alt="" style="width:65px;height:65px;" />'+
					'</a><br />' +
					'<a href="' + this.profileURL + '">' + bCommon.breakLineWrap(this.username) + '</a>' +
					'</div>';
			});
			jQuery('#index-user-newest').html(html);

			html = '';
			jQuery(json.commenters).each(function(){
				html +=
					'<div class="user-icon" style="width:40px;">'+
					'<a href="' + this.profileURL + '">'+
					'<img src="' + this.avatarURL + '" alt="" style="width:32px;height:32px;" />'+
					'</a></div>';
			});
			jQuery('#index-user-commenters').html(html);

		},'json'
	);

	return;
}

function localFetchPosts() {

	jQuery.get(
		'/a/index/posts.api/' + getTime(),
		{ },
		function(json) {

			var html = new String;

			jQuery(json.newest).each(function(){
			
				html +=
					'<tr class="post-link">' +
					'<td><div class="user-icon" style="width:40px"><a href="' + this.url + '"><img src="' + this.avatarURL + '" alt="" style="width:32px;height:32px" /></a></div></td>'+
					'<td><a href="' + this.url + '">' + this.title + '</a><br />by ' + this.username + '</td>'+
					'</tr>';
			
				return;
			});
			jQuery('#index-post-newest').html(html);
			
			return;
		},'json'
	);

	return;
}

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

function localFetchRightNow() {
	jQuery.post(
		'/a/index/happening.api/' + getTime(),
		{ },
		function(obj) {
			jQuery('#index-right-now').empty();
			jQuery(obj.list).reverse().each(function(){ localRenderRightNow(this); });
		},'json'
	);
}

function localRenderRightNow(entry) {
	var eID = 'live-entry-' + entry.id;
	var contain = jQuery('#index-right-now');
	
	// stop if we already have this row in there.
	if(jQuery('#' + eID).length > 0) return;
	
	var html = '';
	html += '<div id="' + eID + '" class="live-entry">';
	html += '<a href="' + entry.link + '"><img src="' + entry.avatar + '" alt="" class="avatar" /></a>';
	html += '<div class="content">' + entry.content_rich + '</div>';
	html += '<div class="info">' + entry.timeago + '</div>';
	html += '</div>';
	
	contain.prepend(html);

	return;
}

function localFetchNewPosts() {
	jQuery.post(
		'/a/index/happening.api/' + getTime(),
		{ 'filter':'post' },
		function(obj) {
			jQuery('#index-posts-newest').empty();
			jQuery(obj.list).reverse().each(function(){ localRenderNewPosts(this); });
		},'json'
	);
}

function localRenderNewPosts(post) {
	var html = '';
	html += '<div class="live-entry">';
	html += '<a href="' + post.link + '"><img src="' + post.avatar + '" alt="" class="avatar" /></a>';
	html += '<div class="content">' + post.username + '<br /><a href="' + post.link + '">' + post.title + '</a></div>';
	html += '<div class="info">' + post.timeago + '</div>';
	html += '</div>';
	
	jQuery('#index-posts-newest').prepend(html);
}

jQuery(document).ready(function(){
	//. doing things for this page load.
	
//	localFetchHappening();
//	setInterval(localFetchHappening,60000);

	localFetchRightNow();
	setInterval(localFetchRightNow,30000);
	
	localFetchNewPosts();
	setInterval(localFetchNewPosts,30000);
	
	localFetchUsers();
//	localFetchPosts();
	
	return;
});

