/**
 * Inferno Shoutbox Javascript Engine
 * Created By Inferno Technologies
 * All Rights Reserved
 * * * * * * * * * * * * * * *
 */

_ishout = function()
{
	this.script = '';
	this.editor = '';
	this.loader = '';
	this.notice = '';
	this.activity = '';
	this.noticemessage = '';
	this.shoutframe = '';
	this.userframe = '';
	this.styleprops = '';
	this.styleprop = new Object;
	this.fetchshout = new Object;
	this.fetchusers = new Object;
	this.shout = new Object;
	this.idle = false;
	this.idletimelimit = 0;
	this.loaded = false;
	this.idletime = 0;
	this.showing = 'shoutbox';
	this.styleproperties = {
		'fontWeight' : ['bold', false],
		'textDecoration' : ['underline', false],
		'fontStyle' : ['italic', false]
	};

	this.initiate = function(thisscript, idletime, dobold, doitalic, dounderline)
	{
		this.editor = fetch_object('shoutbox_editor');
		this.loader = fetch_object('shoutbox_loader');
		this.notice = fetch_object('shoutbox_notice');
		this.noticemessage = fetch_object('shoutbox_notice_message');
		this.shoutframe = fetch_object('shoutbox_frame');
		this.userframe = fetch_object('shoutbox_users_frame');
		this.activity = fetch_object('shoutbox_activity');

		this.script = thisscript;
		this.load_editor_settings(parseInt(dobold), parseInt(doitalic), parseInt(dounderline));
		this.idletimelimit = idletime;
		this.idlecheck();

		this.fetch_shouts();
		this.get_shouts = setInterval("InfernoShoutbox.fetch_shouts();", 5000);
	}

	this.load_editor_settings = function(dobold, doitalic, dounderline)
	{
		if (fetch_object('sb_color_mem'))
		{
			this.adjust_property_selection('color', fetch_object('sb_color_mem'), false);
		}

		if (fetch_object('sb_font_mem'))
		{
			this.adjust_property_selection('fontFamily', fetch_object('sb_font_mem'), false);
		}

		if (dobold && fetch_object('sb_mem_bold'))
		{
			this.adjust_property('fontWeight', this.styleproperties['fontWeight'][0], fetch_object('sb_mem_bold'), false);
		}

		if (doitalic && fetch_object('sb_mem_italic'))
		{
			this.adjust_property('fontStyle', this.styleproperties['fontStyle'][0], fetch_object('sb_mem_italic'), false);
		}

		if (dounderline && fetch_object('sb_mem_underline'))
		{
			this.adjust_property('textDecoration', this.styleproperties['textDecoration'][0], fetch_object('sb_mem_underline'), false);
		}
	}

	this.clear = function()
	{
		this.editor.value = '';
	}

	this.assign_styleprop = function(prop, sobj)
	{
		switch (prop)
		{
			case 'fontWeight':
			case 'textDecoration':
			case 'fontStyle':
			{
				this.adjust_property(prop, this.styleproperties[prop][0], sobj, true);
				return false;
			}

			case 'color':
			case 'fontFamily':
			{
				this.adjust_property_selection(prop, sobj, true);
				return false;
			}
		}
	}

	this.adjust_property_selection = function(property, sobj, update)
	{
		value = sobj.options[sobj.options.selectedIndex].value;

		if (!this.styleproperties[property])
		{
			this.styleproperties[property] = [];
		}

		this.styleproperties[property][1] = value;

		eval('this.editor.style.' + property + ' = "' + value + '";');

		if (update)
		{
			this.update_styleprops();
		}
	}

	this.adjust_property = function(property, value, sobj, update)
	{
		sobj.value = sobj.value + '*';

		if (this.styleproperties[property][1])
		{
			value = '';
			sobj.value = sobj.value.replace(/\*/g, '');
		}

		this.styleproperties[property][1] = !this.styleproperties[property][1];

		eval('this.editor.style.' + property + ' = "' + value + '";');

		if (update)
		{
			this.update_styleprops();
		}
	}

	this.shout = function()
	{
		if (this.posting_shout)
		{
			this.show_notice('A previous message is still being submitted.');

			return false;
		}

		if (this.idle)
		{
			this.hide_notice();
		}

		this.idle = false;
		this.idletime = 0;

		message = PHP.trim(this.editor.value);

		if (message == '')
		{
			this.show_notice('Please enter a message first.');

			return false;
		}

		this.posting_shout = true;

		this.set_loader('');

		this.shout.ajax = new vB_AJAX_Handler(true);
		this.shout.ajax.onreadystatechange(InfernoShoutbox.shout_posted);
		this.shout.ajax.send('infernoshout.php', 'do=shout&message=' + PHP.urlencode(message));

		this.clear();

		return false;
	}

	this.idlecheck = function()
	{
		if (this.idle || this.idletime > this.idletimelimit)
		{
			setTimeout("InfernoShoutbox.idlecheck()", 1000);
			return false;
		}

		this.idletime++;

		if (this.idletime > this.idletimelimit)
		{
			this.idle = true;
		}

		setTimeout("InfernoShoutbox.idlecheck()", 1000);
	}

	this.shout_posted = function()
	{
		ajax = InfernoShoutbox.shout.ajax;

		if (ajax.handler.readyState == 4 && ajax.handler.status == 200)
		{
			if (PHP.trim(ajax.handler.responseText) == 'completed')
			{
				InfernoShoutbox.posting_shout = false;
				InfernoShoutbox.fetch_shouts();
			}
			else
			{
				this.show_notice('Your shout failed to send.');
			}
		}
	}

	this.update_styleprops = function()
	{
		this.set_loader('');

		var bold	= this.styleproperties['fontWeight'][1] ? 1 : 0;
		var italic	= this.styleproperties['fontStyle'][1] ? 1 : 0;
		var underline	= this.styleproperties['textDecoration'][1] ? 1 : 0;
		var colour	= '';
		var fontfamily	= '';

		if (this.styleproperties['color'])
		{
			colour = this.styleproperties['color'][1];
		}

		if (this.styleproperties['fontFamily'])
		{
			fontfamily = this.styleproperties['fontFamily'][1];
		}

		styleproperties = new Array(
			'bold=' + bold,
			'italic=' + italic,
			'underline=' + underline,
			'colour=' + colour,
			'fontfamily=' + fontfamily
		);

		styleproperties = styleproperties.join('&');

		this.styleprop.ajax = new vB_AJAX_Handler(true);
		this.styleprop.ajax.onreadystatechange(InfernoShoutbox.styleprops_updated);
		this.styleprop.ajax.send('infernoshout.php', 'do=styleprops&' + styleproperties);
	}

	this.styleprops_updated = function()
	{
		ajax = InfernoShoutbox.styleprop.ajax;

		if (ajax.handler.readyState == 4 && ajax.handler.status == 200)
		{
			InfernoShoutbox.set_loader('none');
			InfernoShoutbox.show_notice('Your style properties have been updated.');
		}
	}

	this.fetch_shouts = function()
	{
		if (this.posting_shout)
		{
			return false;
		}

		if (this.idle && this.loaded)
		{
			this.show_notice('You are currently idle in the shoutbox.');
			clearTimeout(InfernoShoutbox.kill_notice); // Don't hide this notice

			return false;
		}

		this.loaded = true;

		this.set_loader('');

		this.fetchshout.ajax = new vB_AJAX_Handler(true);
		this.fetchshout.ajax.onreadystatechange(InfernoShoutbox.fetch_shouts_completed);
		this.fetchshout.ajax.send('infernoshout.php', 'do=messages');
	}

	this.fetch_shouts_completed = function()
	{
		ajax = InfernoShoutbox.fetchshout.ajax;

		if (ajax.handler.readyState == 4 && ajax.handler.status == 200)
		{
			data = ajax.handler.responseText.split('<<~~PARSE_^_BREAKER~~>>');

			InfernoShoutbox.update_shouts(PHP.trim(data[0]));
			InfernoShoutbox.activity.innerHTML = PHP.trim(data[1]);
			InfernoShoutbox.set_loader('none');
		}
	}

	this.update_shouts = function(shouts)
	{
		this.shoutframe.innerHTML = '';
		this.shoutframe.innerHTML = shouts;
	}

	this.set_loader = function(set)
	{
		this.loader.style.display = set;
	}

	this.show_notice = function(message)
	{
		clearTimeout(InfernoShoutbox.kill_notice);
		InfernoShoutbox.kill_notice = setTimeout("InfernoShoutbox.hide_notice()", 5000);

		this.noticemessage.innerHTML = message;
		this.notice.style.display = '';
	}

	this.hide_notice = function()
	{
		this.notice.style.display = 'none';
	}

	this.show = function(what)
	{
		if (what == this.showing || this.loader.style.display != 'none')
		{
			return false;
		}

		if (what == 'shoutbox')
		{
			this.shoutframe.style.display = 'block';
			this.userframe.style.display = 'none';
		}
		else
		{
			this.fetch_users();
			this.shoutframe.style.display = 'none';
			this.userframe.style.display = 'block';
		}

		this.showing = what;

		return false;
	}

	this.fetch_users = function()
	{
		this.set_loader('');

		this.fetchusers.ajax = new vB_AJAX_Handler(true);
		this.fetchusers.ajax.onreadystatechange(InfernoShoutbox.fetch_users_completed);
		this.fetchusers.ajax.send('infernoshout.php', 'do=userlist');
	}

	this.fetch_users_completed = function()
	{
		ajax = InfernoShoutbox.fetchusers.ajax;

		if (ajax.handler.readyState == 4 && ajax.handler.status == 200)
		{
			InfernoShoutbox.userframe.innerHTML = ajax.handler.responseText;
			InfernoShoutbox.set_loader('none');
		}
	}
}