var lastIssueDate = new Array();
lastIssueDate['posts'] = null;
lastIssueDate['comments'] = null;

var firstRun = new Array();
firstRun['posts'] = true;
firstRun['comments'] = true;

// use for timestamping
var count = 1;

function updateLiveblog( type )
{
	if( !type ) type = 'posts';
	var parameters = new Array();
	if( type == 'comments' )
	{
		parameters.push('op=liveblogcomments');
		parameters.push('postId='+commentPostId);
	}
	else
	{
		parameters.push('op=liveblog');
		if( typeof postTag != 'undefined' && postTag != '' ) parameters.push('tagName='+postTag);
	}
	parameters.push('lastIssueDate='+ lastIssueDate[type]);
	
	parameters = parameters.join('&');
	
	var req = new Ajax.Request( '/index.php', {
		method: 'get',
		parameters: parameters,
		evalScripts: false,
		asynchronous: true,
		onSuccess: function(resp) {
			var jsonPosts = eval(resp.responseText);
			addItems(jsonPosts, type);
			}
		}
	);
	setTimeout( 'updateLiveblog(\''+ type +'\')', (refreshSecs[type] * 1000) );
}

function addItems(posts, type)
{
	if( !type ) type = 'posts';
	for( var i = posts.length; i > 0; i-- )
	{
		// give 'em a name
		var post = posts[i-1];
		
		if( type == 'comments' )
		{
			// create the new post div
			var postDiv = createCommentDiv( post );
		}
		else
		{
			// create the new post div
			var postDiv = createPostDiv( post );
		}
		
		// show it - script.aculo.us effect included
		$(placeholder[type]).insertBefore( postDiv, $(placeholder[type]).firstChild );
		if( !firstRun[type] ) new Effect.SlideDown(postDiv, {duration: 1});
		
		// store the last issued date
		lastIssueDate[type] = post.issued;
		
		// remove older posts from page
		if( $(placeholder[type]).childNodes.length > itemsOnPage[type] ) $(placeholder[type]).lastChild.remove(true);
	}
	
	firstRun[type] = false;
}

function createPostDiv( post )
{
	// create post wrapper div
	var postDiv = document.createElement('div');
	postDiv.className = 'postText';
	
	// post text placeholder, and content fillup
	var postTextDiv = document.createElement('div');
	postTextDiv.className = 'postText';
	/*
	// every item needs the timestamp!
	count++;
	if( count == 2 )
	{
		postTextDiv.innerHTML = '<h2>' + cleanDate( post.issued ) + '</h2>' + '<p>' + post.entry + '</p>';
		count = 0;
	}
	else
	{
		postTextDiv.innerHTML = '<p>' + post.entry + '</p>';
	}
	*/
	postTextDiv.innerHTML = '<h2>' + cleanDate( post.issued ) + '</h2>' + '<p><em>' + post.author.displayName + '</em>: ' + post.entry + '</p>';
	postDiv.appendChild(postTextDiv);
	
	return postDiv;
}

function createCommentDiv( post )
{
	// create comment wrapper div
	var postDiv = document.createElement('div');
	postDiv.className = 'comment';
	postDiv.id = post.commentId;
	
	var postHead = document.createElement('div');
	postHead.className = 'comment-head';
	postHead.innerHTML  = '<a title="Comments by '+ post.author +'" href="/commenter/'+ post.username +'/" class="CommenterImageLink">';
	postHead.innerHTML += '<img src="'+ post.avatar +'" alt="Image of '+ post.author +'" class="CommenterImage" />';
	postHead.innerHTML += '</a>';
	postHead.innerHTML += '<span class="date"><a class="date" href="#'+ post.commentId +'">'+ post.issuedFormatted +'</a></span><br />';
	postHead.innerHTML += '<a title="Comments by '+ post.author +'" href="/commenter/'+ post.username +'/">'+ post.author +'</a> said:';	
	postDiv.appendChild(postHead);
	
	// comment text placeholder, and content fillup
	var postTextDiv = document.createElement('div');
	postTextDiv.className = 'comment-body';
	postTextDiv.innerHTML = post.commentText;
	postDiv.appendChild(postTextDiv);
	
	return postDiv;
}

function cleanDate( tstamp )
{
	date = new Date( tstamp * 1000 );
	hours = date.getHours();
	if( hours > 12 )
	{
		hours = hours - 12;
	}
	mins = date.getMinutes();
	if( mins < 10 )
	{
		mins = '0' + mins;
	}
	return hours + ':' + mins; 
}