var comments_cur_page = 1;
var comments_num_pages = 1;
var comments_view = 'compact';
var comment_sending = false;
var comments_loggedin = false;

function comment()
{
	$('writecomment').hide();
	$('comment-editor').show();
	$('commenttext').focus();
	if (!ie6)
	{
		$('clickoff').style.height = $('body').getHeight() + 'px';
		//$('clickoff').style.height = document.viewport.getHeight() + 'px';
		$('clickoff').show().onclick = function ()
		{
			$('comment-editor').hide();
			$('writecomment').show();
			$(this).hide();
		}
	}
}
function submit_comment(post_id)
{
	$('comment_sending').show();
	if (comment_sending) return;
	comment_sending = true;
	
	new Ajax.Request(TEMPLATEPATH+'/post-comment.php', {
		parameters:{ comment: $F('commenttext'), comment_post_ID: post_id, author: $LF('author'), email: $LF('email') }, 
		onSuccess: function(transport) {
			if (transport.responseText.startsWith('success'))
			{
				if (comments_cur_page == comments_num_pages || comments_view == 'compact')
				{
					var newcomment = new Element('div');
					$('new-comments').appendChild(newcomment.hide());
	    			newcomment.update(transport.responseText.substring(7));
	    			new Effect.BlindDown(newcomment);
	    		}
	    		else
	    			page_comments(post_id, comments_num_pages);
    			
    			$('commenttext').value = '';
    			if ($('nocomments') && $('nocomments').visible()) new Effect.BlindUp('nocomments');
    			
				if ($('comment-errors').visible()) new Effect.BlindUp('comment-errors');
				
				if (!comments_loggedin) 
				{
					createCookie('comment_author_' + COOKIEHASH, $LF('author'), 365);
					createCookie('comment_author_email_' + COOKIEHASH, $LF('email'), 365);
				}
			}
			else
			{
				$('comment-errors-text').update(transport.responseText).setStyle({color:'#FF9A2E'}).morph('color:#fff');
				if (!$('comment-errors').visible()) new Effect.BlindDown('comment-errors');
			}
		},
		onFailure: function(transport) {
			$('comment-errors-text').update('There was an error while trying to communicate with the server.').setStyle({color:'#FF9A2E'}).morph('color:#fff');
			if (!$('comment-errors').visible()) new Effect.BlindDown('comment-errors');
		},
		onComplete: function(transport) {
			$('comment_sending').hide();
			comment_sending = false;
		}
	});
	return false;
}
function readmore_comments(post_id, num_more)
{
	new Ajax.Request(TEMPLATEPATH+'/get_comments.php', {
		method: 'get',
		parameters:{ p: post_id, mode: 'remaining' }, 
		onSuccess: function(transport) {
			comments_view = 'pages';
			var start_comments_char = 0;
			if (transport.responseText.startsWith('PAGED'))
			{
				start_comments_char = transport.responseText.indexOf(';');
				comments_num_pages = parseInt(transport.responseText.substring(6, start_comments_char));
				start_comments_char++;
			}
			$('comments').update(transport.responseText.substring(start_comments_char));
			new Effect.BlindUp('readmore-link');
			if (num_more > 5) new Effect.BlindUp('entry-rollup');
			new Effect.BlindDown('comments');
			new Effect.BlindDown('rolldown-spacer');
			new Effect.BlindDown('rolldown-button');
			if (comments_num_pages > 1)
			{
				build_comments_pager(post_id);
				new Effect.BlindDown('comments-pager');
				new Effect.BlindUp('lastcomment');
				new Effect.BlindUp('new-comments', { 
					afterFinish: function() {
						$('new-comments').update('').show();
					} 
				});
			}
		},
		onFailure: function(transport) {
			//$('submitprayererrtext').update('There was an error while trying to communicate with the server.');
			//if (!$('submitprayererr').visible()) new Effect.BlindDown('submitprayererr');
		},
		onComplete: function(transport) {
			//$('submitprayerload').hide();
			//$('submitprayersubmit').show();
		}
	});
	return false;
}
function hide_comments()
{
	if (!$('entry-rollup').visible()) new Effect.BlindDown('entry-rollup');
	if ($('comments')) new Effect.BlindUp('comments');
	new Effect.BlindDown('readmore-link');
	new Effect.BlindUp('rolldown-spacer');
	new Effect.BlindUp('rolldown-button');
	if ($('comments-pager').visible()) new Effect.BlindUp('comments-pager');
	if (!$('firstcomment').visible()) new Effect.BlindDown('firstcomment');
	if (!$('lastcomment').visible()) new Effect.BlindDown('lastcomment');
	return false;
}
function page_comments(post_id, page)
{
	if (comments_cur_page == parseInt(page)) return false;
	new Ajax.Request(TEMPLATEPATH+'/get_comments.php', {
		method: 'get',
		parameters:{ p: post_id, page: page }, 
		onSuccess: function(transport) {
			var start_comments_char = 0;
			if (transport.responseText.startsWith('PAGED'))
			{
				start_comments_char = transport.responseText.indexOf(';');
				var num_pages = parseInt(transport.responseText.substring(6, start_comments_char));
				start_comments_char++;
				if (num_pages != comments_num_pages)
				{
					comments_num_pages = num_pages;
					build_comments_pager(post_id);
				}
			}
			
			for(var i=1; i<=comments_num_pages; i++)
			{
				$('c_page_link_'+i).className = (i == parseInt(page) ? 'current' : '');
			}
		
			var newcomments = new Element('div', { style: 'display:none;' });
			newcomments.update(transport.responseText.substring(start_comments_char));
			
			if (parseInt(page) > comments_cur_page) $('comments').insert({after: newcomments});
			else $('comments').insert({before: newcomments});
			
			if ($('firstcomment').visible()) new Effect.BlindUp('firstcomment');
			new Effect.Parallel([
				new Effect.BlindUp('comments', { sync: true }),
				new Effect.BlindDown(newcomments, { sync: true })
			], {
			  	afterFinish: function () {
			  		$('comments').remove();
			  		newcomments.id = 'comments';
			  	}
			});
			
			if ($('new-comments').visible())
			{
				new Effect.BlindUp('new-comments', { 
					afterFinish: function() {
						$('new-comments').update('').show();
					} 
				});
			}
			
			comments_cur_page = parseInt(page);
		}
	});
	return false;
}
function build_comments_pager(post_id)
{
	$('comments-pager-links').update('');
	for(var i=0; i<comments_num_pages; i++)
	{
		var pagelink = new Element('a', { href: '#', id: 'c_page_link_'+(i+1) });
		pagelink.innerHTML = (i+1);
		if (i==0) pagelink.className = 'current';
		pagelink.onclick = function ()
		{
			return page_comments(post_id, this.innerHTML);
		};
		$('comments-pager-links').appendChild(pagelink);
	}
}