/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2010 Fillip Hannisdal AKA Revan/NeoRevan/Belazor     # ||
|| # and David Tobin.												  # ||
|| # 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();
    this.itemsadded = false;
    
	// #########################################################################
	// Creates the criteria menu
    this.create_menu = function(menuid)
	{
    	for (var i in dbtech_status_criteria)
		{
    		var menuContents = YAHOO.util.Dom.get('dbtech_status_moodbits' + dbtech_status_criteria[i]).innerHTML;
    		
    		YAHOO.util.Dom.get('dbtech_status_menu_contents' + this.statusid + '_' + dbtech_status_criteria[i]).innerHTML = menuContents;
    		
    		if (typeof(vbmenu_register) == 'function')
			{
    			vbmenu_register('dbtech_status_mood' +  this.statusid + '_' + dbtech_status_criteria[i]);
    		}
    	}
    	
        var moodItems = YAHOO.util.Dom.getElementsByClassName('dbtech_status_selector', 'a');
        
        YAHOO.util.Event.addListener(moodItems, 'click', this.save_edit_mood, this);
    };	
	
	// #########################################################################
	// 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);
        this.editors['moodbits'] = YAHOO.util.Dom.get('dbtech_status_moodbits');
	};

	// #########################################################################
	// 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';
        
        this.create_menu('statmenu' + this.statusid);
	};
	
	// #########################################################################
	// 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(e, FB)
	{
		// Init this
		var extraparams = '';
		
		// Add mood
		extraparams += '&mood=' + this.id;

		// Add user id
		extraparams += '&userid=' + FB.userid;

		// Do update status
		FB.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';
        
        for (var i in dbtech_status_criteria)
		{
        	YAHOO.util.Dom.get('dbtech_status_menu_contents' + this.statusid + '_' + dbtech_status_criteria[i]).innerHTML = '';
        }
	};

	// #########################################################################
	// 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', 'dbtech_status.php',
		{
			success : this.ajax_completed,
			failure : this.handle_ajax_error,
			timeout : vB_Default_Timeout,
			scope : this
		}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=ajax&action=' + 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() + '] ';
	};
};
