//------------------------------------------------------------------------------
// MiniChat 1.3
//------------------------------------------------------------------------------
// Writing started by: Fabien
//------------------------------------------------------------------------------

var ajax_emo_loaded = 0;
var toogle_ajax_emo = 0;

/*-------------------------------------------------------------------------*/
// Emoticons window
/*-------------------------------------------------------------------------*/
function minichat_emoticons()
{
	if( use_enhanced_js && ajax_emo_loaded == 0)
	{
		minichat_ajax_emoticons();
		ajax_emo_loaded = 1;
		toogle_ajax_emo = 1;
	}
	else if( use_enhanced_js && ajax_emo_loaded == 1 )
	{
		if( toogle_ajax_emo == 1 )
		{
			document.getElementById("minichat_smileys").style.display = 'none';
			toogle_ajax_emo = 0;
		}
		else
		{
			document.getElementById("minichat_smileys").style.display = '';
			toogle_ajax_emo = 1;
		}
	}
	else
	{
		window.open( ipb_var_base_url + "autocom=minichat&code=emoticons","Legends","width=250,height=500,resizable=yes,scrollbars=yes");
	}
}

/*-------------------------------------------------------------------------*/
// Add smilie in form field
/*-------------------------------------------------------------------------*/
function minichat_add_smilie(code, id, ajax)
{
	if( ajax == 1 )
	{
		document.minichat.message.value += ' ' + code + ' ';
	}
	else
	{
		window.opener.document.minichat.message.value += ' ' + code + ' ';
	}
}

/*-------------------------------------------------------------------------*/
// AJAX emoticons
/*-------------------------------------------------------------------------*/
function minichat_ajax_emoticons()
{
	var emodiv = document.getElementById("minichat_smileys");
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			emodiv.innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=minichat&code=emoticons-ajax' );
}

/*-------------------------------------------------------------------------*/
// BBCodes window
/*-------------------------------------------------------------------------*/
function minichat_bbcodes()
{
	window.open( ipb_var_base_url + "act=legends&CODE=bbcode","Legends","width=700,height=500,resizable=yes,scrollbars=yes"); 
}

/*-------------------------------------------------------------------------*/
// Add a message
/*-------------------------------------------------------------------------*/
function add_message(mc)
{
	//----------------------------------
	// Init
	//----------------------------------
	var fields = new Array();
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return true;
	}
	//----------------------------------
	// message not empty?
	//----------------------------------
	if ( mc.elements['message'].value == '' )
	{
		//return true;
	}
	
	//----------------------------------
	// Fields
	//----------------------------------
	fields['message'] = mc.elements['message'].value;
	fields['name'] = mc.elements['name'] ? mc.elements['name'].value : '';
	fields['archives'] = mc.elements['archives'] ? mc.elements['archives'].value : '';
	fields['st'] = mc.elements['st'] ? mc.elements['st'].value : '';
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.minichat.message.value = '';
			document.getElementById("minichat_messages").innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=minichat&code=save-ajax', 'POST', xmlobj.format_for_post(fields) );
	
	return false;
}

/*-------------------------------------------------------------------------*/
// Add a message
/*-------------------------------------------------------------------------*/
function minichat_delete(id,archives)
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return;
	}
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.getElementById("minichat_messages").innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=minichat&code=delete-ajax&id=' + id + '&archives=' + archives);
}


/*-------------------------------------------------------------------------*/
// Refresh the minichat
/*-------------------------------------------------------------------------*/
function minichat_refresh(time)
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return;
	}
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			window.setTimeout("minichat_refresh()",time);
			document.getElementById("minichat_messages").innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=minichat&code=refresh' );
}

/*-------------------------------------------------------------------------*/
// Init minichat
/*-------------------------------------------------------------------------*/
function minichat_init()
{
	var div    = '';
	var link  = '';
	var order = '';
	var type  = '';
	var id    = '';
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	//----------------------------------
	// Replace links
	//----------------------------------
	div = document.getElementById('minichat_messages');
	
	for ( var i = 0 ; i < div.getElementsByTagName('a').length ; i++ )
	{
		link = div.getElementsByTagName('a')[i].href
		
		if( link.search(/autocom=minichat&code=delete/) != -1 )
		{
			id = link.replace( /^(.*)id=(\d+)$/, "$2" );
			
			div.getElementsByTagName("a")[i].href = "javascript:minichat_delete(" + id + "," + archives + ");";
		}
	}
}
