/*
+----------------------------------------------------------
|   (SnS) AJAX Fast Edit for IP.Board 1.3
|   =======================================================
|   (Ñ) 2007 Îëåã «Sannis» Åôèìîâ
|   http://ibresource.ru/forums/index.php?showuser=36662
|   http://ibpower.ru/index.php?showuser=33
|   http://forum.sysman.ru/index.php?showuser=3739
+----------------------------------------------------------
*/

var post_cache = new Array();

function sns_fast_edit_prep(post_id)
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------

	if ( !use_enhanced_js )
	{
		return true;
	}

	//----------------------------------
	// INIT
	//----------------------------------
	
	var url = ipb_var_base_url+'act=xmlout&do=post-edit-show&p='+post_id+'&t='+ipb_input_t+'&f='+ipb_input_f;

	post_cache[ post_id ] = $('post-'+post_id+'-content').innerHTML;

	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			show_loading(post_id);
			return;
		}
		
		var html = xmlobj.xmlhandler.responseText;

		if( html.substr(0,6) == 'error#' )
		{
			jump_act_error(html);
		}
		else
		{			
			$('post-'+post_id+'-content').innerHTML = html;
			$('post-'+post_id+'-table').scrollIntoView();
		}
	};
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	xmlobj.process( url );
}

function sns_fast_edit_save( post_id )
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------

	if ( !use_enhanced_js )
	{
		return true;
	}
	
	//----------------------------------
	// INIT
	//----------------------------------
	
	var url    = ipb_var_base_url+'act=xmlout&do=post-edit-save&p='+post_id+'&t='+ipb_input_t+'&f='+ipb_input_f;
	var fields = new Array();

	//----------------------------------
	// Populate fields
	//----------------------------------
	
	fields['auth_key']         = ipb_md5_check;
	fields['t']                = ipb_input_t;
	fields['f']                = ipb_input_f;
	fields['p']                = post_id;
	fields['act']              = 'xmlout';
	fields['do']               = 'post-edit-save';
	fields['Post']			   = $('quick-edit-area-'+post_id).value;


	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			show_loading(post_id);
			return;
		}
		
		var html = xmlobj.xmlhandler.responseText;
		
		if( html == 'show_form' )
		{
			sns_fast_edit_fulleditor(post_id);
		}
		else if( html.substr(0,6) == 'error#' )
		{
			jump_act_error(html);
		}
		else
		{
			$('post-'+post_id+'-content').innerHTML = html;
			initSpoilers('body'); // For FatCat spoiler tag
			//xmlobj.execute_javascript( html );
		}
	};
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	
	return false;
}

function sns_fast_edit_fulleditor( post_id )
{
	if ( post_id && ipb_input_t && ipb_input_f )
	{
		var _form = $('quick-edit-form');
		var _textarea = $('quick-edit-form-textarea');
		
		if( !_form || !_textarea )
		{
			_form = document.createElement('form');
			_form.setAttribute('name', 'quick-edit-form');
			_form.setAttribute('method', 'POST');
			
			_textarea = document.createElement('textarea');
			_textarea.setAttribute('name', 'Post');
			_textarea.setAttribute('id',   'quick-edit-form-textarea');
			_form.appendChild(_textarea);
			
			document.documentElement.firstChild.appendChild(_form);
		}
		
		var url  = ipb_var_base_url + 'act=Post&CODE=08&f=' + ipb_input_f + '&t=' + ipb_input_t + '&p=' + post_id + '&st=' + ipb_input_st;
		
		_form.setAttribute('action', url);

		_textarea.value = $('quick-edit-area-'+post_id).value;

		_form.submit();

		
		xmlobj.show_loading();
		
		return false;
	}
	else
	{
		return false;
	}
}

function sns_fast_edit_cancel(post_id)
{
	if ( post_cache[ post_id ] != "" )
	{
		$('post-'+post_id+'-content').innerHTML = post_cache[ post_id ];
	}
	
	return false;
}

function show_loading(post_id)
{
	$('post-'+post_id+'-content').innerHTML = snsFastEditLoadingHTML;
	
	/*_form = document.createElement('form');
	_form.setAttribute('name', 'quick-edit-form');
	_form.setAttribute('method', 'POST');
	
	_textarea = document.createElement('textarea');
	_textarea.setAttribute('name', 'Post');
	_textarea.setAttribute('id',   'quick-edit-form-textarea');
	_form.appendChild(_textarea);
	
	document.documentElement.firstChild.appendChild(_form);*/
}
