/***************************************************************************
 *
 *  Custom Status plugin (/jscripts/custom_status.js)
 *  Author: Pirata Nervo
 *  Copyright: © 2009-2010 Pirata Nervo
 *  
 *  Website: http://mybb-plugins.com
 *  License: license.txt
 *
 *  This plugin allows users to set a custom status which appears on index, profile and posts.
 *
 ***************************************************************************/

/****************************************************************************
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

var Custom_Status = {
	
	reset_status: function()
	{
		var success = $('custom_status_changed_success');

		success.innerHTML = '';
	},
	
	get_new: function()
	{
		var new_status = prompt("What are you doing now?", "");
		if (new_status == '' || new_status == null)
			return false;
		else
		{
			if(use_xmlhttprequest != 1)
			{
				return true;
			}
			this.spinner = new ActivityIndicator('body', {image: imagepath + "/spinner_big.gif"});
			new Ajax.Request('xmlhttp.php?action=change_custom_status&my_post_key='+my_post_key, {
				method: 'post',
				postBody: 'status='+encodeURIComponent(new_status),
				onComplete: function(request) { Custom_Status.status_changed(request); }
			});
			document.body.style.cursor = 'wait';
			return false;
		}
	},

	status_changed: function(request)
	{
		if(request.responseText.match(/<error>(.*)<\/error>/)) // error :P
		{
			message = request.responseText.match(/<error>(.*)<\/error>/); // get error mesage
			if(!message[1])
				message[1] = 'An unknown error occurred.';
			
			if(this.spinner)
			{
				this.spinner.destroy();
				this.spinner = '';
			}
			document.body.style.cursor = 'default';
			alert('There was an error performing the update.\n\n'+message[1]);
		}
		else if(request.responseText.match(/<success>(.*)<\/success>/)) // success!
		{	
			var success = $('custom_status_changed_success');
		
			success.style.color = "#00b200";
			success.style.fontWeight = "bold";
			success.style.fontSize = "10px";
			success.style.marginBottoms = "10px";
			success.innerHTML = request.responseText.match(/<success>(.*)<\/success>/)[1];
			
			var status = $('custom_status');
			status.innerHTML = request.responseText.match(/<status>(.*)<\/status>/)[1];
			
			window.setTimeout("Custom_Status.reset_status('custom_status_changed_success')", 5000);
			document.body.style.cursor = 'default';
		}

		if(this.spinner)
		{
			this.spinner.destroy();
			this.spinner = '';
		}
	}
};
