var c_fh;
var c_ah;
var c_ac;
var c_a;
var c_f;
var c_etest;
var c_preventAction;

function showCommentForm()
{
	killErrors();
	killLoading();

	c_ac = document.getElementById('add-comment');
	c_a = c_ac.firstChild;
	c_f = document.getElementById('comment-form');
	c_fh = c_f.offsetHeight;
	if (!c_ah) c_ah = c_a.offsetHeight;

	setOpacity(c_f, 0);
	c_f.style.height = '0';
	c_f.style.position = 'relative';
	c_f.style.left = '0';
	c_f.style.top = '0';
	c_f.style.display = 'block';

	var at = new OpacityTween(c_a, Tween.strongEaseOut, 100, 0, .5);
	at.start();

	var ft = new Tween(c_f.style, 'height', Tween.strongEaseInOut, 0, c_fh-c_ah, .5, 'px');
	ft.start();
	ft.onMotionStopped = function() {
		c_ac.style.display = 'none';
		c_f.style.display = 'block';
		c_f.style.height = c_fh + 'px';
		var fot = new OpacityTween(c_f, Tween.strongEaseOut, 0, 100, .5);
		fot.start();
		fot.onMotionStopped = function()
		{
			c_f.style.height = '';
		}
	}
	ft.onMotionChanged = function() {
		window.scroll(0,20000);
	}

	clearCommentField();
}

function hideCommentForm(suppressShowCommentButton)
{
	if (!c_preventAction)
	{
		c_fh = c_f.offsetHeight;

		fot = new OpacityTween(c_f, Tween.regularEaseIn, 100, 0, .4);
		fot.start();
		fot.onMotionStopped = function() {
			c_ac.style.display = 'block';
			c_f.style.height = (c_fh - c_ah) + 'px';

			if (!suppressShowCommentButton)
			{
				showCommentButton();
			}

			var ft = new Tween(c_f.style, 'height', Tween.strongEaseInOut, c_fh-c_ah, 0, .5, 'px');
			ft.start();
			ft.onMotionStopped = function()
			{
				c_f.style.position = 'absolute';
				c_f.style.left = '-10000px';
				c_f.style.top = '-10000px';
				c_f.style.height = '';
			}
		}

		clearCommentField();
	}
}

function showCommentButton()
{
	var aot = new OpacityTween(c_a, Tween.strongEaseIn, 0, 100, .5);
	aot.start();
}

function errorCheckForm()
{
	var a = document.getElementById('author');
	var e = document.getElementById('email');
	var c = document.getElementById('comment');

	if (a && trim(a.value) == '')
	{
		setError('Please enter your name.');
		setCommentErrorFormColor(a);
		return false;
	}

	if (e && trim(e.value) == '')
	{
		setError('Please enter your email address.');
		setCommentErrorFormColor(e);
		return false;
	}

	if (e && !validEmail(trim(e.value)))
	{
		setError('Please enter a <em>valid</em> email address.');
		setCommentErrorFormColor(e);
		return false;
	}

	if (c && trim(c.value) == '')
	{
		setError('Please enter a comment.');
		setCommentErrorFormColor(c);
		return false;
	}

	//clearErrors();
	return true;
}

function setCommentErrorFormColor(errorField)
{
	var a = document.getElementById('author');
	var e = document.getElementById('email');
	var c = document.getElementById('comment');

	all = [a, e, c];
	errorColor = 'fefcb9';
	normalColor = 'ffffff';

	for (i in all)
	{
		var currentBGColor = forceHexColor(all[i].style.backgroundColor);
		if (!currentBGColor) currentBGColor = normalColor;

		if (all[i] == errorField && currentBGColor != errorColor)
		{
			ct = new ColorTween(all[i].style, 'backgroundColor', Tween.regularEaseInOut, currentBGColor, errorColor, .4);
			ct.start();
		}
		else if (all[i] != errorField && currentBGColor != normalColor)
		{
			ct = new ColorTween(all[i].style, 'backgroundColor', Tween.regularEaseInOut, currentBGColor, normalColor, .2);
			ct.start();
		}
	}
}

function submitComment()
{
	showLoading();

	if (errorCheckForm() && !c_preventAction)
	{
		c_preventAction = true;
		
		f = document.getElementById('comment-form');
		queryString = 'comment_post_ID=' + f.comment_post_ID.value + '&comment_ids=' + f.comment_ids.value + '&comment=' + f.comment.value;
		if (f.author && f.email && f.url)
		{
			queryString += '&author=' + f.author.value + '&email=' + f.email.value + '&url=' + f.url.value;
		}
		myxr.connect('post', f.action, true);
		myxr.sendAndLoad(queryString, submitCommentComplete);
	}
}

function submitCommentComplete(myxr)
{
	c_preventAction = false;
	
	if (myxr.responseText)
	{
		jr = eval("(" + myxr.responseText + ")");
	
		if (jr.success)
		{
			f = document.getElementById('comment-form');
			f.comment_ids.value = jr.commentids;
    
			setTimeout('hideCommentForm(true)', 500);
			setTimeout('showNewComments(jr)', 1500);
			setTimeout('showCommentButton()', 2200);
		}
		else
		{
			setError(jr.message);
		}
	}
	else
	{
		setError('A problem occurred with the server. Please try again.');
	}
}

