/* 
 *  Shoutbox controls
 *
 *  (c) 2007 Yieeha Ltd & Co KG
 *
 */

var ajax;
var lock = false;

function shoutboxLock() {
	if (lock) return false;
	return lock = true;
}
 	
function shoutboxUnlock() {
 	lock = false;
}
	
function shoutboxCreateComment(id, reverse) {
	if (!shoutboxLock()) return;
	ajax = new Ajax.Request('/comment/create',
	                        {asynchronous:true,
	                         parameters:Form.serialize(id+'_form'),
	                         onSuccess:function(t) {
	                         	emptyDiv = $(id+'_empty');
	                         	if(emptyDiv != null)
	                         		emptyDiv.hide();
	                         	if(reverse)
	                         		new Insertion.Top(id, t.responseText);
	                         	else
	                         		new Insertion.Bottom(id, t.responseText);
	                         	$(id+'_textarea').value='';
	                         	shoutboxUnlock();
	                         },
	                         onFailure:shoutboxFail});
}
	
function shoutboxEditCommentOpen(id) {
	Element.hide('comment_'+id+'_view');
	Element.show('comment_'+id+'_editor');
}
	
function shoutboxEditCommentCancel(id) {
	Element.hide('comment_'+id+'_editor');
	Element.show('comment_'+id+'_view');
	$('comment_'+id+'_textarea').value = $F('comment_'+id+'_oldtext');
}
	
function shoutboxEditCommentSave(id) {
	if (!shoutboxLock()) return;
	ajax = new Ajax.Request('/comment/update',
		                    {asynchronous:true,
		                     parameters:Form.serialize('comment_'+id+'_form'),
		                     onSuccess:function(t) {Element.replace('comment_'+id, t.responseText); shoutboxUnlock();},
		                     onFailure:shoutboxFail});
}

function shoutboxDeleteCommentAsk(id) {
	Element.hide('comment_'+id+'_controls');
	Element.show('comment_'+id+'_confirm');
}
	
function shoutboxDeleteCommentCancel(id) {
	Element.hide('comment_'+id+'_confirm');
	Element.show('comment_'+id+'_controls');
}
	
function shoutboxDeleteComment(id) {
	if (!shoutboxLock()) return;
	ajax = new Ajax.Request('/comment/delete',
	                        {asynchronous:true,
	                         parameters: {cid: id},
		                     onSuccess:function(t) {Element.remove($('comment_'+id)); shoutboxUnlock();},
		                     onFailure:shoutboxFail});
}
	
function shoutboxSpamComment(id) {
	if (!shoutboxLock()) return;
	ajax = new Ajax.Request('/comment/spam',
		                    {asynchronous:true,
		                     parameters:{cid: id},
		                     onSuccess:function(t) {Effect.Pulsate($('comment_'+id)); shoutboxUnlock();},
		                     onFailure:shoutboxFail});
}
	
function shoutboxFail() {
	alert('Das hat leider nicht geklappt!');
	shoutboxUnlock();
}
