var vB_status_Editor = null;
function vB_AJAX_Status_Init(statusid)
{
	if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
	{
		var div = fetch_object(statusid);
			if (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax == 0)
			{
				div.style.cursor = 'crosshair';
				div.ondblclick = vB_AJAX_Status_Events.prototype.status_doubleclick;
			}
	}
}	

function vB_AJAX_TitleEdit(obj)
{
	this.obj = obj;
	this.userid = vB_AJAX_Userid;
	this.linkobj = fetch_object('status_title_' + this.userid);
	this.container = this.linkobj.parentNode;
	this.editobj = null;
	this.xml_sender = null;

	this.origtitle = '';
	this.editstate = false;

	// =============================================================================
	// vB_AJAX_TitleEdit methods

	/**
	* Function to initialize the editor for a thread title
	*/
	this.edit = function()
	{
		if (this.editstate == false)
		{
			// create the new editor input box properties...
			this.inputobj = document.createElement('input');
			this.inputobj.type = 'text';
			this.inputobj.size = 20;
			// read in value for titlemaxchars from $vbulletin->options['titlemaxchars'], specified in template or default to 85
			this.inputobj.maxLength = ((typeof(stitlemaxchars) == "number" && stitlemaxchars > 0) ? stitlemaxchars : 24);
			this.inputobj.style.width = '200px';
			this.inputobj.className = 'bginput';
			this.inputobj.value = PHP.unhtmlspecialchars(this.linkobj.innerHTML);
			this.inputobj.title = this.inputobj.value;

			// ... and event handlers
			this.inputobj.onblur = vB_AJAX_Status_Events.prototype.titleinput_onblur;
			this.inputobj.onkeypress = vB_AJAX_Status_Events.prototype.titleinput_onkeypress;

			// insert the editor box and select it
			this.editobj = this.container.insertBefore(this.inputobj, this.linkobj);
			this.editobj.select();

			// store the original text
			this.origtitle = this.linkobj.innerHTML;

			// hide the link object
			this.linkobj.style.display = 'none';

			// declare that we are in an editing state
			this.editstate = true;
		}
	}

	/**
	* Function to restore a thread title in the editing state
	*/
	this.restore = function()
	{
		if (this.editstate == true)
		{
			// do we actually need to save?
			if (this.editobj.value != this.origtitle)
			{
				this.linkobj.innerHTML = PHP.htmlspecialchars(this.editobj.value);
				this.save(this.editobj.value);
			}
			else
			{
				// set the new contents for the link
				this.linkobj.innerHTML = this.editobj.value;
			}

			// remove the editor box
			this.container.removeChild(this.editobj);

			// un-hide the link
			this.linkobj.style.display = '';

			// declare that we are in a normal state
			this.editstate = false;
			this.obj = null;
		}
	}

	/**
	* Function to save an edited thread title
	*
	* @param	string	Edited title text
	*
	* @return	string	Validated title text
	*/
	this.save = function(titletext)
	{
		YAHOO.util.Connect.asyncRequest("POST", "userstatus.php?do=updatestatus&userid=" + this.userid, {
			success: this.handle_ajax_response,
			timeout: 15000,
			scope: this
		}, SESSIONURL + "do=updatestatus&userid=" + this.userid + '&userstatus=' + titletext);
	}

	/**
	* Handles AJAX response request
	*
	* @param	object	YUI AJAX
	*/
	this.handle_ajax_response = function(ajax)
	{
		vB_status_Editor.obj = null;
	}

	// start the editor
	this.edit();
}

function vB_AJAX_Status_Events()
{
}

/**
* Handles double-clicking on thread title cells to initialize title edit
*/
vB_AJAX_Status_Events.prototype.status_doubleclick = function(e)
{
	if (vB_status_Editor && vB_status_Editor.obj == this)
	{
		return false;
	}
	else
	{
		try
		{
			vB_status_Editor.restore();
		}
		catch(e) {}

		vB_status_Editor = new vB_AJAX_TitleEdit(this);
	}
};



/**
* Handles blur events on thread title input boxes
*/
vB_AJAX_Status_Events.prototype.titleinput_onblur = function(e)
{
	vB_status_Editor.restore();
};

/**
* Handles keypress events on thread title input boxes
*/
vB_AJAX_Status_Events.prototype.titleinput_onkeypress = function (e)
{
	e = e ? e : window.event;
	switch (e.keyCode)
	{
		case 13: // return / enter
		{
			vB_status_Editor.inputobj.blur();
			return false;
		}
		case 27: // escape
		{
			vB_status_Editor.inputobj.value = vB_status_Editor.origtitle;
			vB_status_Editor.inputobj.blur();
			return true;
		}
	}
};
	