function showNewComments(jr)
{
	var l = document.getElementById('comment-list');
	if (jr.appendlist != '')
	{
		l.innerHTML += jr.appendlist;
	}
	else
	{
		l.innerHTML = jr.fulllist;
	}

	var nh = document.getElementById('comments');
	var nhot = new OpacityTween(nh, Tween.strongEaseInOut, 100, 0, .2);
	nhot.start();
	nhot.onMotionStopped = function()
	{
		nh.innerHTML = jr.commentsheader;
		var nhot2 = new OpacityTween(nh, Tween.strongEaseInOut, 0, 100, .2);
		nhot2.start();
	}

	// show the animation for "my comment"
	var cid = 'comment-' + jr.mycommentid;
	var c = document.getElementById(cid);
	var cc = c.getElementsByTagName('div')[0];
	var ch = cc.offsetHeight;
	
	c.style.height = '0';
	c.style.position = 'relative';
	c.style.top = '0';
	c.style.left = '0';
	cc.style.position = 'relative';
	cc.style.top = (-ch) + 'px';

	var ct = new Tween(c.style, 'height', Tween.strongEaseInOut, 0, ch, 1, 'px');
	ct.start();
	ct.onMotionChanged = function()
	{
		cc.style.top = (c.style.height.replace(/[^0-9]/g, '') - ch) + 'px';
		window.scroll(0,20000);
	}
	
	// tween the margin of the list down if necissary.
	// it may have been set to 0 by setCommentListMargin if there were no comments when the page loaded.
	var cl = document.getElementById('comment-list');
	if (!cl.style.marginBottom || cl.style.marginBottom.replace(/[^0-9]/g, '') == '0')
	{
		var clt = new Tween(cl.style, 'marginBottom', Tween.strongEaseInOut, 0, 20, 1, 'px');
		clt.start();
	}
}

function clearCommentField()
{
	document.getElementById('comment').value = '';
}

function setError(str)
{
	setTimeout('hideLoading()', 400);

	var exh = 12;
	var e = document.getElementById('form-errors');
	if (!c_etest)
	{
		c_etest = document.createElement('div');
		c_etest.setAttribute('id', 'form-error-test');
		e.parentNode.insertBefore(c_etest, e);
		c_etest.style.width			= '500px';
		c_etest.style.position		= 'absolute';
		c_etest.style.top			= '-10000px';
		c_etest.style.left			= '-10000px';
		c_etest.style.display		= 'block';
	}
	
	c_etest.innerHTML = str;
	eh = c_etest.offsetHeight;

	// error is already there
	if (e.innerHTML != '')
	{
		// fade out what was there
		var eot = new OpacityTween(e, Tween.strongEaseIn, 100, 0, .3);
		eot.start();
		eot.onMotionStopped = function()
		{
			e.innerHTML = str;
			// change height, THEN fade in
			if (e.offsetHeight != eh)
			{
				var et = new Tween(e.style, 'height', Tween.strongEaseInOut, e.offsetHeight, eh+exh, .2, 'px');
				et.start();
				et.onMotionStopped = function()
				{
					var eot2 = new OpacityTween(e, Tween.strongEaseIn, 0, 100, .3);
					eot2.start();
				}
			}
			// height was right, just fade in			
			else
			{
				var eot2 = new OpacityTween(e, Tween.strongEaseIn, 0, 100, .3);
				eot2.start();
			}
		}
	}

	// no error was there, add this one
	else
	{
		setOpacity(e, 0);
		e.style.position = 'relative';
		e.style.height = '0';
		e.style.top = '0';
		e.style.left = '0';
		e.style.display = 'block';

		e.innerHTML = str;

		var et = new Tween(e.style, 'height', Tween.strongEaseInOut, 0, eh+exh, .3, 'px');
		et.start();
		et.onMotionChanged = function()
		{
			window.scroll(0,20000);
		}
		et.onMotionStopped = function()
		{
			var eot = new OpacityTween(e, Tween.strongEaseIn, 0, 100, .3);
			eot.start();
		}
	}

}

function clearErrors()
{
	var e = document.getElementById('form-errors');
	if (e.innerHTML != '')
	{
		var eot = new OpacityTween(e, Tween.strongEaseIn, 100, 0, .3);
		eot.start();
		eot.onMotionStopped = function()
		{
			var et = new Tween(e.style, 'height', Tween.strongEaseInOut, eh, 0, .3, 'px');
			et.start();
			et.onMotionStopped = function()
			{
				e.innerHTML = '';
			}
		}
		return 600;
	}
	return 0;
}

function killErrors()
{
	var e = document.getElementById('form-errors');
	e.style.display = 'none';
	e.innerHTML = '';
	if (document.getElementById('author'))
		document.getElementById('author').style.backgroundColor = '#ffffff';
	if (document.getElementById('email'))
		document.getElementById('email').style.backgroundColor = '#ffffff';
	if (document.getElementById('comment'))
		document.getElementById('comment').style.backgroundColor = '#ffffff';
}

function showLoading()
{
	var l = document.getElementById('loading');
	setOpacity(l, 0);
	l.style.display = 'block';
	var lot = new OpacityTween(l, Tween.strongEaseIn, 0, 100, .2);
	lot.start();

	var s = document.getElementById('submit');
	var sot = new OpacityTween(s, Tween.strongEaseIn, 100, 50, .2);
	sot.start();
}

function hideLoading()
{
	var l = document.getElementById('loading');
	var lot = new OpacityTween(l, Tween.strongEaseIn, 100, 0, .2);
	lot.start();

	var s = document.getElementById('submit');
	var sot = new OpacityTween(s, Tween.strongEaseIn, 50, 100, .2);
	sot.start();
}

function killLoading()
{
	setOpacity(document.getElementById('loading'), 0)
	setOpacity(document.getElementById('submit'), 1);
}

function setCommentListMargin()
{
	var cl = document.getElementById('comment-list');
	// there are either no nodes, or there is one, but its just a text node (probably just whitespace)
	if (cl.childNodes.length == 0 || (cl.childNodes.length == 1 && cl.childNodes[0].nodeType == '3'))
	{
		cl.style.marginBottom = '0';
	}

}
