YAHOO.util.Event.onDOMReady(loadShoutBox);

function loadShoutBox() {
	document.getElementById('shout_form').onsubmit = function() {
		var message = document.getElementById('shout_message').value;
		
		if (message.length < 3) {
			alert("Your shout must be at least 3 characters long.");
			return false;
		}
		if (message.length > 140) {
			alert("Your shout can only be up to 140 characters long.");
			return false;
		}
		
		document.getElementById('shout_message').disabled = true;
		
		var sUrl = '/forums/dn_shout.php?json=1&do=shout&message=' + encodeURI(message);
		YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:shoutSuccess, failure:shoutFail});
		
		return false;
	}
	
	document.getElementById('dn_shout_refresh').onclick = refreshShoutBox;
}

function refreshShoutBox() {
	var sUrl = '/forums/dn_shout.php?json=1&do=list';
	document.getElementById('dn_shout_rfrsh_img').style.display = 'none';
	document.getElementById('dn_shout_rfrsh_msg').style.display = 'block';
	YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:shoutListSuccess, failure:shoutListFail});
	return false;
}

function shoutListSuccess(o) {
	document.getElementById('dn_shout_rfrsh_msg').style.display = 'none';
	document.getElementById('dn_shout_rfrsh_img').style.display = 'block';
	try {
		var result = YAHOO.lang.JSON.parse(o.responseText);
	}
	catch (e) {
		alert("Unexpected problem getting the shouts.");
		return;
	}
	
	if (result.error) {
		var msg = 'Unexpected problem getting the shouts.';
		switch (result.error) {
		}
		alert(msg);
	}
	else if (result.success)
	{
		document.getElementById('dn_shout_posts').innerHTML = result.success;
		dnShoutScroll();
	}
	else
	{
		alert('Something is seriously wrong with the shout box, I think.');
	}
}

function shoutListFail(o) {
	alert("Ran into a problem getting the shouts.");
	document.getElementById('dn_shout_rfrsh_msg').style.display = 'none';
	document.getElementById('dn_shout_rfrsh_img').style.display = 'block';
}

function shoutSuccess(o) {
	try {
		var result = YAHOO.lang.JSON.parse(o.responseText);
	}
	catch (e) {
		alert("Unexpected problem trying to shout.");
		return;
	}
	
	if (result.error) {
		var msg = 'Unexpected problem trying to shout.';
		switch (result.error) {
		case 'too_long':
			msg = 'Your shout can only be up to 140 characters long.';
			break;
		case 'too_short':
			msg = 'Your shout must be at least 3 characters long.';
			break;
		case 'no_perm':
			msg = 'Make sure you\'re logged in.';
			break;
		case 'same_post':
			msg = 'You just said that!';
			break;
		case 'stop_spam':
			msg = 'Stop spamming. Give somebody else a chance to talk.';
			break;
		}
		alert(msg);
	}
	else if (result.success)
	{
		document.getElementById('dn_shout_posts').innerHTML = result.success;
		dnShoutScroll();
		document.getElementById('shout_message').value = '';
		document.getElementById('shout_message').disabled = false;
	}
	else
	{
		alert('Something is seriously wrong with the shout box, I think.');
	}
}

function shoutFail(o) {
	alert("Ran into a problem shouting.");
	document.getElementById('shout_message').disabled = false;
}

function dnShoutScroll() {
	document.getElementById('dn_shout_posts').scrollTop = document.getElementById('dn_shout_posts').scrollHeight;
}
