var shoutbox_busy = false;
var chat_l_loading='Loading... <img src="/images/loading1.gif" alt="" class="icon_inline">';
var chat_l_sending='Sending... <img src="/images/loading1.gif" alt="" class="icon_inline">';
var chat_l_busy='Busy... try again... <img src="/images/loading1.gif" alt="" class="icon_inline">';
var chat_l_inactive='Inactive. (<a href="javascript:enableShoutbox();">Enable shoutbox</a>)';
var busycount = 0;
var inactive = 0;
var shout_disabled = false;

function enableShoutbox(){
	var shoutbox_status = $('shoutbox_status');
	shout_disabled = false;
	inactive = 0;
	shoutbox_status.innerHTML = '';
	getShoutboxPing();
}

function getShoutboxPing(){
	var shoutbox_status = $('shoutbox_status');

	if ( inactive > 225 ) {
		shout_disabled = true;
		shoutbox_status.innerHTML = chat_l_inactive;
	}

	if ( shout_disabled == false ) {
			var req = new Request.HTML({url:absPath+'ajax/shoutbox/ping/'+lastShoutId+'.htmla', 
			onSuccess: function(html, elms, htmltext) {
				var ajax_split = htmltext.split('<|#|#|>');
				if ( ajax_split[0] && ajax_split[1] ) {
					lastShoutId = ajax_split[0];
					$('shoutbox').innerHTML = ajax_split[1];
				}
				setTimeout("getShoutboxPing()", 3000);
			}
			}).send();
			inactive = parseInt(inactive+1);
	}
}

function sendMsg(){
	var shoutbox_status = $('shoutbox_status');
	var msg = $('bericht').value;

		if ( msg ) {

			var req = new Request.HTML({url:absPath+'ajax/shoutbox/postmsg.htmla', 
			onSuccess: function(html, elms, htmltext) {
				var ajax_split = htmltext.split('<|#|#|>');
				if ( ajax_split[0] && ajax_split[1] ) {
					lastShoutId = ajax_split[0];
					$('shoutbox').innerHTML = ajax_split[1];
				}
			}
			}).post({'msg':msg});

			document.getElementById('bericht').value = '';

		}
	return false;
}
/*
function setShoutboxPing(){
	var msgdiv = document.getElementById('shoutbox');
	var shoutbox_status = document.getElementById('shoutbox_status');
	
	if ( ajax.response ) {
	
		var ajax_split = ajax.response.split('<|#|#|>');
		if ( ajax_split[0] && ajax_split[1] ) {
			lastShoutId = ajax_split[0];
			msgdiv.innerHTML = ajax_split[1];
		}
	
	}
	shoutbox_busy = false;

	if ( shoutbox_status.innerHTML != chat_l_busy ) {
		shoutbox_status.innerHTML = '';
	}

	setTimeout("getShoutboxPing()", 4000);

}

function getShoutboxPing(){
	var shoutbox_status = document.getElementById('shoutbox_status');

	if ( inactive > 225 ) {
		shout_disabled = true;
		shoutbox_status.innerHTML = chat_l_inactive;
	}

	if ( shout_disabled == false ) {

		if ( busycount > 2 ) {
			busycount = 0;
			shoutbox_busy = false;
			shoutbox_status.innerHTML = '';
		}
	
		if ( shoutbox_busy ) {
			busycount=parseInt(busycount+1);
		} else {
	
			if ( shoutbox_status.innerHTML != chat_l_busy ) {
				shoutbox_status.innerHTML = chat_l_loading;
			}
		
			ajax.requestFile = absPath+'ajax/shoutbox/ping/'+lastShoutId+'.htmla';
			ajax.onCompletion = setShoutboxPing;
			ajax.runAJAX();
			shoutbox_busy = true;
		}
		inactive = parseInt(inactive+1);
	
	}
}

function sendMsg(){
	var shoutbox_status = document.getElementById('shoutbox_status');
	var msg = document.getElementById('bericht').value;
	if ( shoutbox_busy ) {
		shoutbox_status.innerHTML = chat_l_busy;
	} else {
		if ( msg ) {
			inactive = 0;
			shoutbox_status.innerHTML = chat_l_sending;
			ajax.setVar("msg", msg);
			ajax.requestFile = absPath+'ajax/shoutbox/postmsg.htmla';
			ajax.method = 'POST';
			ajax.runAJAX();
			document.getElementById('bericht').value = '';
		}
	}
	return false;
}
*/
