var IN_PROCESS         = false;
var AUTO_REFRESH_TIMER = false;
var MANUAL_REFRESH     = false;

function chatboxKeyWrap(e)
{	
	e = e || window.event;
	
	if( ( e.keyCode? e.keyCode : e.charCode ) == 13 )
	{
		if( e.ctrlKey )
		{
			doInsert("\n", '', this );
		}
		else
		{
			submitContent(this);
		}
		
		return false;
	}
}

function submitContent(formObj)
{
	if( IN_PROCESS )
	{
		formObj.disable = true;
		
		setTimeout(function(){ submitContent(formObj); }, 30000 );
		
		return false;
	}
	
	formObj.disable = false;
	
	IN_PROCESS = 1;
	
	formObj.value = trim(formObj.value);
	
	if( formObj.value == '' )
	{
		return '';
	}
	
	var post_data = { content : formObj.value };
	
	formObj.value = '';
	
	var xmlobj = new ajax_request();

	var do_request_function = function()
	{
		if( ! xmlobj.readystate_ready_and_ok() )
		{
			return false;
		}
		
		IN_PROCESS = false;

		if( xmlobj.xmlhttp.responseText.substr(0,3) == 'OK:' )
		{
			my_getbyid('chatContentWrapper').innerHTML = xmlobj.xmlhttp.responseText.substr(3);
			
			execute_js(xmlobj.xmlhttp.responseText.substr(3));
			
			checkHeight();
			
			return true;
		}
		else
		{
			alert(xmlobj.xmlhttp.responseText);
		}

		return false;
	}

	xmlobj.onreadystatechange(do_request_function);

	xmlobj.process( basic_url + '/index.php?module=chat&act=save', 'POST', post_data );
}

function loadChatContent(auto,page)
{	
	if( IN_PROCESS )
	{
		if( IN_PROCESS == 1 )
		{
			AUTO_REFRESH_TIMER = setTimeout(function(){ loadChatContent(auto,page); }, 100 );
		}
		
		return false;
	}
	
	page = page ? page : 1;
	
	IN_PROCESS = 2;
	
	var xmlobj = new ajax_request();

	var do_request_function = function()
	{
		if( ! xmlobj.readystate_ready_and_ok() )
		{
			return false;
		}
		
		IN_PROCESS = false;

		my_getbyid('chatContentWrapper').innerHTML = xmlobj.xmlhttp.responseText;
		
		execute_js(xmlobj.xmlhttp.responseText);
		
		if( ! MANUAL_REFRESH )
		{
			if( page < 2 )
			{
				if( auto )
				{
					AUTO_REFRESH_TIMER = setTimeout(function(){ loadChatContent(true); }, 5000 );
				}
			}
			else
			{
				clearTimeout(AUTO_REFRESH_TIMER);
			}
		}
		
		checkHeight();

		return false;
	}

	xmlobj.onreadystatechange(do_request_function);

	xmlobj.process( basic_url + '/index.php?module=chat&act=load&p=' + page );
}

function loadChatterList(page)
{	
	if( IN_PROCESS )
	{
		if( IN_PROCESS != 3 )
		{			
			setTimeout(function(){ loadChatterList(page); }, 100 );
		}
				
		return false;
	}
	
	IN_PROCESS = 3;
	
	var xmlobj = new ajax_request();

	var do_request_function = function()
	{
		if( ! xmlobj.readystate_ready_and_ok() )
		{
			return false;
		}
		
		IN_PROCESS = false;

		my_getbyid('chatterListWrapper').innerHTML = xmlobj.xmlhttp.responseText;
		
		execute_js(xmlobj.xmlhttp.responseText);
		
		checkChatterListHeight();

		return false;
	}

	xmlobj.onreadystatechange(do_request_function);

	xmlobj.process( basic_url + '/index.php?module=chat&act=mlist&p=' + ( page ? page : 1 ) );
}

function updateChatterPerm(formObj,id)
{	
	if( formObj.disable )
	{
		return true;
	}
	
	formObj.disable = true;
	
	var xmlobj = new ajax_request();

	var do_request_function = function()
	{
		if( ! xmlobj.readystate_ready_and_ok() )
		{
			return false;
		}
		
		formObj.disable = false;

		if( xmlobj.xmlhttp.responseText != 1 )
		{
			formObj.checked = ! formObj.checked;			
		}

		return false;
	}

	xmlobj.onreadystatechange(do_request_function);

	xmlobj.process( basic_url + '/index.php?module=chat&act=perm&id=' + id + '&set=' + ( formObj.checked ? 1 : 0 ) );
}

function checkHeight()
{
	my_getbyid('chatContentWrapper').firstChild.style.height = CHAT_CONTENT_HEIGHT + 'px'; 
	return;
	if( my_getbyid('chatContentWrapper').offsetHeight > 500 )
	{
		my_getbyid('chatContentWrapper').firstChild.style.height = '500px'; 
	}
}

function checkChatterListHeight()
{return;
	if( my_getbyid('chatterListWrapper').offsetHeight > 350 )
	{
		my_getbyid('chatterListWrapper').firstChild.style.height = '350px'; 
	}
}

function autoRefreshSwitcher(formObj)
{	
	MANUAL_REFRESH = ! formObj.checked;
	
	if( formObj.checked )
	{
		AUTO_REFRESH_TIMER = setTimeout(function(){ loadChatContent(true); }, 5000 );
	}
	else
	{
		clearTimeout(AUTO_REFRESH_TIMER);
	}
}

function switchPostFormState(state)
{
	my_getbyid('chatPostForm').style.display = state == 1 ? '' : 'none';
	my_getbyid('chatPostFormDisable').style.display = state == 1 ? 'none' : '';
}

my_getbyid('chatboxPostForm').onkeydown = chatboxKeyWrap;
autoRefreshSwitcher(my_getbyid('autoF5'));
checkHeight();

if( ! IN_PLUGIN )
{
	checkChatterListHeight();
}