/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2007-2009 Fillip Hannisdal AKA Revan/NeoRevan/Belazor # ||
|| # All Rights Reserved. 											  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------------------------------------------------------- # ||
|| # You are not allowed to use this on your server unless the files  # ||
|| # you downloaded were done so with permission.					  # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

// #############################################################################
// Setup the main object
FBStatus_Obj = function()
{
	this.userid = 0;
	this.statusid = '';
	this.editors = new Array();
	
	// #########################################################################
	// Sets userid
	this.set_userid = function(userid, statusid)
	{
		// Sets the user id
		this.userid = userid;
		
		// Set the status id
		this.statusid = statusid;
		
		// Set variouse ditors
		this.editors['status'] = YAHOO.util.Dom.get('dbtech_status_status' + this.statusid);
		this.editors['statustext'] = YAHOO.util.Dom.get('dbtech_status_statustext' + this.statusid);
		this.editors['controls'] = YAHOO.util.Dom.get('dbtech_status_controls' + this.statusid);
		this.editors['input'] = YAHOO.util.Dom.get('dbtech_status_input' + this.statusid);
	}
	
	// #########################################################################
	// Opening a status editor
	this.init_edit_status = function()
	{
		// Begin showing editors
		this.editors['status'].style.display = 'none';
		this.editors['controls'].style.display = 'inline';
		this.editors['input'].value = '';
	}
	
	// #########################################################################
	// Saving status editor
	this.save_edit_status = function()
	{
		// Init this
		var extraparams = '';
		
		// Add status update
		extraparams += '&status=' + PHP.urlencode(PHP.trim(this.editors['input'].value));
		
		// Add user id
		extraparams += '&userid=' + this.userid;
		
		// Do update status
		this.ajax_call('updatestatus', extraparams);
		
		return false;
	}
	
	// #########################################################################
	// Closing a status editor
	this.cancel_edit_status = function()
	{
		// Hide editors
		this.editors['status'].style.display = 'inline';
		this.editors['controls'].style.display = 'none';
		this.editors['input'].value = '';
	}
	
	// #########################################################################
	// Finalise fetching of content
	this.ajax_completed = function(ajax)
	{
		if (!ajax.responseXML)
		{
			// Empty response
			this.throw_ajax_error('Invalid response from server: ' + ajax.responseText);
			return false;
		}
		
		// check for error first
		var error = ajax.responseXML.getElementsByTagName('error');
			
		if (error.length)
		{
			// Throw the error returned
			this.throw_ajax_error(error[0].firstChild.nodeValue);
		}
		else
		{
			// All possible tags
			var tags = [
				'status'
			];
			for (var t = 0; t < tags.length; t++)
			{
				eval('var ' + tags[t] + ' = ajax.responseXML.getElementsByTagName("' + tags[t] + '");');
			}
			
			if (status.length)
			{
				// Set the status
				this.editors['statustext'].innerHTML = status[0].firstChild.nodeValue;
			}
			
			// Cancels the status editing now that it's worked
			this.cancel_edit_status();
		}
	}
	
	// #########################################################################
	// Shorthand for an ajax call
	this.ajax_call = function(varname, extraparams)
	{
		return YAHOO.util.Connect.asyncRequest('POST', 'ajax.php', {
			success: this.ajax_completed,
			failure: this.handle_ajax_error,
			timeout: vB_Default_Timeout,
			scope: this
		}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=dbtech_status_' + varname + extraparams);		
	}
	
	// #########################################################################
	// This should never happen.
	this.throw_ajax_error = function(error)
	{
		// Log the error to the console
		console.error(this.timestamp() + "AJAX Error: %s", error);

		// Just pop it up
		alert(error);
	}	
	
	// #########################################################################
	// This should never happen.
	this.handle_ajax_error = function(ajax)
	{
		if (ajax.statusText == 'communication failure' || ajax.statusText == 'transaction aborted')
		{
			// Ignore this error
			return false;
		}
			
		// Log the error to the console
		console.error(this.timestamp() + "AJAX Error: Status = %s: %s", ajax.status, ajax.statusText);

		// Just pop it up
		alert(ajax.statusText);
	}
	
	// #########################################################################
	// Debugging function, generates a timestamp of when something occurred
	this.timestamp = function()
	{
		var d = new Date();
		
		return '[' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '] ';
	}		
}
