/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # 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 mood editors
	this.set_mood_editors = function() {
		this.editors['mood_controls'] = YAHOO.util.Dom
				.get('dbtech_status_mood_controls' + this.statusid);
		this.editors['mood_text'] = YAHOO.util.Dom
				.get('dbtech_status_moodtext' + this.statusid);
		this.editors['mood'] = YAHOO.util.Dom
				.get('dbtech_status_mood' + this.statusid);
	};

	// #########################################################################
	// Set status editors
	this.set_status_editors = function() {
		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 = '';
	};

	// #########################################################################
	// Opening a mood editor
	this.init_edit_mood = function() {
		this.editors['mood'].style.display = 'none';
		this.editors['mood_controls'].style.display = 'inline';
	};

	// #########################################################################
	// 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;
	};

	// #########################################################################
	// Saving mood editor
	this.save_edit_mood = function(moodid) {
		// Init this
		var extraparams = '';

		// Add mood
		extraparams += '&mood=' + moodid;

		// Add user id
		extraparams += '&userid=' + this.userid;

		// Do update status
		this.ajax_call('updatemood', 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 = '';
	};

	// #########################################################################
	// Closing a mood editor
	this.cancel_edit_mood = function() {
		this.editors['mood'].style.display = 'inline';
		this.editors['mood_controls'].style.display = 'none';
	};

	// #########################################################################
	// 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', 'mood' ];
			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;
				this.cancel_edit_status();
			}

			if (mood.length) {
				// Set the mood
				this.editors['mood_text'].innerHTML = mood[0].firstChild.nodeValue;
				this.cancel_edit_mood();
			}
		}
	};

	// #########################################################################
	// 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()
				+ '] ';
	};
};